From 52a647fa97c351b5127af8001677b2fd70a36341 Mon Sep 17 00:00:00 2001 From: patak-dev Date: Thu, 28 Apr 2022 21:55:45 +0200 Subject: [PATCH 1/2] fix: inject esbuild helpers in IIFE and UMD wrappers (fix #7188, fix #5426) --- packages/playground/lib/__tests__/lib.spec.ts | 12 +++++++++ packages/playground/lib/src/main.js | 3 +++ packages/vite/src/node/plugins/esbuild.ts | 25 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/packages/playground/lib/__tests__/lib.spec.ts b/packages/playground/lib/__tests__/lib.spec.ts index 382fb16510ce6f..f1e93e90d8357b 100644 --- a/packages/playground/lib/__tests__/lib.spec.ts +++ b/packages/playground/lib/__tests__/lib.spec.ts @@ -9,10 +9,22 @@ if (isBuild) { test('umd', async () => { expect(await page.textContent('.umd')).toBe('It works') + const code = fs.readFileSync( + path.join(testDir, 'dist/my-lib-custom-filename.umd.js'), + 'utf-8' + ) + // esbuild helpers are injected inside of the UMD wrapper + expect(code).toMatch(/^\(function\(/) }) test('iife', async () => { expect(await page.textContent('.iife')).toBe('It works') + const code = fs.readFileSync( + path.join(testDir, 'dist/my-lib-custom-filename.iife.js'), + 'utf-8' + ) + // esbuild helpers are injected inside of the IIFE wrapper + expect(code).toMatch(/^var MyLib=function\(\){"use strict";/) }) test('Library mode does not include `preload`', async () => { diff --git a/packages/playground/lib/src/main.js b/packages/playground/lib/src/main.js index 2422edf5829a0e..3a42dc4718db30 100644 --- a/packages/playground/lib/src/main.js +++ b/packages/playground/lib/src/main.js @@ -1,3 +1,6 @@ export default function myLib(sel) { + // Force esbuild helpers + console.log({ ...'foo' }) + document.querySelector(sel).textContent = 'It works' } diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 9e8bae24424d76..bc4a1f780a54d5 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -26,6 +26,11 @@ import { searchForWorkspaceRoot } from '..' const debug = createDebugger('vite:esbuild') +const INJECT_HELPERS_IIFE_RE = + /(.*)(var [^\s]+=function\(.*\){"use strict";)(.*)/ +const INJECT_HELPERS_UMD_RE = + /(.*)(\(function\(.*\){.+amd.+function\(.*\){"use strict";)(.*)/ + let server: ViteDevServer export interface ESBuildOptions extends TransformOptions { @@ -254,6 +259,26 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => { } : undefined) }) + + 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 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, rest) => header + helpers + rest + ) + } + } return res } } From a36be20db0179594a2fdd8caf313ded6e95a67ad Mon Sep 17 00:00:00 2001 From: patak Date: Fri, 29 Apr 2022 15:05:27 +0200 Subject: [PATCH 2/2] chore: link to esbuild issue --- packages/playground/lib/src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/playground/lib/src/main.js b/packages/playground/lib/src/main.js index 3a42dc4718db30..cb2fb3b842dc4f 100644 --- a/packages/playground/lib/src/main.js +++ b/packages/playground/lib/src/main.js @@ -1,5 +1,5 @@ export default function myLib(sel) { - // Force esbuild helpers + // Force esbuild spread helpers (https://github.com/evanw/esbuild/issues/951) console.log({ ...'foo' }) document.querySelector(sel).textContent = 'It works'