diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index bd736f9671fdf3..eed44dc9c0379c 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -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/ diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index 682fd5aeba6c85..eb1d2cd9ee5ae8 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -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 () => { @@ -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 () => { diff --git a/playground/lib/__tests__/serve.ts b/playground/lib/__tests__/serve.ts index e07efe526eaf00..249bc7de4fec43 100644 --- a/playground/lib/__tests__/serve.ts +++ b/playground/lib/__tests__/serve.ts @@ -82,6 +82,12 @@ export async function serve(): Promise<{ close(): Promise }> { ), }) + 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) => { diff --git a/playground/lib/src/main-named.js b/playground/lib/src/main-named.js new file mode 100644 index 00000000000000..238bedb07c3507 --- /dev/null +++ b/playground/lib/src/main-named.js @@ -0,0 +1,4 @@ +export const foo = 'foo' + +// Force esbuild spread helpers +console.log({ ...foo }) diff --git a/playground/lib/vite.named-exports.config.js b/playground/lib/vite.named-exports.config.js new file mode 100644 index 00000000000000..5c2f3bdb7f3db9 --- /dev/null +++ b/playground/lib/vite.named-exports.config.js @@ -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', + }, +})