@@ -140,10 +140,26 @@ function isLicenseComment(comment: Comment): boolean {
140140 * @returns true if the code is in method shorthand format
141141 */
142142function isMethodShorthandFormat ( code : string ) : boolean {
143- // Method shorthand detection is not currently needed as webpack doesn't emit true method shorthand yet
144- // Webpack emits either function expressions or arrow functions, both of which should use regular wrapping
145- // This function is kept for future compatibility when webpack adds method shorthand support
146- return false ;
143+ // Find the position of the first opening brace
144+ const firstBraceIndex : number = code . indexOf ( '{' ) ;
145+ if ( firstBraceIndex === - 1 ) {
146+ // No brace found, not a function format
147+ return false ;
148+ }
149+
150+ // Get the code before the first brace
151+ const beforeBrace : string = code . slice ( 0 , firstBraceIndex ) ;
152+
153+ // Check if it contains '=>' or 'function('
154+ // If it does, it's a regular arrow function or function expression, not shorthand
155+ // Use a simple check that handles common whitespace variations
156+ if ( beforeBrace . includes ( '=>' ) || / f u n c t i o n \s * \( / . test ( beforeBrace ) ) {
157+ return false ;
158+ }
159+
160+ // If neither '=>' nor 'function(' are found, assume method shorthand format
161+ // This handles the case where webpack emits: (__params__) { body } without function keyword
162+ return true ;
147163}
148164
149165/**
@@ -499,68 +515,100 @@ export class ModuleMinifierPlugin implements WebpackPluginInstance {
499515
500516 // Verify that this is a JS asset
501517 if ( isJSAsset . test ( assetName ) ) {
502- ++ pendingMinificationRequests ;
503-
504- const { source : wrappedCodeRaw , map } = useSourceMaps
505- ? asset . sourceAndMap ( )
506- : {
507- source : asset . source ( ) ,
508- map : undefined
509- } ;
510-
511- const rawCode : string = wrappedCodeRaw . toString ( ) ;
512- const nameForMap : string = `(chunks)/${ assetName } ` ;
513-
514- const hash : string = hashCodeFragment ( rawCode ) ;
515-
516- minifier . minify (
517- {
518- hash,
519- code : rawCode ,
520- nameForMap : useSourceMaps ? nameForMap : undefined ,
521- externals : undefined
522- } ,
523- ( result : IModuleMinificationResult ) => {
524- if ( isMinificationResultError ( result ) ) {
525- compilation . errors . push ( result . error as WebpackError ) ;
526- // eslint-disable-next-line no-console
527- console . error ( result . error ) ;
528- } else {
529- try {
530- const { code : minified , map : minifierMap } = result ;
531-
532- const rawOutput : sources . Source = useSourceMaps
533- ? new SourceMapSource (
534- minified , // Code
535- nameForMap , // File
536- minifierMap ?? undefined , // Base source map
537- rawCode , // Source from before transform
538- map ?? undefined , // Source Map from before transform
539- true // Remove original source
540- )
541- : new RawSource ( minified ) ;
542-
543- const withIds : sources . Source = postProcessCode ( new ReplaceSource ( rawOutput ) , {
544- compilation,
545- module : undefined ,
546- loggingName : assetName
547- } ) ;
548-
518+ // Check if asset contains module tokens (which would make it invalid for minification)
519+ const assetSource : string = asset . source ( ) . toString ( ) ;
520+ const hasTokens : boolean = assetSource . includes ( CHUNK_MODULE_TOKEN ) ;
521+
522+ if ( hasTokens ) {
523+ // Asset contains tokens - don't try to minify it, just store for rehydration
524+ minifiedAssets . set ( assetName , {
525+ source : postProcessCode ( new ReplaceSource ( asset ) , {
526+ compilation,
527+ module : undefined ,
528+ loggingName : assetName
529+ } ) ,
530+ chunk,
531+ fileName : assetName ,
532+ renderInfo : new Map ( ) ,
533+ type : 'javascript'
534+ } ) ;
535+ } else {
536+ // Asset doesn't have tokens - safe to minify
537+ ++ pendingMinificationRequests ;
538+
539+ const { source : wrappedCodeRaw , map } = useSourceMaps
540+ ? asset . sourceAndMap ( )
541+ : {
542+ source : asset . source ( ) ,
543+ map : undefined
544+ } ;
545+
546+ const rawCode : string = wrappedCodeRaw . toString ( ) ;
547+ const nameForMap : string = `(chunks)/${ assetName } ` ;
548+
549+ const hash : string = hashCodeFragment ( rawCode ) ;
550+
551+ minifier . minify (
552+ {
553+ hash,
554+ code : rawCode ,
555+ nameForMap : useSourceMaps ? nameForMap : undefined ,
556+ externals : undefined
557+ } ,
558+ ( result : IModuleMinificationResult ) => {
559+ if ( isMinificationResultError ( result ) ) {
560+ compilation . errors . push ( result . error as WebpackError ) ;
561+ // eslint-disable-next-line no-console
562+ console . error ( result . error ) ;
563+ // Store unminified asset as fallback
549564 minifiedAssets . set ( assetName , {
550- source : new CachedSource ( withIds ) ,
565+ source : postProcessCode ( new ReplaceSource ( asset ) , {
566+ compilation,
567+ module : undefined ,
568+ loggingName : assetName
569+ } ) ,
551570 chunk,
552571 fileName : assetName ,
553572 renderInfo : new Map ( ) ,
554573 type : 'javascript'
555574 } ) ;
556- } catch ( err ) {
557- compilation . errors . push ( err ) ;
575+ } else {
576+ try {
577+ const { code : minified , map : minifierMap } = result ;
578+
579+ const rawOutput : sources . Source = useSourceMaps
580+ ? new SourceMapSource (
581+ minified , // Code
582+ nameForMap , // File
583+ minifierMap ?? undefined , // Base source map
584+ rawCode , // Source from before transform
585+ map ?? undefined , // Source Map from before transform
586+ true // Remove original source
587+ )
588+ : new RawSource ( minified ) ;
589+
590+ const withIds : sources . Source = postProcessCode ( new ReplaceSource ( rawOutput ) , {
591+ compilation,
592+ module : undefined ,
593+ loggingName : assetName
594+ } ) ;
595+
596+ minifiedAssets . set ( assetName , {
597+ source : new CachedSource ( withIds ) ,
598+ chunk,
599+ fileName : assetName ,
600+ renderInfo : new Map ( ) ,
601+ type : 'javascript'
602+ } ) ;
603+ } catch ( err ) {
604+ compilation . errors . push ( err ) ;
605+ }
558606 }
559- }
560607
561- onFileMinified ( ) ;
562- }
563- ) ;
608+ onFileMinified ( ) ;
609+ }
610+ ) ;
611+ }
564612 } else {
565613 // This isn't a JS asset. Don't try to minify the asset wrapper, though if it contains modules, those might still get replaced with minified versions.
566614 minifiedAssets . set ( assetName , {
0 commit comments