Skip to content

Commit

Permalink
feat(ssr): isolated mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 18, 2021
1 parent e9b0d28 commit e954ed2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -187,7 +187,10 @@ export interface ViteDevServer {
/**
* Load a given URL as an instantiated module for SSR.
*/
ssrLoadModule(url: string): Promise<Record<string, any>>
ssrLoadModule(
url: string,
options?: { isolated?: boolean }
): Promise<Record<string, any>>
/**
* Fix ssr error stacktrace
*/
Expand Down Expand Up @@ -254,11 +257,11 @@ export async function createServer(
transformRequest(url, options) {
return transformRequest(url, server, options)
},
ssrLoadModule(url) {
ssrLoadModule(url, options) {
if (!server._ssrExternals) {
server._ssrExternals = resolveSSRExternal(config.root)
}
return ssrLoadModule(url, server)
return ssrLoadModule(url, server, !!options?.isolated)
},
ssrFixStacktrace(e) {
if (e.stack) {
Expand Down
11 changes: 7 additions & 4 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Expand Up @@ -14,6 +14,7 @@ import { transformRequest } from '../server/transformRequest'
export async function ssrLoadModule(
url: string,
server: ViteDevServer,
isolatedMode: boolean,
urlStack: string[] = []
): Promise<Record<string, any>> {
if (urlStack.includes(url)) {
Expand All @@ -25,11 +26,13 @@ export async function ssrLoadModule(

const { moduleGraph } = server
const mod = await moduleGraph.ensureEntryFromUrl(url)
if (mod.ssrModule) {
if (!isolatedMode && mod.ssrModule) {
return mod.ssrModule
}

const result = await transformRequest(url, server, { ssr: true })
const result =
mod.ssrTransformResult ||
(await transformRequest(url, server, { ssr: true }))
if (!result) {
// TODO more info? is this even necessary?
throw new Error(`failed to load module for ssr: $${url}`)
Expand All @@ -40,7 +43,7 @@ export async function ssrLoadModule(
await Promise.all(
result.deps!.map((dep) => {
if (!isExternal(dep)) {
return ssrLoadModule(dep, server, urlStack.concat(url))
return ssrLoadModule(dep, server, isolatedMode, urlStack.concat(url))
}
})
)
Expand Down Expand Up @@ -70,7 +73,7 @@ export async function ssrLoadModule(
if (isExternal(dep)) {
return Promise.resolve(nodeRequire(dep, mod.file))
} else {
return ssrLoadModule(dep, server, urlStack.concat(url))
return ssrLoadModule(dep, server, isolatedMode, urlStack.concat(url))
}
}

Expand Down

0 comments on commit e954ed2

Please sign in to comment.