简体   繁体   English

Babel 插件 transform-remove-console 不适用于 Vue CLI 4 @vue/cli-plugin-babel/preset?

[英]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.

相关问题 如果从软件包的依赖项安装了cli-plugin-babel,则不会加载babel.config.js - babel.config.js not loaded if cli-plugin-babel is installed from a package's dependencies 无法使用babel-plugin-transform-object-rest-spread为vue cli 3设置传播对象 - Not able to spread an object with babel-plugin-transform-object-rest-spread added for vue cli 3 setup babel-cli没有使用最新的预设 - babel-cli not working with latest preset 当我尝试使用babel cli转换目录中的js文件时,“ReferenceError Unknown plugin” - “ReferenceError Unknown plugin” when I try to transform js files in a directory using babel cli 如何安装 babel-cli babel-preset-react - how to install babel-cli babel-preset-react 没有找到与 @babel/plugin-transform-object-super@^7.8.3 匹配的版本。 使用 Vue.js - No matching version found for @babel/plugin-transform-object-super@^7.8.3. with Vue.js Vue 3 CLI - 如何为Object.entries添加babel polyfill - Vue 3 CLI - How to add babel polyfill for Object.entries Vue-CLI-3:添加Babel Transpile并进行最小化/模糊处理 - Vue-cli-3: Add babel transpile and minify/obfuscate 模块@vue/cli-plugin-unit-jest 应该在根目录下有“jest-preset.js”或“jest-preset.json”文件 - Module @vue/cli-plugin-unit-jest should have "jest-preset.js" or "jest-preset.json" file at the root 如何覆盖babel的预设插件选项 - How to overwrite babel's preset plugin options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM