@@ -28,9 +28,10 @@ import { searchForWorkspaceRoot } from '../server/searchRoot'
2828
2929const debug = createDebugger ( 'vite:esbuild' )
3030
31- // IIFE content looks like `var MyLib = function() {`. Spaces are removed when minified
32- const IIFE_BEGIN_RE =
33- / ( c o n s t | v a r ) \s + \S + \s * = \s * f u n c t i o n \( \) \s * \{ .* " u s e s t r i c t " ; / s
31+ const INJECT_HELPERS_IIFE_RE =
32+ / ^ ( .* ?) ( (?: c o n s t | v a r ) \s + \S + \s * = \s * f u n c t i o n \s * \( [ ^ ) ] * \) \s * \{ \s * " u s e s t r i c t " ; ) / s
33+ const INJECT_HELPERS_UMD_RE =
34+ / ^ ( .* ?) ( \( f u n c t i o n \( [ ^ ) ] * \) \s * \{ .+ ?a m d .+ ?f u n c t i o n \( [ ^ ) ] * \) \s * \{ \s * " u s e s t r i c t " ; ) / s
3435
3536const validExtensionRE = / \. \w + $ /
3637const jsxExtensionsRE = / \. (?: j | t ) s x \b /
@@ -332,30 +333,22 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
332333 if ( config . build . lib ) {
333334 // #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
334335 // names are minified potentially causing collision with other globals.
335- // We inject the helpers inside the wrappers.
336- // e.g. turn:
337- // <esbuild helpers> (function(){ /*actual content/* })()
338- // into:
339- // (function(){ <esbuild helpers> /*actual content/* })()
340- // Not using regex because it's too hard to rule out performance issues like #8738 #8099 #10900 #14065
341- // Instead, using plain string index manipulation (indexOf, slice) which is simple and performant
336+ // We use a regex to inject the helpers inside the wrappers.
342337 // We don't need to create a MagicString here because both the helpers and
343338 // the headers don't modify the sourcemap
344- const esbuildCode = res . code
345- const contentIndex =
346- opts . format === 'iife'
347- ? esbuildCode . match ( IIFE_BEGIN_RE ) ?. index || 0
348- : opts . format === 'umd'
349- ? esbuildCode . indexOf ( `(function(` ) // same for minified or not
350- : 0
351- if ( contentIndex > 0 ) {
352- const esbuildHelpers = esbuildCode . slice ( 0 , contentIndex )
353- res . code = esbuildCode
354- . slice ( contentIndex )
355- . replace ( `"use strict";` , `"use strict";` + esbuildHelpers )
339+ const injectHelpers =
340+ opts . format === 'umd'
341+ ? INJECT_HELPERS_UMD_RE
342+ : opts . format === 'iife'
343+ ? INJECT_HELPERS_IIFE_RE
344+ : undefined
345+ if ( injectHelpers ) {
346+ res . code = res . code . replace (
347+ injectHelpers ,
348+ ( _ , helpers , header ) => header + helpers ,
349+ )
356350 }
357351 }
358-
359352 return res
360353 } ,
361354 }
0 commit comments