项目打包上线如何在项目生产环境去除console log

733 阅读1分钟

首先下载一个babel-plugin-transform-remove-console的包(npm i babel-plugin-transform-remove-console)

image.png 然后在babel.config.js文件中配置如下: (添加红色框框内配置代码)

image.png

const prodPlugin = []

const bool = process.env.NODE_ENV === 'production'
// bool && prodPlugin.push()
if (bool) {
  prodPlugin.push('transform-remove-console')
}
module.exports = {
  presets: [
    // https://github.com/vuejs/vue-cli/tree/master/packages/@vue/babel-preset-app
    '@vue/cli-plugin-babel/preset'
  ],
  env: {
    development: {
      // babel-plugin-dynamic-import-node plugin only does one thing by converting all import() to require().
      // This plugin can significantly increase the speed of hot updates, when you have a large number of pages.
      // https://panjiachen.github.io/vue-element-admin-site/guide/advanced/lazy-loading.html
      plugins: ['dynamic-import-node']
    },
    production: {
      plugins: [...prodPlugin]
    }
  }
}