Skip to content

Commit

Permalink
fix(ssr): fix global variable name conflict (#17809)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Aug 2, 2024
1 parent 0f56e17 commit 6aa2206
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export async function _createServer(
return devHtmlTransformFn(server, url, html, originalUrl)
},
async ssrLoadModule(url, opts?: { fixStacktrace?: boolean }) {
return ssrLoadModule(url, server, undefined, opts?.fixStacktrace)
return ssrLoadModule(url, server, opts?.fixStacktrace)
},
async ssrFetchModule(url: string, importer?: string) {
return ssrFetchModule(server, url, importer)
Expand Down
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)
})
12 changes: 2 additions & 10 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import {
} from './ssrTransform'
import { ssrFixStacktrace } from './ssrStacktrace'

interface SSRContext {
global: typeof globalThis
}

type SSRModule = Record<string, any>

interface NodeImportResolveOptions
Expand All @@ -45,7 +41,6 @@ const importErrors = new WeakMap<Error, { importee: string }>()
export async function ssrLoadModule(
url: string,
server: ViteDevServer,
context: SSRContext = { global },
fixStacktrace?: boolean,
): Promise<SSRModule> {
url = unwrapId(url)
Expand All @@ -59,7 +54,7 @@ export async function ssrLoadModule(
return pending
}

const modulePromise = instantiateModule(url, server, context, fixStacktrace)
const modulePromise = instantiateModule(url, server, fixStacktrace)
pendingModules.set(url, modulePromise)
modulePromise
.catch(() => {
Expand All @@ -74,7 +69,6 @@ export async function ssrLoadModule(
async function instantiateModule(
url: string,
server: ViteDevServer,
context: SSRContext = { global },
fixStacktrace?: boolean,
): Promise<SSRModule> {
const { moduleGraph } = server
Expand Down Expand Up @@ -169,7 +163,7 @@ async function instantiateModule(
}
}

return ssrLoadModule(dep, server, context, fixStacktrace)
return ssrLoadModule(dep, server, fixStacktrace)
} catch (err) {
// tell external error handler which mod was imported with error
importErrors.set(err, { importee: dep })
Expand Down Expand Up @@ -213,7 +207,6 @@ async function instantiateModule(

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

0 comments on commit 6aa2206

Please sign in to comment.