Skip to content

Commit cfd6c3c

Browse files
author
Rich Harris
authored
Use rendered CSS for AMP pages, rather than emitted CSS (#1408)
* failing test for #808 * use rendered CSS for AMP pages, rather than emitted CSS - fixes #808 * bump vite-plugin-svelte * oops
1 parent 3fab470 commit cfd6c3c

File tree

11 files changed

+61
-11
lines changed

11 files changed

+61
-11
lines changed

.changeset/happy-nails-draw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Use rendered CSS for AMP pages

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0-next.104",
44
"type": "module",
55
"dependencies": {
6-
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.9",
6+
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.10",
77
"cheap-watch": "^1.0.3",
88
"sade": "^1.7.4",
99
"vite": "^2.2.4"

packages/kit/src/core/build/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ async function build_client({
167167
plugins: [
168168
...(user_config.plugins || []),
169169
svelte({
170-
extensions: config.extensions
170+
extensions: config.extensions,
171+
emitCss: !config.kit.amp
171172
})
172173
]
173174
});

packages/kit/src/core/dev/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class Watcher extends EventEmitter {
9797
plugins: [
9898
...(user_config.plugins || []),
9999
svelte({
100-
extensions: this.config.extensions
100+
extensions: this.config.extensions,
101+
emitCss: !this.config.kit.amp
101102
})
102103
],
103104
publicDir: this.config.kit.files.assets,

packages/kit/src/runtime/server/page/render.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ export async function render_response({
8787
unsubscribe();
8888
}
8989
} else {
90-
rendered = { head: '', html: '', css: '' };
90+
rendered = { head: '', html: '', css: { code: '', map: null } };
9191
}
9292

9393
const include_js = page_config.router || page_config.hydrate;
9494
if (!include_js) js.clear();
9595

9696
// TODO strip the AMP stuff out of the build if not relevant
9797
const links = options.amp
98-
? styles.size > 0
99-
? `<style amp-custom>${Array.from(styles).join('\n')}</style>`
98+
? styles.size > 0 || rendered.css.code.length > 0
99+
? `<style amp-custom>${Array.from(styles).concat(rendered.css.code).join('\n')}</style>`
100100
: ''
101101
: [
102102
...Array.from(js).map((dep) => `<link rel="modulepreload" href="${dep}">`),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p class="shouty">this component will never be rendered</p>
2+
3+
<style>
4+
p {
5+
color: #40b3ff;
6+
}
7+
</style>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as assert from 'uvu/assert';
2+
3+
/** @type {import('test').TestMaker} */
4+
export default function (test, is_dev) {
5+
test('only includes CSS for rendered components', '/styles', async ({ page }) => {
6+
const style = await page.innerHTML('style[amp-custom]');
7+
8+
assert.ok(style.includes('#ff3e00'), 'Rendered styles are included');
9+
assert.ok(style.includes('uppercase'), 'Imported styles are included');
10+
assert.ok(!style.includes('#40b3ff'), 'Unrendered styles are omitted');
11+
});
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.shouty {
2+
text-transform: uppercase;
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import { amp } from '$app/env';
3+
import Unused from './Unused.svelte';
4+
import './imported.css';
5+
</script>
6+
7+
{#if amp}
8+
<p class="shouty">this text is rendered</p>
9+
{:else}
10+
<Unused/>
11+
{/if}
12+
13+
14+
<style>
15+
p {
16+
color: #ff3e00;
17+
}
18+
</style>

packages/kit/types/internal.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ export type SSRComponent = {
5151
) => {
5252
html: string;
5353
head: string;
54-
css: string;
54+
css: {
55+
code: string;
56+
map: any; // TODO
57+
};
5558
};
5659
};
5760
};

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)