0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

Fix style crawling logic for CSS HMR (#7565)

This commit is contained in:
Bjorn Lu 2023-07-05 14:13:45 +08:00 committed by GitHub
parent c459b81785
commit 5ffdec7580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix style crawling logic for CSS HMR

View file

@ -48,6 +48,7 @@ export interface ModuleNode {
} | null; } | null;
ssrError: Error | null; ssrError: Error | null;
importedModules: Set<ModuleNode>; importedModules: Set<ModuleNode>;
importers: Set<ModuleNode>
} }
export interface ModuleInfo { export interface ModuleInfo {

View file

@ -41,7 +41,6 @@ export async function* crawlGraph(
continue; continue;
} }
if (id === entry.id) { if (id === entry.id) {
const urlDeps = getDepsFromEntry(entry);
scanned.add(id); scanned.add(id);
const entryIsStyle = isCSSRequest(id); const entryIsStyle = isCSSRequest(id);
@ -84,7 +83,9 @@ export async function* crawlGraph(
} }
// Make sure the `importedModule` traversed is explicitly imported by the user, and not by HMR // Make sure the `importedModule` traversed is explicitly imported by the user, and not by HMR
if (urlDeps.includes(importedModule.url) && !isPropagationStoppingPoint) { // TODO: This isn't very performant. Maybe look into using `ssrTransformResult` but make sure it
// doesn't regress UnoCSS. https://github.com/withastro/astro/issues/7529
if (isImportedBy(id, importedModule) && !isPropagationStoppingPoint) {
importedModules.add(importedModule); importedModules.add(importedModule);
} }
} }
@ -103,10 +104,13 @@ export async function* crawlGraph(
} }
} }
function getDepsFromEntry(entry: ModuleNode) { // Verify true imports. If the child module has the parent as an importers, it's
let deps = entry.ssrTransformResult?.deps ?? []; // a real import.
if (entry.ssrTransformResult?.dynamicDeps) { function isImportedBy(parent: string, entry: ModuleNode) {
deps = deps.concat(entry.ssrTransformResult.dynamicDeps); for (const importer of entry.importers) {
if (importer.id === parent) {
return true;
}
} }
return deps.map((dep) => unwrapId(dep)); return false;
} }

View file

@ -29,13 +29,16 @@ describe('Crawling graph for CSS', () => {
{ {
id: aboutId, id: aboutId,
url: aboutId, url: aboutId,
importers: new Set(),
}, },
{ {
id: indexId + '?astro&style.css', id: indexId + '?astro&style.css',
url: indexId + '?astro&style.css', url: indexId + '?astro&style.css',
importers: new Set([{ id: indexId }]),
ssrModule: {}, ssrModule: {},
}, },
], ],
importers: new Set(),
ssrTransformResult: { ssrTransformResult: {
deps: [indexId + '?astro&style.css'], deps: [indexId + '?astro&style.css'],
}, },
@ -46,9 +49,11 @@ describe('Crawling graph for CSS', () => {
{ {
id: aboutId + '?astro&style.css', id: aboutId + '?astro&style.css',
url: aboutId + '?astro&style.css', url: aboutId + '?astro&style.css',
importers: new Set([{ id: aboutId }]),
ssrModule: {}, ssrModule: {},
}, },
], ],
importers: new Set(),
ssrTransformResult: { ssrTransformResult: {
deps: [aboutId + '?astro&style.css'], deps: [aboutId + '?astro&style.css'],
}, },