Skip to content

Commit a6d13aa

Browse files
authored
feat: wrap concatenated ESM/CJS and inline CJS require edges (#21519)
* feat: wrap concatenated ESM/CJS and inline CJS require edges * test * review * review
1 parent f7d3815 commit a6d13aa

183 files changed

Lines changed: 3537 additions & 1060 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Wrap concatenated modules in lazy `__webpack_require__.cw` accessors and inline `require()`.

‎lib/ConcatenationScope.js‎

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,28 @@ const {
1010
NAMESPACE_OBJECT_EXPORT
1111
} = require("./util/concatenate");
1212

13+
/** @typedef {import("webpack-sources").Source} Source */
1314
/** @typedef {import("./Chunk")} Chunk */
1415
/** @typedef {import("./Module")} Module */
1516
/** @typedef {import("./optimize/ConcatenatedModule").ConcatenatedModuleInfo} ConcatenatedModuleInfo */
1617
/** @typedef {import("./optimize/ConcatenatedModule").ModuleInfo} ModuleInfo */
1718
/** @typedef {import("./optimize/ConcatenatedModule").ExportName} Ids */
19+
/** @typedef {import("./javascript/JavascriptParser").Range} Range */
20+
21+
/** @typedef {{ start: number, end: number, name: string }} ModuleReferenceMatch */
1822

1923
const MODULE_REFERENCE_REGEXP =
20-
/^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(_deferredImport)?(_mangleableNs)?(?:_asiSafe(\d))?__$/;
24+
/^__WEBPACK_MODULE_REFERENCE__(\d+)_([\da-f]+|ns)(_call)?(_directImport)?(_deferredImport)?(_mangleableNs)?(_moduleExportsAccess)?(?:_asiSafe(\d))?__$/;
25+
26+
// unanchored variant: wrapped CJS bodies are not re-parsed, so their
27+
// references must be found textually
28+
const MODULE_REFERENCE_SCAN_REGEXP = new RegExp(
29+
MODULE_REFERENCE_REGEXP.source.slice(1, -1),
30+
"g"
31+
);
32+
33+
/** @type {WeakMap<Source, ModuleReferenceMatch[]>} */
34+
const moduleReferencesCache = new WeakMap();
2135

2236
/**
2337
* Encodes how a concatenated module reference should be interpreted when it is
@@ -28,6 +42,7 @@ const MODULE_REFERENCE_REGEXP =
2842
* @property {boolean} directImport true, when this referenced export is directly imported (not via property access)
2943
* @property {boolean} deferredImport true, when this referenced export is deferred
3044
* @property {boolean} mangleableNamespace true, when a whole-namespace reference may use a decoupled namespace object that keeps the original export names
45+
* @property {boolean} moduleExportsAccess true, when the reference resolves to the module's own exports value with plain property access, skipping ESM interop (how `require()` and a wrapped module's side-effect init see it)
3146
* @property {boolean | undefined} asiSafe if the position is ASI safe or unknown
3247
*/
3348

@@ -58,6 +73,10 @@ class ConcatenationScope {
5873
this.usedNames = usedNames;
5974
/** @type {Map<Module, ModuleInfo>} */
6075
this._modulesMap = modulesMap;
76+
// written and read by dependency templates within a single code
77+
// generation pass, never across passes
78+
/** @type {Range[] | undefined} */
79+
this._replacedRequireRanges = undefined;
6180
}
6281

6382
/**
@@ -69,6 +88,63 @@ class ConcatenationScope {
6988
return this._modulesMap.has(module);
7089
}
7190

91+
/**
92+
* Whether an in-scope module's body renders inside the lazy wrapper.
93+
* @param {Module} module the referenced module
94+
* @returns {boolean} true, when it's wrapped
95+
*/
96+
isModuleWrapped(module) {
97+
const info = this._modulesMap.get(module);
98+
return info !== undefined && info.wrapped;
99+
}
100+
101+
/**
102+
* Marks a wrapped module as eagerly imported, so its accessor is called at its
103+
* own slot in evaluation order instead of from inside the importing body.
104+
* @param {Module} module the referenced module
105+
*/
106+
registerEagerModule(module) {
107+
const info = this._modulesMap.get(module);
108+
if (info !== undefined) info.eager = true;
109+
}
110+
111+
/**
112+
* Whether the current module runs inside the lazy wrapper. Exports then live
113+
* on a real exports object, not hoisted bindings, so templates emit runtime
114+
* code.
115+
* @returns {boolean} true, when the current module is wrapped
116+
*/
117+
isWrapped() {
118+
return this._currentModule.wrapped;
119+
}
120+
121+
/**
122+
* Records a `require(...)` call that was replaced as a whole by a
123+
* concatenation reference, so nothing else rewrites a range inside it.
124+
* @param {Range} range source range of the replaced call
125+
*/
126+
registerReplacedRequire(range) {
127+
if (this._replacedRequireRanges === undefined) {
128+
this._replacedRequireRanges = [];
129+
}
130+
this._replacedRequireRanges.push(range);
131+
}
132+
133+
/**
134+
* Checks whether an offset sits inside an already-replaced `require(...)` call.
135+
* Containment, not exact match: `new require(...)` is replaced from `new`.
136+
* @param {number} start start offset to test
137+
* @returns {boolean} true, when the offset was replaced already
138+
*/
139+
isInsideReplacedRequire(start) {
140+
const ranges = this._replacedRequireRanges;
141+
if (ranges === undefined) return false;
142+
for (const [rangeStart, rangeEnd] of ranges) {
143+
if (start >= rangeStart && start < rangeEnd) return true;
144+
}
145+
return false;
146+
}
147+
72148
/**
73149
* Records the symbol that should be used when the current module exports a
74150
* named binding.
@@ -149,6 +225,7 @@ class ConcatenationScope {
149225
directImport = false,
150226
deferredImport = false,
151227
mangleableNamespace = false,
228+
moduleExportsAccess = false,
152229
asiSafe = false
153230
}
154231
) {
@@ -157,6 +234,9 @@ class ConcatenationScope {
157234
const directImportFlag = directImport ? "_directImport" : "";
158235
const deferredImportFlag = deferredImport ? "_deferredImport" : "";
159236
const mangleableNamespaceFlag = mangleableNamespace ? "_mangleableNs" : "";
237+
const moduleExportsAccessFlag = moduleExportsAccess
238+
? "_moduleExportsAccess"
239+
: "";
160240
const asiSafeFlag = asiSafe
161241
? "_asiSafe1"
162242
: asiSafe === false
@@ -166,7 +246,7 @@ class ConcatenationScope {
166246
? Buffer.from(JSON.stringify(ids), "utf8").toString("hex")
167247
: "ns";
168248
// a "._" is appended to allow "delete ...", which would cause a SyntaxError in strict mode
169-
return `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${directImportFlag}${deferredImportFlag}${mangleableNamespaceFlag}${asiSafeFlag}__._`;
249+
return `__WEBPACK_MODULE_REFERENCE__${info.index}_${exportData}${callFlag}${directImportFlag}${deferredImportFlag}${mangleableNamespaceFlag}${moduleExportsAccessFlag}${asiSafeFlag}__._`;
170250
}
171251

172252
/**
@@ -179,6 +259,31 @@ class ConcatenationScope {
179259
return MODULE_REFERENCE_REGEXP.test(name);
180260
}
181261

262+
/**
263+
* Finds all encoded module references in a generated source.
264+
* @param {Source} source generated source
265+
* @returns {ModuleReferenceMatch[]} reference token positions (end is exclusive, excludes the appended "._")
266+
*/
267+
static findModuleReferences(source) {
268+
const cached = moduleReferencesCache.get(source);
269+
if (cached !== undefined) return cached;
270+
const code = source.source().toString();
271+
/** @type {ModuleReferenceMatch[]} */
272+
const result = [];
273+
MODULE_REFERENCE_SCAN_REGEXP.lastIndex = 0;
274+
let match = MODULE_REFERENCE_SCAN_REGEXP.exec(code);
275+
while (match !== null) {
276+
result.push({
277+
start: match.index,
278+
end: match.index + match[0].length,
279+
name: match[0]
280+
});
281+
match = MODULE_REFERENCE_SCAN_REGEXP.exec(code);
282+
}
283+
moduleReferencesCache.set(source, result);
284+
return result;
285+
}
286+
182287
/**
183288
* Parses an encoded module reference back into its module index and
184289
* reference flags.
@@ -189,7 +294,7 @@ class ConcatenationScope {
189294
const match = MODULE_REFERENCE_REGEXP.exec(name);
190295
if (!match) return null;
191296
const index = Number(match[1]);
192-
const asiSafe = match[7];
297+
const asiSafe = match[8];
193298
return {
194299
index,
195300
ids:
@@ -200,6 +305,7 @@ class ConcatenationScope {
200305
directImport: Boolean(match[4]),
201306
deferredImport: Boolean(match[5]),
202307
mangleableNamespace: Boolean(match[6]),
308+
moduleExportsAccess: Boolean(match[7]),
203309
asiSafe: asiSafe ? asiSafe === "1" : undefined
204310
};
205311
}

‎lib/Dependency.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,10 @@ class Dependency {
424424

425425
/**
426426
* Returns true if this dependency can be concatenated
427+
* @param {boolean} concatenateCommonJsModules whether optimization.concatenateModules.commonjs is enabled
427428
* @returns {boolean} true if this dependency can be concatenated
428429
*/
429-
canConcatenate() {
430+
canConcatenate(concatenateCommonJsModules) {
430431
return false;
431432
}
432433

@@ -506,11 +507,12 @@ Dependency.isLowPriorityDependency = (dependency) =>
506507
/**
507508
* Returns true if the dependency can be concatenated (scope hoisting).
508509
* @param {Dependency} dependency dep
510+
* @param {boolean} concatenateCommonJsModules whether optimization.concatenateModules.commonjs is enabled
509511
* @returns {boolean} true if this dependency supports concatenation
510512
*/
511-
Dependency.canConcatenate = (dependency) => {
513+
Dependency.canConcatenate = (dependency, concatenateCommonJsModules) => {
512514
if (typeof dependency.canConcatenate === "function") {
513-
return dependency.canConcatenate();
515+
return dependency.canConcatenate(concatenateCommonJsModules);
514516
}
515517
return false;
516518
};

‎lib/ExternalModule.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const { register } = require("./util/serialization");
4646
/** @typedef {import("./ExportsInfo")} ExportsInfo */
4747
/** @typedef {import("./Generator").GenerateContext} GenerateContext */
4848
/** @typedef {import("./Generator").SourceTypes} SourceTypes */
49+
/** @typedef {import("./Module").BuildMeta} BuildMeta */
4950
/** @typedef {import("./Module").ModuleId} ModuleId */
5051
/** @typedef {import("./Module").BuildCallback} BuildCallback */
5152
/** @typedef {import("./Module").BuildInfo} BuildInfo */
@@ -1107,6 +1108,19 @@ class ExternalModule extends Module {
11071108
return undefined;
11081109
}
11091110

1111+
/**
1112+
* @returns {boolean} true, when it may be wrapped
1113+
*/
1114+
canBeWrappedInConcatenation() {
1115+
if (/** @type {BuildMeta} */ (this.buildMeta).async) {
1116+
return false;
1117+
}
1118+
// every other synchronous type is a plain expression; only "module"
1119+
// renders as a hoisted `import` declaration, and passing it the
1120+
// concatenation scope is what narrows it to named specifiers
1121+
return this._resolveExternalType(this.externalType) !== "module";
1122+
}
1123+
11101124
/**
11111125
* Get request and external type.
11121126
* @private
@@ -1343,6 +1357,13 @@ class ExternalModule extends Module {
13431357
};
13441358
}
13451359
default: {
1360+
// A wrapped external runs as a module body inside the wrapper, so it
1361+
// renders the plain `module.exports =` form: its exports are read
1362+
// through the wrapper accessor, not through a shared-scope binding.
1363+
const wrapped = Boolean(
1364+
concatenationScope &&
1365+
/** @type {ConcatenationScope} */ (concatenationScope).isWrapped()
1366+
);
13461367
const sourceData = this._getSourceData(
13471368
request,
13481369
externalType,
@@ -1351,7 +1372,7 @@ class ExternalModule extends Module {
13511372
chunkGraph,
13521373
runtime,
13531374
this.dependencyMeta,
1354-
concatenationScope
1375+
wrapped ? undefined : concatenationScope
13551376
);
13561377

13571378
// sourceString can be empty str only when there is concatenationScope
@@ -1365,9 +1386,12 @@ class ExternalModule extends Module {
13651386
sourceString = "";
13661387
const scope = /** @type {ConcatenationScope} */ (concatenationScope);
13671388
for (const [specifier, finalName] of specifiers) {
1389+
// the hoisted `import { x as finalName }` binding is a top-level
1390+
// name of the chunk, reserve it before members get renamed
1391+
scope.usedNames.add(finalName);
13681392
scope.registerRawExport(specifier, finalName);
13691393
}
1370-
} else if (concatenationScope) {
1394+
} else if (concatenationScope && !wrapped) {
13711395
sourceString = `${runtimeTemplate.renderConst()} ${
13721396
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
13731397
} = ${sourceString};`;

‎lib/RuntimeGlobals.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ module.exports.chunkCallback = "webpackChunk";
7070
*/
7171
module.exports.chunkName = "__webpack_require__.cn";
7272

73-
/**
74-
* wrap and eagerly execute a CommonJS module body, returning its exports
75-
*/
76-
module.exports.commonJsWrap = "__webpack_require__.cjs";
77-
7873
/**
7974
* compatibility get default export
8075
*/
@@ -85,6 +80,16 @@ module.exports.compatGetDefaultExport = "__webpack_require__.n";
8580
*/
8681
module.exports.compileWasm = "__webpack_require__.vs";
8782

83+
/**
84+
* wrap a concatenated module body as a lazy accessor, returning its exports
85+
*/
86+
module.exports.concatenationWrap = "__webpack_require__.cw";
87+
88+
/**
89+
* apply the `new` return-value rule to a concatenated module's exports
90+
*/
91+
module.exports.constructRequire = "__webpack_require__.cr";
92+
8893
/**
8994
* create a fake namespace object
9095
*/

‎lib/RuntimePlugin.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ const AsyncModuleGeneratorRuntimeModule = require("./runtime/AsyncModuleGenerato
1414
const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
1515
const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
1616
const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
17-
const CommonJsWrapRuntimeModule = require("./runtime/CommonJsWrapRuntimeModule");
1817
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
1918
const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
19+
const ConcatenationWrapRuntimeModule = require("./runtime/ConcatenationWrapRuntimeModule");
20+
const ConstructRequireRuntimeModule = require("./runtime/ConstructRequireRuntimeModule");
2021
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
2122
const CreateScriptRuntimeModule = require("./runtime/CreateScriptRuntimeModule");
2223
const CreateScriptUrlRuntimeModule = require("./runtime/CreateScriptUrlRuntimeModule");
@@ -92,7 +93,9 @@ const GLOBALS_ON_REQUIRE = [
9293
RuntimeGlobals.systemContext,
9394
RuntimeGlobals.onChunksLoaded,
9495
RuntimeGlobals.makeOptimizedDeferredNamespaceObject,
95-
RuntimeGlobals.makeDeferredNamespaceObject
96+
RuntimeGlobals.makeDeferredNamespaceObject,
97+
RuntimeGlobals.concatenationWrap,
98+
RuntimeGlobals.constructRequire
9699
];
97100

98101
const MODULE_DEPENDENCIES = {
@@ -118,7 +121,8 @@ const TREE_DEPENDENCIES = {
118121
RuntimeGlobals.require
119122
],
120123
[RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
121-
[RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty]
124+
[RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty],
125+
[RuntimeGlobals.constructRequire]: [RuntimeGlobals.require]
122126
};
123127

124128
// Tree runtime requirements whose only effect is attaching a zero-arg runtime
@@ -133,7 +137,8 @@ const SIMPLE_TREE_RUNTIME_MODULES = [
133137
],
134138
[RuntimeGlobals.hasOwnProperty, HasOwnPropertyRuntimeModule],
135139
[RuntimeGlobals.compatGetDefaultExport, CompatGetDefaultExportRuntimeModule],
136-
[RuntimeGlobals.commonJsWrap, CommonJsWrapRuntimeModule],
140+
[RuntimeGlobals.concatenationWrap, ConcatenationWrapRuntimeModule],
141+
[RuntimeGlobals.constructRequire, ConstructRequireRuntimeModule],
137142
[
138143
RuntimeGlobals.setAnonymousDefaultName,
139144
SetAnonymousDefaultNameRuntimeModule

0 commit comments

Comments
 (0)