Skip to content

fix(css): crash when CSS @import is ignored with exportType: "style" #22842

Closed

Description

@codeXsidd

Problem

When using native CSS (experiments.css: true) with parser.exportType: "style", @import rules that point to an ignored module (e.g., via IgnorePlugin or resolve.alias: { "...": false }) cause webpack to throw TypeError: Cannot read properties of null (reading 'userRequest') during code generation.

Current Behavior

Building a project that contains a CSS file with @import "./ignored.css"; and exportType: "style", combined with new webpack.IgnorePlugin({ resourceRegExp: /ignored\.css$/ }), crashes during code generation:

TypeError: Cannot read properties of null (reading 'userRequest')
    at CssGenerator._generateImportExpressions (webpack/lib/css/CssGenerator.js:861:25)
    at generateImportCode (webpack/lib/css/CssGenerator.js:364:20)
    at CssGenerator.generate (webpack/lib/css/CssGenerator.js:558:9)

Expected Behavior

The compilation should succeed. If an @import dependency is ignored or has no resolved module (moduleGraph.getModule(dep) is null), CssGenerator._generateImportExpressions should skip it without crashing, matching the behavior in CssGenerator._renderMergedCss.

Reproduction

  1. Enable experiments.css: true.
  2. Configure a CSS rule with exportType: "style":
{
  test: /\.css$/,
  type: "css/auto",
  parser: { exportType: "style" }
}
  1. Add new webpack.IgnorePlugin({ resourceRegExp: /ignored\.css$/ }).
  2. Import a CSS file containing @import "./ignored.css";.
  3. Run webpack build -> crashes with TypeError: Cannot read properties of null (reading 'userRequest').

Technical Context

In lib/css/CssGenerator.js, _generateImportExpressions iterates through module.dependencies for css-import dependencies:

for (const dep of module.dependencies) {
	if (!isDependencyKind(dep, "css-import")) continue;
	const depModule = /** @type {CssModule & BuiltCssModule} */ (
		moduleGraph.getModule(dep)
	);
	if (concatenationScope && concatenationScope.isModuleInScope(depModule)) {
		continue;
	}
	parts.push(
		generateContext.runtimeTemplate.moduleExports({
			module: depModule,
			chunkGraph: generateContext.chunkGraph,
			request: depModule.userRequest,
			weak: false,
			runtimeRequirements: generateContext.runtimeRequirements
		})
	);
}

When dep resolves to false (ignored), moduleGraph.getModule(dep) returns null. Accessing depModule.userRequest causes an unhandled TypeError. Adding if (!depModule) continue; resolves this issue, consistent with _renderMergedCss.

Activity

  1. added a commit that references this issue on Oct 22, 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)

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions