-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ViteDevServer } from '..' | ||
import { transformRequest } from './transformRequest' | ||
|
||
export async function ssrLoadModule( | ||
url: string, | ||
server: ViteDevServer | ||
): Promise<Record<string, any>> { | ||
const { moduleGraph } = server | ||
const mod = await moduleGraph.ensureEntryFromUrl(url) | ||
if (mod.ssrModule) { | ||
return mod.ssrModule | ||
} | ||
|
||
const result = 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}`) | ||
} | ||
|
||
await Promise.all(result.deps!.map((dep) => ssrLoadModule(dep, server))) | ||
|
||
const __import__ = (dep: string) => { | ||
return moduleGraph.urlToModuleMap.get(dep)?.ssrModule | ||
} | ||
const __exports__ = {} | ||
|
||
new Function(`__import__`, `__exports__`, result.code)( | ||
__import__, | ||
__exports__ | ||
) | ||
|
||
mod.ssrModule = __exports__ | ||
return __exports__ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters