Skip to content

Commit 5004d00

Browse files
authored
fix(lib): esbuild helper functions injection not working with named exports (#14539)
1 parent 27bffc4 commit 5004d00

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import type { Plugin } from '../plugin'
2525

2626
const debug = createDebugger('vite:esbuild')
2727

28-
// IIFE content looks like `var MyLib = function() {`. Spaces are removed when minified
28+
// IIFE content looks like `var MyLib = function() {`.
29+
// Spaces are removed and parameters are mangled when minified
2930
const IIFE_BEGIN_RE =
30-
/(const|var)\s+\S+\s*=\s*function\(\)\s*\{.*"use strict";/s
31+
/(const|var)\s+\S+\s*=\s*function\([^()]*\)\s*\{\s*"use strict";/
3132

3233
const validExtensionRE = /\.\w+$/
3334
const jsxExtensionsRE = /\.(?:j|t)sx\b/

playground/lib/__tests__/lib.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ describe.runIf(isBuild)('build', () => {
1919
const noMinifyCode = readFile(
2020
'dist/nominify/my-lib-custom-filename.umd.cjs',
2121
)
22+
const namedCode = readFile('dist/named/my-lib-named.umd.cjs')
2223
// esbuild helpers are injected inside of the UMD wrapper
2324
expect(code).toMatch(/^\(function\(/)
2425
expect(noMinifyCode).toMatch(
2526
/^\(function\(global.+?"use strict";var.+?function\smyLib\(/s,
2627
)
28+
expect(namedCode).toMatch(/^\(function\(/)
2729
})
2830

2931
test('iife', async () => {
@@ -32,11 +34,15 @@ describe.runIf(isBuild)('build', () => {
3234
const noMinifyCode = readFile(
3335
'dist/nominify/my-lib-custom-filename.iife.js',
3436
)
37+
const namedCode = readFile('dist/named/my-lib-named.iife.js')
3538
// esbuild helpers are injected inside of the IIFE wrapper
3639
expect(code).toMatch(/^var MyLib=function\(\)\{\s*"use strict";/)
3740
expect(noMinifyCode).toMatch(
3841
/^var MyLib\s*=\s*function\(\)\s*\{\s*"use strict";/,
3942
)
43+
expect(namedCode).toMatch(
44+
/^var MyLibNamed=function\([^()]+\)\{\s*"use strict";/,
45+
)
4046
})
4147

4248
test('restrisct-helpers-injection', async () => {

playground/lib/__tests__/serve.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
8282
),
8383
})
8484

85+
await build({
86+
root: rootDir,
87+
logLevel: 'warn', // output esbuild warns
88+
configFile: path.resolve(__dirname, '../vite.named-exports.config.js'),
89+
})
90+
8591
// start static file server
8692
const serve = sirv(path.resolve(rootDir, 'dist'))
8793
const httpServer = http.createServer((req, res) => {

playground/lib/src/main-named.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const foo = 'foo'
2+
3+
// Force esbuild spread helpers
4+
console.log({ ...foo })
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import path from 'node:path'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
esbuild: {
6+
supported: {
7+
// Force esbuild inject helpers to test regex
8+
'object-rest-spread': false,
9+
},
10+
},
11+
build: {
12+
lib: {
13+
entry: path.resolve(__dirname, 'src/main-named.js'),
14+
name: 'MyLibNamed',
15+
formats: ['umd', 'iife'],
16+
fileName: 'my-lib-named',
17+
},
18+
outDir: 'dist/named',
19+
},
20+
})

0 commit comments

Comments
 (0)