From eb7a6f9cc08c7c4356bde4e54777a7859b3dac80 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 19 Sep 2020 20:02:56 -0700 Subject: [PATCH 1/2] Preload inject_styles --- src/core/create_compilers/RollupCompiler.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/core/create_compilers/RollupCompiler.ts b/src/core/create_compilers/RollupCompiler.ts index a1b28d962..89fab287f 100644 --- a/src/core/create_compilers/RollupCompiler.ts +++ b/src/core/create_compilers/RollupCompiler.ts @@ -245,6 +245,16 @@ export default class RollupCompiler { return Array.from(dependenciesForTree(chunk, that.chunks, opts)); } + // It's hacky for the plugin to have to be aware of the style injection plugin + // However, there doesn't appear to be any more generic way of handling it + // https://github.com/rollup/rollup/issues/3790 + let inject_styles_file: string; + for (const key of Object.keys(bundle)) { + if (key.startsWith('inject_styles')) { + inject_styles_file = key; + } + } + // We need to handle the entry point separately // If there's a single page and preserveEntrySignatures is false then Rollup will // put everything in the entry point chunk (client.hash.js) @@ -259,8 +269,11 @@ export default class RollupCompiler { function add_dependencies(chunk: RenderedChunk) { for (const module of Object.keys(chunk.modules)) { if (is_route(module)) { - const js_dependencies = js_deps(chunk, + let js_dependencies = js_deps(chunk, { walk: ctx => !ctx.dynamicImport && ctx.chunk.fileName !== entry_chunk.fileName }).map(c => c.fileName); + if (inject_styles_file) { + js_dependencies = js_dependencies.concat(inject_styles_file); + } const css_dependencies = find_css(chunk, bundle).filter(x => !that.css_main.includes(x)); dependencies[module] = js_dependencies.concat(css_dependencies); } From 555f4de0a35ad96501481d630f11f8c4e516238f Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sun, 20 Sep 2020 10:36:04 -0400 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb74c0f10..040623699 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) +* Improve performance by preloading `inject_styles.js` script [#1530](https://github.com/sveltejs/sapper/issues/1530) * Fix flash of unstyled content [#1531](https://github.com/sveltejs/sapper/issues/1531) * Fix duplicate CSS injection with both relative and absolute URLs [#1535](https://github.com/sveltejs/sapper/issues/1535)