Skip to content

Commit

Permalink
vinxi/routes virtual module and hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
mdynnl committed Jul 11, 2024
1 parent c1d444d commit 0f736c3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 63 deletions.
78 changes: 37 additions & 41 deletions packages/vinxi/lib/plugins/fs-watcher.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
import { fileURLToPath } from "node:url";

import { normalize } from "../path.js";
import { moduleId } from "./routes.js";

/**
*
* @param {import('vite').FSWatcher} watcher
* @param {import("../router-modes.js").RouterSchema} router
* @param {import("../router-modes.js").CompiledRouter} routes
*/
function setupWatcher(watcher, router) {
if (router.internals?.routes) {
watcher.on("unlink", async (path) => {
if (router.internals?.routes) {
await router.internals?.routes.removeRoute(path);
}
});

watcher.on("add", async (path) => {
if (router.internals?.routes) {
await router.internals?.routes.addRoute(path);
}
});
function setupWatcher(watcher, routes) {
watcher.on("unlink", (path) => routes.removeRoute(path));
watcher.on("add", (path) => routes.addRoute(path));
watcher.on("change", (path) => routes.updateRoute(path));
}

watcher.on("change", async (path) => {
if (router.internals?.routes) {
await router.internals?.routes.updateRoute(path);
}
});
/**
* @param {import('vite').ViteDevServer} server
* @param {import("../router-modes.js").CompiledRouter} routes
*/
function createRoutesReloader(server, routes) {
routes.addEventListener("reload", handleRoutesReload);
return () => routes.removeEventListener("reload", handleRoutesReload);
function handleRoutesReload() {
const { moduleGraph } = server;
const mod = moduleGraph.getModuleById(moduleId);
if (mod) {
const seen = new Set();
moduleGraph.invalidateModule(mod, seen);
server.reloadModule(mod);
}
if (!server.config.server.hmr) {
server.ws.send({ type: "full-reload" });
}
}
}

export const fileSystemWatcher = () => {
/** @type {import('../vite-dev.js').ViteConfig} */
/** @type {import('../vite-dev.js').ViteConfig & { router: any }} */
let config;

/** @type {undefined|(() => void)} */
let close;

/** @type {import('../vite-dev.js').Plugin} */
const plugin = {
name: "fs-watcher",
Expand All @@ -40,26 +47,15 @@ export const fileSystemWatcher = () => {
config = resolvedConfig;
},
configureServer(server) {
if (config.router.internals?.routes) {
setupWatcher(server.watcher, config.router);
config.router.internals.routes.addEventListener("reload", () => {
const { moduleGraph } = server;
const mods = moduleGraph.getModulesByFile(
normalize(fileURLToPath(new URL("../routes.js", import.meta.url))),
);
if (mods) {
const seen = new Set();
mods.forEach((mod) => {
moduleGraph.invalidateModule(mod, seen);
});
}
// debug.hmr("Reload generated pages.");
server.ws.send({
type: "full-reload",
});
});
const routes = config.router?.internals?.routes;
if (routes) {
setupWatcher(server.watcher, routes);
close = createRoutesReloader(server, routes);
}
},
closeBundle() {
close?.();
},
};
return plugin;
};
26 changes: 12 additions & 14 deletions packages/vinxi/lib/plugins/routes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from "node:url";

import { relative } from "../path.js";

export const moduleId = "vinxi/routes";

/**
*
* @returns {import("../vite-dev.d.ts").Plugin}
Expand Down Expand Up @@ -35,18 +35,16 @@ export function routes() {
root = config.root;
router = config.router;
},
/**
* @param {{ split: (arg0: string) => [any, any]; }} url
*/
async load(url) {
const [id, query] = url.split("?");
if (
id ===
fileURLToPath(new URL("../routes.js", import.meta.url)).replaceAll(
"\\",
"/",
)
) {
resolveId: {
order: "pre",
handler(id) {
if (id === moduleId) {
return id;
}
},
},
async load(id) {
if (id === moduleId) {
const js = jsCode();
const routes = await router.internals.routes?.getRoutes();

Expand Down
2 changes: 1 addition & 1 deletion packages/vinxi/lib/router-modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { resolve } from "./resolve.js";
export { z };
/**
* @typedef {{ routes?: CompiledRouter; devServer?: import('vite').ViteDevServer; appWorker?: import('./app-worker-client.js').AppWorkerClient; type: import("./router-mode.js").RouterMode }} Internals
* @typedef {{ getRoutes(): Promise<any[]>; } & EventTarget} CompiledRouter
* @typedef {import('./fs-router.js').BaseFileSystemRouter} CompiledRouter
* @typedef {(router: RouterSchemaInput, app: import("./app.js").AppOptions) => CompiledRouter} RouterStyleFn
* */
export const staticRouterSchema = z.object({
Expand Down
6 changes: 0 additions & 6 deletions packages/vinxi/lib/routes.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/vinxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"types": "./dist/types/lib/fs-router.d.ts"
},
"./routes": {
"import": "./lib/routes.js",
"types": "./types/routes.d.ts"
},
"./client": {
Expand Down

0 comments on commit 0f736c3

Please sign in to comment.