Skip to content

CSS: ExportType: "style" CSS modules are emitted without __webpack_require__ parameter #22832

Closed

Description

@DevonSchwartzEmerson

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

  1.  npm i webpack webpack-cli
  2.  npx webpack 
  3. Open  dist/commons.js . The CSS module is  (() => { webpack_require.is(...) }) , with no parameters.
  4. 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.

Activity

  1. added
    area: cssCSS support (lib/css)
    area: runtimeEmitted runtime and chunk loading across targets (lib/runtime, lib/web, lib/node, lib/esm)
    regressionWorked in a previous release; pair with the Affected version field
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: cssCSS support (lib/css)area: runtimeEmitted runtime and chunk loading across targets (lib/runtime, lib/web, lib/node, lib/esm)regressionWorked in a previous release; pair with the Affected version field

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions