0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix ?inline and ?raw css query suffixes inlined in style tags in development (#6426)

* test: add test cases

* test: add another expectation

* fix: don't crawl inline and raw css queries

* chore: changeset
This commit is contained in:
Happydev 2023-03-06 13:06:55 +00:00 committed by GitHub
parent 9d236c9417
commit e0844852d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 21 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Prevent `?inline` and `?raw` css query suffixes from injecting style tags in development

View file

@ -2,7 +2,7 @@ import type { ModuleLoader } from '../../module-loader/index';
import { RuntimeMode } from '../../../@types/astro.js';
import { viteID } from '../../util.js';
import { isCSSRequest } from './util.js';
import { isBuildableCSSRequest } from './util.js';
import { crawlGraph } from './vite.js';
/** Given a filePath URL, crawl Vites module graph to find all style imports. */
@ -15,7 +15,7 @@ export async function getStylesForURL(
const importedStylesMap = new Map<string, string>();
for await (const importedModule of crawlGraph(loader, viteID(filePath), true)) {
if (isCSSRequest(importedModule.url)) {
if (isBuildableCSSRequest(importedModule.url)) {
let ssrModule: Record<string, any>;
try {
// The SSR module is possibly not loaded. Load it if it's null.

View file

@ -4,25 +4,64 @@ import { loadFixture } from './test-utils.js';
describe('Importing raw/inlined CSS', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/css-inline/',
});
await fixture.build();
});
describe('Build', () => {
before(async () => {
await fixture.build();
});
it('?inline is imported as a string', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#inline').text()).to.contain('tomato');
expect($('link[rel=stylesheet]')).to.have.lengthOf(1);
expect($('style')).to.have.lengthOf(0);
});
it('?raw is imported as a string', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
expect($('#raw').text()).to.contain('plum');
expect($('link[rel=stylesheet]')).to.have.lengthOf(1);
expect($('style')).to.have.lengthOf(0);
});
});
it('?inline is imported as a string', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
describe('Dev', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
expect($('#inline').text()).to.contain('tomato');
});
before(async () => {
devServer = await fixture.startDevServer();
});
it('?raw is imported as a string', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
after(async () => {
await devServer.stop();
});
expect($('#raw').text()).to.contain('plum');
it("?inline is imported as string and doesn't make css bundled ", async () => {
const response = await fixture.fetch('/');
const html = await response.text();
const $ = cheerio.load(html);
expect($('#inline').text()).to.contain('tomato');
expect($('link[rel=stylesheet]')).to.have.lengthOf(0);
expect($('style')).to.have.lengthOf(1);
});
it("?raw is imported as a string and doesn't make css bundled", async () => {
const response = await fixture.fetch('/');
const html = await response.text();
const $ = cheerio.load(html);
expect($('#raw').text()).to.contain('plum');
expect($('link[rel=stylesheet]')).to.have.lengthOf(0);
expect($('style')).to.have.lengthOf(1);
});
});
});

View file

@ -1120,14 +1120,6 @@ importers:
astro: link:../../..
vue: 3.2.47
packages/astro/test/benchmark/simple:
specifiers:
'@astrojs/node': workspace:*
astro: workspace:*
dependencies:
'@astrojs/node': link:../../../../integrations/node
astro: link:../../..
packages/astro/test/fixtures/0-css:
specifiers:
'@astrojs/react': workspace:*