Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash with importModule when CSS enabled #17140

Merged
merged 4 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/css/CssModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,20 @@ class CssModulesPlugin {
}
return result;
});
const enabledChunks = new WeakSet();
const globalChunkLoading = compilation.outputOptions.chunkLoading;
const isEnabledForChunk = chunk => {
const options = chunk.getEntryOptions();
const chunkLoading =
options && options.chunkLoading !== undefined
? options.chunkLoading
: globalChunkLoading;
return chunkLoading === "jsonp";
};
const onceForChunkSet = new WeakSet();
const handler = (chunk, set) => {
if (enabledChunks.has(chunk)) {
return;
}
enabledChunks.add(chunk);
if (onceForChunkSet.has(chunk)) return;
onceForChunkSet.add(chunk);
if (!isEnabledForChunk(chunk)) return;

set.add(RuntimeGlobals.publicPath);
set.add(RuntimeGlobals.getChunkCssFilename);
Expand Down
9 changes: 9 additions & 0 deletions test/configCases/css/import-module/a-pitching-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import("../../../../").PitchLoaderDefinitionFunction} */
exports.pitch = async function (remaining) {
const result = await this.importModule(
this.resourcePath + '.webpack[javascript/auto]' + '!=!' + remaining, {
publicPath: ''
});

return result.default || result;
};
2 changes: 2 additions & 0 deletions test/configCases/css/import-module/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const red = '#f00';
export const green = '#0f0';
6 changes: 6 additions & 0 deletions test/configCases/css/import-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import stylesheet from './stylesheet.js';

it("should compile", () => {
expect(stylesheet).toBe("body { background: #f00; color: #0f0; }");
});

3 changes: 3 additions & 0 deletions test/configCases/css/import-module/stylesheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { green, red } from './colors.js';

export default `body { background: ${red}; color: ${green}; }`;
19 changes: 19 additions & 0 deletions test/configCases/css/import-module/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const webpack = require("../../../../");
/** @type {import("../../../../").Configuration} */
module.exports = {
plugins: [new webpack.HotModuleReplacementPlugin()],
target: "web",
mode: "development",
module: {
rules: [
{
test: /stylesheet\.js$/i,
use: ["./a-pitching-loader.js"],
type: "asset/source"
}
]
},
experiments: {
css: true
}
};