Improve internal API

This commit is contained in:
Rich Harris
2018-10-08 19:21:15 -04:00
committed by GitHub
parent 5e59855a15
commit 52f40f9e63
46 changed files with 696 additions and 1091 deletions

View File

@@ -1,41 +1,56 @@
import * as fs from 'fs';
import * as path from 'path';
import glob from 'tiny-glob/sync.js';
import { posixify, stringify, write_if_changed } from './utils';
import { dev, locations } from '../config';
import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces';
import { posixify, stringify, walk, write_if_changed } from '../utils';
import { Page, PageComponent, ManifestData } from '../interfaces';
export function create_main_manifests({ bundler, manifest_data, dev_port }: {
export function create_main_manifests({
bundler,
manifest_data,
dev_port,
dev,
cwd,
src,
dest,
routes,
output
}: {
bundler: string,
manifest_data: ManifestData;
dev_port?: number;
dev: boolean;
cwd: string;
src: string;
dest: string;
routes: string;
output: string
}) {
const manifest_dir = path.resolve('__sapper__');
if (!fs.existsSync(manifest_dir)) fs.mkdirSync(manifest_dir);
if (!fs.existsSync(output)) fs.mkdirSync(output);
const path_to_routes = path.relative(manifest_dir, locations.routes());
const path_to_routes = path.relative(output, routes);
const client_manifest = generate_client(manifest_data, path_to_routes, bundler, dev_port);
const server_manifest = generate_server(manifest_data, path_to_routes);
const client_manifest = generate_client(manifest_data, path_to_routes, bundler, dev, dev_port);
const server_manifest = generate_server(manifest_data, path_to_routes, cwd, src, dest, dev);
write_if_changed(
`${manifest_dir}/_layout.html`,
`${output}/_layout.html`,
`<svelte:component this={child.component} {...child.props}/>`
);
write_if_changed(`${manifest_dir}/client.js`, client_manifest);
write_if_changed(`${manifest_dir}/server.js`, server_manifest);
write_if_changed(`${output}/client.js`, client_manifest);
write_if_changed(`${output}/server.js`, server_manifest);
}
export function create_serviceworker_manifest({ manifest_data, client_files }: {
export function create_serviceworker_manifest({ manifest_data, output, client_files, static_files }: {
manifest_data: ManifestData;
output: string;
client_files: string[];
static_files: string;
}) {
let files;
let files: string[];
// TODO remove in a future version
if (fs.existsSync(locations.static())) {
files = glob('**', { cwd: locations.static(), filesOnly: true });
if (fs.existsSync(static_files)) {
files = walk(static_files);
} else {
// TODO remove in a future version
if (fs.existsSync('assets')) {
throw new Error(`As of Sapper 0.21, the assets/ directory should become static/`);
}
@@ -55,13 +70,14 @@ export function create_serviceworker_manifest({ manifest_data, client_files }: {
export const routes = [\n\t${manifest_data.pages.map((r: Page) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
`.replace(/^\t\t/gm, '').trim();
write_if_changed(`__sapper__/service-worker.js`, code);
write_if_changed(`${output}/service-worker.js`, code);
}
function generate_client(
manifest_data: ManifestData,
path_to_routes: string,
bundler: string,
dev: boolean,
dev_port?: number
) {
const template_file = path.resolve(__dirname, '../templates/client.js');
@@ -120,7 +136,7 @@ function generate_client(
let footer = '';
if (dev()) {
if (dev) {
const sapper_dev_client = posixify(
path.resolve(__dirname, '../sapper-dev-client.js')
);
@@ -145,7 +161,11 @@ function generate_client(
function generate_server(
manifest_data: ManifestData,
path_to_routes: string
path_to_routes: string,
cwd: string,
src: string,
dest: string,
dev: boolean
) {
const template_file = path.resolve(__dirname, '../templates/server.js');
const template = fs.readFileSync(template_file, 'utf-8');
@@ -206,13 +226,13 @@ function generate_server(
error
};`.replace(/^\t\t/gm, '').trim();
const build_dir = path.relative(process.cwd(), locations.dest());
const src_dir = path.relative(process.cwd(), locations.src());
const build_dir = path.relative(cwd, dest);
const src_dir = path.relative(cwd, src);
return `// This file is generated by Sapper — do not edit it!\n` + template
.replace('__BUILD__DIR__', JSON.stringify(build_dir))
.replace('__SRC__DIR__', JSON.stringify(src_dir))
.replace('__DEV__', dev() ? 'true' : 'false')
.replace('__DEV__', dev ? 'true' : 'false')
.replace(/const manifest = __MANIFEST__;/, code);
}