-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
Mentioned this in Gitter earlier but it deserves an issue. Right now, it's a bit too hard to do the right thing when it comes to CSS (where 'the right thing' is pre-rendering the CSS and adding it via a <link>
or — for critical path CSS — an inline <style>
. This is much better for performance and prevents CSP violations). You have to use the ssr
compiler, and do something like the following:
require( 'svelte/ssr/register' ); // or use a bundler with `generate: 'ssr'`
var App = require( './App.html' );
var { css } = App.renderCss();
fs.writeFileSync( 'public/main.css', css );
This can be finicky if your app isn't Node-friendly (e.g. it imports libraries that expect window
to exist, etc). It would be much easier if you could do something like
var css = svelte.extractCss( 'App.html', {
children: true // or `false` if you *don't* want child component CSS included
});
With the accompanying command line interface:
svelte css App.html > public/main.css
Because Svelte components are easy to statically analyse, it ought to be straightforward to trace imported child components, at least if they can be resolved as relative paths or with the standard Node resolution algorithm. (It will be even easier to only extract CSS for one component at a time, but you might end up with some duplication that way.)
Everything is up for bikeshedding at this point (is extractCss
the right name? Should this live in a separate project, rather than in Svelte core? How does this interact with #181 and #65?).
Note: Doing the right thing also means remembering to add css: false
to the compiler options in production. (How do we make it more likely that this happens?)