From 41e437fd6ff566a158efeabcaf781a9a24803381 Mon Sep 17 00:00:00 2001 From: Kenneth Skovhus Date: Tue, 21 Nov 2023 04:34:51 +0100 Subject: [PATCH] perf(hmr): skip traversed modules when checking circular imports (#15034) --- packages/vite/src/node/server/hmr.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 3aa5996bff9751..45ba8d5344081f 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -361,11 +361,13 @@ function propagateUpdate( * @param nodeChain The chain of nodes/imports that lead to the node. * (The last node in the chain imports the `node` parameter) * @param currentChain The current chain tracked from the `node` parameter + * @param traversedModules The set of modules that have traversed */ function isNodeWithinCircularImports( node: ModuleNode, nodeChain: ModuleNode[], currentChain: ModuleNode[] = [node], + traversedModules = new Set(), ): HasDeadEnd { // To help visualize how each parameters work, imagine this import graph: // @@ -383,6 +385,11 @@ function isNodeWithinCircularImports( // It works by checking if any `node` importers are within `nodeChain`, which // means there's an import loop with a HMR-accepted module in it. + if (traversedModules.has(node)) { + return false + } + traversedModules.add(node) + for (const importer of node.importers) { // Node may import itself which is safe if (importer === node) continue @@ -416,6 +423,7 @@ function isNodeWithinCircularImports( importer, nodeChain, currentChain.concat(importer), + traversedModules, ) if (result) return result }