Vue 项目打包去除 console.log 输出的完整指南
在 Vue 项目中,可以通过在打包时使用 babel 插件来去掉所有的 console.log 输出。
首先,安装 babel 插件:babel-plugin-transform-remove-console
npm install babel-plugin-transform-remove-console --save-dev
然后,在项目的根目录下新建一个名为 babel.config.js 的文件,并添加以下配置:
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
],
plugins: [
['transform-remove-console', { 'exclude': [ 'error', 'warn'] }]
]
}
这里的 exclude 选项是用来排除一些不想去掉的 console 方法,比如 error 和 warn。
最后,重新运行打包命令即可去掉所有的 console.log 输出:
npm run build
注意:这种方法只会在打包时去掉 console.log 输出,开发环境下仍然会输出。如果需要在开发环境下也去掉输出,可以在开发环境的 babel 配置中添加相同的插件配置。
原文地址: https://www.cveoy.top/t/topic/o58r 著作权归作者所有。请勿转载和采集!