安装babel-plugin-transform-remove-console 项目打包去除console
npm install babel-plugin-transform-remove-console --save-dev
在vue项目中babel.config.js中:
module.exports = {
plugins: [
"transform-remove-console",
],
};
如果只想在生产环境来去除console的话
建立一个生产环境配置数组来判断一下当前环境是否为生产环境,
是生产环境的话就放入transform-remove-console
// 创建生产环境中的plugins数组
const prodPlugins = [];
// 判断当前环境是否为生产环境 如果是生产环境把transform-remove-console放入数组中
if (process.env.NODE_ENC === "production") {
prodPlugins.push("transform-remove-console");
}
module.exports = {
plugins: [
// 生产环境下plugins 扩展运算符把数组展开
...prodPlugins,
],
};
更多使用属性参考babel-plugin-transform-remove-console的官方文档
babel-plugin-transform-remove-console · Babel 中文网## Examplehttps://www.babeljs.cn/docs/babel-plugin-transform-remove-consolebabel-plugin-transform-remove-console - npmRemove all console.* calls.. Latest version: 6.9.4, last published: 5 years ago. Start using babel-plugin-transform-remove-console in your project by running `npm i babel-plugin-transform-remove-console`. There are 203 other projects in the npm registry using babel-plugin-transform-remove-console.https://www.npmjs.com/package/babel-plugin-transform-remove-console