Inspired by C conditionals directive, conditional loader decides if a given block should be included in the final bundle.
Useful for removing instrumentation code and making your final production bundle smaller (therefore faster).
npm install --save-dev webpack-conditional-loader
webpack-conditional-loader as the last loader in the array, so it will process the code before
module:rules:test: /\.js$/use: 'babel-loader' 'webpack-conditional-loader'
Get an example config file here
On your code
// #if expression and
// #endif to wrap blocks of code you want to be removed if a given
predicate is false.
// #if process.env.NODE_ENV === 'DEVELOPMENT'consoleconsole// #endif
In the example above, the code will be removed if the enviroment variable
NODE_ENV is not
DEVELOPMENT, removing unnecessary code from your production bundle.
The same technique can be used to prevent loading packages in the production bundle.
// #if process.env.NODE_ENV !== 'BUILD'// #endif