尝试多次终于配置成功,jenkins自动构建正常,使用命令行打包构建及运行均正常
注意compression-webpack-plugin的版本要旧一点,用最新报错,我用的6.1.1
vue.config.js中配置如下:
现在顶部引入:

const CompressionPlugin = require('compression-webpack-plugin')
在这  configureWebpack: config => {
    config.devtool = 'source-map'
    // 生产环境取消 console.log
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
      // 为生产环境修改配置...
      config.mode = 'production'
      // 将每个依赖包打包成单独的js文件
      let optimization = {
        runtimeChunk: 'single',
        splitChunks: {
          chunks: 'all',
          maxInitialRequests: Infinity,
          minSize: 20000, // 依赖包超过20000bit将被单独打包
          cacheGroups: {
            vendor: {
              test: /[\\/]node_modules[\\/]/,
              name (module) {
                // get the name. E.g. node_modules/packageName/not/this/part.js
                // or node_modules/packageName
                const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
                // npm package names are URL-safe, but some servers don't like @ symbols
                return `npm.${packageName.replace('@', '')}`
              }
            }
          }
        }
      }
      config.optimization = { ...config.optimization, ...optimization }
      let plugins = [
        new CompressionPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: new RegExp(
            '\\.(' +
            ['js', 'css'].join('|') +
            ')$'
          ),
          threshold: 10240,
          minRatio: 0.8
        })
      ]
      config.plugins = [...config.plugins, ...plugins]
    }
  },里插入代码片
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐