@@ -318,6 +318,37 @@ const rollupToEsbuildFormatMap: Record<
318318 iife : undefined ,
319319}
320320
321+ // #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
322+ // names are minified potentially causing collision with other globals.
323+ // We inject the helpers inside the wrappers.
324+ // e.g. turn:
325+ // <esbuild helpers> (function(){ /*actual content/* })()
326+ // into:
327+ // (function(){ <esbuild helpers> /*actual content/* })()
328+ // Not using regex because it's too hard to rule out performance issues like #8738 #8099 #10900 #14065
329+ // Instead, using plain string index manipulation (indexOf, slice) which is simple and performant
330+ // We don't need to create a MagicString here because both the helpers and
331+ // the headers don't modify the sourcemap
332+ export const injectEsbuildHelpers = (
333+ esbuildCode : string ,
334+ format : string ,
335+ ) : string => {
336+ const contentIndex =
337+ format === 'iife'
338+ ? Math . max ( esbuildCode . search ( IIFE_BEGIN_RE ) , 0 )
339+ : format === 'umd'
340+ ? esbuildCode . indexOf ( `(function(` ) // same for minified or not
341+ : 0
342+
343+ if ( contentIndex > 0 ) {
344+ const esbuildHelpers = esbuildCode . slice ( 0 , contentIndex )
345+ return esbuildCode
346+ . slice ( contentIndex )
347+ . replace ( '"use strict";' , ( m : string ) => m + esbuildHelpers )
348+ }
349+ return esbuildCode
350+ }
351+
321352export const buildEsbuildPlugin = ( ) : Plugin => {
322353 return {
323354 name : 'vite:esbuild-transpile' ,
@@ -346,30 +377,7 @@ export const buildEsbuildPlugin = (): Plugin => {
346377 )
347378
348379 if ( config . build . lib ) {
349- // #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
350- // names are minified potentially causing collision with other globals.
351- // We inject the helpers inside the wrappers.
352- // e.g. turn:
353- // <esbuild helpers> (function(){ /*actual content/* })()
354- // into:
355- // (function(){ <esbuild helpers> /*actual content/* })()
356- // Not using regex because it's too hard to rule out performance issues like #8738 #8099 #10900 #14065
357- // Instead, using plain string index manipulation (indexOf, slice) which is simple and performant
358- // We don't need to create a MagicString here because both the helpers and
359- // the headers don't modify the sourcemap
360- const esbuildCode = res . code
361- const contentIndex =
362- opts . format === 'iife'
363- ? Math . max ( esbuildCode . search ( IIFE_BEGIN_RE ) , 0 )
364- : opts . format === 'umd'
365- ? esbuildCode . indexOf ( `(function(` ) // same for minified or not
366- : 0
367- if ( contentIndex > 0 ) {
368- const esbuildHelpers = esbuildCode . slice ( 0 , contentIndex )
369- res . code = esbuildCode
370- . slice ( contentIndex )
371- . replace ( `"use strict";` , `"use strict";` + esbuildHelpers )
372- }
380+ res . code = injectEsbuildHelpers ( res . code , opts . format )
373381 }
374382
375383 return res
0 commit comments