Skip to content

Commit

Permalink
fix(lib): esbuild helper functions injection not working with named e…
Browse files Browse the repository at this point in the history
…xports (#14539)
  • Loading branch information
sapphi-red authored Oct 9, 2023
1 parent 27bffc4 commit 5004d00
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import type { Plugin } from '../plugin'

const debug = createDebugger('vite:esbuild')

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

const validExtensionRE = /\.\w+$/
const jsxExtensionsRE = /\.(?:j|t)sx\b/
Expand Down
6 changes: 6 additions & 0 deletions playground/lib/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ describe.runIf(isBuild)('build', () => {
const noMinifyCode = readFile(
'dist/nominify/my-lib-custom-filename.umd.cjs',
)
const namedCode = readFile('dist/named/my-lib-named.umd.cjs')
// esbuild helpers are injected inside of the UMD wrapper
expect(code).toMatch(/^\(function\(/)
expect(noMinifyCode).toMatch(
/^\(function\(global.+?"use strict";var.+?function\smyLib\(/s,
)
expect(namedCode).toMatch(/^\(function\(/)
})

test('iife', async () => {
Expand All @@ -32,11 +34,15 @@ describe.runIf(isBuild)('build', () => {
const noMinifyCode = readFile(
'dist/nominify/my-lib-custom-filename.iife.js',
)
const namedCode = readFile('dist/named/my-lib-named.iife.js')
// esbuild helpers are injected inside of the IIFE wrapper
expect(code).toMatch(/^var MyLib=function\(\)\{\s*"use strict";/)
expect(noMinifyCode).toMatch(
/^var MyLib\s*=\s*function\(\)\s*\{\s*"use strict";/,
)
expect(namedCode).toMatch(
/^var MyLibNamed=function\([^()]+\)\{\s*"use strict";/,
)
})

test('restrisct-helpers-injection', async () => {
Expand Down
6 changes: 6 additions & 0 deletions playground/lib/__tests__/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
),
})

await build({
root: rootDir,
logLevel: 'warn', // output esbuild warns
configFile: path.resolve(__dirname, '../vite.named-exports.config.js'),
})

// start static file server
const serve = sirv(path.resolve(rootDir, 'dist'))
const httpServer = http.createServer((req, res) => {
Expand Down
4 changes: 4 additions & 0 deletions playground/lib/src/main-named.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const foo = 'foo'

// Force esbuild spread helpers
console.log({ ...foo })
20 changes: 20 additions & 0 deletions playground/lib/vite.named-exports.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
esbuild: {
supported: {
// Force esbuild inject helpers to test regex
'object-rest-spread': false,
},
},
build: {
lib: {
entry: path.resolve(__dirname, 'src/main-named.js'),
name: 'MyLibNamed',
formats: ['umd', 'iife'],
fileName: 'my-lib-named',
},
outDir: 'dist/named',
},
})

0 comments on commit 5004d00

Please sign in to comment.