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

fix: avoid inlining raw/url CSS imports #9925

Merged
merged 8 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/eleven-ravens-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: avoid trying to inline raw or url css imports
15 changes: 6 additions & 9 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,17 @@ export async function dev(vite, vite_config, svelte_config) {
const styles = {};

for (const dep of deps) {
const url = new URL(dep.url, 'http://localhost/');
const url = new URL(dep.url, 'dummy:/');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should dummy:/ be dummy://? why doesn't localhost work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should dummy:/ be dummy://?

image

why doesn't localhost work?

changed it while tinkering around but figured I'd keep it since I think it conveys the purpose better.

const query = url.searchParams;

if (
isCSSRequest(dep.file) ||
(query.has('svelte') && query.get('type') === 'style')
(isCSSRequest(dep.file) ||
(query.has('svelte') && query.get('type') === 'style')) &&
!(query.has('raw') || query.has('url') || query.has('inline'))
) {
// setting `?inline` to load CSS modules as css string
query.set('inline', '');

try {
const mod = await loud_ssr_load_module(
`${url.pathname}${url.search}${url.hash}`
);
query.set('inline', '');
const mod = await vite.ssrLoadModule(`${url.pathname}${url.search}${url.hash}`);
bluwy marked this conversation as resolved.
Show resolved Hide resolved
styles[dep.url] = mod.default;
} catch {
// this can happen with dynamically imported modules, I think
Expand Down
17 changes: 8 additions & 9 deletions packages/kit/test/apps/basics/src/routes/css/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<script>
import './_styles.css';
import './_manual.css?url';
import './_manual.css?raw';
import './_manual.css?inline';
</script>

<div class="styled">
this text is red
</div>
<div class="styled">this text is red</div>

<div class="also-styled">
this text is blue
</div>
<div class="also-styled">this text is blue</div>

<div class="overridden">
this text is green
</div>
<div class="overridden">this text is green</div>

<div class="not">this text is black</div>

<a href="/css/other">other</a>

Expand Down
3 changes: 3 additions & 0 deletions packages/kit/test/apps/basics/src/routes/css/_manual.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.not {
color: blue;
}
33 changes: 16 additions & 17 deletions packages/kit/test/apps/basics/test/cross-platform/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@ import { test } from '../../../../utils.js';
test.describe.configure({ mode: 'parallel' });

test.describe('CSS', () => {
test('applies imported styles', async ({ page, get_computed_style }) => {
test('applies styles correctly', async ({ page, get_computed_style }) => {
await page.goto('/css');

expect(await get_computed_style('.styled', 'color')).toBe('rgb(255, 0, 0)');
});

test('applies layout styles', async ({ page, get_computed_style }) => {
await page.goto('/css');

expect(await get_computed_style('footer', 'color')).toBe('rgb(128, 0, 128)');
});
test.step('applies imported styles', async () => {
expect(await get_computed_style('.styled', 'color')).toBe('rgb(255, 0, 0)');
});

test('applies local styles', async ({ page, get_computed_style }) => {
await page.goto('/css');
test.step('applies imported styles in the correct order', async () => {
expect(await get_computed_style('.overridden', 'color')).toBe('rgb(0, 128, 0)');
});

expect(await get_computed_style('.also-styled', 'color')).toBe('rgb(0, 0, 255)');
});
test.step('applies layout styles', async () => {
expect(await get_computed_style('footer', 'color')).toBe('rgb(128, 0, 128)');
});

test('applies imported styles in the correct order', async ({ page, get_computed_style }) => {
await page.goto('/css');
test.step('applies local styles', async () => {
expect(await get_computed_style('.also-styled', 'color')).toBe('rgb(0, 0, 255)');
});

const color = await get_computed_style('.overridden', 'color');
expect(color).toBe('rgb(0, 128, 0)');
test.step('does not apply raw and url', async () => {
expect(await get_computed_style('.not', 'color')).toBe('rgb(0, 0, 0)');
});
});
});

Expand Down