Skip to content

Commit e8855d6

Browse files
test: assert the module flag for fragment-named .mjs/.cjs assets
Claude-Session: https://claude.ai/code/session_01EpeANYvMKyZwSDTRVE5LoG
1 parent 9c21eb8 commit e8855d6

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

‎test/test-option.test.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,65 @@ describe("test option", () => {
139139
: "[name].js#[fullhash]",
140140
assetModuleFilename: "[name][ext][query][fragment]",
141141
},
142+
plugins: [
143+
{
144+
// Emitted as a copy would be: no `javascriptModule`, so the name decides.
145+
apply(childCompiler) {
146+
childCompiler.hooks.thisCompilation.tap("Copy", (compilation) => {
147+
compilation.hooks.processAssets.tap(
148+
{
149+
name: "Copy",
150+
stage:
151+
childCompiler.webpack.Compilation
152+
.PROCESS_ASSETS_STAGE_ADDITIONAL,
153+
},
154+
() => {
155+
const { RawSource } = childCompiler.webpack.sources;
156+
157+
for (const name of ["copy.mjs#m", "copy.cjs#c"]) {
158+
compilation.emitAsset(
159+
name,
160+
new RawSource("var foo = 12;\nconsole.log(foo);\n"),
161+
);
162+
}
163+
},
164+
);
165+
});
166+
},
167+
},
168+
],
142169
});
143170

171+
// What `.mjs` / `.cjs` read as, which the name decides past the fragment.
172+
const moduleByName = new Map();
173+
const terserMinify = (input, sourceMap, minimizerOptions) => {
174+
for (const name of Object.keys(input)) {
175+
moduleByName.set(name.replace(/#.*$/, ""), minimizerOptions.module);
176+
}
177+
178+
return MinimizerPlugin.terserMinify(input, sourceMap, minimizerOptions);
179+
};
180+
Object.assign(terserMinify, MinimizerPlugin.terserMinify);
181+
144182
new MinimizerPlugin({
183+
parallel: false,
145184
test: /\.(?:[cm]?js|json)$/i,
146-
minify: [MinimizerPlugin.terserMinify, MinimizerPlugin.jsonMinify],
185+
minify: [terserMinify, MinimizerPlugin.jsonMinify],
147186
}).apply(compiler);
148187

149188
const stats = await compile(compiler);
150189
const assets = readsAssets(compiler, stats);
151190
const names = Object.keys(assets);
152191

192+
expect(moduleByName.get("copy.mjs")).toBe(true);
193+
expect(moduleByName.get("copy.cjs")).toBe(false);
194+
153195
expect(names).toEqual(
154196
expect.arrayContaining([
155197
expect.stringMatching(/^js\.js#[0-9a-f]+$/),
156198
expect.stringMatching(/^mjs\.mjs#[0-9a-f]+$/),
199+
"copy.mjs#m",
200+
"copy.cjs#c",
157201
"file.json#data",
158202
]),
159203
);

0 commit comments

Comments
 (0)