Skip to content

Commit c895379

Browse files
author
sun0day
authoredFeb 1, 2023
fix(esbuild): avoid polluting global namespace while minify is false (#11882)
1 parent 6f9fa83 commit c895379

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import { searchForWorkspaceRoot } from '..'
2727
const debug = createDebugger('vite:esbuild')
2828

2929
const INJECT_HELPERS_IIFE_RE =
30-
/^(.*?)((?:const|var) \S+=function\([^)]*\)\{"use strict";)/s
30+
/^(.*?)((?:const|var)\s+\S+\s*=\s*function\s*\([^)]*\)\s*\{.*?"use strict";)/s
3131
const INJECT_HELPERS_UMD_RE =
32-
/^(.*?)(\(function\([^)]*\)\{.+amd.+function\([^)]*\)\{"use strict";)/s
32+
/^(.*?)(\(function\([^)]*\)\s*\{.+amd.+function\([^)]*\)\s*\{.*?"use strict";)/s
3333

3434
let server: ViteDevServer
3535

‎playground/lib/__tests__/lib.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ describe.runIf(isBuild)('build', () => {
1616
test('umd', async () => {
1717
expect(await page.textContent('.umd')).toBe('It works')
1818
const code = readFile('dist/my-lib-custom-filename.umd.js')
19+
const noMinifyCode = readFile('dist/nominify/my-lib-custom-filename.umd.js')
1920
// esbuild helpers are injected inside of the UMD wrapper
2021
expect(code).toMatch(/^\(function\(/)
22+
expect(noMinifyCode).toMatch(/^\(function\(global/)
2123
})
2224

2325
test('iife', async () => {
2426
expect(await page.textContent('.iife')).toBe('It works')
2527
const code = readFile('dist/my-lib-custom-filename.iife.js')
28+
const noMinifyCode = readFile(
29+
'dist/nominify/my-lib-custom-filename.iife.js',
30+
)
2631
// esbuild helpers are injected inside of the IIFE wrapper
2732
expect(code).toMatch(/^var MyLib=function\(\)\{"use strict";/)
33+
expect(noMinifyCode).toMatch(
34+
/^var MyLib\s*=\s*function\(\)\s*\{.*?"use strict";/s,
35+
)
2836
})
2937

3038
test('Library mode does not include `preload`', async () => {

‎playground/lib/__tests__/serve.ts

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
6161
configFile: path.resolve(__dirname, '../vite.dyimport.config.js'),
6262
})
6363

64+
await build({
65+
root: rootDir,
66+
logLevel: 'warn', // output esbuild warns
67+
configFile: path.resolve(__dirname, '../vite.nominify.config.js'),
68+
})
69+
6470
// start static file server
6571
const serve = sirv(path.resolve(rootDir, 'dist'))
6672
const httpServer = http.createServer((req, res) => {
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const baseConfig = require('./vite.config')
2+
3+
module.exports = {
4+
...baseConfig,
5+
build: {
6+
...baseConfig.build,
7+
minify: false,
8+
outDir: 'dist/nominify',
9+
},
10+
plugins: [],
11+
}

0 commit comments

Comments
 (0)
Please sign in to comment.