3
votes

With a VueJS project created by Vue CLI 4, you get Babel configured with this handy preset in babel.config.js:

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.

Installed the plugin as a dev dependency by: npm i -D babel-plugin-transform-remove-console

Then modified 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?

Is Vue CLI's preset somehow mixing it up for not being able to set plugins via this configuration file?

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.

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.

2
cache is one thing messed up node_modules is different thing. On windows I had once similar problem and I had to remove manually node_modules and contents of AppData/local/temp and than reinstall modules - maybe this will solve your problem too.Zydnar

2 Answers

2
votes

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.

1
votes

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. There was some Vue CLI plugins that had different versions.

After nuking node_modules and upgrading all the packages this Babel plugin started working!