Repository navigation
Description
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
- Enable
experiments.css: true. - Configure a CSS rule with
exportType: "style":
{
test: /\.css$/,
type: "css/auto",
parser: { exportType: "style" }
}- Add
new webpack.IgnorePlugin({ resourceRegExp: /ignored\.css$/ }). - Import a CSS file containing
@import "./ignored.css";. - 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.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
fix: resolve issue #22842 skip ignored CSS imports with exportType st…