diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index bd1f7dfae90ba9..246fb8f9667a63 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -27,9 +27,9 @@ import { searchForWorkspaceRoot } from '..' const debug = createDebugger('vite:esbuild') const INJECT_HELPERS_IIFE_RE = - /^(.*?)((?:const|var) \S+=function\([^)]*\)\{"use strict";)/s + /^(.*?)((?:const|var)\s+\S+\s*=\s*function\s*\([^)]*\)\s*\{.*?"use strict";)/s const INJECT_HELPERS_UMD_RE = - /^(.*?)(\(function\([^)]*\)\{.+amd.+function\([^)]*\)\{"use strict";)/s + /^(.*?)(\(function\([^)]*\)\s*\{.+amd.+function\([^)]*\)\s*\{.*?"use strict";)/s let server: ViteDevServer diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index 50419ee5bb2874..5940f8ed0a93ce 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -16,15 +16,23 @@ describe.runIf(isBuild)('build', () => { test('umd', async () => { expect(await page.textContent('.umd')).toBe('It works') const code = readFile('dist/my-lib-custom-filename.umd.js') + const noMinifyCode = readFile('dist/nominify/my-lib-custom-filename.umd.js') // esbuild helpers are injected inside of the UMD wrapper expect(code).toMatch(/^\(function\(/) + expect(noMinifyCode).toMatch(/^\(function\(global/) }) test('iife', async () => { expect(await page.textContent('.iife')).toBe('It works') const code = readFile('dist/my-lib-custom-filename.iife.js') + const noMinifyCode = readFile( + '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";/) + expect(noMinifyCode).toMatch( + /^var MyLib\s*=\s*function\(\)\s*\{.*?"use strict";/s, + ) }) test('Library mode does not include `preload`', async () => { diff --git a/playground/lib/__tests__/serve.ts b/playground/lib/__tests__/serve.ts index c12368fb581fea..0329eb34af56e4 100644 --- a/playground/lib/__tests__/serve.ts +++ b/playground/lib/__tests__/serve.ts @@ -61,6 +61,12 @@ export async function serve(): Promise<{ close(): Promise }> { configFile: path.resolve(__dirname, '../vite.dyimport.config.js'), }) + await build({ + root: rootDir, + logLevel: 'warn', // output esbuild warns + configFile: path.resolve(__dirname, '../vite.nominify.config.js'), + }) + // start static file server const serve = sirv(path.resolve(rootDir, 'dist')) const httpServer = http.createServer((req, res) => { diff --git a/playground/lib/vite.nominify.config.js b/playground/lib/vite.nominify.config.js new file mode 100644 index 00000000000000..9c2eb7f1511611 --- /dev/null +++ b/playground/lib/vite.nominify.config.js @@ -0,0 +1,11 @@ +const baseConfig = require('./vite.config') + +module.exports = { + ...baseConfig, + build: { + ...baseConfig.build, + minify: false, + outDir: 'dist/nominify', + }, + plugins: [], +}