// gzip 压缩代码
const CompressionPlugin = require('compression-webpack-plugin')
// 打包分析
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
// terser-webpack-plugin 可不引入, @vue/cli-service 中已经引入了terser-webpack-plugin
// const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
    devServer: {
        port: 8092,
        open: true,
        proxy: {
            '/data': {
                // target: 'http://10.199.143.126', //生产环境
                target: 'http://60.205.177.43', //阿里云
                changeOrigin: true,
                secure: false,
                pathRewrite: {
                    '^/data': '/data',
                },
            },
            // 绘图服务
            '/serve': {
                //    target: 'http://10.199.143.129:8080', //生产环境
                target: 'http://60.205.177.43:8080', //阿里云
                changeOrigin: true,
                pathRewrite: {
                    '^/serve': '',
                },
            },
        },
        hot: true,
        // 关闭esline
        overlay: {
            warnings: false,
            errors: false,
        },
    },
    chainWebpack: (config) => {
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        svgRule
            .use('babel-loader')
            .loader('babel-loader')
            .end()
            .use('vue-svg-loader')
            .loader('vue-svg-loader')
        config.output.filename('static/js/[name].[hash].js').end()
        config.output.chunkFilename('static/js/[name].[hash].js').end()
    },
    lintOnSave: false,
    publicPath: '/wandaBmGuideH5',
    // 打包名称
    outputDir: 'wandaBmGuideH5',
    // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录
    assetsDir: 'static',
    transpileDependencies: [
        '@saga-web', // 指定对第三方依赖包进行babel-polyfill处理
    ],
    productionSourceMap: false,
    // CSS 相关选项
    css: {
        // 将组件内的 CSS 提取到一个单独的 CSS 文件 (只用在生产环境中)
        extract: true,
    },
    // 配置webpack
    configureWebpack: (config) => {
        // 生成环境,删除console.log和debugger
        // TODO: 1.
        if (process.env.NODE_ENV === 'production' && false) {
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true //删除console
            config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true //删除 debugger
            config.optimization.minimizer[0].options.terserOptions.compress.pure_funcs = ['console.log'] //删除
        }
        let plugins = [
            // 压缩代码
            new CompressionPlugin({
                test: /\.js$|\.html$|\.css$/, // 匹配文件名
                threshold: 10240, // 对超过10k的数据压缩
                deleteOriginalAssets: false, // false 不删除源文件 true 删除源文件
            }),
        ]
        // 生产环境打包分析体积 命令 npm run  build --report 或者 yarn build --report
        if (process.env.NODE_ENV === 'production' && (process.env.npm_config_report || process.env.npm_config_argv.indexOf('--report') !== -1)) {
            plugins.push(new BundleAnalyzerPlugin())
        }
        return {
            plugins,
        }
    },
}