[英]Babel plugin transform-remove-console not working with Vue CLI 4 @vue/cli-plugin-babel/preset?
With a VueJS project created by Vue CLI 4, you get Babel configured with this handy preset in babel.config.js
:使用由 Vue CLI 4 创建的 VueJS 项目,您可以使用
babel.config.js
这个方便的预设配置 Babel:
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset',
],
};
I'm trying to use babel-plugin-transform-remove-console to remove console.*
from the built JS files.我正在尝试使用babel-plugin-transform-remove-console从构建的 JS 文件中删除
console.*
。
Installed the plugin as a dev dependency by: npm i -D babel-plugin-transform-remove-console
通过以下方式安装插件作为开发依赖项:
npm i -D babel-plugin-transform-remove-console
Then modified babel.config.js
:然后修改
babel.config.js
:
module.exports = (api) => {
var env = api.cache(() => process.env.NODE_ENV);
var plugins = [];
// Change to 'production' when configs are working
if (env === 'development') {
plugins.push(['transform-remove-console', { exclude: ['error', 'warn'] }]);
}
return {
presets: ['@vue/cli-plugin-babel/preset'],
// plugins,
// Doesn't work even when always on?
plugins: ['transform-remove-console'],
};
};
This should work by running npm run serve -- --reset-cache
, and I've also tried building the app many times with different environments, but the console loggings are still showing up in the browser's console?这应该通过运行
npm run serve -- --reset-cache
,我还尝试在不同的环境中多次构建应用程序,但控制台日志记录仍然显示在浏览器的控制台中?
Is Vue CLI's preset somehow mixing it up for not being able to set plugins via this configuration file? Vue CLI 的预设是否因无法通过此配置文件设置插件而以某种方式混淆?
UPDATE: Created a bug report to Vue CLI repo , and while creating a minimal bug reproduction repository, I found out this plugin is working with a new project .更新:为 Vue CLI repo创建了一个错误报告,在创建一个最小的错误再现存储库时,我发现这个插件正在处理一个新项目。
However, I have no idea what is causing this as I've synced this application with the newest CLI bootstrapped template, and also tried nuking NPM cache by `npm cache clean --force.但是,我不知道是什么导致了这种情况,因为我已经将这个应用程序与最新的 CLI 引导模板同步,并且还尝试通过 `npm cache clean --force 核对 NPM 缓存。
I ran into the same problem.我遇到了同样的问题。 This did not work:
这不起作用:
plugins: ['transform-remove-console']
But this worked:但这有效:
plugins: [['transform-remove-console', { exclude: ['error', 'warn'] }]]
Hope this helps others who encounter the same issue.希望这可以帮助遇到相同问题的其他人。
It seems @Zydnar's suggestion of nuking node_modules
folder may have helped, however, I also found out that my recent NPM packages upgrades had been interrupted and had not been fully successful.似乎@Zydnar 建议使用 nuking
node_modules
文件夹可能有所帮助,但是,我还发现我最近的 NPM 软件包升级已被中断并且没有完全成功。 There was some Vue CLI plugins that had different versions.有一些 Vue CLI 插件有不同的版本。
After nuking node_modules
and upgrading all the packages this Babel plugin started working!在 nuking
node_modules
并升级所有包之后,这个 Babel 插件开始工作了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.