Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ?inline and ?raw CSS from being bundled as CSS #6161

Merged
merged 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-rocks-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevent ?inline and ?raw CSS from being bundled as CSS
7 changes: 6 additions & 1 deletion packages/astro/src/core/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ const cssRe = new RegExp(
.map((s) => s.slice(1))
.join('|')})($|\\?)`
);
export const isCSSRequest = (request: string): boolean => cssRe.test(request);

const rawRE = /(?:\?|&)raw(?:&|$)/;
const inlineRE = /(?:\?|&)inline\b/;

export const isCSSRequest = (request: string): boolean => cssRe.test(request) &&
!rawRE.test(request) && !inlineRE.test(request);
28 changes: 28 additions & 0 deletions packages/astro/test/css-inline.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
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();
});

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');
});

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');
});
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/css-inline/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/css-inline",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/css-inline/src/inline.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: tomato;
}
35 changes: 35 additions & 0 deletions packages/astro/test/fixtures/css-inline/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
export interface Props {
title: string;
}

const { title } = Astro.props;
---

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
</head>
<body>
<slot />
</body>
</html>
<style is:global>
:root {
--accent: 124, 58, 237;
--accent-gradient: linear-gradient(45deg, rgb(var(--accent)), #da62c4 30%, white 60%);
}
html {
font-family: system-ui, sans-serif;
background-color: #F6F6F6;
}
code {
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
</style>
19 changes: 19 additions & 0 deletions packages/astro/test/fixtures/css-inline/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import Layout from '../layouts/Layout.astro';
import inline from '../inline.css?inline';
import raw from '../raw.css?raw';
---
<Layout title="Welcome to Astro.">
<main>
<h1>Welcome to Astro</h1>
<p>
This are some `?inline` styles to show as object:
</p>
<p id="inline">
{inline}
</p>
<p id="raw">
{raw}
</p>
</main>
</Layout>
3 changes: 3 additions & 0 deletions packages/astro/test/fixtures/css-inline/src/raw.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
main {
background: plum;
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.