Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use string manipulation instead of regex to inject esbuild helpers #14094

Merged
merged 4 commits into from Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 23 additions & 16 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -28,10 +28,9 @@ import { searchForWorkspaceRoot } from '../server/searchRoot'

const debug = createDebugger('vite:esbuild')

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

const validExtensionRE = /\.\w+$/
const jsxExtensionsRE = /\.(?:j|t)sx\b/
Expand Down Expand Up @@ -333,22 +332,30 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
if (config.build.lib) {
// #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
// names are minified potentially causing collision with other globals.
// We use a regex to inject the helpers inside the wrappers.
// We inject the helpers inside the wrappers.
// e.g. turn:
// <esbuild helpers> (function(){ /*actual content/* })()
// into:
// (function(){ <esbuild helpers> /*actual content/* })()
// Not using regex because it's too hard to rule out performance issues like #8738 #8099 #10900 #14065
// Instead, using plain string index manipulation (indexOf, slice) which is simple and performant
// We don't need to create a MagicString here because both the helpers and
// the headers don't modify the sourcemap
const injectHelpers =
opts.format === 'umd'
? INJECT_HELPERS_UMD_RE
: opts.format === 'iife'
? INJECT_HELPERS_IIFE_RE
: undefined
if (injectHelpers) {
res.code = res.code.replace(
injectHelpers,
(_, helpers, header) => header + helpers,
)
const esbuildCode = res.code
const contentIndex =
opts.format === 'iife'
? esbuildCode.match(IIFE_BEGIN_RE)?.index || 0
: opts.format === 'umd'
? esbuildCode.indexOf(`(function(`) // same for minified or not
: 0
if (contentIndex > 0) {
const esbuildHelpers = esbuildCode.slice(0, contentIndex)
res.code = esbuildCode
.slice(contentIndex)
.replace(`"use strict";`, `"use strict";` + esbuildHelpers)
}
}

return res
},
}
Expand Down
4 changes: 3 additions & 1 deletion playground/lib/__tests__/lib.spec.ts
Expand Up @@ -33,7 +33,9 @@ describe.runIf(isBuild)('build', () => {
'dist/nominify/my-lib-custom-filename.iife.js',
)
// esbuild helpers are injected inside of the IIFE wrapper
expect(code).toMatch(/^var MyLib=function\(\)\{"use strict";/)
// esbuild has a bug that injects some statements before `"use strict"`: https://github.com/evanw/esbuild/issues/3322
// remove the `.*?` part once it's fixed
expect(code).toMatch(/^var MyLib=function\(\)\{.*?"use strict";/)
expect(noMinifyCode).toMatch(
/^var MyLib\s*=\s*function\(\)\s*\{.*?"use strict";/s,
)
Expand Down
3 changes: 3 additions & 0 deletions playground/lib/src/main.js
Expand Up @@ -10,3 +10,6 @@ export default function myLib(sel) {
// make sure umd helper has been moved to the right position
console.log(`amd function(){ "use strict"; }`)
}

// For triggering unhandled global esbuild helpers in previous regex-based implementation for injection
;(function () {})()?.foo
1 change: 1 addition & 0 deletions playground/lib/vite.config.js
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
supported: {
// Force esbuild inject helpers to test regex
'object-rest-spread': false,
'optional-chain': false,
},
},
build: {
Expand Down