// 拷贝文件插件
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");
const path = require("path");
let cesiumSource = "./node_modules/cesium/Source";
let cesiumWorkers = "../Build/Cesium/Workers";

module.exports = {
    // 基本路径
    publicPath: "./",
    // 输出文件目录
    outputDir: "dist",
    assetsDir: "static",
    indexPath: "index.html",
    filenameHashing: true,
    productionSourceMap: false,
    lintOnSave: false, // 在保存代码的时候开启eslint代码检查机制
    devServer: {
        // 实时保存，编译的配置段
        port: 8085, // http服务的端口号码设定
        open: false,
        proxy: {
            [process.env.VUE_APP_BASE_API]: {
                target: "http://124.16.188.131:28030",
                changeOrigin: true,
                pathRewrite: {
                    ["^" + process.env.VUE_APP_BASE_API]: ""
                }
            },
            [process.env.VUE_APP_MICROWAVE_API]: {
                target: "http://124.16.188.131:18086/macApi",
                changeOrigin: true,
                pathRewrite: {
                    ["^" + process.env.VUE_APP_MICROWAVE_API]: ""
                }
            },
            [process.env.VUE_APP_REALITY_API]: {
                // target: "https://124.16.188.131:28092/microwave",
                target: "http://192.168.1.227:18092/microwave",
                changeOrigin: true,
                pathRewrite: {
                    ["^" + process.env.VUE_APP_REALITY_API]: ""
                }
            },
            [process.env.VUE_APP_FILE_API]: {
                // target: "https://124.16.188.131:28093/file",
                target: "http://192.168.1.227:18093/file",
                changeOrigin: true,
                pathRewrite: {
                    ["^" + process.env.VUE_APP_FILE_API]: ""
                }
            }
        }
    },
    configureWebpack: {
        output: {
            sourcePrefix: " "
        },
        amd: {
            toUrlUndefined: true
        },
        resolve: {
            alias: {
                vue$: "vue/dist/vue.esm.js",
                //可以对同一个地址起2个不同的别名
                "@": path.resolve("src"),
                src: path.resolve("src"),
                cesium: path.resolve(__dirname, cesiumSource)
            }
        },
        plugins: [
            new CopyWebpackPlugin([
                { from: path.join(cesiumSource, cesiumWorkers), to: "static/Workers" }
            ]),
            new CopyWebpackPlugin([
                { from: path.join(cesiumSource, "Assets"), to: "static/Assets" }
            ]),
            new CopyWebpackPlugin([
                { from: path.join(cesiumSource, "Widgets"), to: "static/Widgets" }
            ]),
            new CopyWebpackPlugin([
                {
                    from: path.join(cesiumSource, "ThirdParty/Workers"),
                    to: "static/ThirdParty/Workers"
                }
            ]),
            new webpack.DefinePlugin({
                CESIUM_BASE_URL: JSON.stringify("./static")
            })
            // new CopyWebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers'}]),
            // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets'}]),
            // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets'}]),
            // new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers'}]),
            // new webpack.DefinePlugin({
            //  //定义 Cesium 从哪里加载资源，如果使用默认的''，
            //  //却变成了绝对路径了，所以这里使用'./',使用相对路径
            //     CESIUM_BASE_URL: JSON.stringify('./')
            // })
        ],
        optimization: {
            splitChunks: {
                cacheGroups: {
                    commons: {
                        name: "Cesium",
                        test: /[\\/]node_modules[\\/]cesium/,
                        chunks: "all"
                    }
                }
            }
        },
        module: {
            // unknownContextCritical: /^.\/.*$/,
            unknownContextCritical: false
        }
    }
};
