Skip to content

Commit

Permalink
fix(esbuild): avoid polluting global namespace while minify is false (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Feb 1, 2023
1 parent 6f9fa83 commit c895379
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions playground/lib/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
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 @@ -61,6 +61,12 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
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) => {
Expand Down
11 changes: 11 additions & 0 deletions playground/lib/vite.nominify.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const baseConfig = require('./vite.config')

module.exports = {
...baseConfig,
build: {
...baseConfig.build,
minify: false,
outDir: 'dist/nominify',
},
plugins: [],
}

0 comments on commit c895379

Please sign in to comment.