Skip to content

Commit fc19a92

Browse files
authored
feat: scope css to js module to allow treeshaking it (requires vite 6.2) (#1092)
* feat: scope css to js module to allow treeshaking it (requires vite 6.2) * chore: improve condition and add comment * refactor: replace zimmerframe+walk with regex test in preprocessor * chore: remove extraneous check * refactor: use svelte 5.26 hasGlobal flag instead of custom impl * chore: revert code change from previous impl * chore: update changeset
1 parent 876c1c5 commit fc19a92

File tree

17 files changed

+167
-3
lines changed

17 files changed

+167
-3
lines changed

.changeset/plenty-eyes-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
scope css to js module to enable treeshaking scoped css from unused components. Requires vite 6.2 and svelte 5.26
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { browserLogs, findAssetFile, getColor, getEl, getText, isBuild } from '~utils';
2+
import { expect } from 'vitest';
3+
4+
test('should not have failed requests', async () => {
5+
browserLogs.forEach((msg) => {
6+
expect(msg).not.toMatch('404');
7+
});
8+
});
9+
10+
test('should apply css from used components', async () => {
11+
expect(await getText('#app')).toBe('App');
12+
expect(await getColor('#app')).toBe('blue');
13+
expect(await getText('#a')).toBe('A');
14+
expect(await getColor('#a')).toBe('red');
15+
});
16+
17+
test('should apply css from unused components that contain global styles', async () => {
18+
expect(await getEl('head style[src]'));
19+
expect(await getColor('#test')).toBe('green'); // from B.svelte
20+
});
21+
22+
test('should not render unused components', async () => {
23+
expect(await getEl('#b')).toBeNull();
24+
expect(await getEl('#c')).toBeNull();
25+
});
26+
27+
if (isBuild) {
28+
test('should include unscoped global styles from unused components', async () => {
29+
const cssOutput = findAssetFile(/index-.*\.css/);
30+
expect(cssOutput).toContain('#test{color:green}'); // from B.svelte
31+
});
32+
test('should not include scoped styles from unused components', async () => {
33+
const cssOutput = findAssetFile(/index-.*\.css/);
34+
// from C.svelte
35+
expect(cssOutput).not.toContain('.unused');
36+
});
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
7+
<title>Svelte app</title>
8+
9+
<script type="module" src="/src/main.js"></script>
10+
</head>
11+
12+
<body></body>
13+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "e2e-tests-css-treeshake",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@sveltejs/vite-plugin-svelte": "workspace:^",
13+
"sass": "^1.85.1",
14+
"svelte": "^5.20.5",
15+
"vite": "^6.2.0"
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1 id="a">A</h1>
2+
3+
<style>
4+
h1 {
5+
color: red;
6+
}
7+
</style>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import { A } from './barrel.js';
3+
</script>
4+
5+
<div id="test">test</div>
6+
<h1 id="app">App</h1>
7+
<A />
8+
9+
<style>
10+
#app {
11+
color: blue;
12+
}
13+
</style>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h1 id="b">B</h1>
2+
3+
<style>
4+
h1 {
5+
color: green;
6+
}
7+
:global(#test) {
8+
color: green;
9+
}
10+
</style>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h1 id="c" class="unused"><strong>C</strong></h1>
2+
3+
<style>
4+
.unused {
5+
color: magenta;
6+
}
7+
h1 :global {
8+
background: blue;
9+
}
10+
11+
h1 :global(strong) {
12+
color: magenta;
13+
}
14+
</style>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as A } from './A.svelte';
2+
// B and C are unused, their css should not be included
3+
export { default as B } from './B.svelte';
4+
export { default as C } from './C.svelte';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import App from './App.svelte';
2+
import { mount } from 'svelte';
3+
mount(App, { target: document.body });

0 commit comments

Comments
 (0)