Skip to content

Commit 5d186f4

Browse files
committed
add fswatcher
1 parent 4afe460 commit 5d186f4

2 files changed

Lines changed: 76 additions & 20 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { FSWatcher, ModuleGraph, ModuleNode, PluginOption, ViteDevServer } from "vite";
2+
import { moduleId } from "./index.js";
3+
4+
interface CompiledRouter {
5+
removeRoute(path: string): void;
6+
addRoute(path: string): void;
7+
updateRoute(path: string): void;
8+
addEventListener(event: "reload", handler: () => void): void;
9+
removeEventListener(event: "reload", handler: () => void): void;
10+
}
11+
12+
function setupWatcher(watcher: FSWatcher, routes: CompiledRouter): void {
13+
watcher.on("unlink", path => routes.removeRoute(path));
14+
watcher.on("add", path => routes.addRoute(path));
15+
watcher.on("change", path => routes.updateRoute(path));
16+
}
17+
18+
function createRoutesReloader(server: ViteDevServer, routes: CompiledRouter): () => void {
19+
routes.addEventListener("reload", handleRoutesReload);
20+
return () => routes.removeEventListener("reload", handleRoutesReload);
21+
22+
function handleRoutesReload(): void {
23+
const { moduleGraph }: { moduleGraph: ModuleGraph } = server;
24+
const mod: ModuleNode | undefined = moduleGraph.getModuleById(moduleId);
25+
if (mod) {
26+
const seen = new Set<ModuleNode>();
27+
moduleGraph.invalidateModule(mod, seen);
28+
server.reloadModule(mod);
29+
}
30+
if (!server.config.server.hmr) {
31+
server.ws.send({ type: "full-reload" });
32+
}
33+
}
34+
}
35+
36+
export const fileSystemWatcher = (): PluginOption => {
37+
let close: (() => void) | undefined;
38+
39+
const plugin: PluginOption = {
40+
name: "fs-watcher",
41+
apply: "serve",
42+
async configureServer(server: ViteDevServer) {
43+
const router = (globalThis as any).ROUTERS["server"](server.config);
44+
(globalThis as any).ROUTERS["server"] = router;
45+
46+
const routerClient = (globalThis as any).ROUTERS["client"](server.config);
47+
(globalThis as any).ROUTERS["client"] = routerClient;
48+
49+
if (router) {
50+
setupWatcher(server.watcher, router);
51+
close = createRoutesReloader(server, router);
52+
}
53+
},
54+
closeBundle() {
55+
close?.();
56+
}
57+
};
58+
return plugin;
59+
};

packages/start/src/config/fs-routes/index.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { relative } from "node:path";
22
import type { PluginOption, ResolvedConfig } from "vite";
3+
import { fileSystemWatcher } from "./fs-watcher.js";
34
import { manifest } from "./manifest.js";
45
import type { BaseFileSystemRouter } from "./router.js";
56
import { treeShake } from "./tree-shake.js";
@@ -13,11 +14,8 @@ export interface FsRoutesArgs {
1314
handlers: Record<"client" | "server", string>;
1415
}
1516

16-
export function fsRoutes({
17-
routers,
18-
handlers
19-
}: FsRoutesArgs): Array<PluginOption> {
20-
(globalThis as any).ROUTERS = {};
17+
export function fsRoutes({ routers, handlers }: FsRoutesArgs): Array<PluginOption> {
18+
(globalThis as any).ROUTERS = routers;
2119

2220
return [
2321
manifest(handlers),
@@ -34,11 +32,7 @@ export function fsRoutes({
3432
if (id !== moduleId) return;
3533
const js = jsCode();
3634

37-
const router = routers[this.environment.name as keyof typeof routers](
38-
this.environment.config
39-
);
40-
41-
(globalThis as any).ROUTERS[this.environment.name] = router;
35+
const router = (globalThis as any).ROUTERS[this.environment.name];
4236

4337
const routes = await router.getRoutes();
4438

@@ -79,18 +73,20 @@ export function fsRoutes({
7973

8074
const code = `
8175
${js.getImportStatements()}
82-
${this.environment.name === "server"
83-
? ""
84-
: `
76+
${
77+
this.environment.name === "server"
78+
? ""
79+
: `
8580
function clientManifestImport(id) {
8681
return import(/* @vite-ignore */ globalThis.MANIFEST.inputs[id].output.path)
8782
}`
88-
}
83+
}
8984
export default ${routesCode}`;
9085
return code;
9186
}
9287
},
93-
treeShake()
88+
treeShake(),
89+
fileSystemWatcher()
9490
];
9591
}
9692

@@ -129,18 +125,19 @@ function jsCode() {
129125

130126
return Object.keys(id).length > 0
131127
? `{ ${Object.keys(id)
132-
.map(k => `${k} as ${id[k]}`)
133-
.join(", ")} }`
128+
.map(k => `${k} as ${id[k]}`)
129+
.join(", ")} }`
134130
: "";
135131
};
136132

137133
const getImportStatements = () => {
138134
return `${[...imports.keys()]
139135
.map(
140136
i =>
141-
`import ${imports.get(i).default
142-
? `${imports.get(i).default}${Object.keys(imports.get(i)).length > 1 ? ", " : ""}`
143-
: ""
137+
`import ${
138+
imports.get(i).default
139+
? `${imports.get(i).default}${Object.keys(imports.get(i)).length > 1 ? ", " : ""}`
140+
: ""
144141
} ${getNamedExport(i)} from '${i}';`
145142
)
146143
.join("\n")}`;

0 commit comments

Comments
 (0)