Commit 3e13d69
Add ECMAScript method shorthand support to webpack5-module-minifier-plugin (#5620)
* Initial plan
* Add infrastructure for ECMAScript method shorthand support
- Added MODULE_WRAPPER_SHORTHAND_PREFIX and MODULE_WRAPPER_SHORTHAND_SUFFIX constants
- Added isMethodShorthandFormat() detection function (currently returns false for backward compatibility)
- Updated wrapping/unwrapping logic to handle both regular and shorthand formats
- Updated MockMinifier to handle shorthand format
- Exported new constants from index.ts
- All tests passing
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Implement ECMAScript method shorthand detection and handling
- Implemented isMethodShorthandFormat() to detect shorthand by checking for absence of '=>' or 'function(' before first '{'
- Updated unwrapping logic to correctly extract shorthand format by finding __DEFAULT_ID__ marker and removing wrapper
- Fixed MockMinifier to properly handle shorthand format without breaking the method syntax
- All tests passing with updated snapshots
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Address code review feedback
- Use regex pattern /function\s*\(/ for more robust function keyword detection with whitespace variations
- Replace magic number 3 with explicit minifiedSuffix variable for better maintainability
- Add documentation showing complete wrapper structure in constants comments
- All tests still passing, no security issues found
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* 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>
* Fix syntax errors in chunks by using property access token
- Changed module token from `__WEBPACK_CHUNK_MODULE__hash` to `(){this["__WEBPACK_CHUNK_MODULE__hash"]}`
- This creates syntactically valid JavaScript that terser can parse and minify
- After minification, terser converts bracket notation to dot notation: `this.__WEBPACK_CHUNK_MODULE__hash`
- Updated CHUNK_MODULE_REGEX to match the minified format
- All tests passing with no syntax errors in chunks
- Terser can now successfully minify assets containing module placeholders
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Revert to simpler token and fix detection logic
- Changed shorthand detection to return false (webpack doesn't emit true shorthand yet)
- Using regular wrapper format for all modules
- Token format: `(){void "__WEBPACK_CHUNK_MODULE__hash"}`
- Regex matches complete token structure
- Tests passing but terser still has syntax errors (need to investigate webpack output formats)
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Fix syntax errors by re-enabling shorthand detection and skipping token asset minification
- Re-enabled isMethodShorthandFormat() detection (webpack emits modules without function keyword)
- Modules wrapped as __MINIFY_MODULE__({__DEFAULT_ID__(__params__){body}}); for shorthand format
- Assets with tokens skip minification to avoid syntax errors
- Added WebpackOutputFormats tests to verify code sent to minifier
- All tests passing with ZERO syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Address code review feedback - improve comments and remove redundant eslint-disable
- Clarified shorthand detection comment to reference ECMAScript method shorthand in object literals
- Added comment explaining why void operator is used for token format
- Removed redundant eslint-disable comments (underscore prefix already signals intentionally unused)
- All tests passing, no warnings, zero syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Address review feedback: improve tests and MockMinifier comments
- MockMinifier: Added comment markers for shorthand format matching regular format style
- WebpackOutputFormats: Deduplicated test code into shared runWebpackWithEnvironment function
- Added methodShorthand=true test case to cover all three syntaxes
- Test expectations now based on environment settings (methodShorthand, arrowFunction)
- All 7 tests passing with zero syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Construct shorthand constants by concatenating to base constants
- MODULE_WRAPPER_SHORTHAND_PREFIX now built as MODULE_WRAPPER_PREFIX + '{__DEFAULT_ID__'
- MODULE_WRAPPER_SHORTHAND_SUFFIX now built as '}' + MODULE_WRAPPER_SUFFIX
- Makes the relationship between base and shorthand constants explicit
- API signature updated (types changed from literals to string)
- All 7 tests still passing with zero syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Fix regex to handle all three token representations and hoist function regex
- Updated CHUNK_MODULE_REGEX to capture optional leading ':' as first group
- Regex now handles: "id":token (object, no shorthand), token (array), "id"token (object, shorthand)
- Rehydration logic preserves ':' for non-shorthand, discards for shorthand
- Added isShorthand field to IModuleInfo interface
- Hoisted FUNCTION_KEYWORD_REGEX outside isMethodShorthandFormat function
- All 7 tests passing with zero syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Fix module wrapping and token format per review feedback
- Use isShorthand from closure instead of moduleShorthandFormat Set (removed Set entirely)
- Token format: plain token for non-shorthand, ':' prefix for shorthand (':__WEBPACK_CHUNK_MODULE__hash')
- Simplified CHUNK_MODULE_REGEX to match plain token with optional ':' prefix
- Reverted asset token skip logic (was categorically wrong)
- Updated comment: regular format is function(...) or (...)=>, not (...){}
- All 7 tests passing with zero syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
* Final cleanup: strictNullChecks compliance, template types, and indexOf pattern
- MockMinifier: Use local function with multiple returns for strictNullChecks compliance
- Constants: Use template string types for MODULE_WRAPPER_SHORTHAND_PREFIX/SUFFIX to show exact types in API docs
- ModuleMinifierPlugin: Change indexOf check from === -1 to < 0 (more concise, language-friendly)
- All 7 tests passing with zero syntax errors
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>1 parent 2234ea4 commit 3e13d69
12 files changed
Lines changed: 1278 additions & 85 deletions
File tree
- common
- changes/@rushstack/webpack5-module-minifier-plugin
- reviews/api
- webpack/webpack5-module-minifier-plugin/src
- test
- __snapshots__
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
113 | 120 | | |
114 | 121 | | |
115 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
17 | 34 | | |
18 | 35 | | |
19 | 36 | | |
| |||
22 | 39 | | |
23 | 40 | | |
24 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
25 | 47 | | |
26 | 48 | | |
27 | | - | |
| 49 | + | |
28 | 50 | | |
29 | 51 | | |
30 | 52 | | |
| |||
Lines changed: 73 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| 79 | + | |
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
| |||
125 | 128 | | |
126 | 129 | | |
127 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
128 | 171 | | |
129 | 172 | | |
130 | 173 | | |
| |||
333 | 376 | | |
334 | 377 | | |
335 | 378 | | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
336 | 385 | | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
342 | 389 | | |
343 | 390 | | |
344 | 391 | | |
| |||
386 | 433 | | |
387 | 434 | | |
388 | 435 | | |
389 | | - | |
390 | | - | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
391 | 448 | | |
392 | 449 | | |
393 | 450 | | |
| |||
402 | 459 | | |
403 | 460 | | |
404 | 461 | | |
405 | | - | |
| 462 | + | |
| 463 | + | |
406 | 464 | | |
407 | 465 | | |
408 | 466 | | |
| |||
414 | 472 | | |
415 | 473 | | |
416 | 474 | | |
417 | | - | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
418 | 480 | | |
419 | 481 | | |
420 | | - | |
| 482 | + | |
| 483 | + | |
421 | 484 | | |
422 | 485 | | |
423 | 486 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
77 | 82 | | |
78 | 83 | | |
79 | 84 | | |
| |||
Lines changed: 17 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
| 50 | + | |
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
69 | 79 | | |
70 | 80 | | |
71 | 81 | | |
| |||
78 | 88 | | |
79 | 89 | | |
80 | 90 | | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
81 | 97 | | |
82 | 98 | | |
83 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
Lines changed: 29 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
12 | 17 | | |
13 | 18 | | |
14 | 19 | | |
| |||
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
27 | | - | |
28 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
29 | 47 | | |
30 | 48 | | |
31 | | - | |
32 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
33 | 57 | | |
34 | 58 | | |
35 | 59 | | |
| |||
0 commit comments