-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dx): reload page if store circular import detected
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
9 changed files
with
51 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,59 @@ | ||
import { red, yellow } from "kolorist" | ||
import type { HmrContext, Plugin } from "vite" | ||
|
||
function hasCircularDependency(mod: any, server: any, visited = new Set()): boolean { | ||
if (visited.has(mod.id)) return true | ||
visited.add(mod.id) | ||
function isNodeWithinCircularImports( | ||
node: any, | ||
nodeChain: any[], | ||
currentChain: any[] = [node], | ||
traversedModules = new Set<any>(), | ||
): boolean { | ||
if (traversedModules.has(node)) { | ||
return false | ||
} | ||
traversedModules.add(node) | ||
|
||
for (const importer of node.importers) { | ||
if (importer === node) continue | ||
|
||
const importers = server.moduleGraph.getModulesByFile(mod.file) || new Set() | ||
for (const importer of importers) { | ||
if (hasCircularDependency(importer, server, new Set(visited))) { | ||
const importerIndex = nodeChain.indexOf(importer) | ||
if (importerIndex !== -1) { | ||
const importChain = [ | ||
importer, | ||
...[...currentChain].reverse(), | ||
...nodeChain.slice(importerIndex, -1).reverse(), | ||
] | ||
console.warn( | ||
yellow(`Circular imports detected: ${importChain.map((m) => m.file).join(" -> ")}`), | ||
) | ||
return true | ||
} | ||
} | ||
|
||
if (!currentChain.includes(importer)) { | ||
const result = isNodeWithinCircularImports( | ||
importer, | ||
nodeChain, | ||
currentChain.concat(importer), | ||
traversedModules, | ||
) | ||
if (result) return result | ||
} | ||
} | ||
return false | ||
} | ||
|
||
export const circularImportRefreshPlugin = (): Plugin => ({ | ||
name: "circular-import-refresh", | ||
handleHotUpdate({ file, server }: HmrContext) { | ||
if (file.includes("store")) { | ||
const mod = server.moduleGraph.getModuleById(file) | ||
if (mod && hasCircularDependency(mod, server)) { | ||
console.warn(`Circular dependency detected in ${file}. Performing full page refresh.`) | ||
server.ws.send({ type: "full-reload" }) | ||
return [] | ||
} | ||
const mod = server.moduleGraph.getModuleById(file) | ||
if (mod && isNodeWithinCircularImports(mod, [mod])) { | ||
console.error( | ||
red( | ||
`Circular dependency detected in ${file} involving store files. Performing full page refresh.`, | ||
), | ||
) | ||
|
||
server.ws.send({ type: "full-reload" }) | ||
return [] | ||
} | ||
}, | ||
}) |