Webpack Configurator
Install
$ npm install webpack-configurator
Motivation
In a number of my old projects, I found it difficult to DRY up the configuration files. My setup often contained a number of build modes (e.g. dev, test, and production), each sharing similar parts to one another. These common chunks were placed in a 'base' build mode. I wanted to still maintain the flexibility of including build mode specific configuration, while at the same time making slight changes to things such as loader query strings. In the end, I still found that my build mode files contained repetitive boilerplate code that I really wanted to avoid.
API
config.merge(config)
Arguments
config
(Object|Function): If an object is passed, this will be merged with the current value of config._config using the default way of merging (concat arrays nested within the data structure). If a function is passed, the first parameter will be a copy of the value contained within config._config. You can then make all the necessary changes to the data structure before returning the new value.
Returns
(Object)
: The config object to allow function chaining.
Example
// Config as an object.config; // Config as a function.config;
config.loader(key, config, resolver)
Provides a way of adding loaders to the config. You can add two other types of loaders using config.preLoader
and config.postLoader
.
Arguments
key
(String): Name of the loader. This is used to differentiate between loaders when merging/extending. When resolving, this value is used as a fallback for the 'loader' property value.config
(Object|Function): If an object is passed, this will be merged with the current value of the loader's config using the default way of merging (concat arrays nested within the data structure). If a function is passed, the first parameter will be a copy of the loader's config. You can then make all the necessary changes to the data structure before returning the new value.resolver
(Function): This works in a similar way to theconfig
parameter, however, it is only called when resolving. It provides an opportunity to make final changes once the configuration is has been completely merged. Note: If the loader already has a resolver, the value will simply get replaced.
Returns
(Object)
: The config object to allow function chaining.
Examples
Config as an object.
config;
Config as a function.
config;
Config as an object with a resolver function.
var ExtractTextPlugin = ; config;
config.removeLoader(key)
Provides a way to remove loaders without directly modifying internal data structures on the instance. You can remove two other types of loaders using the following: config.removePreLoader(key)
and config.removePostLoader(key)
.
Arguments
key
(String): Name of the loader you wish to remove. This is the same value used when calling the 'loader' method.
Returns
(Object)
: The config object to allow function chaining.
Example
// Create a loader with the key 'dustjs-linkedin'config; // Remove the loader using the same key as above.config;
config.plugin(key, constructor, parameters)
Arguments
key
(String): Name of the plugin. This is used to differentiate between plugins when merging/extending.constructor
(Class): The class constructor that you wish to be instantiated when resolving. Note: If the plugin already has a constructor, the value will simply get replaced. You may merge/extendparameters
by passing null for this parameter.parameters
(Array|Function): If an array is passed, this will be merged with the current value of the plugin's parameters using the default way of merging (concat arrays nested within the data structure). If a function is passed, the first parameter will be a copy of the plugin's parameters array. You can then make all the necessary changes to the data structure before returning the new value. Note This must be an array.
Returns
(Object)
: The config object to allow function chaining.
Examples
Parameters as an array.
var Webpack = ; config;
Parameters as a function.
var Webpack = ; config;
config.removePlugin(key)
Arguments
key
(String): Name of the plugin you wish to remove. This is the same value used when calling the 'plugin' method.
Returns
(Object)
: The config object to allow function chaining.
Example
var Webpack = ; // Create a plugin with the key 'webpack-define'.config; // Remove the plugin using the same key as above.config;
config.resolve()
Call when you want to return a complete Webpack configuration object, typically at the end. It can be called numerous times since it doesn't produce any side effects.
Returns
(Object)
: A valid Webpack configuration object
Examples
A simple webpack.config.js file demonstrating the module's use.
var Config = ;var Webpack = ; moduleexports = { var config = ; config; config; config; config; return config;};