mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
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
This commit is contained in:
parent
32bde967f4
commit
f359d77b18
4 changed files with 90 additions and 1 deletions
5
.changeset/tiny-lemons-sit.md
Normal file
5
.changeset/tiny-lemons-sit.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Prevent accidental inclusion of page CSS in dev mode
|
|
@ -42,6 +42,10 @@ export interface ModuleNode {
|
||||||
id: string | null;
|
id: string | null;
|
||||||
url: string;
|
url: string;
|
||||||
ssrModule: Record<string, any> | null;
|
ssrModule: Record<string, any> | null;
|
||||||
|
ssrTransformResult: {
|
||||||
|
deps?: string[];
|
||||||
|
dynamicDeps?: string[];
|
||||||
|
} | null;
|
||||||
ssrError: Error | null;
|
ssrError: Error | null;
|
||||||
importedModules: Set<ModuleNode>;
|
importedModules: Set<ModuleNode>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ export async function* crawlGraph(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (id === entry.id) {
|
if (id === entry.id) {
|
||||||
|
const urlDeps = getDepsFromEntry(entry);
|
||||||
scanned.add(id);
|
scanned.add(id);
|
||||||
const entryIsStyle = isCSSRequest(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);
|
importedModules.add(importedModule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,3 +101,19 @@ export async function* crawlGraph(
|
||||||
yield* crawlGraph(loader, importedModule.id, false, scanned);
|
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;
|
||||||
|
}
|
||||||
|
|
63
packages/astro/test/units/dev/styles.test.js
Normal file
63
packages/astro/test/units/dev/styles.test.js
Normal file
|
@ -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);
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue