1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const CompressionWebpackPlugin = require('compression-webpack-plugin');
 
module.exports = {
    configureWebpack: config => {
        if (process.env.UNI_PLATFORM === 'h5') {
            filePath = 'static/js/'; //打包文件存放文件夹路径
            Timestamp = '.' + new Date().getTime(); //时间戳
        }
 
        config.plugins.push(
            new CompressionWebpackPlugin({
                filename: "[path][base].gz",
                algorithm: "gzip",
                test: /\.js$/,
                threshold: 10240,
                minRatio: 0.8,
                exclude: /node_modules/,
                deleteOriginalAssets: false // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
            })
        );
    },
    chainWebpack: config => {
        //通过运行 vue inspect plugins 的值html插件别名。。
 
        config.when(process.env.UNI_PLATFORM === 'h5', config => {
            config.plugin('html-index').tap(args => {
                args[0].minify = {
                    removeAttributeQuotes: false
                }
                return args
            })
        })
        config.when(process.env.UNI_PLATFORM === 'h5'&&process.env.NODE_ENV === "production", config => {
            config.plugin('html-index').tap(args => {
                args[0].minify = {
                    removeAttributeQuotes: false
                }
                return args
            })
            config.output.filename("static/js/[name]-[contenthash].js");
            config.output.chunkFilename("static/js/[id]-[chunkhash].js");
            config.optimization.splitChunks({
                name: false, //官方建议在生产环境时将 name 设置为 false,为了“it doesn't change names unnecessarily”
                hidePathInfo: true,
            })
 
        })
    },
}