@@ -28,9 +28,10 @@ import { searchForWorkspaceRoot } from '../server/searchRoot'
28
28
29
29
const debug = createDebugger ( 'vite:esbuild' )
30
30
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
34
35
35
36
const validExtensionRE = / \. \w + $ /
36
37
const jsxExtensionsRE = / \. (?: j | t ) s x \b /
@@ -332,30 +333,22 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
332
333
if ( config . build . lib ) {
333
334
// #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
334
335
// 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.
342
337
// We don't need to create a MagicString here because both the helpers and
343
338
// 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
+ )
356
350
}
357
351
}
358
-
359
352
return res
360
353
} ,
361
354
}
0 commit comments