const path = require('path')
const resolve = dir => {
    return path.join(__dirname, dir)
}
/* const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
    .BundleAnalyzerPlugin */
/* const compressionPlugin = require("compression-webpack-plugin"); */
// 注册页面和协议页面在商家端和平台的都独立打包
const pages = {}
if (process.env.VUE_APP_MODE === 'call') {
    Object.assign(pages, {
        index: {
            entry: 'src/pages/call/main.js',
            template: 'public/call.html',
            filename: 'index.html',
            title: process.env.VUE_APP_TITLE,
            chunks: ['chunk-vendors', 'chunk-common', 'iview-area', 'index']
        }
    })
}
if (process.env.VUE_APP_MODE === 'platform') {
    Object.assign(pages, {
        index: {
            entry: 'src/pages/platform/main.js',
            template: 'public/index.html',
            filename: 'index.html',
            title: process.env.VUE_APP_TITLE,
            chunks: ['chunk-vendors', 'chunk-common', 'iview-area', 'index']
        }
    })
}
module.exports = {
    pages,
    lintOnSave: false,
    devServer: {
        hot: true
    },
    chainWebpack: config => {
        /*  console.log(config, 'chainWebpack') */
        // 移除 prefetch 插件
        config.plugins.delete('prefetch-index')
        // 移除 preload 插件
        config.plugins.delete('preload-index')
        config.resolve.symlinks(true)
        config.resolve.alias
            .set('@', resolve('src'))
            .set('_c', resolve('src/components'))
            .set('_m', resolve('src/mixins'))
            .set('_a', resolve('src/assets'))
            .set('_s', resolve('src/style'))
            .set('_p', resolve('src/pages'))
            .set('_call', resolve('src/pages/call'))
            .set('_platform', resolve('src/pages/platform'))
    },
    configureWebpack: (config) => {
        if (process.env.NODE_ENV === 'production') {
            // 取消console打印
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
            // 打包文件大小配置
            config.performance = {
                maxEntrypointSize: 10000000,
                maxAssetSize: 30000000
            }
            return {
                plugins: [],
                externals: {
                    vue: 'Vue',
                    'view-design': 'iview',
                    'vue-router': 'VueRouter',
                    iview: 'ViewUI',
                    vuex: 'Vuex'
                }
            }
        } else {
            return {
                optimization: {
                    splitChunks: {
                        chunks: 'all',
                        cacheGroups: {
                            vendors: {
                                name: `chunk-vendors`,
                                test: /[\\/]node_modules[\\/]/,
                                priority: -10,
                                chunks: 'all'
                            },
                            common: {
                                name: `chunk-common`,
                                minChunks: 1,
                                priority: -20,
                                minSize: 1,
                                chunks: 'all',
                                reuseExistingChunk: true
                            }
                        }
                    }
                },
                externals: {
                    //排除一些引入的模块，不进行打包
                    vue: 'Vue',
                    'view-design': 'iview',
                    'vue-router': 'VueRouter',
                    iview: 'ViewUI',
                    vuex: 'Vuex'
                }
            }
        }
    },
    css: {
        extract: process.env.NODE_ENV === 'production' ? {
            ignoreOrder: true,
        } : false
    },
    // 测试环境下打包sourceMap
    productionSourceMap: false
}
