Skip to content

Commit f1934f0

Browse files
committed
try fix windows
1 parent df3fcdb commit f1934f0

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

packages/plugin-vite/src/plugins/server_snapshot.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
408408
handler(_code, id) {
409409
if (server) {
410410
const ssrGraph = server.environments.ssr.moduleGraph;
411-
const mod = ssrGraph.getModuleById(id);
411+
const mod = getSsrModule(server.environments.ssr, id);
412412
if (mod === undefined) return;
413413

414414
const snapshot = ssrGraph.getModuleById("\0fresh:server-snapshot");
@@ -424,10 +424,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
424424
if (name !== undefined) {
425425
const route = routes.get(name);
426426
if (route !== undefined) {
427-
const mod = ssrGraph.getModuleById(id);
428-
if (mod !== undefined) {
429-
route.css.push(mod.url);
430-
}
427+
route.css.push(mod.url);
431428

432429
const routeMod = ssrGraph.getModuleById(
433430
`\0fresh-route-css::${name}`,
@@ -611,17 +608,14 @@ async function collectRouteCss(
611608
if (seen.has(current)) continue;
612609
seen.add(current);
613610

614-
// Modules may be registered under either their public id or Vite's
615-
// internal "\0" id, depending on when they entered the graph.
616-
let mod = env.moduleGraph.getModuleById(current) ??
617-
env.moduleGraph.getModuleById(`\0${current}`);
618-
611+
let mod = getSsrModule(env, current);
619612
if (mod?.transformResult == null) {
620613
// Dev transforms are lazy. Force Vite to load the module before we
621614
// inspect its imports for CSS dependencies.
622-
await env.fetchModule(current);
623-
mod = env.moduleGraph.getModuleById(current) ??
624-
env.moduleGraph.getModuleById(`\0${current}`);
615+
await env.fetchModule(
616+
path.isAbsolute(current) ? path.toFileUrl(current).href : current,
617+
);
618+
mod = getSsrModule(env, current);
625619
}
626620

627621
// Some ids still won't resolve into the SSR graph (for example, if Vite
@@ -646,6 +640,28 @@ async function collectRouteCss(
646640
return Array.from(out);
647641
}
648642

643+
function getSsrModule(
644+
env: ViteDevServer["environments"]["ssr"],
645+
id: string,
646+
): EnvironmentModuleNode | undefined {
647+
// Real files are keyed by file path in Vite's module graph, while Fresh's
648+
// virtual route modules are addressed by their module ids.
649+
if (path.isAbsolute(id)) {
650+
const mods = env.moduleGraph.getModulesByFile(path.normalize(id));
651+
if (mods === undefined) return undefined;
652+
653+
for (const mod of mods) {
654+
if (mod.transformResult !== null) return mod;
655+
}
656+
657+
return mods.values().next().value;
658+
}
659+
660+
const mod = env.moduleGraph.getModuleById(id) ??
661+
env.moduleGraph.getModuleById(`\0${id}`);
662+
return mod;
663+
}
664+
649665
function walkUp(
650666
mod: EnvironmentModuleNode,
651667
fn: (mod: EnvironmentModuleNode) => boolean,

0 commit comments

Comments
 (0)