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?

使用 Vue CLI 4 创建的 VueJS 项目,您可以在 babel.config.js:

中使用这个方便的预设配置 Babel
module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
  ],
};

我正在尝试使用 babel-plugin-transform-remove-console 从构建的 JS 文件中删除 console.*

通过以下方式将插件安装为开发依赖项:npm i -D babel-plugin-transform-remove-console

然后修改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'],
  };
};

这应该在 运行 npm run serve -- --reset-cache 之前工作,我也尝试过多次在不同的环境中构建应用程序,但控制台日志记录仍然显示在浏览器的控制台中?

Vue CLI 的预设是否因无法通过此配置文件设置插件而以某种方式混淆了它?

更新: 创建了 a bug report to Vue CLI repo,并且在创建一个最小的错误复制存储库时,我发现这个插件正在与一个新项目一起工作.

但是,我不知道是什么原因造成的,因为我已经将此应用程序与最新的 CLI 引导模板同步,并且还尝试通过 `npm cache clean --force 对 NPM 缓存进行核对。

似乎@Zydnar 对 node_modules 文件夹进行核对的建议可能有所帮助,但是,我还发现我最近的 NPM 包升级已被中断并且没有完全成功。 有些 Vue CLI 插件有不同的版本。

核对 node_modules 并升级所有包后,此 Babel 插件开始工作!

我运行陷入同样的​​问题。这没有用:

plugins: ['transform-remove-console']

但这行得通:

plugins: [['transform-remove-console', { exclude: ['error', 'warn'] }]]

希望这对遇到同样问题的其他人有所帮助。