mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 12:24:47 +00:00
tidy up
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -12,5 +12,5 @@ sapper
|
|||||||
runtime.js
|
runtime.js
|
||||||
dist
|
dist
|
||||||
!rollup.config.js
|
!rollup.config.js
|
||||||
templates/app.mjs
|
/runtime/app.mjs
|
||||||
templates/server.mjs
|
/runtime/server.mjs
|
||||||
@@ -11,8 +11,8 @@
|
|||||||
"config",
|
"config",
|
||||||
"sapper",
|
"sapper",
|
||||||
"dist/*.js",
|
"dist/*.js",
|
||||||
"templates/*.js",
|
"runtime/*.js",
|
||||||
"templates/*.html"
|
"runtime/*.html"
|
||||||
],
|
],
|
||||||
"directories": {
|
"directories": {
|
||||||
"test": "test"
|
"test": "test"
|
||||||
@@ -79,7 +79,7 @@
|
|||||||
"prepare": "npm run build",
|
"prepare": "npm run build",
|
||||||
"dev": "rollup -cw",
|
"dev": "rollup -cw",
|
||||||
"prepublishOnly": "npm test",
|
"prepublishOnly": "npm test",
|
||||||
"update_mime_types": "curl http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types | grep -e \"^[^#]\" > templates/src/server/middleware/mime-types.md"
|
"update_mime_types": "curl http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types | grep -e \"^[^#]\" > runtime/src/server/middleware/mime-types.md"
|
||||||
},
|
},
|
||||||
"repository": "https://github.com/sveltejs/sapper",
|
"repository": "https://github.com/sveltejs/sapper",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|||||||
@@ -14,16 +14,16 @@ const external = [].concat(
|
|||||||
|
|
||||||
function template(kind, external) {
|
function template(kind, external) {
|
||||||
return {
|
return {
|
||||||
input: `templates/src/${kind}/index.ts`,
|
input: `runtime/src/${kind}/index.ts`,
|
||||||
output: {
|
output: {
|
||||||
file: `templates/${kind}.mjs`,
|
file: `runtime/${kind}.mjs`,
|
||||||
format: 'es',
|
format: 'es',
|
||||||
paths: id => id.replace('@sapper', '.')
|
paths: id => id.replace('@sapper', '.')
|
||||||
},
|
},
|
||||||
external,
|
external,
|
||||||
plugins: [
|
plugins: [
|
||||||
resolve({
|
resolve({
|
||||||
extensions: ['.js', '.ts']
|
extensions: ['.mjs', '.js', '.ts']
|
||||||
}),
|
}),
|
||||||
commonjs(),
|
commonjs(),
|
||||||
string({
|
string({
|
||||||
@@ -37,8 +37,8 @@ function template(kind, external) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
template('app', ['__ROOT__', '__ERROR__', 'svelte', '@sapper/App.html']),
|
template('app', id => /^(svelte\/?|@sapper\/)/.test(id)),
|
||||||
template('server', builtinModules),
|
template('server', id => builtinModules.includes(id)),
|
||||||
|
|
||||||
{
|
{
|
||||||
input: [
|
input: [
|
||||||
@@ -57,7 +57,7 @@ export default [
|
|||||||
plugins: [
|
plugins: [
|
||||||
json(),
|
json(),
|
||||||
resolve({
|
resolve({
|
||||||
extensions: ['.js', '.ts']
|
extensions: ['.mjs', '.js', '.ts']
|
||||||
}),
|
}),
|
||||||
commonjs(),
|
commonjs(),
|
||||||
sucrase({
|
sucrase({
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { setContext } from 'svelte';
|
import { setContext } from 'svelte';
|
||||||
import { CONTEXT_KEY } from './internal';
|
import { CONTEXT_KEY } from './shared';
|
||||||
|
|
||||||
export let Root;
|
export let Root;
|
||||||
export let props;
|
export let props;
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import App from '@sapper/App.html';
|
|
||||||
import { stores } from '@sapper/internal';
|
|
||||||
import Root from '__ROOT__';
|
|
||||||
import { preload as root_preload } from '__ROOT_PRELOAD__';
|
|
||||||
import ErrorComponent from '__ERROR__';
|
|
||||||
import { writable } from 'svelte/store.mjs';
|
import { writable } from 'svelte/store.mjs';
|
||||||
|
import Sapper from '@sapper/internal/Sapper.html';
|
||||||
|
import { stores } from '@sapper/internal/shared';
|
||||||
|
import { Root, root_preload, ErrorComponent, ignore, components, routes } from '@sapper/internal/manifest-client';
|
||||||
import {
|
import {
|
||||||
Target,
|
Target,
|
||||||
ScrollPosition,
|
ScrollPosition,
|
||||||
@@ -16,15 +14,9 @@ import {
|
|||||||
} from './types';
|
} from './types';
|
||||||
import goto from './goto';
|
import goto from './goto';
|
||||||
|
|
||||||
// injected at build time
|
declare const __SAPPER__;
|
||||||
declare const __IGNORE__, __COMPONENTS__, __PAGES__, __SAPPER__;
|
|
||||||
|
|
||||||
export const initial_data = typeof __SAPPER__ !== 'undefined' && __SAPPER__;
|
export const initial_data = typeof __SAPPER__ !== 'undefined' && __SAPPER__;
|
||||||
|
|
||||||
const ignore = __IGNORE__;
|
|
||||||
export const components: ComponentLoader[] = __COMPONENTS__;
|
|
||||||
export const routes: Route[] = __PAGES__;
|
|
||||||
|
|
||||||
let ready = false;
|
let ready = false;
|
||||||
let root_component: Component;
|
let root_component: Component;
|
||||||
let current_token: {};
|
let current_token: {};
|
||||||
@@ -200,7 +192,7 @@ async function render(redirect: Redirect, branch: any[], props: any, page: Page)
|
|||||||
detach(end);
|
detach(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
root_component = new App({
|
root_component = new Sapper({
|
||||||
target,
|
target,
|
||||||
props: {
|
props: {
|
||||||
Root,
|
Root,
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getContext } from 'svelte';
|
import { getContext } from 'svelte';
|
||||||
import { CONTEXT_KEY, stores } from '@sapper/internal';
|
import { CONTEXT_KEY, stores } from '@sapper/internal/shared';
|
||||||
|
|
||||||
export const preloading = { subscribe: stores.preloading.subscribe };
|
export const preloading = { subscribe: stores.preloading.subscribe };
|
||||||
export const page = { subscribe: stores.page.subscribe };
|
export const page = { subscribe: stores.page.subscribe };
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { components, routes, load_component } from "../app";
|
import { components, routes } from '@sapper/internal/manifest-client';
|
||||||
|
import { load_component } from '../app';
|
||||||
|
|
||||||
export default function prefetchRoutes(pathnames: string[]) {
|
export default function prefetchRoutes(pathnames: string[]) {
|
||||||
return routes
|
return routes
|
||||||
@@ -9,7 +9,7 @@ type Child = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export interface ComponentConstructor {
|
export interface ComponentConstructor {
|
||||||
new (options: { target: Node, props: any, store: Store, hydrate: boolean }): Component;
|
new (options: { target: Node, props: any, hydrate: boolean }): Component;
|
||||||
preload: (props: { params: Params, query: Query }) => Promise<any>;
|
preload: (props: { params: Params, query: Query }) => Promise<any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -55,10 +55,6 @@ export type Redirect = {
|
|||||||
location: string;
|
location: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Store = {
|
|
||||||
get: () => any;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Page = {
|
export type Page = {
|
||||||
path: string;
|
path: string;
|
||||||
params: Record<string, string>;
|
params: Record<string, string>;
|
||||||
1
runtime/src/server/constants.js
Normal file
1
runtime/src/server/constants.js
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export const IGNORE = '__SAPPER__IGNORE__';
|
||||||
@@ -5,10 +5,11 @@ import cookie from 'cookie';
|
|||||||
import devalue from 'devalue';
|
import devalue from 'devalue';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import URL from 'url';
|
import URL from 'url';
|
||||||
import { build_dir, dev, src_dir, IGNORE } from '../placeholders';
|
import { IGNORE } from '../constants';
|
||||||
import { Manifest, Page, Props, Req, Res } from './types';
|
import { Manifest, Page, Props, Req, Res } from './types';
|
||||||
import { stores } from '@sapper/internal';
|
import { build_dir, dev, src_dir } from '@sapper/internal/manifest-server';
|
||||||
import App from '@sapper/App.html';
|
import { stores } from '@sapper/internal/shared';
|
||||||
|
import Sapper from '@sapper/internal/Sapper.html';
|
||||||
|
|
||||||
export function get_page_handler(
|
export function get_page_handler(
|
||||||
manifest: Manifest,
|
manifest: Manifest,
|
||||||
@@ -229,7 +230,7 @@ export function get_page_handler(
|
|||||||
params: params
|
params: params
|
||||||
});
|
});
|
||||||
|
|
||||||
const { html, head, css } = App.render({
|
const { html, head, css } = Sapper.render({
|
||||||
Root: manifest.root,
|
Root: manifest.root,
|
||||||
props: props,
|
props: props,
|
||||||
session: writable(session)
|
session: writable(session)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IGNORE } from '../placeholders';
|
import { IGNORE } from '../constants';
|
||||||
import { Req, Res, ServerRoute } from './types';
|
import { Req, Res, ServerRoute } from './types';
|
||||||
|
|
||||||
export function get_server_route_handler(routes: ServerRoute[]) {
|
export function get_server_route_handler(routes: ServerRoute[]) {
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { build_dir, dev, manifest, IGNORE } from '../placeholders';
|
import { build_dir, dev, manifest } from '@sapper/internal/manifest-server';
|
||||||
import { Handler, Req, Res, Store } from './types';
|
import { Handler, Req, Res } from './types';
|
||||||
import { get_server_route_handler } from './get_server_route_handler';
|
import { get_server_route_handler } from './get_server_route_handler';
|
||||||
import { get_page_handler } from './get_page_handler';
|
import { get_page_handler } from './get_page_handler';
|
||||||
import { lookup } from './mime';
|
import { lookup } from './mime';
|
||||||
|
import { IGNORE } from '../constants';
|
||||||
|
|
||||||
export default function middleware(opts: {
|
export default function middleware(opts: {
|
||||||
session?: (req: Req, res: Res) => any,
|
session?: (req: Req, res: Res) => any,
|
||||||
@@ -26,10 +26,6 @@ export type Manifest = {
|
|||||||
|
|
||||||
export type Handler = (req: Req, res: Res, next: () => void) => void;
|
export type Handler = (req: Req, res: Res, next: () => void) => void;
|
||||||
|
|
||||||
export type Store = {
|
|
||||||
get: () => any
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Props = {
|
export type Props = {
|
||||||
error?: { message: string };
|
error?: { message: string };
|
||||||
status?: number;
|
status?: number;
|
||||||
@@ -59,7 +55,7 @@ export interface Res extends ServerResponse {
|
|||||||
export { ServerResponse };
|
export { ServerResponse };
|
||||||
|
|
||||||
interface Component {
|
interface Component {
|
||||||
render: (data: any, opts: { store: Store }) => {
|
render: (data: any) => {
|
||||||
head: string;
|
head: string;
|
||||||
css: { code: string, map: any };
|
css: { code: string, map: any };
|
||||||
html: string
|
html: string
|
||||||
@@ -9,6 +9,7 @@ import read_template from '../core/read_template';
|
|||||||
import { CompileResult } from '../core/create_compilers/interfaces';
|
import { CompileResult } from '../core/create_compilers/interfaces';
|
||||||
import { noop } from './utils/noop';
|
import { noop } from './utils/noop';
|
||||||
import validate_bundler from './utils/validate_bundler';
|
import validate_bundler from './utils/validate_bundler';
|
||||||
|
import { copy_runtime } from './utils/copy_runtime';
|
||||||
|
|
||||||
type Opts = {
|
type Opts = {
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
@@ -49,6 +50,7 @@ export async function build({
|
|||||||
|
|
||||||
rimraf.sync(path.join(output, '**/*'));
|
rimraf.sync(path.join(output, '**/*'));
|
||||||
mkdirp.sync(output);
|
mkdirp.sync(output);
|
||||||
|
copy_runtime(output);
|
||||||
|
|
||||||
rimraf.sync(path.join(dest, '**/*'));
|
rimraf.sync(path.join(dest, '**/*'));
|
||||||
mkdirp.sync(`${dest}/client`);
|
mkdirp.sync(`${dest}/client`);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import { copy_shimport } from './utils/copy_shimport';
|
|||||||
import { ManifestData, FatalEvent, ErrorEvent, ReadyEvent, InvalidEvent } from '../interfaces';
|
import { ManifestData, FatalEvent, ErrorEvent, ReadyEvent, InvalidEvent } from '../interfaces';
|
||||||
import read_template from '../core/read_template';
|
import read_template from '../core/read_template';
|
||||||
import { noop } from './utils/noop';
|
import { noop } from './utils/noop';
|
||||||
|
import { copy_runtime } from './utils/copy_runtime';
|
||||||
|
|
||||||
type Opts = {
|
type Opts = {
|
||||||
cwd?: string,
|
cwd?: string,
|
||||||
@@ -147,6 +148,7 @@ class Watcher extends EventEmitter {
|
|||||||
|
|
||||||
rimraf.sync(path.join(output, '**/*'));
|
rimraf.sync(path.join(output, '**/*'));
|
||||||
mkdirp.sync(output);
|
mkdirp.sync(output);
|
||||||
|
copy_runtime(output);
|
||||||
|
|
||||||
rimraf.sync(dest);
|
rimraf.sync(dest);
|
||||||
mkdirp.sync(`${dest}/client`);
|
mkdirp.sync(`${dest}/client`);
|
||||||
|
|||||||
21
src/api/utils/copy_runtime.ts
Normal file
21
src/api/utils/copy_runtime.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
import mkdirp from 'mkdirp';
|
||||||
|
|
||||||
|
const runtime = [
|
||||||
|
'app.mjs',
|
||||||
|
'server.mjs',
|
||||||
|
'internal/shared.mjs',
|
||||||
|
'internal/Sapper.html',
|
||||||
|
'internal/layout.html'
|
||||||
|
].map(file => ({
|
||||||
|
file,
|
||||||
|
source: fs.readFileSync(path.join(__dirname, `../runtime/${file}`), 'utf-8')
|
||||||
|
}));
|
||||||
|
|
||||||
|
export function copy_runtime(output: string) {
|
||||||
|
runtime.forEach(({ file, source }) => {
|
||||||
|
mkdirp.sync(path.dirname(`${output}/${file}`));
|
||||||
|
fs.writeFileSync(`${output}/${file}`, source);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -3,10 +3,6 @@ import * as path from 'path';
|
|||||||
import { posixify, stringify, walk, write_if_changed } from '../utils';
|
import { posixify, stringify, walk, write_if_changed } from '../utils';
|
||||||
import { Page, PageComponent, ManifestData } from '../interfaces';
|
import { Page, PageComponent, ManifestData } from '../interfaces';
|
||||||
|
|
||||||
const app = fs.readFileSync(path.resolve(__dirname, '../templates/App.html'), 'utf-8');
|
|
||||||
const internal = fs.readFileSync(path.resolve(__dirname, '../templates/internal.mjs'), 'utf-8');
|
|
||||||
const layout = fs.readFileSync(path.resolve(__dirname, '../templates/layout.html'), 'utf-8');
|
|
||||||
|
|
||||||
export function create_main_manifests({
|
export function create_main_manifests({
|
||||||
bundler,
|
bundler,
|
||||||
manifest_data,
|
manifest_data,
|
||||||
@@ -30,16 +26,13 @@ export function create_main_manifests({
|
|||||||
}) {
|
}) {
|
||||||
if (!fs.existsSync(output)) fs.mkdirSync(output);
|
if (!fs.existsSync(output)) fs.mkdirSync(output);
|
||||||
|
|
||||||
const path_to_routes = path.relative(output, routes);
|
const path_to_routes = path.relative(`${output}/internal`, routes);
|
||||||
|
|
||||||
const client_manifest = generate_client(manifest_data, path_to_routes, bundler, dev, dev_port);
|
const client_manifest = generate_client_manifest(manifest_data, path_to_routes, bundler, dev, dev_port);
|
||||||
const server_manifest = generate_server(manifest_data, path_to_routes, cwd, src, dest, dev);
|
const server_manifest = generate_server_manifest(manifest_data, path_to_routes, cwd, src, dest, dev);
|
||||||
|
|
||||||
write_if_changed(`${output}/_layout.html`, layout);
|
write_if_changed(`${output}/internal/manifest-client.mjs`, client_manifest);
|
||||||
write_if_changed(`${output}/internal.mjs`, internal);
|
write_if_changed(`${output}/internal/manifest-server.mjs`, server_manifest);
|
||||||
write_if_changed(`${output}/App.html`, app);
|
|
||||||
write_if_changed(`${output}/app.mjs`, client_manifest);
|
|
||||||
write_if_changed(`${output}/server.mjs`, server_manifest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function create_serviceworker_manifest({ manifest_data, output, client_files, static_files }: {
|
export function create_serviceworker_manifest({ manifest_data, output, client_files, static_files }: {
|
||||||
@@ -74,16 +67,13 @@ export function create_serviceworker_manifest({ manifest_data, output, client_fi
|
|||||||
write_if_changed(`${output}/service-worker.js`, code);
|
write_if_changed(`${output}/service-worker.js`, code);
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_client(
|
function generate_client_manifest(
|
||||||
manifest_data: ManifestData,
|
manifest_data: ManifestData,
|
||||||
path_to_routes: string,
|
path_to_routes: string,
|
||||||
bundler: string,
|
bundler: string,
|
||||||
dev: boolean,
|
dev: boolean,
|
||||||
dev_port?: number
|
dev_port?: number
|
||||||
) {
|
) {
|
||||||
const template_file = path.resolve(__dirname, '../templates/app.mjs');
|
|
||||||
const template = fs.readFileSync(template_file, 'utf-8');
|
|
||||||
|
|
||||||
const page_ids = new Set(manifest_data.pages.map(page =>
|
const page_ids = new Set(manifest_data.pages.map(page =>
|
||||||
page.pattern.toString()));
|
page.pattern.toString()));
|
||||||
|
|
||||||
@@ -103,65 +93,59 @@ function generate_client(
|
|||||||
component_indexes[component.name] = i;
|
component_indexes[component.name] = i;
|
||||||
|
|
||||||
return `{
|
return `{
|
||||||
js: () => import(${annotation}${stringify(source)}),
|
js: () => import(${annotation}${stringify(source)}),
|
||||||
css: "__SAPPER_CSS_PLACEHOLDER:${stringify(component.file, false)}__"
|
css: "__SAPPER_CSS_PLACEHOLDER:${stringify(component.file, false)}__"
|
||||||
}`;
|
}`;
|
||||||
}).join(',\n\t\t')}
|
}).join(',\n\t\t\t\t')}
|
||||||
]`.replace(/^\t/gm, '').trim();
|
]`.replace(/^\t/gm, '');
|
||||||
|
|
||||||
let needs_decode = false;
|
let needs_decode = false;
|
||||||
|
|
||||||
let pages = `[
|
let routes = `[
|
||||||
${manifest_data.pages.map(page => `{
|
${manifest_data.pages.map(page => `{
|
||||||
// ${page.parts[page.parts.length - 1].component.file}
|
// ${page.parts[page.parts.length - 1].component.file}
|
||||||
pattern: ${page.pattern},
|
pattern: ${page.pattern},
|
||||||
parts: [
|
parts: [
|
||||||
${page.parts.map(part => {
|
${page.parts.map(part => {
|
||||||
if (part === null) return 'null';
|
if (part === null) return 'null';
|
||||||
|
|
||||||
if (part.params.length > 0) {
|
if (part.params.length > 0) {
|
||||||
needs_decode = true;
|
needs_decode = true;
|
||||||
const props = part.params.map((param, i) => `${param}: d(match[${i + 1}])`);
|
const props = part.params.map((param, i) => `${param}: d(match[${i + 1}])`);
|
||||||
return `{ i: ${component_indexes[part.component.name]}, params: match => ({ ${props.join(', ')} }) }`;
|
return `{ i: ${component_indexes[part.component.name]}, params: match => ({ ${props.join(', ')} }) }`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `{ i: ${component_indexes[part.component.name]} }`;
|
return `{ i: ${component_indexes[part.component.name]} }`;
|
||||||
}).join(',\n\t\t\t\t')}
|
}).join(',\n\t\t\t\t\t\t')}
|
||||||
]
|
]
|
||||||
}`).join(',\n\n\t\t')}
|
}`).join(',\n\n\t\t\t\t')}
|
||||||
]`.replace(/^\t/gm, '').trim();
|
]`.replace(/^\t/gm, '');
|
||||||
|
|
||||||
if (needs_decode) {
|
if (needs_decode) {
|
||||||
pages = `(d => ${pages})(decodeURIComponent)`
|
routes = `(d => ${routes})(decodeURIComponent)`
|
||||||
}
|
}
|
||||||
|
|
||||||
let footer = '';
|
return `
|
||||||
|
// This file is generated by Sapper — do not edit it!
|
||||||
|
export { default as Root } from '${stringify(get_file(path_to_routes, manifest_data.root), false)}';
|
||||||
|
export { preload as root_preload } from '${manifest_data.root.has_preload ? stringify(get_file(path_to_routes, manifest_data.root), false) : './shared'}';
|
||||||
|
export { default as ErrorComponent } from '${stringify(posixify(`${path_to_routes}/_error.html`), false)}';
|
||||||
|
|
||||||
if (dev) {
|
export const ignore = [${server_routes_to_ignore.map(route => route.pattern).join(', ')}];
|
||||||
const sapper_dev_client = posixify(
|
|
||||||
path.resolve(__dirname, '../sapper-dev-client.js')
|
|
||||||
);
|
|
||||||
|
|
||||||
footer = `
|
export const components = ${components};
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
export const routes = ${routes};
|
||||||
import(${stringify(sapper_dev_client)}).then(client => {
|
|
||||||
client.connect(${dev_port});
|
|
||||||
});
|
|
||||||
}`.replace(/^\t{3}/gm, '');
|
|
||||||
}
|
|
||||||
|
|
||||||
return `// This file is generated by Sapper — do not edit it!\n` + template
|
${dev ? `if (typeof window !== 'undefined') {
|
||||||
.replace(/__ROOT__/g, stringify(get_file(path_to_routes, manifest_data.root), false))
|
import(${stringify(posixify(path.resolve(__dirname, '../sapper-dev-client.js')))}).then(client => {
|
||||||
.replace(/__ROOT_PRELOAD__/g, manifest_data.root.has_preload ? stringify(get_file(path_to_routes, manifest_data.root), false) : './internal')
|
client.connect(${dev_port});
|
||||||
.replace(/__ERROR__/g, stringify(posixify(`${path_to_routes}/_error.html`), false))
|
});
|
||||||
.replace(/__IGNORE__/g, `[${server_routes_to_ignore.map(route => route.pattern).join(', ')}]`)
|
}` : ''}
|
||||||
.replace(/__COMPONENTS__/g, components)
|
`.replace(/^\t{2}/gm, '').trim();
|
||||||
.replace(/__PAGES__/g, pages) +
|
|
||||||
footer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_server(
|
function generate_server_manifest(
|
||||||
manifest_data: ManifestData,
|
manifest_data: ManifestData,
|
||||||
path_to_routes: string,
|
path_to_routes: string,
|
||||||
cwd: string,
|
cwd: string,
|
||||||
@@ -169,29 +153,38 @@ function generate_server(
|
|||||||
dest: string,
|
dest: string,
|
||||||
dev: boolean
|
dev: boolean
|
||||||
) {
|
) {
|
||||||
const template_file = path.resolve(__dirname, '../templates/server.mjs');
|
|
||||||
const template = fs.readFileSync(template_file, 'utf-8');
|
|
||||||
|
|
||||||
const imports = [].concat(
|
const imports = [].concat(
|
||||||
manifest_data.server_routes.map(route =>
|
manifest_data.server_routes.map((route, i) =>
|
||||||
`import * as __${route.name} from ${stringify(posixify(`${path_to_routes}/${route.file}`))};`),
|
`import * as route_${i} from ${stringify(posixify(`${path_to_routes}/${route.file}`))};`),
|
||||||
manifest_data.components.map(component =>
|
manifest_data.components.map((component, i) =>
|
||||||
`import __${component.name}${component.has_preload ? `, { preload as __${component.name}_preload }` : ''} from ${stringify(get_file(path_to_routes, component))};`),
|
`import component_${i}${component.has_preload ? `, { preload as preload_${i} }` : ''} from ${stringify(get_file(path_to_routes, component))};`),
|
||||||
`import root${manifest_data.root.has_preload ? `, { preload as root_preload }` : ''} from ${stringify(get_file(path_to_routes, manifest_data.root))};`,
|
`import root${manifest_data.root.has_preload ? `, { preload as root_preload }` : ''} from ${stringify(get_file(path_to_routes, manifest_data.root))};`,
|
||||||
`import error from ${stringify(posixify(`${path_to_routes}/_error.html`))};`
|
`import error from ${stringify(posixify(`${path_to_routes}/_error.html`))};`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const component_lookup: Record<string, number> = {};
|
||||||
|
manifest_data.components.forEach((component, i) => {
|
||||||
|
component_lookup[component.name] = i;
|
||||||
|
});
|
||||||
|
|
||||||
let code = `
|
let code = `
|
||||||
${imports.join('\n')}${manifest_data.root.has_preload ? '' : `\n\nconst root_preload = () => {};`}
|
`.replace(/^\t\t/gm, '').trim();
|
||||||
|
|
||||||
|
const build_dir = posixify(path.relative(cwd, dest));
|
||||||
|
const src_dir = posixify(path.relative(cwd, src));
|
||||||
|
|
||||||
|
return `
|
||||||
|
// This file is generated by Sapper — do not edit it!
|
||||||
|
${imports.join('\n')}
|
||||||
|
|
||||||
const d = decodeURIComponent;
|
const d = decodeURIComponent;
|
||||||
|
|
||||||
export const manifest = {
|
export const manifest = {
|
||||||
server_routes: [
|
server_routes: [
|
||||||
${manifest_data.server_routes.map(route => `{
|
${manifest_data.server_routes.map((route, i) => `{
|
||||||
// ${route.file}
|
// ${route.file}
|
||||||
pattern: ${route.pattern},
|
pattern: ${route.pattern},
|
||||||
handlers: __${route.name},
|
handlers: route_${i},
|
||||||
params: ${route.params.length > 0
|
params: ${route.params.length > 0
|
||||||
? `match => ({ ${route.params.map((param, i) => `${param}: d(match[${i + 1}])`).join(', ')} })`
|
? `match => ({ ${route.params.map((param, i) => `${param}: d(match[${i + 1}])`).join(', ')} })`
|
||||||
: `() => ({})`}
|
: `() => ({})`}
|
||||||
@@ -209,8 +202,8 @@ function generate_server(
|
|||||||
const props = [
|
const props = [
|
||||||
`name: "${part.component.name}"`,
|
`name: "${part.component.name}"`,
|
||||||
`file: ${stringify(part.component.file)}`,
|
`file: ${stringify(part.component.file)}`,
|
||||||
`component: __${part.component.name}`,
|
`component: component_${component_lookup[part.component.name]}`,
|
||||||
part.component.has_preload && `preload: __${part.component.name}_preload`
|
part.component.has_preload && `preload: preload_${component_lookup[part.component.name]}`
|
||||||
].filter(Boolean);
|
].filter(Boolean);
|
||||||
|
|
||||||
if (part.params.length > 0) {
|
if (part.params.length > 0) {
|
||||||
@@ -225,12 +218,16 @@ function generate_server(
|
|||||||
],
|
],
|
||||||
|
|
||||||
root,
|
root,
|
||||||
root_preload,
|
root_preload${manifest_data.root.has_preload ? '' : `: () => {}`},
|
||||||
error
|
error
|
||||||
};`.replace(/^\t\t/gm, '').trim();
|
};
|
||||||
|
|
||||||
const build_dir = posixify(path.relative(cwd, dest));
|
export const build_dir = ${JSON.stringify(build_dir)};
|
||||||
const src_dir = posixify(path.relative(cwd, src));
|
|
||||||
|
export const src_dir = ${JSON.stringify(src_dir)};
|
||||||
|
|
||||||
|
export const dev = ${dev ? 'true' : 'false'};
|
||||||
|
`.replace(/^\t{2}/gm, '').trim();
|
||||||
|
|
||||||
return `// This file is generated by Sapper — do not edit it!\n` + template
|
return `// This file is generated by Sapper — do not edit it!\n` + template
|
||||||
.replace('__BUILD__DIR__', JSON.stringify(build_dir))
|
.replace('__BUILD__DIR__', JSON.stringify(build_dir))
|
||||||
@@ -240,9 +237,6 @@ function generate_server(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get_file(path_to_routes: string, component: PageComponent) {
|
function get_file(path_to_routes: string, component: PageComponent) {
|
||||||
if (component.default) {
|
if (component.default) return `./layout.html`;
|
||||||
return `./_layout.html`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return posixify(`${path_to_routes}/${component.file}`);
|
return posixify(`${path_to_routes}/${component.file}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
import { Manifest } from './types';
|
|
||||||
|
|
||||||
export const manifest: Manifest = __MANIFEST__;
|
|
||||||
|
|
||||||
export const build_dir = __BUILD__DIR__;
|
|
||||||
|
|
||||||
export const src_dir = __SRC__DIR__;
|
|
||||||
|
|
||||||
export const dev = __DEV__;
|
|
||||||
|
|
||||||
export const IGNORE = '__SAPPER__IGNORE__';
|
|
||||||
Reference in New Issue
Block a user