const {defineConfig} = require('@vue/cli-service')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
const path = require("path")
const webpack = require("webpack")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const cesiumSource = "node_modules/cesium/Source"
const cesiumWorkers = "../Build/Cesium/Workers"

module.exports = defineConfig({
    transpileDependencies: true,
    lintOnSave: false,
    configureWebpack: {
        resolve: {
            alias: {
                // 添加一个cesium别名，以便我们在项目中轻松的引入它
                cesium: path.resolve(cesiumSource)
            }
        },
        amd: {
            // Cesium源码模块化使用的requireJs
            // 此配置允许webpack友好地在铯中使用require，使webpack打包cesium
            // 告诉Cesium, AMD的webpack版本用来评估要求的声明是不符合标准的toUrl功能
            toUrlUndefined: true
        },
        module: {
            // 解决require引入警告
            unknownContextCritical: false
        },
        plugins: [
            new webpack.DefinePlugin({
                // 在cesium中定义用于加载资源的相对基路径
                CESIUM_BASE_URL: JSON.stringify("/")
            }),
            // 对build生效，拷贝到dist目录下。如：dist/Assets
            new CopyWebpackPlugin({
                patterns: [
                    {
                        from: path.join(cesiumSource, cesiumWorkers),
                        to: "Workers"
                    },
                    {
                        from: path.join(cesiumSource, "Assets"),
                        to: "Assets"
                    },
                    {
                        from: path.join(cesiumSource, "Widgets"),
                        to: "Widgets"
                    },
                    {
                        from: path.join(cesiumSource, "ThirdParty"),
                        to: "ThirdParty"
                    }
                ]
            }),
            // 使Cesium对象实例可在每个js中使用而无须import
            new webpack.ProvidePlugin({
                Cesium: ["cesium/Cesium"]
            }),
            new NodePolyfillPlugin()
        ],
        optimization: {
            // 抽取公共模块执行压缩
            minimize: process.env.NODE_ENV === "production"
        },
        // sourcemap生成
        devtool: process.env.NODE_ENV === "production" ? false : "source-map"
    },
    devServer: {
        port: 8080,
        proxy: {
            // '/proxy_oauth/': {
            //     target: process.env.VUE_APP_OAUTHURL,
            //     changeOrigin: true,
            //     pathRewrite: {
            //         '^/proxy_oauth': ''
            //     }
            // },
            // '/proxy_dtbserver/': {
            //     target: process.env.VUE_APP_BASEURL,
            //     changeOrigin: true,
            //     pathRewrite: {
            //         '^/proxy_dtbserver': ''
            //     }
            // },
        }
    }
})
