Add ECMAScript method shorthand support to webpack5-module-minifier-plugin#5620
Add ECMAScript method shorthand support to webpack5-module-minifier-plugin#5620dmichon-msft merged 14 commits intomainfrom
Conversation
- 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>
- 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>
- 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>
dmichon-msft
left a comment
There was a problem hiding this comment.
This appears to still be broken.
webpack/webpack5-module-minifier-plugin/src/test/__snapshots__/MultipleRuntimes.test.ts.snap
Outdated
Show resolved
Hide resolved
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
- 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>
Fixed in cb7e191. The issues were:
All addressed with simplified implementation and tests now passing. |
dmichon-msft
left a comment
There was a problem hiding this comment.
Add at least one snapshot unit test verifying the code being sent to the minifier. Customize output.environment.methodShorthand and output.environment.arrowFunction to test all three possible syntaxes.
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
- 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>
- 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>
…en 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>
Added in 2a20639. Created WebpackOutputFormats test suite with snapshot tests that capture the code being sent to the minifier. Tests verify with both |
…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>
- 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>
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
…n 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>
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
- 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>
dmichon-msft
left a comment
There was a problem hiding this comment.
Almost there, just a couple minor bits of cleanup.
webpack/webpack5-module-minifier-plugin/src/test/MockMinifier.ts
Outdated
Show resolved
Hide resolved
webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts
Outdated
Show resolved
Hide resolved
…Of 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>
Implementation Complete - Ready for Merge
All review feedback addressed with final cleanup changes.
Final Cleanup
Complete Implementation
Method Shorthand Detection: Hoisted regex, detects modules without
functionor=>Module Wrapping: Shorthand uses
{__DEFAULT_ID__...}, regular usesfunctionor arrowToken Format: Plain token or
:tokenprefix for shorthandConstants: Template types show exact structure in API docs
Final Results
✅ All 7 tests passing
✅ Zero syntax errors
✅ strictNullChecks compliant
✅ Clean API documentation
✅ All 14 review comments addressed
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.