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:
parent
9d236c9417
commit
e0844852d3
4 changed files with 57 additions and 21 deletions
5
.changeset/witty-taxis-accept.md
Normal file
5
.changeset/witty-taxis-accept.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Prevent `?inline` and `?raw` css query suffixes from injecting style tags in development
|
|
@ -2,7 +2,7 @@ import type { ModuleLoader } from '../../module-loader/index';
|
||||||
|
|
||||||
import { RuntimeMode } from '../../../@types/astro.js';
|
import { RuntimeMode } from '../../../@types/astro.js';
|
||||||
import { viteID } from '../../util.js';
|
import { viteID } from '../../util.js';
|
||||||
import { isCSSRequest } from './util.js';
|
import { isBuildableCSSRequest } from './util.js';
|
||||||
import { crawlGraph } from './vite.js';
|
import { crawlGraph } from './vite.js';
|
||||||
|
|
||||||
/** Given a filePath URL, crawl Vite’s module graph to find all style imports. */
|
/** Given a filePath URL, crawl Vite’s module graph to find all style imports. */
|
||||||
|
@ -15,7 +15,7 @@ export async function getStylesForURL(
|
||||||
const importedStylesMap = new Map<string, string>();
|
const importedStylesMap = new Map<string, string>();
|
||||||
|
|
||||||
for await (const importedModule of crawlGraph(loader, viteID(filePath), true)) {
|
for await (const importedModule of crawlGraph(loader, viteID(filePath), true)) {
|
||||||
if (isCSSRequest(importedModule.url)) {
|
if (isBuildableCSSRequest(importedModule.url)) {
|
||||||
let ssrModule: Record<string, any>;
|
let ssrModule: Record<string, any>;
|
||||||
try {
|
try {
|
||||||
// The SSR module is possibly not loaded. Load it if it's null.
|
// The SSR module is possibly not loaded. Load it if it's null.
|
||||||
|
|
|
@ -4,25 +4,64 @@ import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
describe('Importing raw/inlined CSS', () => {
|
describe('Importing raw/inlined CSS', () => {
|
||||||
let fixture;
|
let fixture;
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/css-inline/',
|
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 () => {
|
describe('Dev', () => {
|
||||||
const html = await fixture.readFile('/index.html');
|
/** @type {import('./test-utils').DevServer} */
|
||||||
const $ = cheerio.load(html);
|
let devServer;
|
||||||
|
|
||||||
expect($('#inline').text()).to.contain('tomato');
|
before(async () => {
|
||||||
});
|
devServer = await fixture.startDevServer();
|
||||||
|
});
|
||||||
|
|
||||||
it('?raw is imported as a string', async () => {
|
after(async () => {
|
||||||
const html = await fixture.readFile('/index.html');
|
await devServer.stop();
|
||||||
const $ = cheerio.load(html);
|
});
|
||||||
|
|
||||||
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1120,14 +1120,6 @@ importers:
|
||||||
astro: link:../../..
|
astro: link:../../..
|
||||||
vue: 3.2.47
|
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:
|
packages/astro/test/fixtures/0-css:
|
||||||
specifiers:
|
specifiers:
|
||||||
'@astrojs/react': workspace:*
|
'@astrojs/react': workspace:*
|
||||||
|
|
Loading…
Reference in a new issue