summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2022-08-02 10:26:38 -0500
committerGravatar GitHub <noreply@github.com> 2022-08-02 10:26:38 -0500
commit26cc0bbf78320e1797de9b4562ace92c5c03b666 (patch)
tree5516c2143941387ef1390a4a84cd8d632bcd2668
parent004995eecd773bbf0a5ddb990ca51c9a9c45c4c8 (diff)
downloadastro-26cc0bbf78320e1797de9b4562ace92c5c03b666.tar.gz
astro-26cc0bbf78320e1797de9b4562ace92c5c03b666.tar.zst
astro-26cc0bbf78320e1797de9b4562ace92c5c03b666.zip
Do not crawl non-style deps imported by styles (#4115)
* fix: do not crawl non-style deps imported by styles * Update vite.ts Co-authored-by: Nate Moore <nate@astro.build>
-rw-r--r--.changeset/weak-crabs-pump.md5
-rw-r--r--packages/astro/src/core/render/dev/vite.ts9
2 files changed, 14 insertions, 0 deletions
diff --git a/.changeset/weak-crabs-pump.md b/.changeset/weak-crabs-pump.md
new file mode 100644
index 000000000..44ac3467e
--- /dev/null
+++ b/.changeset/weak-crabs-pump.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix edge case with hoisted scripts and Tailwind during dev
diff --git a/packages/astro/src/core/render/dev/vite.ts b/packages/astro/src/core/render/dev/vite.ts
index babbe0079..58f40a7dc 100644
--- a/packages/astro/src/core/render/dev/vite.ts
+++ b/packages/astro/src/core/render/dev/vite.ts
@@ -1,5 +1,6 @@
import npath from 'path';
import vite from 'vite';
+import { STYLE_EXTENSIONS } from '../util.js';
import { unwrapId } from '../../util.js';
/**
@@ -36,6 +37,7 @@ export async function* crawlGraph(
}
if (id === entry.id) {
scanned.add(id);
+ const entryIsStyle = STYLE_EXTENSIONS.has(npath.extname(id))
for (const importedModule of entry.importedModules) {
// some dynamically imported modules are *not* server rendered in time
// to only SSR modules that we can safely transform, we check against
@@ -43,6 +45,13 @@ export async function* crawlGraph(
if (importedModule.id) {
// use URL to strip special query params like "?content"
const { pathname } = new URL(`file://${importedModule.id}`);
+ // 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 && !STYLE_EXTENSIONS.has(npath.extname(pathname))) {
+ continue;
+ }
if (fileExtensionsToSSR.has(npath.extname(pathname))) {
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
if (!mod?.ssrModule) {