Repository navigation
Description
Have you used AI?
Yes
Which part of webpack is affected?
css
Bug Description
With native CSS ( experiments.css: true ) and parser: { exportType: "style" } , the generated module function for a CSS module has no parameters but calls webpack_require.is(...) :
Example:
(() => {
__webpack_require__.is(1, ".a{color:red}\r\n\n");
}),
A single bundle works only because webpack_require happens to be in lexical scope. When the runtime is in a different file from the modules ( optimization.runtimeChunk plus splitChunks ), loading the bundle throws:
ReferenceError: __webpack_require__ is not defined
Link to Minimal Reproduction and steps to reproduce
Tested on webpack 5.110.3, 5.111.1 and 5.112.0 (Node 20, Windows). All three fail the same way
- npm i webpack webpack-cli
- npx webpack
- Open dist/commons.js . The CSS module is (() => { webpack_require.is(...) }) , with no parameters.
- Load runtime.js , commons.js , main.js in a browser or vm context. commons.js throws ReferenceError:
__webpack_require__is not defined
** Note: The real-world trigger was Karma with karma-webpack , which uses a separate runtime.js and commons.js **
Files to recreate issue:
webpack.config.js
entry2.js
entry.js
a.css
Expected Behavior
The module function should receive __webpack_require__ , as (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) => {...} , because the generated code uses RuntimeGlobals.cssInjectStyle ( __webpack_require__.is ).
Actual Behavior
Loading the bundle throws: ReferenceError: __webpack_require__ is not defined
Environment
• webpack versions: 5.110.3, 5.111.1, 5.112.0 (the bug reproduces on all three)
• Node.js: 20.11.0
• OS: Windows 10/11
• Loaders: none are needed to reproduce. This also happens with sass-loader and postcss-loader in a real project.Is this a regression?
Yes (please specify version below)
Last Working Version
5.105
Additional Context
The generated module uses the cssInjectStyle runtime global but the module doesn't declare RuntimeGlobals.require , so the wrapper omits the parameter. This plugin fixes it:
const { RuntimeGlobals } = require("webpack");
class CssStyleRequireFixPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap("CssStyleRequireFixPlugin", compilation => {
compilation.hooks.additionalModuleRuntimeRequirements.tap("CssStyleRequireFixPlugin", (module, requirements) => {
if (module.exportType === "style") requirements.add(RuntimeGlobals.require);
});
});
}
}
In addition, 5.106.0: adds exportType: "style" , which injects styles at runtime like style-loader . This is the changelog entry "Add exportType: "style" for CSS modules to inject styles into the DOM via HTMLStyleElement, similar to style-loader functionality.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Effort
Activity