vue打包大小

1.安装npm i compression-webpack-plugin

2. 复制一下代码到文件vue.comfig.js

const path = require('path')

const CompressionWebpackPlugin = require('compression-webpack-plugin')

// const VueLoaderPlugin = require('vue-loader/lib/plugin')

const resolve = dir => {

  return path.join(__dirname, dir)

}

const isProduction = process.env.NODE_ENV === 'production'

const plugins = []

if (isProduction) {

  plugins.push(

    new CompressionWebpackPlugin({

      filename: '[path].gz[query]',

      algorithm: 'gzip',

      // test: /\.css$|\.ttf$|\.html$|\.svg$|\.json$|\.js$/,

      test: /\.css$|\.ttf$|\.svg$|\.json$|\.js$/,

      threshold: 0, // 只有大小大于该值的资源会被处理

      minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理

      deleteOriginalAssets: true// 删除原文件

    })

  )

}

module.exports = {

  productionSourceMap: false,

  // 如果你不需要使用eslint,把lintOnSave设为false即可

  // lintOnSave: false,

  devServer: {

    overlay: {

      warnings: true,

      errors: true

    }

  },

  chainWebpack: config => {

    config.resolve.alias.set('@', resolve('src'))

    // config.output.filename('[name].[hash].js').end()

    config.plugins.delete('preload') // TODO: need test

    config.plugins.delete('prefetch') // TODO: need test

    config.module

      .rule('vue')

      .use('vue-loader')

      .loader('vue-loader')

      .tap(options => {

        options.compilerOptions.preserveWhitespace = true

        return options

      })

      .end()

    config.entry('index').add('babel-polyfill')

    config

      .when(process.env.NODE_ENV !== 'development',

        config => {

          config

            .plugin('ScriptExtHtmlWebpackPlugin')

            .after('html')

            .use('script-ext-html-webpack-plugin', [{

            // `runtime` must same as runtimeChunk name. default is `runtime`

              inline: /runtime\..*\.js$/

            }])

            .end()

          config

            .optimization.splitChunks({

              chunks: 'all',

              cacheGroups: {

                libs: {

                  name: 'chunk-libs',

                  test: /[\\/]node_modules[\\/]/,

                  priority: 10,

                  chunks: 'initial' // only package third parties that are initially dependent

                },

                elementUI: {

                  name: 'chunk-elementUI', // split elementUI into a single package

                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app

                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm

                },

                commons: {

                  name: 'chunk-commons',

                  test: resolve('src/components'), // can customize your rules

                  minChunks: 3, //  minimum common number

                  priority: 5,

                  reuseExistingChunk: true

                }

              }

            })

          config.optimization.runtimeChunk('single')

        }

      )

    config.module

      .rule('images')

      .use('image-webpack-loader')

      .loader('image-webpack-loader')

      .options({

        bypassOnDebug: true

      })

      .end()

  },

  configureWebpack: {

    // plugins: []

  }

}


3.在package.json配置

4.开始打包

npm run build

你可能感兴趣的:(vue打包大小)