Skip to content

Commit

Permalink
fix(ssr): fix 'global' variable name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Aug 2, 2024
1 parent 0f56e17 commit 6bdff8b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const global = 'ok'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default global
12 changes: 12 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ test('import.meta.filename/dirname returns same value with Node', async () => {
expect(viteValue.dirname).toBe(path.dirname(filename))
expect(viteValue.filename).toBe(filename)
})

test('can export global', async () => {
const server = await createDevServer()
const mod = await server.ssrLoadModule('/fixtures/global/export.js')
expect(mod.global).toBe('ok')
})

test('can access nodejs global', async () => {
const server = await createDevServer()
const mod = await server.ssrLoadModule('/fixtures/global/test.js')
expect(mod.default).toBe(globalThis)
})
2 changes: 0 additions & 2 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ async function instantiateModule(

try {
const initModule = new AsyncFunction(
`global`,
ssrModuleExportsKey,
ssrImportMetaKey,
ssrImportKey,
Expand All @@ -224,7 +223,6 @@ async function instantiateModule(
`\n//# sourceURL=${mod.id}${sourceMapSuffix}`,
)
await initModule(
context.global,
ssrModule,
ssrImportMeta,
ssrImport,
Expand Down

0 comments on commit 6bdff8b

Please sign in to comment.