0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

Skip crawling into CSS requests when crawling module graph (#10911)

This commit is contained in:
Bjorn Lu 2024-04-30 17:06:01 +08:00 committed by GitHub
parent 05d58eff07
commit a86dc9d269
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Skips adding CSS dependencies of CSS Vite modules as style tags in the HTML

View file

@ -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;
@ -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.