Webpack API
API
webpack.init(env: IWebpackEnv)
Required: initialize the internal env
object that’s used throughout the config building process.
The passed env should be an object containing key-value pairs. This is generally handled by webpack.
webpack.useConfig(config: string | false)
Optional: specify base config - defaults to auto-discovery.
Passing false
will opt out from using a base config; however, this is generally never recommended.
webpack.chainWebpack(chainFn, options?)
Optional: add a new chainFn
to the internal chain that will be called while resolving the final config.
The chainFn
should be a function that accepts 2 parameters —
config
and env
.
The options
is an optional object with the following optional properties:
-
order: number
: controls the order in which thechainFn
should be applied.Useful when related plugins rely on changes made in the right order. For example,
plugin1
can specifyorder: 1
andplugin2
can specifyorder: 2
- this will guarantee thatplugin1
'schainFn
is called first, and thatplugin2
can rely on values set byplugin1
.
Example: Force production mode
webpack.chainWebpack((config, env) => {
config.mode('production')
})
Example: Run a config "last"
Setting order: 10
doesn’t necessarily guarantee the chainFn
will be applied last, since other calls to chainWebpack
could specify a higher number.
We recommend against setting higher values, and using 10
as a conventional "last".
webpack.chainWebpack(
(config, env) => {
config.set('somethingThatShouldBeSetLast', true)
},
{ order: 10 }
)
webpack.mergeWebpack(mergeFnOrObject)
Optional: merges an object (or an object returned by a function) into the resolved chain config.
Example
// merge an object into the internal config
webpack.mergeWebpack({
something: true
})
// or pass a function that returns an object
webpack.mergeWebpack(env => {
return {
something: true
}
})