From d3ec222553fb82b2bd1ee9c77558c3ad5b5bc76a Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Mon, 24 Feb 2025 23:38:50 +0530 Subject: [PATCH] fix hmr --- src/node/plugins/dynamicRoutesPlugin.ts | 18 +++++++++++++----- src/node/utils/moduleGraph.ts | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/node/plugins/dynamicRoutesPlugin.ts b/src/node/plugins/dynamicRoutesPlugin.ts index 44340524..5aabf2e6 100644 --- a/src/node/plugins/dynamicRoutesPlugin.ts +++ b/src/node/plugins/dynamicRoutesPlugin.ts @@ -33,6 +33,10 @@ export type ResolvedRouteConfig = UserRouteConfig & { * absolute fs path */ fullPath: string + /** + * the path to the paths loader module + */ + loaderPath: string } export interface RouteModule { @@ -128,7 +132,9 @@ export const dynamicRoutesPlugin = async ( if (matched) { const { route, params, content } = matched const routeFile = normalizePath(path.resolve(config.srcDir, route)) + moduleGraph.add(id, [routeFile]) + moduleGraph.add(routeFile, [matched.loaderPath]) let baseContent = fs.readFileSync(routeFile, 'utf-8') @@ -222,7 +228,9 @@ async function resolveDynamicRoutes( let watch: ResolvedRouteModule['watch'] let loader: ResolvedRouteModule['loader'] - const existing = routeModuleCache.get(normalizePath(pathsFile)) + const loaderPath = normalizePath(pathsFile) + const existing = routeModuleCache.get(loaderPath) + if (existing) { // use cached routes if not invalidated by hmr if (existing.routes) { @@ -284,10 +292,9 @@ async function resolveDynamicRoutes( // record deps for hmr newModuleGraph.add( - normalizePath(pathsFile), - mod.dependencies.map((f) => normalizePath(path.resolve(f))) + loaderPath, + mod.dependencies.map((p) => normalizePath(path.resolve(p))) ) - newModuleGraph.add(fullPath, [normalizePath(pathsFile)]) } const resolveRoute = async (): Promise => { @@ -317,11 +324,12 @@ async function resolveDynamicRoutes( path: resolvedPath, fullPath: normalizePath(path.resolve(srcDir, resolvedPath)), route, + loaderPath, ...userConfig } }) - routeModuleCache.set(normalizePath(pathsFile), { watch, routes, loader }) + routeModuleCache.set(loaderPath, { watch, routes, loader }) return routes } diff --git a/src/node/utils/moduleGraph.ts b/src/node/utils/moduleGraph.ts index ff521014..61e5eecf 100644 --- a/src/node/utils/moduleGraph.ts +++ b/src/node/utils/moduleGraph.ts @@ -27,7 +27,7 @@ export class ModuleGraph { // Merge the new dependencies with any that already exist. for (const dep of dependencies) { - if (!moduleNode.dependencies.has(dep)) { + if (!moduleNode.dependencies.has(dep) && dep !== module) { moduleNode.dependencies.add(dep) // Ensure the dependency exists in the graph. if (!this.nodes.has(dep)) {