Skip to content

Commit 13ec116

Browse files
authored
fix: allow wrapping an external concatenated module too (#22226)
1 parent ab902ff commit 13ec116

9 files changed

Lines changed: 63 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Allow wrapping an external concatenated module too.

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,9 @@ const needWrapped = (module) =>
368368
* @returns {boolean} true, when it may be wrapped
369369
*/
370370
const entryCanBeWrapped = (entry) =>
371-
(entry.module instanceof NormalModule &&
372-
!entry.module.type.startsWith("css/")) ||
373-
(entry.module instanceof ExternalModule &&
374-
entry.module.canBeWrappedInConcatenation());
371+
entry.module instanceof ExternalModule
372+
? entry.module.canBeWrappedInConcatenation()
373+
: !entry.module.type.startsWith("css/");
375374

376375
/** @typedef {Set<ConcatenatedModuleInfo>} NeededNamespaceObjects */
377376

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
global.__nestedConcatOrder = (global.__nestedConcatOrder || []).concat(
2+
"config"
3+
);
4+
5+
// not statically known, so it cannot be inlined away along with the edge
6+
export const config = global.__nestedConcatSeed || "root";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { settings } from "./settings";
2+
import { value as separateValue } from "./keeps-settings-separate";
3+
4+
global.__nestedConcatOrder = (global.__nestedConcatOrder || []).concat(
5+
"feature"
6+
);
7+
8+
export const value = settings.value;
9+
export { separateValue };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// "feature" is wrapped because only a require() reaches it; its dependency is
2+
// a separate concatenation, and must not be pulled forward ahead of the wrapper
3+
import { tag } from "./member";
4+
5+
global.__nestedConcatOrder = (global.__nestedConcatOrder || []).concat("root");
6+
7+
it("should not evaluate a concatenated dependency of a wrapped member early", () => {
8+
expect(tag).toBe("member");
9+
expect(global.__nestedConcatOrder).toEqual(["root"]);
10+
});
11+
12+
it("should evaluate it once the wrapped member is required", () => {
13+
expect(require("./feature").value).toBe("settings:root");
14+
expect(global.__nestedConcatOrder).toEqual(["root", "config", "feature"]);
15+
delete global.__nestedConcatOrder;
16+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// `module.id` keeps this module out of the concatenation, so "settings" has an
2+
// importer outside of it and becomes the root of its own concatenation
3+
import { settings } from "./settings";
4+
5+
export const id = module.id;
6+
export const value = settings.value;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const tag = "member";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { config } from "./config";
2+
3+
export const settings = { value: `settings:${config}` };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
mode: "production",
6+
devtool: false,
7+
optimization: {
8+
concatenateModules: { commonjs: true },
9+
minimize: false,
10+
usedExports: true,
11+
moduleIds: "named",
12+
chunkIds: "named"
13+
}
14+
};

0 commit comments

Comments
 (0)