Skip to content

Commit 28e9822

Browse files
committed
fix: resolve modules with vite fetchModule during dev style collection
1 parent d7de8ed commit 28e9822

3 files changed

Lines changed: 18 additions & 30 deletions

File tree

packages/start/src/config/manifest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { type PluginOption, type ViteDevServer } from "vite";
33
import { findStylesInModuleGraph } from "../server/collect-styles.ts";
44
import { VIRTUAL_MODULES } from "./constants.ts";
55
import { type SolidStartOptions } from "./index.ts";
6+
import { wrapId } from "./vite-utils.ts";
67

78
export function manifest(start: SolidStartOptions): PluginOption {
89
let devServer: ViteDevServer = undefined!;
@@ -72,7 +73,7 @@ export function manifest(start: SolidStartOptions): PluginOption {
7273
"data-vite-dev-id": "${key}",
7374
"data-vite-ref": "0",
7475
},
75-
children: () => import("${value}?inline").then(mod => mod.default),
76+
children: () => import("${wrapId(value)}?inline").then(mod => mod.default),
7677
}`,
7778
);
7879

packages/start/src/config/vite-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const FS_PREFIX = `/@fs/`;
1212
export const VALID_ID_PREFIX = `/@id/`;
1313

1414
export const NULL_BYTE_PLACEHOLDER = `__x00__`;
15+
const NULL_BYTE_REGEX = /^\0/;
1516

1617
export function normalizeResolvedIdToUrl(
1718
environment: DevEnvironment,
@@ -52,10 +53,13 @@ export function normalizeResolvedIdToUrl(
5253
return url;
5354
}
5455

56+
/**
57+
* Inspired by:
58+
* https://github.com/withastro/astro/blob/fddde5fad81007795eb263c7fd0cea096b8e2cba/packages/astro/src/core/util.ts#L115
59+
* https://github.com/vitejs/vite/blob/130e7181a55c524383c63bbfb1749d0ff7185cad/packages/vite/src/shared/utils.ts#L11
60+
*/
5561
export function wrapId(id: string): string {
56-
return id.startsWith(VALID_ID_PREFIX)
57-
? id
58-
: VALID_ID_PREFIX + id.replace("\0", NULL_BYTE_PLACEHOLDER);
62+
return id.replace(NULL_BYTE_REGEX, `${VALID_ID_PREFIX}${NULL_BYTE_PLACEHOLDER}`);
5963
}
6064

6165
export function unwrapId(id: string): string {

packages/start/src/server/collect-styles.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
11
import path from "node:path";
2-
import { resolve } from "pathe";
32
import type { DevEnvironment, EnvironmentModuleNode } from "vite";
43

5-
async function getViteModuleNode(vite: DevEnvironment, file: string) {
6-
let nodePath = file;
7-
let node = vite.moduleGraph.getModuleById(file);
8-
9-
if (!node) {
10-
const resolvedId = await vite.pluginContainer.resolveId(file, undefined);
11-
if (!resolvedId) return;
12-
13-
nodePath = resolvedId.id;
14-
node = vite.moduleGraph.getModuleById(file);
15-
}
16-
17-
if (!node) {
18-
nodePath = resolve(nodePath);
19-
node = await vite.moduleGraph.getModuleByUrl(file);
20-
}
21-
22-
if (!node) {
23-
await vite.moduleGraph.ensureEntryFromUrl(nodePath, false);
24-
node = vite.moduleGraph.getModuleById(nodePath);
25-
}
26-
27-
return node;
4+
async function getViteModuleNode(vite: DevEnvironment, file: string, importer?: string) {
5+
try {
6+
const res = await vite.fetchModule(file, importer);
7+
if (!("id" in res)) return;
8+
return vite.moduleGraph.getModuleById(res.id);
9+
} catch (err) {}
2810
}
2911

3012
async function findModuleDependencies(
3113
vite: DevEnvironment,
3214
file: string,
3315
deps: Set<EnvironmentModuleNode>,
3416
crawledFiles = new Set<string>(),
17+
importer?: string,
3518
) {
3619
crawledFiles.add(file);
37-
const module = await getViteModuleNode(vite, file);
20+
const module = await getViteModuleNode(vite, file, importer);
3821
if (!module?.id || deps.has(module)) return;
3922

4023
deps.add(module);
@@ -53,7 +36,7 @@ async function findModuleDependencies(
5336
if (crawledFiles.has(dep)) {
5437
continue;
5538
}
56-
await findModuleDependencies(vite, dep, deps, crawledFiles);
39+
await findModuleDependencies(vite, dep, deps, crawledFiles, module.id);
5740
}
5841
}
5942

0 commit comments

Comments
 (0)