Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: iife error in lib mode(fix #9318) #9319

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -27,7 +27,7 @@ import { searchForWorkspaceRoot } from '..'
const debug = createDebugger('vite:esbuild')

const INJECT_HELPERS_IIFE_RE =
/^(.*)((?:const|var) [^\s]+=function\([^)]*?\){"use strict";)/s
/^(.*)((?:var) [^\s]+=function\([^)]*?\){"use strict";)/s
const INJECT_HELPERS_UMD_RE =
/^(.*)(\(function\([^)]*?\){.+amd.+function\([^)]*?\){"use strict";)/s

Expand Down
2 changes: 1 addition & 1 deletion playground/lib/__tests__/lib.spec.ts
Expand Up @@ -23,7 +23,7 @@ describe.runIf(isBuild)('build', () => {
expect(await page.textContent('.iife')).toBe('It works')
const code = readFile('dist/my-lib-custom-filename.iife.js')
// esbuild helpers are injected inside of the IIFE wrapper
expect(code).toMatch(/^const MyLib=function\(\){"use strict";/)
expect(code).toMatch(/^var MyLib=function\(\){"use strict";/)
})

test('Library mode does not include `preload`', async () => {
Expand Down
4 changes: 2 additions & 2 deletions playground/lib/index.dist.html
Expand Up @@ -27,11 +27,11 @@

<script src="./my-lib-custom-filename.umd.js"></script>
<script>
MyLib('.umd')
window.MyLib('.umd')
delete window.MyLib // so that it won't affect iife
</script>

<script src="./my-lib-custom-filename.iife.js"></script>
<script>
MyLib('.iife')
window.MyLib('.iife')
</script>