UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

62 lines (61 loc) 2.41 kB
import path from "node:path"; import { isPathInsideDirectory } from "../../utils/routeFileWatch.mjs"; function createRouteModuleHmrPlugin(routerRoot) { return { name: "route-module-hmr-fix", // the router swallows a failed route import and renders an empty component // in its place (see useViteRoutes resolve). that is right for production and // wrong for dev, where it meant a broken import anywhere in a route's module // graph produced a blank app, no terminal output, and one console line — with // every provider inside that route silently never mounting. the client paints // its own overlay (devtools/dev.mjs) and reports here so the failure also // reaches the terminal, where the person running the dev server is looking. configureServer(server) { server.hot.on("one:route-error", data => { const id = data?.id || "unknown route"; const message = data?.message || "unknown error"; server.config.logger.error(`[one] route failed to load: ${id} ${message} the router rendered an empty component in its place, so nothing inside this route mounted.`, { timestamp: true }); }); }, hotUpdate({ server, modules, file }) { const absoluteRouterRoot = path.resolve(server.config.root, routerRoot); const fileRelativePath = path.relative(server.config.root, file); if (this.environment?.name === "ssr" && isPathInsideDirectory(file, absoluteRouterRoot)) { return []; } let hasRouteUpdate = false; const result = modules.map(module => { if (!module.id) return module; const moduleFile = module.id.split("?")[0]; if (isPathInsideDirectory(moduleFile, absoluteRouterRoot)) { const relativeRoutePath = path.relative(absoluteRouterRoot, moduleFile); module.acceptedHmrExports = /* @__PURE__ */new Set(); if (/^(?:\([^)]+\)[\\/])?_layout\.[jt]sx?$/.test(relativeRoutePath)) { hasRouteUpdate = true; } } return module; }); if (hasRouteUpdate) { server.hot.send({ type: "custom", event: "one:route-update", data: { file: fileRelativePath } }); } return result; } }; } export { createRouteModuleHmrPlugin }; //# sourceMappingURL=routeModuleHmrPlugin.mjs.map