Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Fix style output with emitCss: false (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Sep 20, 2020
1 parent 8c8d5fc commit b06edba
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

* Minify and hash inject_styles.js [#1524](https://github.com/sveltejs/sapper/pull/1524)
* Fix support for legacy browsers [#1525](https://github.com/sveltejs/sapper/pull/1525)
* Fix flash of unstyled content [#1531](https://github.com/sveltejs/sapper/issues/1531)

## 0.28.7

Expand Down
5 changes: 3 additions & 2 deletions src/core/create_compilers/RollupCompiler.ts
Expand Up @@ -253,15 +253,16 @@ export default class RollupCompiler {

// We consider the dependencies of the entry chunk as well when finding the CSS in
// case preserveEntrySignatures is true and there are multiple chunks
that.css_main = find_css(entry_chunk, bundle);
const entry_css = find_css(entry_chunk, bundle);
that.css_main = entry_css && entry_css.length ? entry_css : undefined;

// Routes dependencies
function add_dependencies(chunk: RenderedChunk) {
for (const module of Object.keys(chunk.modules)) {
if (is_route(module)) {
const js_dependencies = js_deps(chunk,
{ walk: ctx => !ctx.dynamicImport && ctx.chunk.fileName !== entry_chunk.fileName }).map(c => c.fileName);
const css_dependencies = find_css(chunk, bundle).filter(x => !that.css_main.includes(x));
const css_dependencies = find_css(chunk, bundle).filter(x => !that.css_main || !that.css_main.includes(x));
dependencies[module] = js_dependencies.concat(css_dependencies);
}
}
Expand Down
61 changes: 61 additions & 0 deletions test/apps/css-inlined/rollup.config.js
@@ -0,0 +1,61 @@
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import svelte from 'rollup-plugin-svelte';

const mode = process.env.NODE_ENV;
const dev = mode === 'development';

const config = require('../../../config/rollup.js');

export default {
client: {
input: config.client.input(),
output: config.client.output(),
plugins: [
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
svelte({
dev,
hydratable: true,
emitCss: false
}),
resolve()
],
preserveEntrySignatures: false
},

server: {
input: config.server.input(),
output: config.server.output(),
plugins: [
replace({
'process.browser': false,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
svelte({
generate: 'ssr',
dev
}),
resolve({
preferBuiltins: true
})
],
external: ['sirv', 'polka'],
preserveEntrySignatures: 'strict'
},

serviceworker: {
input: config.serviceworker.input(),
output: config.serviceworker.output(),
plugins: [
resolve(),
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode)
})
],
preserveEntrySignatures: false
}
};
9 changes: 9 additions & 0 deletions test/apps/css-inlined/src/client.js
@@ -0,0 +1,9 @@
import * as sapper from '@sapper/app';

window.start = () => sapper.start({
target: document.querySelector('#sapper')
});

window.prefetchRoutes = () => sapper.prefetchRoutes();
window.prefetch = href => sapper.prefetch(href);
window.goto = href => sapper.goto(href);
18 changes: 18 additions & 0 deletions test/apps/css-inlined/src/routes/_error.svelte
@@ -0,0 +1,18 @@
<script>
export let status;
export let error;
</script>

<style>
h1 {
color: purple;
}
</style>

<h1>This is an error page</h1>

<h2>{status}</h2>

<p>{error.message}</p>

<pre>{error.stack}</pre>
10 changes: 10 additions & 0 deletions test/apps/css-inlined/src/routes/index.svelte
@@ -0,0 +1,10 @@
<h1>Great success!</h1>

<a href="foo">foo</a>
<a href="bar">bar</a>

<style>
h1 {
color: red;
}
</style>
9 changes: 9 additions & 0 deletions test/apps/css-inlined/src/server.js
@@ -0,0 +1,9 @@
import polka from 'polka';
import * as sapper from '@sapper/server';

import { start } from '../../common.js';

const app = polka()
.use(sapper.middleware());

start(app);
82 changes: 82 additions & 0 deletions test/apps/css-inlined/src/service-worker.js
@@ -0,0 +1,82 @@
import * as sapper from '@sapper/service-worker';

const ASSETS = `cache${sapper.timestamp}`;

// `shell` is an array of all the files generated by webpack,
// `files` is an array of everything in the `static` directory
const to_cache = sapper.shell.concat(sapper.files);
const cached = new Set(to_cache);

self.addEventListener('install', event => {
event.waitUntil(
caches
.open(ASSETS)
.then(cache => cache.addAll(to_cache))
.then(() => {
self.skipWaiting();
})
);
});

self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(async keys => {
// delete old caches
for (const key of keys) {
if (key !== ASSETS) await caches.delete(key);
}

self.clients.claim();
})
);
});

self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;

const url = new URL(event.request.url);

// don't try to handle e.g. data: URIs
if (!url.protocol.startsWith('http')) return;

// ignore dev server requests
if (url.hostname === self.location.hostname && url.port !== self.location.port) return;

// always serve assets and webpack-generated files from cache
if (url.host === self.location.host && cached.has(url.pathname)) {
event.respondWith(caches.match(event.request));
return;
}

// for pages, you might want to serve a shell `index.html` file,
// which Sapper has generated for you. It's not right for every
// app, but if it's right for yours then uncomment this section
/*
if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
event.respondWith(caches.match('/index.html'));
return;
}
*/

if (event.request.cache === 'only-if-cached') return;

// for everything else, try the network first, falling back to
// cache if the user is offline. (If the pages never change, you
// might prefer a cache-first approach to a network-first one.)
event.respondWith(
caches
.open(`offline${sapper.timestamp}`)
.then(async cache => {
try {
const response = await fetch(event.request);
cache.put(event.request, response.clone());
return response;
} catch (err) {
const response = await cache.match(event.request);
if (response) return response;

throw err;
}
})
);
});
14 changes: 14 additions & 0 deletions test/apps/css-inlined/src/template.html
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset='utf-8'>

%sapper.base%
%sapper.styles%
%sapper.head%
%sapper.scripts%
</head>
<body>
<div id='sapper'>%sapper.html%</div>
</body>
</html>
24 changes: 24 additions & 0 deletions test/apps/css-inlined/test.ts
@@ -0,0 +1,24 @@
import * as assert from 'assert';
import { build } from '../../../api';
import { AppRunner } from '../AppRunner';

describe('css inlined', function() {
this.timeout(10000);

let r: AppRunner;

// hooks
before('build app', () => build({ cwd: __dirname }));
before('start runner', async () => {
r = await new AppRunner().start(__dirname);
});

after(() => r && r.end());

// tests
it('includes critical CSS with server render', async () => {
await r.load('/');

assert((await r.page.content()).includes('color:red'));
});
});

0 comments on commit b06edba

Please sign in to comment.