webpack-if
webpack-if
provides some helper methods to create webpack configurations with tests inside of an object literal so that your configuration can mutate due to the state of the execution environment without needing to split up construction that can leading to config builders or files with hard to follow files.
Install
Use it as a dev dependency with
npm install --save-dev webpack-if
or
yarn install -D webpack-if
Usage
webpack-if
's two main helpers are its ifElse
member and its exported function.
ifElse
called with a condition or test returns a function that can be called with two arguments. If the condition is true the first argument is returned. Otherwise the second argument is returned.
const webpackIf = ; const ifProd = webpackIf; moduleexports = entry: // ... plugins: ;
ifElse
simplest use is for testing some environment variable to determine the intent of the build. Is it for development or production where you may want to use different entry points, or plugins and other configuration.
In the above case if not part of a production build ifProd(() => new UglifyJsPlugin())
will return null
. Normally webpack will have an issue with null
in the plugins array or other parts of the configuration. To help clean up those null
values the exported value for webpack-if
itself is a function to do that job.
moduleexports = ;
Calling the webpackIf
with the configuration as an argument will let the function recursively go through the configuraiton and remove object keys and array indices with null
or undefined
values. After that empty objects and arrays are also cleaned up.
Here's a possible full webpack configuration using this library.
const webpackIf = ; const join = ; const ExtractTextPlugin = ;const HardSourceWebpackPlugin = ;const wepback = ; const CommonsChunkPlugin = webpackoptimizeCommonsChunkPlugin;const DefinePlugin = webpackDefinePlugin;const UglifyJsPlugin = webpackoptimizeUglifyJsPlugin; if !processenvNODE_ENV processenvNODE_ENV === 'development'; const nodeEnv = processenvNODE_ENV; const ifDev = webpackIf;const ifProd = webpackIf; moduleexports = ;