Webpack 5.113
Webpack 5.113 is here! Give your HTML page a <style module> and keep writing class="button": webpack connects the scoped styles to your markup. This release also shrinks SVGs and lets loaders skip files that don't need them. Hot updates get a round of fixes too, so moving code between chunks causes fewer interruptions while you work.
Explore what's new:
- CSS Modules in HTML
- Smaller SVG Assets
- Run Loaders Only When Needed
- Better CommonJS Tree Shaking
- More Reliable Hot Updates
- Control Where Files Are Written
- Other Improvements
To update an existing project, run:
npm install --save-dev webpack@5.113.0CSS Modules in HTML
Your page has a .button. So does the stylesheet you just added. CSS Modules help keep those names from colliding, but your markup needs to know the generated names too.
Webpack now handles that connection for native HTML pages. Add module to a <style> tag and use the original class names in your markup:
src/index.html
<style module>
.button {
color: white;
background: navy;
}
</style>
<button class="button">Save</button>Use the page as your entry point:
webpack.config.js
export default {
entry: "./src/index.html",
};Webpack gives .button a scoped name and writes that same name into the button's class. You write the CSS and HTML together; webpack keeps them in sync. Linked .module.css stylesheets work too, and classes that no CSS Module declares stay unchanged.
When two modules both declare .button, name the module you want to use:
<style module="card">
.button {
background: navy;
}
</style>
<button class="card.button">Save card</button>Scoped IDs stay connected to their labels, ARIA references and links within the page. Selectors written inside JavaScript, such as document.querySelector(".button"), are not rewritten, so use a separate attribute such as data-action for those hooks.
Smaller SVG Assets
An SVG exported from a design tool can carry long path commands, verbose color values and editor metadata. Webpack 5.113 expands the built-in SVG optimizer so more of that work can happen during your build.
Enable optimization for emitted .svg and .xml assets with an xml options object:
export default {
mode: "production",
optimization: {
minimizeOptions: {
xml: {},
},
},
};With xml: {}, webpack shortens numbers, colors and path data, removes unused namespace declarations, and trims whitespace in SVG containers where it renders nothing. Styles inside an SVG follow your CSS optimization settings and browser target.
You can opt into more changes: removeMetadata: true removes metadata, precision rounds geometry, and cleanupIds: true removes unused IDs and renames the rest. These stay off by default because metadata can contain licensing information, rounding can move points, and IDs can be referenced from another file.
SVG optimization is opt-in unless you use experiments.futureDefaults. Set xml: false to turn it off.
Run Loaders Only When Needed
Imagine an instrumentation loader that only does useful work in files with an @instrument comment. A rule matching /\.js$/ still sends every JavaScript file through it.
The new sourceTest condition lets a loader check its input before running its transformation:
export default {
module: {
rules: [
{
test: /\.js$/i,
use: [
{
loader: "./loaders/instrument-loader.js",
sourceTest: /@instrument/,
},
],
},
],
},
};In this example, files containing @instrument run through your custom loader. Other JavaScript files skip its normal function. That lets you avoid work for files the loader would leave unchanged.
The check sees the current input, including changes made by loaders to its right. Pitch functions still run, and input that is not valid UTF-8 is processed normally.
Better CommonJS Tree Shaking
CommonJS packages get better tree shaking in this release. A simple export assignment like exports.answer = 42 no longer makes an otherwise unused module appear to have side effects. Webpack can also remove unused require() calls when the referenced module has no side effects. Modules that perform initialization still run.
More Reliable Hot Updates
Move a module into a lazy route, save, and keep working. This release fixes hot updates that previously aborted when modules moved between chunks or an update needed a chunk the browser had not loaded yet.
Styles stay in sync too. New chunks load their stylesheets, removed chunks lose theirs, and existing stylesheets keep their attributes and disabled state.
Watch mode also picks up files added inside subdirectories of recursive contexts. A newly added file can join the next build without restarting the watcher.
Control Where Files Are Written
Some builds need to write files outside their main output directory, such as a manifest consumed by a server. output.allowOutside lets you choose where webpack can write, copy and clean files beyond output.path:
falsekeeps those operations inside the output directory.- An array of absolute directory paths allows specific locations outside it.
trueallows any location.
With experiments.futureDefaults, this defaults to false. Without it, webpack preserves the existing behavior: assets emitted outside output.path can still be written, while copying and cleaning stay inside.
Other Improvements
- TypeScript parsing. Webpack's built-in TypeScript support now parses files once, without a separate type-stripping step. No configuration changes are needed.
- Tree shaking with cached modules.
experiments.cacheUnaffectednow works withoptimization.usedExportsandoptimization.mangleExports. Webpack tracks changes in export usage and names while reusing unaffected work. - Languages inside HTML. A
<style lang="scss">or<script lang="ts">body is typed as that language, so loaders can target it through amimetyperule. - Lazy compilation on your development server. A new
transporthook lets a development server serve the lazy compilation backend through its own server. - Async work after multiple builds. The
MultiCompilerdonehook accepts asynchronous taps. Calling it synchronously with.call()is deprecated. - Custom hot update data. Plugins can add their own data through the new
updateManifestHMR hook. - Keep chunk hashes in sync.
compilation.hooks.chunkHashDependencieslets plugins declare when a chunk's hash depends on another chunk.
For plugins using the experimental parser APIs, taps can now read nodes through a path with path: true, and scope analysis is available through webpack.javascript.syntax.scopes. JavascriptParser.extend and the experimental TypeScriptPlugin have been removed.
Bug Fixes
- WebAssembly files load correctly when runtime chunks live in subdirectories or filenames use the full hash in universal builds.
- HTML keeps the right URLs for remote resources, encoded asset paths and pages with
<base href>. Inlined assets also keep custom license banners. - CommonJS and ES modules work together more reliably, including circular re-exports and modules used by both
importandrequire(). - CSS keeps its order and context. Imports retain their layer and media conditions, and CSS collected asynchronously on the server keeps request order. Imported
@valueURLs resolve relative to their source, and longercomposeschains work correctly.
See the full webpack 5.113.0 changelog for all improvements and fixes.
Thanks
Thanks to everyone who contributed code, documentation, tests and bug reports, and to our sponsors for supporting webpack's development. Try the release in your project and let us know how it goes.



