Skip to content

Commit

Permalink
Skip crawling into CSS requests when crawling module graph (#10911)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 30, 2024
1 parent 05d58ef commit a86dc9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-mirrors-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Skips adding CSS dependencies of CSS Vite modules as style tags in the HTML
17 changes: 9 additions & 8 deletions packages/astro/src/vite-plugin-astro-server/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export async function* crawlGraph(
}
if (id === entry.id) {
scanned.add(id);
const entryIsStyle = isCSSRequest(id);

// CSS requests `importedModules` are usually from `@import`, but we don't really need
// to crawl into those as the `@import` code are already inlined into this `id`.
// If CSS requests `importedModules` contain non-CSS files, e.g. Tailwind might add HMR
// dependencies as `importedModules`, we should also skip them as they aren't really
// imported. Without this, every hoisted script in the project is added to every page!
if (isCSSRequest(id)) {
continue
}

for (const importedModule of entry.importedModules) {
if (!importedModule.id) continue;
Expand All @@ -54,13 +62,6 @@ export async function* crawlGraph(
// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
// For example, `virtual:image-loader` if you don't have the plugin installed.
const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');
// If the entry is a style, skip any modules that are not also styles.
// Tools like Tailwind might add HMR dependencies as `importedModules`
// but we should skip them--they aren't really imported. Without this,
// every hoisted script in the project is added to every page!
if (entryIsStyle && !isCSSRequest(importedModulePathname)) {
continue;
}

const isFileTypeNeedingSSR = fileExtensionsToSSR.has(npath.extname(importedModulePathname));
// A propagation stopping point is a module with the ?astroPropagatedAssets flag.
Expand Down

0 comments on commit a86dc9d

Please sign in to comment.