Vue CLI4.x 配置指南

Vue CLI4.x 配置指南

持续更新中

Version

Vue CLI 测试版本:@vue/cli 4.5.4

Table of content

⚡去掉console.log

// 内置插件不需要安装
const TerserPlugin = require('terser-webpack-plugin')

// 方法一:简单配置
module.exports = {
  optimization: {
    minimizer: [
      new TerserPlugin({
          terserOptions: {
              compress: {
                  drop_console: true,
                  drop_debugger: true,
              },
          },
          sourceMap: false,
      })
    ],
  },
};
// 方法二:基于环境有条件地配置
module.exports = {
  configureWebpack: (config) => {
          if (process.env.NODE_ENV === 'production') {
              const plugins = []
              plugins.push(
                  new TerserPlugin({
                      terserOptions: {
                          compress: {
                              drop_console: true,
                              drop_debugger: true,
                          },
                      },
                      sourceMap: false,
                  })
              )
              config.optimization.minimizer = [
                  ...config.optimization.minimizer,
                  ...plugins,
              ]
          }
      },
}

⚡添加打包分析

  • Install
# NPM
npm install --save-dev webpack-bundle-analyzer
# Yarn
yarn add -D webpack-bundle-analyzer

  • Usage
// 方法一:通过configureWebpack选项配置
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
  configureWebpack: (config) => {
          if (process.env.NODE_ENV === 'production') {
              const plugins = []
              plugins.push(
                  new BundleAnalyzerPlugin()
              )
              config.plugins = [
                  ...config.plugins,
                  ...plugins,
              ]
          }
      },
}

License

MIT

repo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐