From 3445ec66ac2dfd4ddbd4481c6b5b925e7df9fa1f Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Fri, 1 Feb 2019 09:28:24 -0500 Subject: [PATCH] figure out if component has preload early on --- package.json | 3 +++ src/core/create_manifest_data.ts | 26 ++++++++++++++++++++++---- src/interfaces.ts | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 97c282b..c25be2d 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,9 @@ "webpack-format-messages": "^2.0.5", "yootils": "0.0.14" }, + "peerDependencies": { + "svelte": "^3.0.0" + }, "scripts": { "test": "mocha --opts mocha.opts", "pretest": "npm run build", diff --git a/src/core/create_manifest_data.ts b/src/core/create_manifest_data.ts index 9f47093..1d38442 100644 --- a/src/core/create_manifest_data.ts +++ b/src/core/create_manifest_data.ts @@ -1,5 +1,6 @@ import * as fs from 'fs'; import * as path from 'path'; +import svelte from 'svelte/compiler'; import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces'; import { posixify, reserved_words } from '../utils'; @@ -9,6 +10,19 @@ export default function create_manifest_data(cwd: string): ManifestData { throw new Error(`As of Sapper 0.21, the routes/ directory should become src/routes/`); } + function has_preload(file: string) { + const source = fs.readFileSync(path.join(cwd, file), 'utf-8'); + + if (/preload/.test(source)) { + try { + const { stats } = svelte.compile(source, { generate: false }); + return !!stats.vars.find((variable: any) => variable.module && variable.export_name === 'preload'); + } catch (err) {} + } + + return false; + } + const components: PageComponent[] = []; const pages: Page[] = []; const server_routes: ServerRoute[] = []; @@ -16,7 +30,8 @@ export default function create_manifest_data(cwd: string): ManifestData { const default_layout: PageComponent = { default: true, name: '_default_layout', - file: null + file: null, + has_preload: false }; function walk( @@ -107,7 +122,8 @@ export default function create_manifest_data(cwd: string): ManifestData { const component = fs.existsSync(index) && { name: `${get_slug(item.file)}__layout`, - file: `${item.file}/_layout.html` + file: `${item.file}/_layout.html`, + has_preload: has_preload(`${item.file}/_layout.html`) }; if (component) components.push(component); @@ -125,7 +141,8 @@ export default function create_manifest_data(cwd: string): ManifestData { else if (item.is_page) { const component = { name: get_slug(item.file), - file: item.file + file: item.file, + has_preload: has_preload(item.file) }; const parts = stack.concat({ @@ -162,7 +179,8 @@ export default function create_manifest_data(cwd: string): ManifestData { const root = fs.existsSync(root_file) ? { name: 'main', - file: '_layout.html' + file: '_layout.html', + has_preload: has_preload(root_file) } : default_layout; diff --git a/src/interfaces.ts b/src/interfaces.ts index f9eea04..50dc6ab 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -29,6 +29,7 @@ export type PageComponent = { default?: boolean; name: string; file: string; + has_preload: boolean; }; export type Page = {