Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

webpack-5-chain

sorrycc283.8kMPL-2.08.0.2TypeScript support: included

[![NPM version][npm-image]][npm-url] [![NPM downloads][npm-downloads]][npm-url] [![CI Status][ci-image]][ci-url]

webpack, config, chain, fluent, api

readme

webpack-chain

NPM version NPM downloads CI Status

Use a chaining API to generate and simplify the modification of webpack 4 configurations.

This documentation corresponds to v6 of webpack-chain. For previous versions, see:

Note: while webpack-chain is utilized extensively in Neutrino, this package is completely standalone and can be used by any project.

Chinese docs(中文文档)

Introduction

webpack's core configuration is based on creating and modifying a potentially unwieldy JavaScript object. While this is OK for configurations on individual projects, trying to share these objects across projects and make subsequent modifications gets messy, as you need to have a deep understanding of the underlying object structure to make those changes.

webpack-chain attempts to improve this process by providing a chainable or fluent API for creating and modifying webpack configurations. Key portions of the API can be referenced by user-specified names, which helps to standardize how to modify a configuration across projects.

This is easier explained through the examples following.

Installation

webpack-chain requires Node.js 10 or higher. webpack-chain also only creates configuration objects designed for use with webpack 4.

You may install this package using either Yarn or npm (choose one):

Yarn

yarn add --dev webpack-chain

npm

npm install --save-dev webpack-chain

Getting Started

Once you have webpack-chain installed, you can start creating a webpack configuration. For this guide, our example base configuration will be webpack.config.js in the root of our project directory.

// Require the webpack-chain module. This module exports a single
// constructor function for creating a configuration API.
const Config = require('webpack-chain');

// Instantiate the configuration with a new API
const config = new Config();

// Make configuration changes using the chain API.
// Every API call tracks a change to the stored configuration.

config
  // Interact with entry points
  .entry('index')
    .add('src/index.js')
    .end()
  // Modify output settings
  .output
    .path('dist')
    .filename('[name].bundle.js');

// Create named rules which can be modified later
config.module
  .rule('lint')
    .test(/\.js$/)
    .pre()
    .include
      .add('src')
      .end()
    // Even create named uses (loaders)
    .use('eslint')
      .loader('eslint-loader')
      .options({
        rules: {
          semi: 'off'
        }
      });

config.module
  .rule('compile')
    .test(/\.js$/)
    .include
      .add('src')
      .add('test')
      .end()
    .use('babel')
      .loader('babel-loader')
      .options({
        presets: [
          ['@babel/preset-env', { modules: false }]
        ]
      });

// Create named plugins too!
config
  .plugin('clean')
    .use(CleanPlugin, [['dist'], { root: '/dir' }]);

// Export the completed configuration object to be consumed by webpack
module.exports = config.toConfig();

Having shared configurations is also simple. Just export the configuration and call .toConfig() prior to passing to webpack.

// webpack.core.js
const Config = require('webpack-chain');
const config = new Config();

// Make configuration shared across targets
// ...

module.exports = config;

// webpack.dev.js
const config = require('./webpack.core');

// Dev-specific configuration
// ...
module.exports = config.toConfig();

// webpack.prod.js
const config = require('./webpack.core');

// Production-specific configuration
// ...
module.exports = config.toConfig();

ChainedMap

One of the core API interfaces in webpack-chain is a ChainedMap. A ChainedMap operates similar to a JavaScript Map, with some conveniences for chaining and generating configuration. If a property is marked as being a ChainedMap, it will have an API and methods as described below:

Unless stated otherwise, these methods will return the ChainedMap, allowing you to chain these methods.

// Remove all entries from a Map.
clear()
// Remove a single entry from a Map given its key.
// key: *
delete(key)
// Fetch the value from a Map located at the corresponding key.
// key: *
// returns: value
get(key)
// Fetch the value from a Map located at the corresponding key.
// If the key is missing, the key is set to the result of function fn.
// key: *
// fn: Function () -> value
// returns: value
getOrCompute(key, fn)
// Set a value on the Map stored at the `key` location.
// key: *
// value: *
set(key, value)
// Returns `true` or `false` based on whether a Map as has a value set at a
// particular key.
// key: *
// returns: Boolean
has(key)
// Returns an array of all the values stored in the Map.
// returns: Array
values()
// Returns an object of all the entries in the backing Map
// where the key is the object property, and the value
// corresponding to the key. Will return `undefined` if the backing
// Map is empty.
// This will order properties by their name if the value is
// a ChainedMap that used .before() or .after().
// returns: Object, undefined if empty
entries()
// Provide an object which maps its properties and values
// into the backing Map as keys and values.
// You can also provide an array as the second argument
// for property names to omit from being merged.
// obj: Object
// omit: Optional Array
merge(obj, omit)
// Execute a function against the current configuration context
// handler: Function -> ChainedMap
  // A function which is given a single argument of the ChainedMap instance
batch(handler)
// Conditionally execute a function to continue configuration
// condition: Boolean
// whenTruthy: Function -> ChainedMap
  // invoked when condition is truthy, given a single argument of the ChainedMap instance
// whenFalsy: Optional Function -> ChainedMap
  // invoked when condition is falsy, given a single argument of the ChainedMap instance
when(condition, whenTruthy, whenFalsy)

ChainedValueMap

ChainedValueMap inherited ChainedMap but callable. call it by value means to set it to it and clean all data in map. set any key/value in map will clear value setted by call it.

It is chainable,so calling it will return the original instance, allowing you to continue to chain. For example, config.node is a ChainedValueMap instance, so it can be used as:

// call as function will setting a value on a ChainedMap
config.node(false);

// use as `ChainedMap`
config.node.set('amd', 'true');

ChainedSet

Another of the core API interfaces in webpack-chain is a ChainedSet. A ChainedSet operates similar to a JavaScript Set, with some conveniences for chaining and generating configuration. If a property is marked as being a ChainedSet, it will have an API and methods as described below:

Unless stated otherwise, these methods will return the ChainedSet, allowing you to chain these methods.

// Add/append a value to the end of a Set.
// value: *
add(value)
// Add a value to the beginning of a Set.
// value: *
prepend(value)
// Remove all values from a Set.
clear()
// Remove a specific value from a Set.
// value: *
delete(value)
// Returns `true` or `false` based on whether or not the
// backing Set contains the specified value.
// value: *
// returns: Boolean
has(value)
// Returns an array of values contained in the backing Set.
// returns: Array
values()
// Concatenates the given array to the end of the backing Set.
// arr: Array
merge(arr)
// Execute a function against the current configuration context
// handler: Function -> ChainedSet
  // A function which is given a single argument of the ChainedSet instance
batch(handler)
// Conditionally execute a function to continue configuration
// condition: Boolean
// whenTruthy: Function -> ChainedSet
  // invoked when condition is truthy, given a single argument of the ChainedSet instance
// whenFalsy: Optional Function -> ChainedSet
  // invoked when condition is falsy, given a single argument of the ChainedSet instance
when(condition, whenTruthy, whenFalsy)

Shorthand methods

A number of shorthand methods exist for setting a value on a ChainedMap with the same key as the shorthand method name. For example, devServer.hot is a shorthand method, so it can be used as:

// A shorthand method for setting a value on a ChainedMap
devServer.hot(true);

// This would be equivalent to:
devServer.set('hot', true);

A shorthand method is chainable, so calling it will return the original instance, allowing you to continue to chain.

Config

Create a new configuration object.

const Config = require('webpack-chain');

const config = new Config();

Moving to deeper points in the API will change the context of what you are modifying. You can move back to the higher context by either referencing the top-level config again, or by calling .end() to move up one level. If you are familiar with jQuery, .end() works similarly. All API calls will return the API instance at the current context unless otherwise specified. This is so you may chain API calls continuously if desired.

For details on the specific values that are valid for all shorthand and low-level methods, please refer to their corresponding name in the webpack docs hierarchy.

Config : ChainedMap

Config shorthand methods

config
  .context(context)
  .mode(mode)
  .devtool(devtool)
  .target(target)
  .watch(watch)
  .watchOptions(watchOptions)
  .externals(externals)
  .externalsType(externalsType)
  .externalsPresets(externalsPresets)
  .stats(stats)
  .experiments(experiments)
  .amd(amd)
  .bail(bail)
  .cache(cache)
  .dependencies(dependencies)
  .ignoreWarnings(ignoreWarnings)
  .loader(loader)
  .parallelism(parallelism)
  .profile(profile)
  .recordsPath(recordsPath)
  .recordsInputPath(recordsInputPath)
  .recordsOutputPath(recordsOutputPath)
  .name(name)
  .infrastructureLogging(infrastructureLogging)
  .snapshot(snapshot)

Config entryPoints

// Backed at config.entryPoints : ChainedMap
config.entry(name) : ChainedSet

config
  .entry(name)
    .add(value)
    .add(value)

config
  .entry(name)
    .clear()

// Using low-level config.entryPoints:

config.entryPoints
  .get(name)
    .add(value)
    .add(value)

config.entryPoints
  .get(name)
    .clear()

Config output: shorthand methods

config.output : ChainedMap

config.output
  .auxiliaryComment(auxiliaryComment)
  .charset(charset)
  .chunkFilename(chunkFilename)
  .chunkLoadTimeout(chunkLoadTimeout)
  .chunkLoadingGlobal(chunkLoadingGlobal)
  .chunkLoading(chunkLoading)
  .chunkFormat(chunkFormat)
  .enabledChunkLoadingTypes(enabledChunkLoadingTypes)
  .crossOriginLoading(crossOriginLoading)
  .devtoolFallbackModuleFilenameTemplate(devtoolFallbackModuleFilenameTemplate)
  .devtoolModuleFilenameTemplate(devtoolModuleFilenameTemplate)
  .devtoolNamespace(devtoolNamespace)
  .filename(filename)
  .assetModuleFilename(assetModuleFilename)
  .globalObject(globalObject)
  .uniqueName(uniqueName)
  .hashDigest(hashDigest)
  .hashDigestLength(hashDigestLength)
  .hashFunction(hashFunction)
  .hashSalt(hashSalt)
  .hotUpdateChunkFilename(hotUpdateChunkFilename)
  .hotUpdateFunction(hotUpdateFunction)
  .hotUpdateMainFilename(hotUpdateMainFilename)
  .library(library)
  .libraryExport(libraryExport)
  .libraryTarget(libraryTarget)
  .importFunctionName(importFunctionName)
  .path(path)
  .pathinfo(pathinfo)
  .publicPath(publicPath)
  .scriptType(scriptType)
  .sourceMapFilename(sourceMapFilename)
  .sourcePrefix(sourcePrefix)
  .strictModuleErrorHandling(strictModuleErrorHandling)
  .strictModuleExceptionHandling(strictModuleExceptionHandling)
  .umdNamedDefine(umdNamedDefine)
  .workerChunkLoading(workerChunkLoading)
  .enabledLibraryTypes(enabledLibraryTypes)
  .environment(environment)
  .compareBeforeEmit(compareBeforeEmit)
  .wasmLoading(wasmLoading)
  .enabledWasmLoadingTypes(enabledWasmLoadingTypes)
  .iife(iife)
  .module(module)
  .clean(clean)

Config resolve: shorthand methods

config.resolve : ChainedMap

config.resolve
  .cachePredicate(cachePredicate)
  .cacheWithContext(cacheWithContext)
  .enforceExtension(enforceExtension)
  .symlinks(symlinks)
  .unsafeCache(unsafeCache)
  .preferRelative(preferRelative)
  .preferAbsolute(preferAbsolute)

Config resolve alias

config.resolve.alias : ChainedMap

config.resolve.alias
  .set(key, value)
  .set(key, value)
  .delete(key)
  .clear()

Config resolve aliasFields

config.resolve.aliasFields : ChainedSet

config.resolve.aliasFields
  .add(value)
  .prepend(value)
  .clear()

Config resolve descriptionFields

config.resolve.descriptionFields : ChainedSet

config.resolve.descriptionFields
  .add(value)
  .prepend(value)
  .clear()

Config resolve extensions

config.resolve.extensions : ChainedSet

config.resolve.extensions
  .add(value)
  .prepend(value)
  .clear()

Config resolve mainFields

config.resolve.mainFields : ChainedSet

config.resolve.mainFields
  .add(value)
  .prepend(value)
  .clear()

Config resolve mainFiles

config.resolve.mainFiles : ChainedSet

config.resolve.mainFiles
  .add(value)
  .prepend(value)
  .clear()

Config resolve exportsFields

config.resolve.exportsFields : ChainedSet

config.resolve.exportsFields
  .add(value)
  .prepend(value)
  .clear()

Config resolve importsFields

config.resolve.importsFields : ChainedSet

config.resolve.importsFields
  .add(value)
  .prepend(value)
  .clear()

Config resolve restrictions

config.resolve.restrictions : ChainedSet

config.resolve.restrictions
  .add(value)
  .prepend(value)
  .clear()

Config resolve roots

config.resolve.roots : ChainedSet

config.resolve.roots
  .add(value)
  .prepend(value)
  .clear()

Config resolve modules

config.resolve.modules : ChainedSet

config.resolve.modules
  .add(value)
  .prepend(value)
  .clear()

Config resolve plugin

config.resolve
  .plugin(name) : ChainedMap

Config resolve plugin: adding

NOTE: Do not use new to create the resolve plugin, as this will be done for you.

config.resolve
  .plugin(name)
  .use(WebpackResolvePlugin, args)

// Examples

config.resolve
  .plugin('resolve-css')
  .use(ResolveCSSPlugin, [{ cssBasePath: true }])

// Resolve plugins can also be specified by their path, allowing the expensive require()s to be
// skipped in cases where the plugin or webpack configuration won't end up being used.
config.resolve
  .plugin('resolve-css')
  .use(require.resolve('resolve-css-plugin'), [{ cssBasePath: true }])

Config resolve plugin: modify arguments

config.resolve
  .plugin(name)
  .tap(args => newArgs)

// Example
config.resolve
  .plugin('resolve-css')
  .tap(args => [...args, { cssBasePath: false }])

Config resolve plugin: modify instantiation

config.resolve
  .plugin(name)
  .init((Plugin, args) => new Plugin(...args));

Config resolve plugin: removing

config.resolve.plugins.delete(name)

--- end

Config resolve fallback

config.resolve.fallback : ChainedMap

config.resolve.fallback
  .set(key, value)
  .set(key, value)
  .delete(key)
  .clear()

Config resolve byDependency

config.resolve.byDependency : ChainedMap

config.resolve.byDependency
  .set(key, value)
  .set(key, value)
  .delete(key)
  .clear()

Config resolveLoader

The API for config.resolveLoader is identical to config.resolve with the following additions:

Config resolveLoader modules

config.resolveLoader.modules : ChainedSet

config.resolveLoader.modules
  .add(value)
  .prepend(value)
  .clear()

Config resolveLoader moduleExtensions

config.resolveLoader.moduleExtensions : ChainedSet

config.resolveLoader.moduleExtensions
  .add(value)
  .prepend(value)
  .clear()

Config resolveLoader packageMains

config.resolveLoader.packageMains : ChainedSet

config.resolveLoader.packageMains
  .add(value)
  .prepend(value)
  .clear()

Config performance: shorthand methods

config.performance : ChainedValueMap

config.performance(false)
  .performance
  .assetFilter(assetFilter)
  .hints(hints)
  .maxEntrypointSize(maxEntrypointSize)
  .maxAssetSize(maxAssetSize)

Configuring optimizations: shorthand methods

config.optimization : ChainedMap

config.optimization
  .minimize(minimize)
  .runtimeChunk(runtimeChunk)
  .emitOnErrors(emitOnErrors)
  .moduleIds(moduleIds)
  .chunkIds(chunkIds)
  .nodeEnv(nodeEnv)
  .mangleWasmImports(mangleWasmImports)
  .removeAvailableModules(removeAvailableModules)
  .removeEmptyChunks(removeEmptyChunks)
  .mergeDuplicateChunks(mergeDuplicateChunks)
  .flagIncludedChunks(flagIncludedChunks)
  .providedExports(providedExports)
  .usedExports(usedExports)
  .concatenateModules(concatenateModules)
  .sideEffects(sideEffects)
  .portableRecords(portableRecords)
  .mangleExports(mangleExports)
  .innerGraph(innerGraph)
  .realContentHash(realContentHash)

Config optimization minimizers

// Backed at config.optimization.minimizers
config.optimization
  .minimizer(name) : ChainedMap

Config optimization minimizers: adding

NOTE: Do not use new to create the minimizer plugin, as this will be done for you.

config.optimization
  .minimizer(name)
  .use(WebpackPlugin, args)

// Examples

config.optimization
  .minimizer('css')
  .use(OptimizeCSSAssetsPlugin, [{ cssProcessorOptions: { safe: true } }])

// Minimizer plugins can also be specified by their path, allowing the expensive require()s to be
// skipped in cases where the plugin or webpack configuration won't end up being used.
config.optimization
  .minimizer('css')
  .use(require.resolve('optimize-css-assets-webpack-plugin'), [{ cssProcessorOptions: { safe: true } }])

Config optimization minimizers: modify arguments

config.optimization
  .minimizer(name)
  .tap(args => newArgs)

// Example
config.optimization
  .minimizer('css')
  .tap(args => [...args, { cssProcessorOptions: { safe: false } }])

Config optimization minimizers: modify instantiation

config.optimization
  .minimizer(name)
  .init((Plugin, args) => new Plugin(...args));

Config optimization minimizers: removing

config.optimization.minimizers.delete(name)

Config optimization splitChunks

config.optimization.splitChunks : ChainedValueMap

config.optimization
  .splitChunks({
    chunks: all
  }
  .set(key, value)
  .set(key, value)
  .delete(key)
  .clear()

Config plugins

// Backed at config.plugins
config.plugin(name) : ChainedMap

Config plugins: adding

NOTE: Do not use new to create the plugin, as this will be done for you.

config
  .plugin(name)
  .use(WebpackPlugin, args)

// Examples

config
  .plugin('hot')
  .use(webpack.HotModuleReplacementPlugin);

// Plugins can also be specified by their path, allowing the expensive require()s to be
// skipped in cases where the plugin or webpack configuration won't end up being used.
config
  .plugin('env')
  .use(require.resolve('webpack/lib/EnvironmentPlugin'), [{ 'VAR': false }]);

Config plugins: modify arguments

config
  .plugin(name)
  .tap(args => newArgs)

// Example
config
  .plugin('env')
  .tap(args => [...args, 'SECRET_KEY']);

Config plugins: modify instantiation

config
  .plugin(name)
  .init((Plugin, args) => new Plugin(...args));

Config plugins: removing

config.plugins.delete(name)

Config plugins: ordering before

Specify that the current plugin context should operate before another named plugin. You cannot use both .before() and .after() on the same plugin.

config
  .plugin(name)
    .before(otherName)

// Example

config
  .plugin('html-template')
    .use(HtmlWebpackTemplate)
    .end()
  .plugin('script-ext')
    .use(ScriptExtWebpackPlugin)
    .before('html-template');

Config plugins: ordering after

Specify that the current plugin context should operate after another named plugin. You cannot use both .before() and .after() on the same plugin.

config
  .plugin(name)
    .after(otherName)

// Example

config
  .plugin('html-template')
    .after('script-ext')
    .use(HtmlWebpackTemplate)
    .end()
  .plugin('script-ext')
    .use(ScriptExtWebpackPlugin);

Config resolve plugins

// Backed at config.resolve.plugins
config.resolve.plugin(name) : ChainedMap

Config resolve plugins: adding

NOTE: Do not use new to create the plugin, as this will be done for you.

config.resolve
  .plugin(name)
  .use(WebpackPlugin, args)

Config resolve plugins: modify arguments

config.resolve
  .plugin(name)
  .tap(args => newArgs)

Config resolve plugins: modify instantiation

config.resolve
  .plugin(name)
  .init((Plugin, args) => new Plugin(...args))

Config resolve plugins: removing

config.resolve.plugins.delete(name)

Config resolve plugins: ordering before

Specify that the current plugin context should operate before another named plugin. You cannot use both .before() and .after() on the same resolve plugin.

config.resolve
  .plugin(name)
    .before(otherName)

// Example

config.resolve
  .plugin('beta')
    .use(BetaWebpackPlugin)
    .end()
  .plugin('alpha')
    .use(AlphaWebpackPlugin)
    .before('beta');

Config resolve plugins: ordering after

Specify that the current plugin context should operate after another named plugin. You cannot use both .before() and .after() on the same resolve plugin.

config.resolve
  .plugin(name)
    .after(otherName)

// Example

config.resolve
  .plugin('beta')
    .after('alpha')
    .use(BetaWebpackTemplate)
    .end()
  .plugin('alpha')
    .use(AlphaWebpackPlugin);

Config node

config.node : ChainedValueMap

config.node(false)
  .node
  .set('__dirname', 'mock')
  .set('__filename', 'mock');

Config devServer

config.devServer : ChainedMap

Config devServer allowedHosts

config.devServer.allowedHosts : ChainedSet

config.devServer.allowedHosts
  .add(value)
  .prepend(value)
  .clear()

Config devServer: shorthand methods

config.devServer
  .after(after)
  .before(before)
  .bonjour(bonjour)
  .clientLogLevel(clientLogLevel)
  .compress(compress)
  .contentBase(contentBase)
  .contentBasePublicPath(contentBasePublicPath)
  .disableHostCheck(disableHostCheck)
  .filename(filename)
  .headers(headers)
  .historyApiFallback(historyApiFallback)
  .host(host)
  .hot(hot)
  .hotOnly(hotOnly)
  .http2(http2)
  .https(https)
  .index(index)
  .injectClient(injectClient)
  .injectHot(injectHot)
  .inline(inline)
  .lazy(lazy)
  .liveReload(liveReload)
  .mimeTypes(mimeTypes)
  .noInfo(noInfo)
  .onListening(onListening)
  .open(open)
  .openPage(openPage)
  .overlay(overlay)
  .pfx(pfx)
  .pfxPassphrase(pfxPassphrase)
  .port(port)
  .progress(progress)
  .proxy(proxy)
  .public(public)
  .publicPath(publicPath)
  .quiet(quiet)
  .serveIndex(serveIndex)
  .setup(setup)
  .socket(socket)
  .sockHost(sockHost)
  .sockPath(sockPath)
  .sockPort(sockPort)
  .staticOptions(staticOptions)
  .stats(stats)
  .stdin(stdin)
  .transportMode(transportMode)
  .useLocalIp(useLocalIp)
  .watchContentBase(watchContentBase)
  .watchOptions(watchOptions)
  .writeToDisk(writeToDisk)

Config module

config.module : ChainedMap

Config module: shorthand methods

config.module : ChainedMap

config.module
  .noParse(noParse)
  .unsafeCache(unsafeCache)
  .wrappedContextCritical(wrappedContextCritical)
  .exprContextRegExp(exprContextRegExp)
  .wrappedContextRecursive(wrappedContextRecursive)
  .strictExportPresence(strictExportPresence)
  .wrappedContextRegExp(wrappedContextRegExp)

Config module rules: shorthand methods

config.module.rules : ChainedMap

config.module
  .rule(name)
    .test(test)
    .pre()
    .post()
    .enforce(preOrPost)

Config module rules uses (loaders): creating

config.module.rules{}.uses : ChainedMap

config.module
  .rule(name)
    .use(name)
      .loader(loader)
      .options(options)

// Example

config.module
  .rule('compile')
    .use('babel')
      .loader('babel-loader')
      .options({ presets: ['@babel/preset-env'] });

Config module rules uses (loaders): modifying options

config.module
  .rule(name)
    .use(name)
      .tap(options => newOptions)

// Example

config.module
  .rule('compile')
    .use('babel')
      .tap(options => merge(options, {
        plugins: ['@babel/plugin-proposal-class-properties']
      }));

Config module rules nested rules:

config.module.rules{}.rules : ChainedMap<Rule>

config.module
  .rule(name)
    .rule(name)

// Example

config.module
  .rule('css')
    .test(/\.css$/)
    .use('style')
      .loader('style-loader')
      .end()
    .rule('postcss')
      .resourceQuery(/postcss/)
      .use('postcss')
        .loader('postcss-loader')

Config module rules nested rules: ordering before

Specify that the current rule context should operate before another named rule. You cannot use both .before() and .after() on the same rule.

config.module.rules{}.rules : ChainedMap<Rule>

config.module
  .rule(name)
    .rule(name)
      .before(otherName)

// Example

config.module
  .rule('css')
    .use('style')
      .loader('style-loader')
      .end()
    .rule('postcss')
      .resourceQuery(/postcss/)
      .use('postcss')
        .loader('postcss-loader')
        .end()
      .end()
    .rule('css-loader')
      .resourceQuery(/css-loader/)
      .before('postcss')
      .use('css-loader')
        .loader('css-loader')

Config module rules nested rules: ordering after

Specify that the current rule context should operate after another named rule. You cannot use both .before() and .after() on the same rule.

config.module.rules{}.rules : ChainedMap<Rule>

config.module
  .rule(name)
    .rule(name)
      .after(otherName)

// Example

config.module
  .rule('css')
    .use('style')
      .loader('style-loader')
      .end()
    .rule('postcss')
      .resourceQuery(/postcss/)
      .after('css-loader')
      .use('postcss')
        .loader('postcss-loader')
        .end()
      .end()
    .rule('css-loader')
      .resourceQuery(/css-loader/)
      .use('css-loader')
        .loader('css-loader')

Config module rules oneOfs (conditional rules):

config.module.rules{}.oneOfs : ChainedMap<Rule>

config.module
  .rule(name)
    .oneOf(name)

// Example

config.module
  .rule('css')
    .oneOf('inline')
      .resourceQuery(/inline/)
      .use('url')
        .loader('url-loader')
        .end()
      .end()
    .oneOf('external')
      .resourceQuery(/external/)
      .use('file')
        .loader('file-loader')

Config module rules oneOfs (conditional rules): ordering before

Specify that the current oneOf context should operate before another named oneOf. You cannot use both .before() and .after() on the same oneOf.

config.module
  .rule(name)
    .oneOf(name)
      .before()

// Example

config.module
  .rule('scss')
    .test(/\.scss$/)
    .oneOf('normal')
      .use('sass')
        .loader('sass-loader')
        .end()
      .end()
    .oneOf('sass-vars')
      .before('normal')
      .resourceQuery(/\?sassvars/)
      .use('sass-vars')
        .loader('sass-vars-to-js-loader')

Config module rules oneOfs (conditional rules): ordering after

Specify that the current oneOf context should operate after another named oneOf. You cannot use both .before() and .after() on the same oneOf.

config.module
  .rule(name)
    .oneOf(name)
      .after()

// Example

config.module
  .rule('scss')
    .test(/\.scss$/)
    .oneOf('vue')
      .resourceQuery(/\?vue/)
      .use('vue-style')
        .loader('vue-style-loader')
        .end()
      .end()
    .oneOf('normal')
      .use('sass')
        .loader('sass-loader')
        .end()
      .end()
    .oneOf('sass-vars')
      .after('vue')
      .resourceQuery(/\?sassvars/)
      .use('sass-vars')
        .loader('sass-vars-to-js-loader')

Config module rules resolve

Specify a resolve configuration to be merged over the default config.resolve for modules that match the rule.

See "Config resolve" sections above for full syntax.

Note: This option is supported by webpack since 4.36.1.

config.module
  .rule(name)
    .resolve

// Example

config.module
  .rule('scss')
    .test(/\.scss$/)
    .resolve
      .symlinks(true)

Merging Config

webpack-chain supports merging in an object to the configuration instance which matches a layout similar to how the webpack-chain schema is laid out.

Note: This object does not match the webpack configuration schema exactly (for example the [name] keys for entry/rules/plugins), so you may need to transform webpack configuration objects (such as those output by webpack-chain's .toConfig()) to match the layout below prior to passing to .merge().

config.merge({ devtool: 'source-map' });

config.get('devtool') // "source-map"
config.merge({
  [key]: value,

  amd,
  bail,
  cache,
  context,
  devtool,
  externals,
  loader,
  mode,
  parallelism,
  profile,
  recordsPath,
  recordsInputPath,
  recordsOutputPath,
  stats,
  target,
  watch,
  watchOptions,

  entry: {
    [name]: [...values]
  },

  plugin: {
    [name]: {
      plugin: WebpackPlugin,
      args: [...args],
      before,
      after
    }
  },

  devServer: {
    [key]: value,

    clientLogLevel,
    compress,
    contentBase,
    filename,
    headers,
    historyApiFallback,
    host,
    hot,
    hotOnly,
    https,
    inline,
    lazy,
    noInfo,
    overlay,
    port,
    proxy,
    quiet,
    setup,
    stats,
    watchContentBase
  },

  node: {
    [key]: value
  },

  optimization: {
    concatenateModules,
    flagIncludedChunks,
    mergeDuplicateChunks,
    minimize,
    minimizer: {
      [name]: {
        plugin: WebpackPlugin,
        args: [...args],
        before,
        after
      }
    },
    namedChunks,
    namedModules,
    nodeEnv,
    noEmitOnErrors,
    occurrenceOrder,
    portableRecords,
    providedExports,
    removeAvailableModules,
    removeEmptyChunks,
    runtimeChunk,
    sideEffects,
    splitChunks,
    usedExports,
  },

  performance: {
    [key]: value,

    hints,
    maxEntrypointSize,
    maxAssetSize,
    assetFilter
  },

  resolve: {
    [key]: value,

    alias: {
      [key]: value
    },
    aliasFields: [...values],
    descriptionFields: [...values],
    extensions: [...values],
    mainFields: [...values],
    mainFiles: [...values],
    modules: [...values],

    plugin: {
      [name]: {
        plugin: WebpackPlugin,
        args: [...args],
        before,
        after
      }
    }
  },

  resolveLoader: {
    [key]: value,

    alias: {
      [key]: value
    },
    aliasFields: [...values],
    descriptionFields: [...values],
    extensions: [...values],
    mainFields: [...values],
    mainFiles: [...values],
    modules: [...values],
    moduleExtensions: [...values],
    packageMains: [...values],

    plugin: {
      [name]: {
        plugin: WebpackPlugin,
        args: [...args],
        before,
        after
      }
    }
  },

  module: {
    [key]: value,

    rule: {
      [name]: {
        [key]: value,

        enforce,
        issuer,
        parser,
        resource,
        resourceQuery,
        test,

        include: [...paths],
        exclude: [...paths],

        rules: {
          [name]: Rule
        },

        oneOf: {
          [name]: Rule
        },

        use: {
          [name]: {
            loader: LoaderString,
            options: LoaderOptions,
            before,
            after
          }
        }
      }
    }
  }
})

Conditional configuration

When working with instances of ChainedMap and ChainedSet, you can perform conditional configuration using when. You must specify an expression to when() which will be evaluated for truthiness or falsiness. If the expression is truthy, the first function argument will be invoked with an instance of the current chained instance. You can optionally provide a second function to be invoked when the condition is falsy, which is also given the current chained instance.

// Example: Only add minify plugin during production
config
  .when(process.env.NODE_ENV === 'production', config => {
    config
      .plugin('minify')
      .use(BabiliWebpackPlugin);
  });
// Example: Only add minify plugin during production,
// otherwise set devtool to source-map
config
  .when(process.env.NODE_ENV === 'production',
    config => config.plugin('minify').use(BabiliWebpackPlugin),
    config => config.devtool('source-map')
  );

Inspecting generated configuration

You can inspect the generated webpack config using config.toString(). This will generate a stringified version of the config with comment hints for named rules, uses and plugins:

config
  .module
    .rule('compile')
      .test(/\.js$/)
      .use('babel')
        .loader('babel-loader');

config.toString();

/*
{
  module: {
    rules: [
      /* config.module.rule('compile') */
      {
        test: /\.js$/,
        use: [
          /* config.module.rule('compile').use('babel') */
          {
            loader: 'babel-loader'
          }
        ]
      }
    ]
  }
}
*/

By default the generated string cannot be used directly as real webpack config if it contains objects and plugins that need to be required. In order to generate usable config, you can customize how objects and plugins are stringified by setting a special __expression property on them:

const sass = require('sass');
sass.__expression = `require('sass')`;

class MyPlugin {}
MyPlugin.__expression = `require('my-plugin')`;

function myFunction () {}
myFunction.__expression = `require('my-function')`;

config
  .plugin('example')
    .use(MyPlugin, [{ fn: myFunction, implementation: sass, }]);

config.toString();

/*
{
  plugins: [
    new (require('my-plugin'))({
      fn: require('my-function'),
      implementation: require('sass')
    })
  ]
}
*/

Plugins specified via their path will have their require() statement generated automatically:

config
  .plugin('env')
    .use(require.resolve('webpack/lib/ProvidePlugin'), [{ jQuery: 'jquery' }])

config.toString();

/*
{
  plugins: [
    new (require('/foo/bar/src/node_modules/webpack/lib/EnvironmentPlugin.js'))(
      {
        jQuery: 'jquery'
      }
    )
  ]
}
*/

You can also call toString as a static method on Config in order to modify the configuration object prior to stringifying.

Config.toString({
  ...config.toConfig(),
  module: {
    defaultRules: [
      {
        use: [
          {
            loader: 'banner-loader',
            options: { prefix: 'banner-prefix.txt' },
          },
        ],
      },
    ],
  },
})
{
  plugins: [
    /* config.plugin('foo') */
    new TestPlugin()
  ],
  module: {
    defaultRules: [
      {
        use: [
          {
            loader: 'banner-loader',
            options: {
              prefix: 'banner-prefix.txt'
            }
          }
        ]
      }
    ]
  }
}

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v8.0.0

  • Update dependency jest to v27
  • Lock file maintenance
  • README: Replace Travis CI badge with GitHub Actions badge
  • Migrate from Travis CI to GitHub Actions
  • Update dependency webpack to v5
  • Update dependency webpack to v5
  • feat: update d.ts
  • refactor: use createClass to support multi-extend, avoid name conflict
  • feat: add ChainedValueMap to support false/object value type
  • doc: update readme
  • chore: style:fix
  • feat: support webpack5
  • chore: use github action
  • feat: better name confict
  • feat: support webpack5
  • feat: support webpack5
  • test: fix test for new webpack validate api
  • feat: support webpack5
  • fix: optimization.splitChunks should be chainedValueMap
  • test: add test
  • chore: style fix
  • fix: chainValueMap should return parent when used as function
  • chore: style fix, update lock
  • chore: fix code style
  • feat: convert optimization.splitChunks to ChainedValueMap
  • fix: allow config.performance(false)
  • feat: support webpack5
  • feat: add batch dts for chained
  • chore: fix code style
  • fix: remove webpack5 removed options
  • chore: bump version to 7.0.0
  • fix: syntax mistakes
  • fix: devserver allowedHosts duplicate
  • chore: remove empty file

v7.0.0

25 August 2021

  • Drop documented support for webpack 3 and earlier
  • Drop support for Node 8
  • Lock file maintenance
  • Update dependency eslint-plugin-jest to v24
  • Lock file maintenance
  • Update dependency typescript to v4
  • Docs: Correct the documented version of Node supported
  • chore: rename to webpack-5-chain

v6.5.1

25 July 2020

  • Improve error message when .tap() used on an undefined plugin
  • Improve error message when .use() not called for a Plugin
  • Clean up linting configuration
  • Remove unused dev-dependency @types/node
  • Lock file maintenance
  • Travis: Test against Node 14
  • Improve error message when .tap() used on an undefined plugin (#271)

v6.5.0

1 July 2020

  • Add rule.resolve
  • Update dependency eslint to v7
  • Switch from Ava to Jest
  • Update dependency prettier to v2
  • Update dependency auto-changelog to v2
  • Lock file maintenance
  • Test against Node 13
  • Switch to a newer Travis base image

v6.4.0

3 February 2020

  • Fix Rule.merge() when include or exclude are strings
  • Add Types to Plugin Arguments
  • Add devServer shorthands for sockHost, sockPort, sockPath
  • Add devtoolNamespace TypeScript declaration
  • Fix Rule.merge() when include or exclude are strings (#243)
  • Add devServer shorthands for sockHost, sockPort, sockPath (#238)

v6.3.1

28 January 2020

  • Lock file maintenance
  • docs: Emphasise that merge() doesn't accept webpack config objects
  • Update types and documentation for DevServer
  • Validate that Plugin 'args' is an array
  • Improve error message when legacy minimizer syntax used
  • Add missing engines definition to package.json
  • docs: Correct the .merge() example for optimization.minimizer
  • docs: Fix formatting typos in README
  • Validate that Plugin 'args' is an array (#229)

v6.3.0

22 December 2019

  • Add support for nested rules (rule.rules)
  • Fix missing type TypedChainedMap.getOrCompute
  • fix: fix type definition for Rule#oneOf
  • fix: fix type definition for Rule#oneOf (#218)

v6.2.0

22 December 2019

  • Add support for module.strictExportPresence and output.futureEmitAssets
  • Add support for module.strictExportPresence and output.futureEmitAssets (#207)

v6.1.0

13 December 2019

  • README: Update Travis badge to point to travis-ci.com
  • Lock file maintenance
  • Fix types for Config.resolve plugins and improve Plugin types
  • Update dependency eslint-plugin-ava to v9
  • Add Chinese docs link to README
  • Lock file maintenance
  • Update dependency eslint to v6
  • Update dependency eslint-config-airbnb-base to v14
  • Lock file maintenance
  • Update dependency eslint-config-prettier to v6
  • Lock file maintenance
  • Update dependency eslint-plugin-ava to v7
  • Lock file maintenance
  • Lock file maintenance
  • Lock file maintenance
  • feat: rule test supports function
  • Lock file maintenance

v6.0.0

3 May 2019

  • Lock file maintenance
  • Update linting configuration, support Node.js 12 in CI
  • Extended DevServer method
  • Lock file maintenance
  • Update dependency eslint-plugin-ava to v6
  • Point docs to v6

v5.2.4

25 March 2019

  • fix Use#end return type in OneOf
  • make __expression property works in any object
  • Lock file maintenance
  • docs: Fix typo in config.optimization example

v5.2.3

22 March 2019

  • optimize type definitions
  • Lock file maintenance

v5.2.2

12 March 2019

  • Fix README comment rendering
  • Lock file maintenance
  • Update dependency javascript-stringify to v2
  • Fix stringify master bustage

v5.2.1

7 March 2019

  • Lock file maintenance
  • Add config.output.globalObject type
  • add module-rule-type
  • Update config.mode type
  • Update dependency eslint-config-prettier to v4

v5.2.0

23 January 2019

  • Add config.name type
  • Add TypeScript type definitions
  • docs: Fix typo of 'optimization'
  • Add TypeScript type definitions (#132)

v5.1.0

16 January 2019

  • Support config.name() setter
  • Allow use of before() and after() with oneOf rules
  • Travis: Test against Node 11
  • docs: Fix typo in devServer options
  • Allow use of before() and after() with oneOf rules (#133)
  • Update dependency ava to v1
  • Lock file maintenance

v5.0.1

22 October 2018

  • Fix toString() output for alternative types of plugin
  • Fix toString() output for alternative types of plugin (#116)

v5.0.0

8 October 2018

  • README: Add NPM/Travis badges
  • Provide the same API for config.optimization.minimizer as config.plugins
  • README: Add NPM/Travis badges (#112)
  • Provide the same API for config.optimization.minimizer as config.plugins (#84)

v4.12.1

3 October 2018

  • Switch from changelog to auto-changelog
  • Allow passing entry as a string to config.merge()
  • Lock file maintenance
  • Update dependency eslint-plugin-prettier to v3

v4.12.0

3 October 2018

  • merge resolve plugins just like config
  • linted
  • Revert changes to gitignore

v4.11.0

13 September 2018

  • Support specifying plugins by path
  • Lock file maintenance
  • Lock file maintenance

v4.10.0

6 September 2018

  • Use the Resolve API to define ResolveLoader according to webpack
  • Migrate to new org
  • test: 'clean' in 'ChainedMap'
  • Lock file maintenance
  • Lock file maintenance

v4.9.0

15 August 2018

  • Update to ESLint 5
  • Lock file maintenance
  • Implement ChainedMap.getOrCompute
  • Support Object literal plugin usage
  • Lock file maintenance
  • Lock file maintenance
  • Update to ESLint 5 (#89)
  • Update dependency eslint-config-airbnb-base to v13
  • Fix linting :/
  • Fix README bug, test in Node.js v6
  • Run yarn lint --fix

v4.8.0

16 May 2018

  • Expose toString as a static method on Config
  • Add test for Config.toString, add README note

v4.7.0

15 May 2018

  • Lint with eslint, prettier, airbnb
  • Support Config.toString() with name hints
  • Configure Renovate
  • Lock file maintenance

v4.6.0

16 April 2018

  • Support Webpack 4.x
  • Update devDependencies

v4.5.0

22 November 2017

  • Introduce method for performing a batch of operations against a context

v4.4.2

10 October 2017

  • Update changelog
  • Hotfix - guard against non-defined entries when ordering chainedmap
  • Updating changelog

v4.4.1

6 October 2017

  • Updating changelog
  • Missing schema before/after

v4.4.0

6 October 2017

  • Feature: allow specifying to use before or after other use
  • Docs: Upstream fixes made to Neutrino's webpack-chain docs
  • Improve documentation for plugin configuration
  • Allow omitting keys from source merge object
  • Feature: allow specifying .before or .after to order plugins and uses
  • Rename when arguments to be clearer
  • Bumping deps
  • Update changelog

v4.3.0

13 September 2017

  • Update API for base config, dev server, and output
  • Update changelog

v4.2.0

13 September 2017

  • Add new shorthands from resolve and output
  • changelog
  • Updating README with shorthands

v4.1.0

12 September 2017

  • Updating rule definition shortcuts, adding oneOf

v4.0.0

3 October 2018

v4

3 August 2017

  • Switch noParse to getter/setter to allow webpack v3 function argument
  • Serialize performance into config output
  • Release v4.0.0

v3.3.0

3 October 2018

  • Adding noParse on module
  • Releasing v3.3.0

v3.2.0

3 October 2018

  • Adding updated shorthand methods for devServer

v3.1.0

3 October 2018

  • Allow conditional configuration via when
  • Update README with links to previous docs versions
  • Update README with links to previous docs versions

v3.0.0

3 October 2018

v3

3 August 2017

  • Adding noParse on module
  • Adding updated shorthand methods for devServer
  • Allow conditional configuration via when
  • Make rule.include, rule.exclude, loaders and plugins more extensible
  • Releasing v3.3.0
  • Update README with links to previous docs versions
  • Update README with links to previous docs versions

v2.0.1

3 October 2018

  • undefined plugin

v2.0.0

3 October 2018

v2

8 March 2017

  • undefined plugin
  • Adding testing, which informed v2 API, updated docs to reflect
  • Make Plugin API consistent with Loader API
  • MPL license, moving to mozilla-neutrino

v1.4.3

6 March 2017

  • Adding ChainedMap and ChainedSet documentation
  • Removing empty entities from cluttering configuration object
  • Docs: getConfig -> toConfig

v1.4.2

3 October 2018

  • Fix bug where exclude doesn't return this
  • Bumping to v1.4.2

v1.4.1

3 October 2018

  • Allowing config merge to append to existing rule loaders
  • docs(readme): fix typo in devtool option

v1.4.0

3 October 2018

  • Adds rule test merge via string to regex, fixes externals not chainable

v1.3.0

3 October 2018

  • Adding functionality for merging and object into a Config instance

v1.2.0

3 October 2018

  • Adds hot flag for Config.DevServer

v1.1.0

3 October 2018

  • Adding ChainedSet#prepend functionality

v1.0.3

3 October 2018

  • Fixes exception with empty rule entries with loader only

v1.0.2

3 October 2018

  • Fixes plugin methods not chaining

v1.0.1

3 October 2018

  • Shared configuration documentation
  • Avoid exceptions in empty config

v1.0.0

3 October 2018

  • initial commit