move app logic into templates (#444)

This commit is contained in:
Rich Harris
2018-09-30 10:30:00 -04:00
parent ff24877d8f
commit e30842caa8
19 changed files with 661 additions and 618 deletions

View File

@@ -10,7 +10,7 @@ export function create_main_manifests({ bundler, manifest_data, dev_port }: {
manifest_data: ManifestData;
dev_port?: number;
}) {
const manifest_dir = path.join(locations.src(), 'manifest');
const manifest_dir = path.join(locations.src(), '__sapper__');
if (!fs.existsSync(manifest_dir)) fs.mkdirSync(manifest_dir);
const path_to_routes = path.relative(manifest_dir, locations.routes());
@@ -55,7 +55,7 @@ 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(`${locations.src()}/manifest/service-worker.js`, code);
write_if_changed(`${locations.src()}/__sapper__/service-worker.js`, code);
}
function generate_client(
@@ -64,6 +64,9 @@ function generate_client(
bundler: string,
dev_port?: number
) {
const template_file = path.resolve(__dirname, '../templates/dist/client.js');
const template = fs.readFileSync(template_file, 'utf-8');
const page_ids = new Set(manifest_data.pages.map(page =>
page.pattern.toString()));
@@ -71,7 +74,6 @@ function generate_client(
!page_ids.has(route.pattern.toString()));
let code = `
// This file is generated by Sapper — do not edit it!
import root from ${stringify(get_file(path_to_routes, manifest_data.root))};
import error from ${stringify(posixify(`${path_to_routes}/_error.html`))};
@@ -90,7 +92,7 @@ function generate_client(
};`;
}).join('\n')}
export const manifest = {
const manifest = {
ignore: [${server_routes_to_ignore.map(route => route.pattern).join(', ')}],
pages: [
@@ -115,10 +117,7 @@ function generate_client(
root,
error
};
// this is included for legacy reasons
export const routes = {};`.replace(/^\t\t/gm, '').trim();
};`.replace(/^\t\t/gm, '').trim();
if (dev()) {
const sapper_dev_client = posixify(
@@ -132,13 +131,17 @@ function generate_client(
});`.replace(/^\t{3}/gm, '');
}
return code;
return `// This file is generated by Sapper — do not edit it!\n` + template
.replace(/const manifest = __MANIFEST__;/, code);
}
function generate_server(
manifest_data: ManifestData,
path_to_routes: string
) {
const template_file = path.resolve(__dirname, '../templates/dist/server.js');
const template = fs.readFileSync(template_file, 'utf-8');
const imports = [].concat(
manifest_data.server_routes.map(route =>
`import * as ${route.name} from ${stringify(posixify(`${path_to_routes}/${route.file}`))};`),
@@ -149,7 +152,6 @@ function generate_server(
);
let code = `
// This file is generated by Sapper — do not edit it!
${imports.join('\n')}
const d = decodeURIComponent;
@@ -199,7 +201,11 @@ function generate_server(
// this is included for legacy reasons
export const routes = {};`.replace(/^\t\t/gm, '').trim();
return code;
return `// This file is generated by Sapper — do not edit it!\n` + template
.replace('__BUILD__DIR__', JSON.stringify(locations.dest()))
.replace('__SRC__DIR__', JSON.stringify(locations.src()))
.replace('__DEV__', dev() ? 'true' : 'false')
.replace(/const manifest = __MANIFEST__;/, code);
}
function get_file(path_to_routes: string, component: PageComponent) {