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

feat(ssr): support circular dependencies in ssr #2489

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ export async function ssrLoadModule(
url = unwrapId(url)

if (urlStack.includes(url)) {
server.config.logger.warn(
`Circular dependency: ${urlStack.join(' -> ')} -> ${url}`
)
return {}
const { moduleGraph } = server
const mod = await moduleGraph.ensureEntryFromUrl(url)
return mod.ssrModule as SSRModule
}

// when we instantiate multiple dependency modules in parallel, they may
Expand Down Expand Up @@ -76,6 +75,8 @@ async function instantiateModule(
}
Object.defineProperty(ssrModule, '__esModule', { value: true })

mod.ssrModule = ssrModule

const isExternal = (dep: string) => dep[0] !== '.' && dep[0] !== '/'

await Promise.all(
Expand Down Expand Up @@ -117,7 +118,6 @@ async function instantiateModule(
}
}
}

try {
new Function(
`global`,
Expand All @@ -136,6 +136,7 @@ async function instantiateModule(
ssrExportAll
)
} catch (e) {
mod.ssrModule = null
e.stack = ssrRewriteStacktrace(e.stack, moduleGraph)
server.config.logger.error(
`Error when evaluating SSR module ${url}:\n${e.stack}`,
Expand All @@ -147,7 +148,7 @@ async function instantiateModule(
throw e
}

mod.ssrModule = Object.freeze(ssrModule)
Object.freeze(ssrModule)
return ssrModule
}

Expand Down