Skip to content

Commit a59de43

Browse files
authored
refactor: share the module factory function header with wrapped bodies (#22254)
1 parent 87a62de commit a59de43

26 files changed

Lines changed: 231 additions & 163 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+
Give a wrapped concatenated body the same function header as a module factory.

‎lib/externals/ExternalModule.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ class ExternalModule extends Module {
15771577
}
15781578

15791579
let runtimeRequirements = sourceData.runtimeRequirements;
1580-
if (!concatenationScope) {
1580+
if (!concatenationScope || wrapped) {
15811581
if (!runtimeRequirements) {
15821582
runtimeRequirements = RUNTIME_REQUIREMENTS;
15831583
} else {

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -956,35 +956,17 @@ class JavascriptModulesPlugin {
956956
}
957957
if (source === undefined) {
958958
const factorySource = new ConcatSource();
959-
/** @type {string[]} */
960-
const args = [];
961-
if (needExports || needRequire || needModule) {
962-
args.push(
963-
needModule
964-
? module.moduleArgument
965-
: `__unused_webpack_${module.moduleArgument}`
966-
);
967-
}
968-
if (needExports || needRequire) {
969-
args.push(
970-
needExports
971-
? module.exportsArgument
972-
: `__unused_webpack_${module.exportsArgument}`
973-
);
974-
}
975-
if (needRequire) args.push(RuntimeGlobals.require);
976-
977-
if (renderShorthand) {
978-
// we can optimize function to methodShorthand if render module factory in object
979-
factorySource.add(`(${args.join(", ")}) {\n\n`);
980-
} else if (
981-
!needThisAsExports &&
982-
runtimeTemplate.supportsArrowFunction()
983-
) {
984-
factorySource.add(`/***/ ((${args.join(", ")}) => {\n\n`);
985-
} else {
986-
factorySource.add(`/***/ (function(${args.join(", ")}) {\n\n`);
987-
}
959+
const header = runtimeTemplate.moduleFactoryFnHeader({
960+
module,
961+
needModule,
962+
needExports,
963+
needRequire,
964+
needThisAsExports,
965+
shorthand: renderShorthand
966+
});
967+
factorySource.add(
968+
renderShorthand ? `${header}\n\n` : `/***/ (${header}\n\n`
969+
);
988970

989971
if (needStrict) {
990972
factorySource.add('"use strict";\n');

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,14 +2715,26 @@ ${defineGetters}`
27152715
for (const info of wrappedInfos) {
27162716
if (info.type === "concatenated") {
27172717
runtimeRequirements.add(RuntimeGlobals.concatenationWrap);
2718+
const bodyRuntimeRequirements =
2719+
/** @type {ReadOnlyRuntimeRequirements} */
2720+
(info.runtimeRequirements);
27182721
result.add(
27192722
`\n// MODULE: ${info.module.readableIdentifier(
27202723
requestShortener
27212724
)}\nvar ${info.namespaceFnName} = /*#__PURE__*/${
27222725
RuntimeGlobals.concatenationWrap
2723-
}(function(${info.module.moduleArgument}, ${
2724-
info.module.exportsArgument
2725-
}) {\n`
2726+
}(${runtimeTemplate.moduleFactoryFnHeader({
2727+
module: info.module,
2728+
// the trailing `module.exports` assignment below reads it
2729+
needModule:
2730+
Boolean(info.namespaceExportSymbol) ||
2731+
bodyRuntimeRequirements.has(RuntimeGlobals.module),
2732+
needExports: bodyRuntimeRequirements.has(RuntimeGlobals.exports),
2733+
needRequire: false,
2734+
needThisAsExports: bodyRuntimeRequirements.has(
2735+
RuntimeGlobals.thisAsExports
2736+
)
2737+
})}\n`
27262738
);
27272739
result.add(/** @type {ReplaceSource} */ (info.source));
27282740
// A generated body binds its value to a local rather than writing

‎lib/template/RuntimeTemplate.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4304,6 +4304,49 @@ class RuntimeTemplate {
43044304
return this.returningFunction(factory);
43054305
}
43064306

4307+
/**
4308+
* @param {object} options options
4309+
* @param {Module} options.module the module the body belongs to
4310+
* @param {boolean} options.needModule whether the body reads `module`
4311+
* @param {boolean} options.needExports whether the body reads `exports`
4312+
* @param {boolean} options.needRequire whether the body reads `__webpack_require__`
4313+
* @param {boolean} options.needThisAsExports whether the body reads `this` as exports
4314+
* @param {boolean=} options.shorthand render as a method shorthand, without the `function` keyword
4315+
* @returns {string} the header, ending with the opening brace
4316+
*/
4317+
moduleFactoryFnHeader({
4318+
module,
4319+
needModule,
4320+
needExports,
4321+
needRequire,
4322+
needThisAsExports,
4323+
shorthand = false
4324+
}) {
4325+
/** @type {string[]} */
4326+
const args = [];
4327+
if (needExports || needRequire || needModule) {
4328+
args.push(
4329+
needModule
4330+
? module.moduleArgument
4331+
: `__unused_webpack_${module.moduleArgument}`
4332+
);
4333+
}
4334+
if (needExports || needRequire) {
4335+
args.push(
4336+
needExports
4337+
? module.exportsArgument
4338+
: `__unused_webpack_${module.exportsArgument}`
4339+
);
4340+
}
4341+
if (needRequire) args.push(RuntimeGlobals.require);
4342+
const parameters = args.join(", ");
4343+
if (shorthand) return `(${parameters}) {`;
4344+
if (!needThisAsExports && this.supportsArrowFunction()) {
4345+
return `(${parameters}) => {`;
4346+
}
4347+
return `function(${parameters}) {`;
4348+
}
4349+
43074350
/**
43084351
* Define es module flag statement.
43094352
* @param {object} options options

‎test/configCases/concatenate-modules/commonjs-exports-template/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-exports-template comm
3030
\\\\******************************/
3131
3232
// MODULE: ./cjs.js
33-
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
33+
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw((__unused_webpack_module, exports) => {
3434
var __webpack_unused_export__;
3535
3636
@@ -42,7 +42,7 @@ exports.readBack = \`\${exports.used}!\`;
4242
});
4343
4444
// MODULE: ./this-cjs.js
45-
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
45+
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(__unused_webpack_module, exports) {
4646
4747
4848
// \`this\` is the exports object at module top level, and stays untouched
@@ -104,7 +104,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-exports-template comm
104104
\\\\******************************/
105105
106106
// MODULE: ./cjs.js
107-
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
107+
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw((__unused_webpack_module, exports) => {
108108
var __webpack_unused_export__;
109109
110110
@@ -116,7 +116,7 @@ exports.readBack = \`\${exports.used}!\`;
116116
});
117117
118118
// MODULE: ./this-cjs.js
119-
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
119+
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(__unused_webpack_module, exports) {
120120
121121
122122
// \`this\` is the exports object at module top level, and stays untouched
@@ -178,7 +178,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-exports-template comm
178178
\\\\******************************/
179179
180180
// MODULE: ./cjs.js
181-
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
181+
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw((__unused_webpack_module, exports) => {
182182
var __webpack_unused_export__;
183183
184184
@@ -190,7 +190,7 @@ exports.readBack = \`\${exports.used}!\`;
190190
});
191191
192192
// MODULE: ./this-cjs.js
193-
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
193+
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(__unused_webpack_module, exports) {
194194
195195
196196
// \`this\` is the exports object at module top level, and stays untouched
@@ -252,7 +252,7 @@ exports[`ConfigCacheTestCases concatenate-modules commonjs-exports-template comm
252252
\\\\******************************/
253253
254254
// MODULE: ./cjs.js
255-
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
255+
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw((__unused_webpack_module, exports) => {
256256
var __webpack_unused_export__;
257257
258258
@@ -264,7 +264,7 @@ exports.readBack = \`\${exports.used}!\`;
264264
});
265265
266266
// MODULE: ./this-cjs.js
267-
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
267+
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(__unused_webpack_module, exports) {
268268
269269
270270
// \`this\` is the exports object at module top level, and stays untouched

‎test/configCases/concatenate-modules/commonjs-exports-template/__snapshots__/ConfigTest.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports[`ConfigTestCases concatenate-modules commonjs-exports-template commonjs-
3030
\\\\******************************/
3131
3232
// MODULE: ./cjs.js
33-
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
33+
var cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw((__unused_webpack_module, exports) => {
3434
var __webpack_unused_export__;
3535
3636
@@ -42,7 +42,7 @@ exports.readBack = \`\${exports.used}!\`;
4242
});
4343
4444
// MODULE: ./this-cjs.js
45-
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
45+
var this_cjs_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(__unused_webpack_module, exports) {
4646
4747
4848
// \`this\` is the exports object at module top level, and stays untouched

0 commit comments

Comments
 (0)