Skip to content

Commit cb7e191

Browse files
Copilotdmichon-msft
andcommitted
Fix whitespace and simplify unwrapping logic per review feedback
- Remove whitespace from MODULE_WRAPPER_SHORTHAND_PREFIX/SUFFIX (minifier strips it) - Change moduleShorthandFormat from Map to Set (only true values matter) - Simplify unwrapping logic to just use prefix/suffix lengths instead of complex searching - All tests passing Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
1 parent 0990711 commit cb7e191

5 files changed

Lines changed: 23 additions & 46 deletions

File tree

common/reviews/api/webpack5-module-minifier-plugin.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ export interface IRenderedModulePosition {
111111
export const MODULE_WRAPPER_PREFIX: '__MINIFY_MODULE__(';
112112

113113
// @public
114-
export const MODULE_WRAPPER_SHORTHAND_PREFIX: '__MINIFY_MODULE__({\n__DEFAULT_ID__';
114+
export const MODULE_WRAPPER_SHORTHAND_PREFIX: '__MINIFY_MODULE__({__DEFAULT_ID__';
115115

116116
// @public
117-
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: '\n});';
117+
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: '});';
118118

119119
// @public
120120
export const MODULE_WRAPPER_SUFFIX: ');';

webpack/webpack5-module-minifier-plugin/src/Constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ export const MODULE_WRAPPER_SUFFIX: ');' = ');';
2121
* Public because alternate Minifier implementations may wish to know about it.
2222
* @public
2323
*/
24-
export const MODULE_WRAPPER_SHORTHAND_PREFIX: '__MINIFY_MODULE__({\n__DEFAULT_ID__' =
25-
'__MINIFY_MODULE__({\n__DEFAULT_ID__';
24+
export const MODULE_WRAPPER_SHORTHAND_PREFIX: '__MINIFY_MODULE__({__DEFAULT_ID__' =
25+
'__MINIFY_MODULE__({__DEFAULT_ID__';
2626
/**
2727
* Suffix to wrap ECMAScript method shorthand `(module, __webpack_exports__, __webpack_require__) { ... }` so that the minifier doesn't delete it.
2828
* Used when webpack emits modules using shorthand syntax.
2929
* Combined with the prefix, creates: `__MINIFY_MODULE__({__DEFAULT_ID__(params){body}});`
3030
* Public because alternate Minifier implementations may wish to know about it.
3131
* @public
3232
*/
33-
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: '\n});' = '\n});';
33+
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: '});' = '});';
3434

3535
/**
3636
* Token preceding a module id in the emitted asset so the minifier can operate on the Webpack runtime or chunk boilerplate in isolation

webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,9 @@ export class ModuleMinifierPlugin implements WebpackPluginInstance {
258258
const submittedModules: Set<string | number> = new Set();
259259

260260
/**
261-
* Map to track which modules use ECMAScript method shorthand format.
262-
* Key is the module hash, value is true if shorthand format is used.
261+
* Set of module hashes that use ECMAScript method shorthand format.
263262
*/
264-
const moduleShorthandFormat: Map<string, boolean> = new Map();
263+
const moduleShorthandFormat: Set<string> = new Set();
265264

266265
/**
267266
* The text and comments of all minified modules.
@@ -403,7 +402,9 @@ export class ModuleMinifierPlugin implements WebpackPluginInstance {
403402
submittedModules.add(hash);
404403

405404
// Track whether this module uses shorthand format
406-
moduleShorthandFormat.set(hash, isShorthand);
405+
if (isShorthand) {
406+
moduleShorthandFormat.add(hash);
407+
}
407408

408409
++pendingMinificationRequests;
409410

@@ -436,39 +437,15 @@ export class ModuleMinifierPlugin implements WebpackPluginInstance {
436437
const len: number = minified.length;
437438

438439
// Trim off the boilerplate used to preserve the factory
439-
// Use different logic for shorthand vs regular format
440-
const isShorthandModule: boolean = moduleShorthandFormat.get(hash) || false;
440+
// Use different prefix/suffix lengths for shorthand vs regular format
441+
const isShorthandModule: boolean = moduleShorthandFormat.has(hash);
441442
if (isShorthandModule) {
442-
// For shorthand format, we wrapped it as: __MINIFY_MODULE__({\n__DEFAULT_ID__(args) {...}\n});
443-
// After minification, it becomes: __MINIFY_MODULE__({__DEFAULT_ID__(args){...}});
444-
// We need to extract just: (args){...}
445-
446-
// Strategy: Find and remove the prefix up to and including '__DEFAULT_ID__'
447-
const defaultIdStr: string = '__DEFAULT_ID__';
448-
const shorthandPrefixEnd: number = minified.indexOf(defaultIdStr);
449-
if (shorthandPrefixEnd >= 0) {
450-
// Remove from start up to and including '__DEFAULT_ID__'
451-
unwrapped.replace(0, shorthandPrefixEnd + defaultIdStr.length - 1, '');
452-
// Remove the suffix from the end
453-
// After minification, the suffix is typically '});' (3 characters, newline removed)
454-
const minifiedSuffix: string = '});';
455-
if (minified.endsWith(minifiedSuffix)) {
456-
unwrapped.replace(len - minifiedSuffix.length, len - 1, '');
457-
} else if (minified.endsWith(MODULE_WRAPPER_SHORTHAND_SUFFIX)) {
458-
// In case minifier keeps the newline
459-
unwrapped.replace(len - MODULE_WRAPPER_SHORTHAND_SUFFIX.length, len - 1, '');
460-
} else {
461-
// Fallback: assume '});' suffix
462-
unwrapped.replace(len - minifiedSuffix.length, len - 1, '');
463-
}
464-
} else {
465-
// Fallback: If __DEFAULT_ID__ is not found (shouldn't happen), remove by length
466-
const minifiedSuffix: string = '});';
467-
unwrapped.replace(0, MODULE_WRAPPER_SHORTHAND_PREFIX.length - 1, '');
468-
unwrapped.replace(len - minifiedSuffix.length, len - 1, '');
469-
}
443+
// For shorthand format: __MINIFY_MODULE__({__DEFAULT_ID__(args){...}});
444+
// Remove prefix and suffix by their lengths
445+
unwrapped.replace(0, MODULE_WRAPPER_SHORTHAND_PREFIX.length - 1, '');
446+
unwrapped.replace(len - MODULE_WRAPPER_SHORTHAND_SUFFIX.length, len - 1, '');
470447
} else {
471-
// Regular format
448+
// Regular format: __MINIFY_MODULE__((args){...});
472449
unwrapped.replace(0, MODULE_WRAPPER_PREFIX.length - 1, '');
473450
unwrapped.replace(len - MODULE_WRAPPER_SUFFIX.length, len - 1, '');
474451
}

webpack/webpack5-module-minifier-plugin/src/test/__snapshots__/AmdExternals.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`ModuleMinifierPlugin Handles AMD externals (mock): Content 1`] = `
44
Object {
55
"/release/async.js": "/*! For license information please see async.js.LICENSE.txt */
6-
// Begin Asset Hash=68ab9ab7e0ab87d08307091bb1e31e56e04b12fc882535d275e7d4c9402a5d8b
6+
// Begin Asset Hash=cfb7f7d353fd9d01c51f613f9235ad90b50fe78f746b088cf52a92517a5fc458
77
\\"use strict\\";
88
(self[\\"webpackChunk\\"] = self[\\"webpackChunk\\"] || []).push([[157],{
99
@@ -344,7 +344,7 @@ exports[`ModuleMinifierPlugin Handles AMD externals (terser): Content 1`] = `Obj
344344
exports[`ModuleMinifierPlugin Handles AMD externals (terser): Errors 1`] = `
345345
Array [
346346
Object {
347-
"message": "Unexpected token name «__WEBPACK_CHUNK_MODULE__54d801f652de2ab1c87a646d9bc448cff4235f0d68b5519fdb388f0c0c0af447», expected punc «,»",
347+
"message": "Unexpected token name «__WEBPACK_CHUNK_MODULE__aa928190bac95603578bbc24c65647633e6305dc69d46edaf07d710fc69da78d», expected punc «,»",
348348
},
349349
]
350350
`;

webpack/webpack5-module-minifier-plugin/src/test/__snapshots__/MultipleRuntimes.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`ModuleMinifierPlugin Handles multiple runtimes (mock): Content 1`] = `
44
Object {
55
"/release/async-1.js": "/*! For license information please see async-1.js.LICENSE.txt */
6-
// Begin Asset Hash=69b04c1d29fa46faf36d042bbe433bfe80bf6de89433dda1e91248932b0bdaaf
6+
// Begin Asset Hash=b53e0a088f83c5d7fc7ba3ca806e0471adea1460f448a26004ed99835ccffac3
77
\\"use strict\\";
88
(self[\\"webpackChunk\\"] = self[\\"webpackChunk\\"] || []).push([[527],{
99
@@ -39,7 +39,7 @@ function async1() { console.log('async-1'); }
3939
// @license MIT
4040
",
4141
"/release/async-2.js": "/*! For license information please see async-2.js.LICENSE.txt */
42-
// Begin Asset Hash=d8c163257b9c0ffa0929f47ce896d7edbb1412dc70ce4327c3fc9e2d7a028e92
42+
// Begin Asset Hash=2e943d5dc5a407a894b565d54ceeb3f554742f52442d698c22b8795be4d4694c
4343
\\"use strict\\";
4444
(self[\\"webpackChunk\\"] = self[\\"webpackChunk\\"] || []).push([[324],{
4545
@@ -635,10 +635,10 @@ exports[`ModuleMinifierPlugin Handles multiple runtimes (terser): Content 1`] =
635635
exports[`ModuleMinifierPlugin Handles multiple runtimes (terser): Errors 1`] = `
636636
Array [
637637
Object {
638-
"message": "Unexpected token name «__WEBPACK_CHUNK_MODULE__c3587d3a82556f8e3f1b0b4d1fcbdefd62eedc2f1c5708b0a824a504bef85276», expected punc «,»",
638+
"message": "Unexpected token name «__WEBPACK_CHUNK_MODULE__522ee27cac2edf706f26b73b1e5ec4bc44feefe1107c5e303f671547dff3b8d1», expected punc «,»",
639639
},
640640
Object {
641-
"message": "Unexpected token name «__WEBPACK_CHUNK_MODULE__bf7c8493af96fc819aeda647ea3306127d1b5c0239a14cb225810a4644dfe910», expected punc «,»",
641+
"message": "Unexpected token name «__WEBPACK_CHUNK_MODULE__9fa9db01a39f213b0bad42889493581d8a3cc9d76f3b77543aa34e2cfc6622ed», expected punc «,»",
642642
},
643643
]
644644
`;

0 commit comments

Comments
 (0)