Run Webpack plugin only with flag

1.4k views Asked by At

i have some plugin in my Webpack config:

plugins: [
    new somePlugin()
]

But i want does not start this plugin when i just run npm run start, but run if i add some flags, like npm run start --plugin for example. Is it possible to implement this without dividing the Webpack into different configurations?

1

There are 1 answers

0
Muhammad Danial Iqbal On

You can do this by passing env variables with the webpack command

For Reference read this https://webpack.js.org/api/cli/#environment-options

run => webpack --env.production

Change the module.exports in webpack.config.js as this

module.exports = function(env, argv) {
  return {
    mode: env.production ? 'production' : 'development',
    devtool: env.production ? 'source-maps' : 'eval',
     plugins: [
       new TerserPlugin({
         terserOptions: {
          compress: argv['optimize-minimize'] // only if -p or --optimize-minimize were passed
         }
       })
     ]
  };
};

Pass any env variable and use it to conditionally push plugins in your config.