Skip to content

Commit 519187b

Browse files
fix: avoid module concatenation in child compilation for module library (#19457)
1 parent 52dc193 commit 519187b

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed

lib/library/ModuleLibraryPlugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
6464
apply(compiler) {
6565
super.apply(compiler);
6666

67-
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
67+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
6868
const { exportsDefinitions } =
6969
ConcatenatedModule.getCompilationHooks(compilation);
7070
exportsDefinitions.tap(PLUGIN_NAME, (definitions, module) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { myClass } from "./style.custom";
2+
3+
it("should work", function() {
4+
expect(myClass).toBe("my-class");
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/** @type {import("../../../../").LoaderDefinitionFunction} */
2+
module.exports = async function loader() {
3+
const callback = this.async();
4+
const loader = this;
5+
const compilerName = `extract:${loader.resourcePath}`;
6+
const compiler = loader._compiler;
7+
const filename = "*";
8+
const childCompiler = loader._compilation.createChildCompiler(
9+
compilerName,
10+
{
11+
filename,
12+
},
13+
[]
14+
);
15+
16+
const NodeTemplatePlugin = compiler.webpack.node.NodeTemplatePlugin;
17+
new NodeTemplatePlugin().apply(childCompiler);
18+
19+
const NodeTargetPlugin = compiler.webpack.node.NodeTargetPlugin;
20+
new NodeTargetPlugin().apply(childCompiler);
21+
22+
const {
23+
EntryOptionPlugin,
24+
library: {
25+
EnableLibraryPlugin
26+
}
27+
} = loader._compiler.webpack;
28+
29+
new EnableLibraryPlugin('commonjs2').apply(childCompiler);
30+
31+
EntryOptionPlugin.applyEntryOption(childCompiler, loader.context, {
32+
child: {
33+
library: {
34+
type: 'commonjs2'
35+
},
36+
import: [`!!${loader.resourcePath}`]
37+
}
38+
});
39+
40+
const LimitChunkCountPlugin = compiler.webpack.optimize.LimitChunkCountPlugin;
41+
42+
new LimitChunkCountPlugin({
43+
maxChunks: 1
44+
}).apply(childCompiler);
45+
46+
let source;
47+
48+
childCompiler.hooks.compilation.tap(compilerName, compilation => {
49+
compilation.hooks.processAssets.tap(compilerName, () => {
50+
source = compilation.assets[filename] && compilation.assets[filename].source();
51+
52+
// Remove all chunk assets
53+
compilation.chunks.forEach(chunk => {
54+
chunk.files.forEach(file => {
55+
compilation.deleteAsset(file);
56+
});
57+
});
58+
});
59+
});
60+
61+
try {
62+
await new Promise((resolve, reject) => {
63+
childCompiler.runAsChild((err, _entries, compilation) => {
64+
if (err) {
65+
return reject(err);
66+
}
67+
68+
if (compilation.errors.length > 0) {
69+
return reject(compilation.errors[0]);
70+
}
71+
72+
resolve();
73+
});
74+
})
75+
} catch (e) {
76+
callback(e);
77+
return;
78+
}
79+
80+
if (!source) {
81+
callback(new Error("Didn't get a result from child compiler"));
82+
return;
83+
}
84+
85+
callback(null, source);
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import style from "./test.mjs";
2+
export const myClass = style("my-class");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function(name) {
2+
return name;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** @type {import("../../../../").Configuration} */
2+
module.exports = {
3+
mode: "production",
4+
target: "web",
5+
output: {
6+
module: true,
7+
library: {
8+
type: "module"
9+
}
10+
},
11+
module: {
12+
strictExportPresence: true,
13+
rules: [
14+
{
15+
test: /\.custom$/i,
16+
loader: require.resolve("./loader")
17+
}
18+
]
19+
},
20+
experiments: {
21+
outputModule: true
22+
}
23+
};

0 commit comments

Comments
 (0)