Skip to content

Commit 54e1275

Browse files
committed
revert: "fix: use string manipulation instead of regex to inject esbuild helpers
(#14094)" This reverts commit 128ad8f.
1 parent f3a3e77 commit 54e1275

File tree

4 files changed

+17
-30
lines changed

4 files changed

+17
-30
lines changed

packages/vite/src/node/plugins/esbuild.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import { searchForWorkspaceRoot } from '../server/searchRoot'
2828

2929
const debug = createDebugger('vite:esbuild')
3030

31-
// IIFE content looks like `var MyLib = function() {`. Spaces are removed when minified
32-
const IIFE_BEGIN_RE =
33-
/(const|var)\s+\S+\s*=\s*function\(\)\s*\{.*"use strict";/s
31+
const INJECT_HELPERS_IIFE_RE =
32+
/^(.*?)((?:const|var)\s+\S+\s*=\s*function\s*\([^)]*\)\s*\{\s*"use strict";)/s
33+
const INJECT_HELPERS_UMD_RE =
34+
/^(.*?)(\(function\([^)]*\)\s*\{.+?amd.+?function\([^)]*\)\s*\{\s*"use strict";)/s
3435

3536
const validExtensionRE = /\.\w+$/
3637
const jsxExtensionsRE = /\.(?:j|t)sx\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
}

playground/lib/__tests__/lib.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ describe.runIf(isBuild)('build', () => {
3333
'dist/nominify/my-lib-custom-filename.iife.js',
3434
)
3535
// esbuild helpers are injected inside of the IIFE wrapper
36-
// esbuild has a bug that injects some statements before `"use strict"`: https://github.com/evanw/esbuild/issues/3322
37-
// remove the `.*?` part once it's fixed
38-
expect(code).toMatch(/^var MyLib=function\(\)\{.*?"use strict";/)
36+
expect(code).toMatch(/^var MyLib=function\(\)\{"use strict";/)
3937
expect(noMinifyCode).toMatch(
4038
/^var MyLib\s*=\s*function\(\)\s*\{.*?"use strict";/s,
4139
)

playground/lib/src/main.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,3 @@ export default function myLib(sel) {
1010
// make sure umd helper has been moved to the right position
1111
console.log(`amd function(){ "use strict"; }`)
1212
}
13-
14-
// For triggering unhandled global esbuild helpers in previous regex-based implementation for injection
15-
;(function () {})()?.foo

playground/lib/vite.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default defineConfig({
77
supported: {
88
// Force esbuild inject helpers to test regex
99
'object-rest-spread': false,
10-
'optional-chain': false,
1110
},
1211
},
1312
build: {

0 commit comments

Comments
 (0)