Skip to content

Commit

Permalink
Fix style crawling logic for CSS HMR (#7565)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and matthewp committed Jul 11, 2023
1 parent a273b53 commit b6613ae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-turtles-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix style crawling logic for CSS HMR
1 change: 1 addition & 0 deletions packages/astro/src/core/module-loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface ModuleNode {
} | null;
ssrError: Error | null;
importedModules: Set<ModuleNode>;
importers: Set<ModuleNode>
}

export interface ModuleInfo {
Expand Down
18 changes: 11 additions & 7 deletions packages/astro/src/core/render/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export async function* crawlGraph(
continue;
}
if (id === entry.id) {
const urlDeps = getDepsFromEntry(entry);
scanned.add(id);
const entryIsStyle = isCSSRequest(id);

Expand Down Expand Up @@ -84,7 +83,9 @@ export async function* crawlGraph(
}

// 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);
}
}
Expand All @@ -103,10 +104,13 @@ export async function* crawlGraph(
}
}

function getDepsFromEntry(entry: ModuleNode) {
let deps = entry.ssrTransformResult?.deps ?? [];
if (entry.ssrTransformResult?.dynamicDeps) {
deps = deps.concat(entry.ssrTransformResult.dynamicDeps);
// Verify true imports. If the child module has the parent as an importers, it's
// a real import.
function isImportedBy(parent: string, entry: ModuleNode) {
for (const importer of entry.importers) {
if (importer.id === parent) {
return true;
}
}
return deps.map((dep) => unwrapId(dep));
return false;
}
5 changes: 5 additions & 0 deletions packages/astro/test/units/dev/styles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ describe('Crawling graph for CSS', () => {
{
id: aboutId,
url: aboutId,
importers: new Set(),
},
{
id: indexId + '?astro&style.css',
url: indexId + '?astro&style.css',
importers: new Set([{ id: indexId }]),
ssrModule: {},
},
],
importers: new Set(),
ssrTransformResult: {
deps: [indexId + '?astro&style.css'],
},
Expand All @@ -46,9 +49,11 @@ describe('Crawling graph for CSS', () => {
{
id: aboutId + '?astro&style.css',
url: aboutId + '?astro&style.css',
importers: new Set([{ id: aboutId }]),
ssrModule: {},
},
],
importers: new Set(),
ssrTransformResult: {
deps: [aboutId + '?astro&style.css'],
},
Expand Down

0 comments on commit b6613ae

Please sign in to comment.