- https://www.jianshu.com/p/ce95b259b45e
- /* 以下 两个 是 webpack.prod.config.js***/
- new UglifyJsPlugin({
- uglifyOptions: {
- // 最紧凑的输出
- beautify: false,
- // 删除所有的注释
- comments: false,
- compress: {
- // 在 UglifyJs 删除没有用到的代码时不输出警告
- warnings: false,
- // 删除所有的 console 语句
- // 还可以兼容 ie 浏览器
- drop_console: true,
- // 内嵌定义了但是只用到一次的变量
- collapse_vars: true,
- // 提取出出现多次但是没有定义成变量去引用的静态值
- reduce_vars: true
- }
- },
- plugins: [
- // http://vuejs.github.io/vue-loader/en/workflow/production.html
- new webpack.DefinePlugin({'process.env': prodEnv}),
- new UglifyJsPlugin({
- uglifyOptions: {
- // 最紧凑的输出
- beautify: false,
- // 删除所有的注释
- comments: false,
- compress: {
- // 在 UglifyJs 删除没有用到的代码时不输出警告
- warnings: false,
- // 删除所有的 console 语句
- // 还可以兼容 ie 浏览器
- drop_console: true,
- // 内嵌定义了但是只用到一次的变量
- collapse_vars: true,
- // 提取出出现多次但是没有定义成变量去引用的静态值
- reduce_vars: true
- }
- },
- sourceMap: config.build.productionSourceMap,
- parallel: true
- }),
- if (config.build.productionGzip) {
- const CompressionWebpackPlugin = require('compression-webpack-plugin')
- webpackConfig.plugins.push(
- new CompressionWebpackPlugin({
- asset: '[path].gz[query]',
- algorithm: 'gzip',
- test: new RegExp(
- '\.(' +
- config.build.productionGzipExtensions.join('|') +
- ')$'
- ),
- // test: /.js$|.CSS$|.html$/,
- threshold: 10240,
- minRatio: 0
- })
- )
- }
- /**** config index.js
- productionSourceMap: false,
- productionGzip: true,
- *****/
- build: {
- // Template for index.html
- index: path.resolve(__dirname, '../dist/index.html'),
- // Paths
- assetsRoot: path.resolve(__dirname, '../dist'),
- assetsSubDirectory: 'static',
- assetsPublicPath: '/',
- /**
- * Source Maps
- */
- productionSourceMap: false,
- // https://webpack.js.org/configuration/devtool/#production
- devtool: '#source-map',
- // Gzip off by default as many popular static hosts such as
- // Surge or Netlify already gzip all static assets for you.
- // Before setting to `true`, make sure to:
- // npm install --save-dev compression-webpack-plugin
- productionGzip: true,
- productionGzipExtensions: ['js', 'css'],
- // Run the build command with an extra argument to
- // View the bundle analyzer report after build finishes:
- // `npm run build --report`
- // Set to `true` or `false` to always turn it on or off
- bundleAnalyzerReport: process.env.npm_config_report
- }
- /**** pack json "compression-webpack-plugin": "^1.1.11", *****/
来源: http://www.qdfuns.com/article/46106/780ccb2cbc2a9920a91fb6ecb057cb96.html