In Webpack, I have the following plugins:
plugins: [ new ExtractTextPlugin('styles.css'), new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, drop_console: true, }), ]
I would like to apply the UglifyJsPlugin
only for a specific target, so I tried using my intended conditional:
plugins: [ new ExtractTextPlugin('styles.css'), (TARGET === 'build') && new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, drop_console: true, }), ]
However, this fails, showing the following error message:
E:\myProject\node_modules\tapable\lib\Tapable.js:164 arguments[i].apply(this); ^ TypeError: arguments[i].apply is not a function
Note that the above code is similar to having false
at the end of the plugins
array (which emits the same error):
plugins: [ new ExtractTextPlugin('styles.css'), false ]
So, the question is: Is there a way to have conditional plugins on Webpack? (other than using variables?)
A webpack plugin is a JavaScript object that has an apply method. This apply method is called by the webpack compiler, giving access to the entire compilation lifecycle.
Loaders work at the individual file level during or before the bundle is generated. Plugins: Plugins work at bundle or chunk level and usually work at the end of the bundle generation process.
Oct 29, 2021. Webpack's DefinePlugin() function lets you replace a given token in the compiled code with another token. A common use case is using it to define environment variables when you cannot use an . env file directly.
You can use this syntax which uses the spread operator
plugins: [ new MiniCssExtractPlugin({ filename: '[name].css' }), ...(prod ? [] : [new BundleAnalyzerPlugin()]), ],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With