figure out if component has preload early on

This commit is contained in:
Richard Harris
2019-02-01 09:28:24 -05:00
parent 4940c5d5be
commit 3445ec66ac
3 changed files with 26 additions and 4 deletions

View File

@@ -67,6 +67,9 @@
"webpack-format-messages": "^2.0.5", "webpack-format-messages": "^2.0.5",
"yootils": "0.0.14" "yootils": "0.0.14"
}, },
"peerDependencies": {
"svelte": "^3.0.0"
},
"scripts": { "scripts": {
"test": "mocha --opts mocha.opts", "test": "mocha --opts mocha.opts",
"pretest": "npm run build", "pretest": "npm run build",

View File

@@ -1,5 +1,6 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import svelte from 'svelte/compiler';
import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces'; import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces';
import { posixify, reserved_words } from '../utils'; 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/`); 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 components: PageComponent[] = [];
const pages: Page[] = []; const pages: Page[] = [];
const server_routes: ServerRoute[] = []; const server_routes: ServerRoute[] = [];
@@ -16,7 +30,8 @@ export default function create_manifest_data(cwd: string): ManifestData {
const default_layout: PageComponent = { const default_layout: PageComponent = {
default: true, default: true,
name: '_default_layout', name: '_default_layout',
file: null file: null,
has_preload: false
}; };
function walk( function walk(
@@ -107,7 +122,8 @@ export default function create_manifest_data(cwd: string): ManifestData {
const component = fs.existsSync(index) && { const component = fs.existsSync(index) && {
name: `${get_slug(item.file)}__layout`, 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); if (component) components.push(component);
@@ -125,7 +141,8 @@ export default function create_manifest_data(cwd: string): ManifestData {
else if (item.is_page) { else if (item.is_page) {
const component = { const component = {
name: get_slug(item.file), name: get_slug(item.file),
file: item.file file: item.file,
has_preload: has_preload(item.file)
}; };
const parts = stack.concat({ const parts = stack.concat({
@@ -162,7 +179,8 @@ export default function create_manifest_data(cwd: string): ManifestData {
const root = fs.existsSync(root_file) const root = fs.existsSync(root_file)
? { ? {
name: 'main', name: 'main',
file: '_layout.html' file: '_layout.html',
has_preload: has_preload(root_file)
} }
: default_layout; : default_layout;

View File

@@ -29,6 +29,7 @@ export type PageComponent = {
default?: boolean; default?: boolean;
name: string; name: string;
file: string; file: string;
has_preload: boolean;
}; };
export type Page = { export type Page = {