webpack-v5-chain
v2.2.0
Published
A chaining API to generate and simplify webpack 5 configurations.
Readme
webpack-v5-chain
A chaining API to generate and simplify webpack 5 configurations.
Why webpack-v5-chain
webpack-chainis archived and unmaintained.webpack-5-chainis a fork with some fixes, but has many API mismatches with the latest webpack and is not actively maintained.
webpack-v5-chain keeps the API up to date with the latest webpack, is rewritten in TypeScript, and has 100% test coverage.
The package ships CommonJS with named exports, so update your require/import:
- const Config = require('webpack-chain'); + const { Config } = require('webpack-v5-chain');ES modules are supported as well:
import { Config } from "webpack-v5-chain";.
Introduction
Webpack's core configuration is a plain JavaScript object. While fine for single projects, sharing and modifying these objects across projects requires deep knowledge of the internal structure.
webpack-v5-chain provides a chainable API for creating and modifying webpack configurations. Named references let you standardize modifications across projects.
Getting Started
const path = require('node:path');
// imports the webpack-v5-chain module.
const { Config } = require('webpack-v5-chain');
// Used by the cleanup plugin below.
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// webpack 5 requires absolute paths for `output.path` and rule conditions.
// 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(path.resolve(__dirname, 'dist'))
.filename('[name].bundle.js');
// Create named rules which can be modified later
config.module
.rule('lint')
.test(/\.js$/)
.pre()
.include
.add(path.resolve(__dirname, 'src'))
.end()
// Even create named uses (loaders)
.use('eslint')
.loader('eslint-loader')
.options({
rules: {
semi: 'off'
}
});
config.module
.rule('compile')
.test(/\.js$/)
.include
.add(path.resolve(__dirname, 'src'))
.add(path.resolve(__dirname, 'test'))
.end()
.use('babel')
.loader('babel-loader')
.options({
presets: [
['@babel/preset-env', { modules: false }]
]
});
// Create named plugins too!
config.plugin('clean').use(CleanWebpackPlugin);
// Export the completed configuration object to be consumed by webpack
module.exports = config.toConfig();Shared Configurations
Export the config and call .toConfig() before passing to webpack:
// webpack.core.js
const { Config } = require('webpack-v5-chain');
const config = new Config();
// ...shared configuration
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');
// ...prod-specific configuration
module.exports = config.toConfig();Core Interfaces
ChainedMap
A ChainedMap behaves like a JavaScript Map with chaining conveniences. Unless stated otherwise, all methods return the instance for chaining. Chainable is exported as an alias of Chained for backwards compatibility.
| Method | Description |
| ----------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| clear() | Remove all entries. |
| delete(key) | Remove a single entry by key. |
| get(key) → value | Fetch the value at the given key. |
| getOrCompute(key, fn) → value | Get the value, or compute and set it via fn() if missing. |
| set(key, value) | Set a value at the given key. |
| has(key) → boolean | Whether the map has a value at the key. |
| values() → Array | All values in the map. |
| entries() → Object \| undefined | All entries as an object. Returns undefined if empty. The object follows JavaScript's own key ordering for integer-like names; use values() for insertion order and .before() / .after() ordering. |
| merge(obj, omit?) | Merge an object's properties into the map. The optional omit array skips specified keys. |
| batch(handler) | Invoke handler with the current instance. |
| when(condition, truthy, falsy?) | Conditionally invoke truthy or falsy handler with the current instance. |
ChainedValueMap
ChainedValueMap extends ChainedMap and is callable. Calling it as a function sets its value and clears all map entries; the call returns the parent instance (for example config.performance(false) returns the Config instance). Conversely, setting any key clears the callable value.
// Callable — sets a direct value
config.optimization.splitChunks(false);
// ChainedMap usage
config.optimization.splitChunks.set('amd', true);ChainedSet
A ChainedSet behaves like a JavaScript Set with chaining conveniences. Unless stated otherwise, all methods return the instance.
| Method | Description |
| --------------------------------- | --------------------------------------------------------- |
| add(value) | Append a value. |
| prepend(value) | Add a value to the beginning. |
| clear() | Remove all values. |
| delete(value) | Remove a specific value. |
| has(value) → boolean | Whether the set contains the value. |
| values() → Array | All values in the set. |
| merge(arr) | Concatenate an array to the end. |
| batch(handler) | Invoke handler with the current instance. |
| when(condition, truthy, falsy?) | Conditionally invoke a handler with the current instance. |
Shorthand Methods
Many ChainedMap properties have shorthand methods named after the key. They are chainable and equivalent to calling .set(key, value):
devServer.hot(true);
// equivalent to:
devServer.set('hot', true);API Reference
Navigate the API by chaining — use .end() to return to the parent context. For valid values, refer to the webpack documentation.
Config
const config = new Config(); // extends ChainedMapShorthand methods
config
.context(context)
.mode(mode)
.cache(cache)
.devtool(devtool)
.target(target)
.watch(watch)
.watchOptions(watchOptions)
.externals(externals)
.externalsType(externalsType)
.externalsPresets(externalsPresets)
.dotenv(dotenv)
.node(node)
.stats(stats)
.experiments(experiments)
.infrastructureLogging(infrastructureLogging)
.amd(amd)
.bail(bail)
.dependencies(dependencies)
.extends(extends)
.ignoreWarnings(ignoreWarnings)
.loader(loader)
.name(name)
.parallelism(parallelism)
.profile(profile)
.recordsInputPath(recordsInputPath)
.recordsOutputPath(recordsOutputPath)
.recordsPath(recordsPath)
.snapshot(snapshot)
.validate(validate)Entry points
// 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()Output
config.output : ChainedMap
config.output
.amdContainer(amdContainer)
.assetModuleFilename(assetModuleFilename)
.asyncChunks(asyncChunks)
.auxiliaryComment(auxiliaryComment)
.charset(charset)
.chunkFilename(chunkFilename)
.chunkFormat(chunkFormat)
.chunkLoadTimeout(chunkLoadTimeout)
.chunkLoadingGlobal(chunkLoadingGlobal)
.chunkLoading(chunkLoading)
.clean(clean)
.compareBeforeEmit(compareBeforeEmit)
.copy(copy)
.crossOriginLoading(crossOriginLoading)
.cssChunkFilename(cssChunkFilename)
.cssFilename(cssFilename)
.devtoolFallbackModuleFilenameTemplate(devtoolFallbackModuleFilenameTemplate)
.devtoolModuleFilenameTemplate(devtoolModuleFilenameTemplate)
.devtoolNamespace(devtoolNamespace)
.enabledChunkLoadingTypes(enabledChunkLoadingTypes)
.enabledLibraryTypes(enabledLibraryTypes)
.enabledWasmLoadingTypes(enabledWasmLoadingTypes)
.environment(environment)
.filename(filename)
.globalObject(globalObject)
.hashDigest(hashDigest)
.hashDigestLength(hashDigestLength)
.hashFunction(hashFunction)
.hashSalt(hashSalt)
.hotUpdateChunkFilename(hotUpdateChunkFilename)
.hotUpdateGlobal(hotUpdateGlobal)
.hotUpdateMainFilename(hotUpdateMainFilename)
.html(html)
.htmlChunkFilename(htmlChunkFilename)
.htmlFilename(htmlFilename)
.iife(iife)
.ignoreBrowserWarnings(ignoreBrowserWarnings)
.importFunctionName(importFunctionName)
.importMetaName(importMetaName)
.library(library)
.libraryExport(libraryExport)
.libraryTarget(libraryTarget)
.module(module)
.path(path)
.pathinfo(pathinfo)
.publicPath(publicPath)
.resourceHints(resourceHints)
.scriptType(scriptType)
.sourceMapFilename(sourceMapFilename)
.sourcePrefix(sourcePrefix)
.strictModuleErrorHandling(strictModuleErrorHandling)
.strictModuleExceptionHandling(strictModuleExceptionHandling)
.strictModuleResolution(strictModuleResolution)
.trustedTypes(trustedTypes)
.umdNamedDefine(umdNamedDefine)
.uniqueName(uniqueName)
.wasmLoading(wasmLoading)
.wasmStreamingFallback(wasmStreamingFallback)
.webassemblyModuleFilename(webassemblyModuleFilename)
.workerChunkFilename(workerChunkFilename)
.workerChunkLoading(workerChunkLoading)
.workerPublicPath(workerPublicPath)
.workerWasmLoading(workerWasmLoading)Resolve
config.resolve : ChainedMap
Shorthand: .cache .cachePredicate .cacheWithContext .enforceExtension .fileSystem .fullySpecified .preferAbsolute .preferRelative .resolver .symlinks .tsconfig .unsafeCache .useSyncFileSystemCalls
ChainedSet properties: .aliasFields .conditionNames .descriptionFiles .exportsFields .extensions .importsFields .mainFields .mainFiles .modules .restrictions .roots
ChainedMap properties: .alias .byDependency .extensionAlias .fallback
config.resolve.alias.set(key, value);
config.resolve.extensions.add('.js').prepend('.ts');
config.resolve.fallback.set('path', 'path-browserify');Resolve plugins
config.resolve.plugin(name) → Plugin
// Adding — do not use `new`
config.resolve.plugin(name).use(Plugin, args);
config.resolve.plugin(name).use(require.resolve('plugin-path'), args);
// Modify arguments
config.resolve.plugin(name).tap(args => newArgs);
// Modify instantiation
config.resolve.plugin(name).init((Plugin, args) => new Plugin(...args));
// Remove
config.resolve.plugins.delete(name);
// Ordering
config.resolve.plugin(name).before(otherName);
config.resolve.plugin(name).after(otherName);ResolveLoader
Identical to config.resolve.
Performance
config.performance : ChainedValueMap
Shorthand: .all .analyzableBailouts .assetFilter .asyncChunkWaterfalls .broadContexts .cacheEffectiveness .circularDependencies .conflictingResourceHints .duplicateModules .duplicatePackages .dynamicExports .evalUsage .hints .hotspots .inlinedAssets .largeModules .legacyJavascript .maxAssetSize .maxEntrypointSize .missingSideEffects .mixedExports .osDependentRules .pureAnnotations .redundantDynamicImports .scopeHoistingBailouts .sourceMaps .splitChunksCapped .tinyChunks .topLevelThis .unsplitVendors .unusedAssets .unusedConfig .unusedModules
config.performance(false); // callable — disables performance hints
config.performance
.assetFilter(fn)
.hints('warning')
.maxEntrypointSize(250000)
.maxAssetSize(250000);Optimization
config.optimization : ChainedMap
Shorthand: .checkWasmTypes .chunkIds .concatenateModules .emitOnErrors .avoidEntryIife .flagIncludedChunks .inlineExports .innerGraph .mangleExports .mangleWasmImports .mergeDuplicateChunks .minimize .minimizeOptions .moduleIds .nodeEnv .noEmitOnErrors .portableRecords .providedExports .realContentHash .removeAvailableModules .removeEmptyChunks .runtimeChunk .sideEffects .usedExports
SplitChunks: config.optimization.splitChunks : ChainedValueMap
config.optimization.splitChunks({ chunks: 'all' });
config.optimization.splitChunks.set('minSize', 10000);Minimizers:
config.optimization.minimizer(name) → Plugin
config.optimization.minimizer(name).use(Plugin, args);
config.optimization.minimizer(name).tap(args => newArgs);
config.optimization.minimizer(name).init((Plugin, args) => new Plugin(...args));
config.optimization.minimizers.delete(name);Plugins
config.plugin(name) → Plugin
config.plugin(name).use(Plugin, args);
config.plugin(name).use(require.resolve('plugin-path'), args);
config.plugin(name).tap(args => newArgs);
config.plugin(name).init((Plugin, args) => new Plugin(...args));
config.plugins.delete(name);
config.plugin(name).before(otherName); // cannot also use .after()
config.plugin(name).after(otherName); // cannot also use .before()DevServer
config.devServer : ChainedMap
Shorthand: .app .bonjour .compress .devMiddleware .headers .historyApiFallback .host .hot .ipc .liveReload .onListening .open .port .proxy .server .setupExitSignals .setupMiddlewares .static .watchFiles .webSocketServer
ChainedMap: .client — webpack-dev-server client options. Supported methods: .logging(), .overlay(), .progress(), .reconnect(), .webSocketTransport(), .webSocketURL(); call .end() to go back to the dev server.
config.devServer
.client
.logging('info')
.overlay({ errors: true, warnings: false })
.progress(true)
.reconnect(3)
.webSocketTransport('ws')
.webSocketURL('ws://localhost:8080/ws')
.end()
.port(8080);To disable the client entry entirely, call disableClient(). It takes precedence over the .client options and configuring .client afterwards does not re-enable it:
config.devServer.disableClient();ChainedSet: .allowedHosts
Module
config.module : ChainedMap
Shorthand: .noParse .unsafeCache .exprContextCritical .exprContextRecursive .exprContextRegExp .exprContextRequest .unknownContextCritical .unknownContextRecursive .unknownContextRegExp .unknownContextRequest .wrappedContextCritical .wrappedContextRecursive .wrappedContextRegExp .strictExportPresence .strictThisContextOnImports
Module rules
config.module.rule(name) : Rule
Shorthand: .assert .compiler .dependency .descriptionData .descriptionRelativePath .enforce .issuer .issuerLayer .layer .loader .extractSourceMap .glob .mimetype .options .parser .generator .phase .realResource .resource .resourceFragment .resourceQuery .scheme .sideEffects .test .type .with
config.module.rule(name) → Rule
config.module.rule(name)
.test(/\.js$/)
.pre() // enforce: 'pre'
.post() // enforce: 'post'
.enforce(value) // enforce: value
// Nested rules
config.module.rule(name).rule(name) → Rule
config.module.rule(name).rule(name).before(otherName);
config.module.rule(name).rule(name).after(otherName);
// OneOf rules
config.module.rule(name).oneOf(name) → Rule
config.module.rule(name).oneOf(name).before(otherName);
config.module.rule(name).oneOf(name).after(otherName);
// Resolve override (webpack ≥ 4.36.1)
config.module.rule(name).resolve.symlinks(true);Rule uses (loaders)
config.module.rule(name).use(name) → Use
config.module.rule(name).use(name)
.ident('babel')
.loader('babel-loader')
.options({ presets: ['@babel/preset-env'] });
// Modify options
config.module.rule(name).use(name).tap(options => mergedOptions);Merging Config
Merge an object matching the chain schema layout. Note: this schema uses named keys (not webpack's raw config format).
config.merge({ devtool: 'source-map' });
config.get('devtool'); // "source-map"config.merge({
amd, bail, cache, context, dependencies, devtool, dotenv,
experiments, extends, externals, externalsPresets, externalsType,
ignoreWarnings, infrastructureLogging, loader, mode, name,
parallelism, profile, recordsPath, recordsInputPath, recordsOutputPath,
snapshot, stats, target, validate, watch, watchOptions, node: {},
entry: { name: [...] },
output: { /* shorthand keys */ },
plugin: { name: { plugin, args, before, after } },
devServer: { /* shorthand keys */ },
optimization: {
/* shorthand keys */,
minimizer: { name: { plugin, args, before, after } },
splitChunks: {}
},
performance: { hints, maxEntrypointSize, maxAssetSize, assetFilter },
resolve: {
/* shorthand keys */,
alias: { key: value },
aliasFields: [...], byDependency: {}, conditionNames: [...],
descriptionFiles: [...], exportsFields: [...], extensionAlias: {},
extensions: [...], fallback: {}, importsFields: [...],
mainFields: [...], mainFiles: [...], modules: [...],
restrictions: [...], roots: [...],
plugin: { name: { plugin, args, before, after } }
},
resolveLoader: { /* same as resolve */ },
module: {
/* shorthand keys */,
rule: {
name: {
/* shorthand keys */,
include: [...], exclude: [...],
rules: { name: Rule },
oneOf: { name: Rule },
use: { name: { loader, options, before, after } }
}
}
}
})Conditional Configuration
Use .when() on any ChainedMap or ChainedSet:
config.when(
process.env.NODE_ENV === 'production',
config => config.plugin('minify').use(BabiliWebpackPlugin),
config => config.devtool('source-map')
);Inspecting Config
Use config.toString() to generate a commented, stringified config:
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' }
]
}
]
}
}Stringifying objects & plugins
By default the output is not directly usable as webpack config. Set a __expression property on objects to control how they are stringified:
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 by path auto-generate require():
config.plugin('env').use(require.resolve('webpack/lib/provide/ProvidePlugin'), [{ jQuery: 'jquery' }]);
config.toString();
/*
{
plugins: [
new (require('/foo/bar/node_modules/webpack/lib/provide/ProvidePlugin.js'))({ jQuery: 'jquery' })
]
}
*/Static Config.toString()
Call Config.toString() statically to modify the config before stringifying:
Config.toString({
...config.toConfig(),
module: {
defaultRules: [{
use: [{ loader: 'banner-loader', options: { prefix: 'banner-prefix.txt' } }]
}]
}
})