summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2023-06-22 14:36:35 -0400
committerGravatar GitHub <noreply@github.com> 2023-06-22 14:36:35 -0400
commitf359d77b1844335ceeb103b9d3753eb2f440ed5f (patch)
tree9eb27541db9a54cc9badaf02dce6e772aa8ce6ce
parent32bde967f4b21648b1e11dbfa7964bf7f348f7b9 (diff)
downloadastro-f359d77b1844335ceeb103b9d3753eb2f440ed5f.tar.gz
astro-f359d77b1844335ceeb103b9d3753eb2f440ed5f.tar.zst
astro-f359d77b1844335ceeb103b9d3753eb2f440ed5f.zip
Prevent accidental CSS inclusion in dev (#7381)
* Prevent accidental CSS inclusion in dev * Use ssrTransformResult.deps instead * Fix types * Get dynamic deps too * Handle virtual module deps * Fix test on windows
-rw-r--r--.changeset/tiny-lemons-sit.md5
-rw-r--r--packages/astro/src/core/module-loader/loader.ts4
-rw-r--r--packages/astro/src/core/render/dev/vite.ts19
-rw-r--r--packages/astro/test/units/dev/styles.test.js63
4 files changed, 90 insertions, 1 deletions
diff --git a/.changeset/tiny-lemons-sit.md b/.changeset/tiny-lemons-sit.md
new file mode 100644
index 000000000..8e43429f7
--- /dev/null
+++ b/.changeset/tiny-lemons-sit.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Prevent accidental inclusion of page CSS in dev mode
diff --git a/packages/astro/src/core/module-loader/loader.ts b/packages/astro/src/core/module-loader/loader.ts
index 84bb862d0..39c24253e 100644
--- a/packages/astro/src/core/module-loader/loader.ts
+++ b/packages/astro/src/core/module-loader/loader.ts
@@ -42,6 +42,10 @@ export interface ModuleNode {
id: string | null;
url: string;
ssrModule: Record<string, any> | null;
+ ssrTransformResult: {
+ deps?: string[];
+ dynamicDeps?: string[];
+ } | null;
ssrError: Error | null;
importedModules: Set<ModuleNode>;
}
diff --git a/packages/astro/src/core/render/dev/vite.ts b/packages/astro/src/core/render/dev/vite.ts
index fe4d3f791..5cfea6e49 100644
--- a/packages/astro/src/core/render/dev/vite.ts
+++ b/packages/astro/src/core/render/dev/vite.ts
@@ -41,6 +41,7 @@ export async function* crawlGraph(
continue;
}
if (id === entry.id) {
+ const urlDeps = getDepsFromEntry(entry);
scanned.add(id);
const entryIsStyle = isCSSRequest(id);
@@ -82,7 +83,7 @@ export async function* crawlGraph(
}
}
}
- if (!isPropagationStoppingPoint) {
+ if (urlDeps.includes(urlId(importedModule.url)) && !isPropagationStoppingPoint) {
importedModules.add(importedModule);
}
}
@@ -100,3 +101,19 @@ export async function* crawlGraph(
yield* crawlGraph(loader, importedModule.id, false, scanned);
}
}
+
+// Virtual modules URL should start with /@id/ but do not
+function urlId(url: string) {
+ if (url.startsWith('astro:scripts')) {
+ return '/@id/' + url;
+ }
+ return url;
+}
+
+function getDepsFromEntry(entry: ModuleNode) {
+ let deps = entry.ssrTransformResult?.deps ?? [];
+ if(entry.ssrTransformResult?.dynamicDeps) {
+ return deps.concat(entry.ssrTransformResult.dynamicDeps);
+ }
+ return deps;
+}
diff --git a/packages/astro/test/units/dev/styles.test.js b/packages/astro/test/units/dev/styles.test.js
new file mode 100644
index 000000000..44dbc3be5
--- /dev/null
+++ b/packages/astro/test/units/dev/styles.test.js
@@ -0,0 +1,63 @@
+import { expect } from 'chai';
+import { fileURLToPath } from 'url';
+
+import {
+ getStylesForURL
+} from '../../../dist/core/render/dev/css.js';
+import { viteID } from '../../../dist/core/util.js';
+
+const root = new URL('../../fixtures/alias/', import.meta.url);
+
+class TestLoader {
+ constructor(modules) {
+ this.modules = new Map(modules.map(m => [m.id, m]))
+ }
+ getModuleById(id) {
+ return this.modules.get(id);
+ }
+ getModulesByFile(id) {
+ return this.modules.has(id) ? [this.modules.get(id)] : [];
+ }
+}
+
+describe('Crawling graph for CSS', () => {
+ let loader;
+ before(() => {
+ const indexId = viteID(new URL('./src/pages/index.astro', root));
+ const aboutId = viteID(new URL('./src/pages/about.astro', root));
+ loader = new TestLoader([
+ {
+ id: indexId,
+ importedModules: [{
+ id: aboutId,
+ url: aboutId,
+ }, {
+ id: indexId + '?astro&style.css',
+ url: indexId + '?astro&style.css',
+ ssrModule: {}
+ }],
+ ssrTransformResult: {
+ deps: [indexId + '?astro&style.css']
+ }
+ },
+ {
+ id: aboutId,
+ importedModules: [{
+ id: aboutId + '?astro&style.css',
+ url: aboutId + '?astro&style.css',
+ ssrModule: {}
+ }],
+ ssrTransformResult: {
+ deps: [aboutId + '?astro&style.css']
+ }
+ }
+ ]);
+ })
+
+ it('importedModules is checked against the child\'s importers', async () => {
+ // In dev mode, HMR modules tracked are added to importedModules. We use `importers`
+ // to verify that they are true importers.
+ const res = await getStylesForURL(new URL('./src/pages/index.astro', root), loader, 'development')
+ expect(res.urls.size).to.equal(1);
+ })
+})