remove all hard-coded locations (#181)

This commit is contained in:
Rich Harris
2018-03-11 13:43:11 -04:00
parent bdc248f09a
commit 1d71b86c0f
3 changed files with 17 additions and 15 deletions

View File

@@ -95,12 +95,12 @@ export async function dev(opts: { port: number }) {
const hot_update_server = create_hot_update_server(dev_port);
watch_files('routes/**/*', ['add', 'unlink'], () => {
watch_files(`${locations.routes()}/**/*`, ['add', 'unlink'], () => {
const routes = create_routes();
create_main_manifests({ routes, dev_port });
});
watch_files('app/template.html', ['change'], () => {
watch_files(`${locations.app()}/template.html`, ['change'], () => {
hot_update_server.send({
action: 'reload'
});

View File

@@ -3,18 +3,20 @@ import * as path from 'path';
import * as glob from 'glob';
import create_routes from './create_routes';
import { posixify, write_if_changed } from './utils';
import { dev } from '../config';
import { dev, locations } from '../config';
import { Route } from '../interfaces';
export function create_main_manifests({ routes, dev_port }: {
routes: Route[];
dev_port?: number;
}) {
const client_manifest = generate_client(routes, dev_port);
const server_manifest = generate_server(routes);
const path_to_routes = path.relative(`${locations.app()}/manifest`, locations.routes());
write_if_changed(`app/manifest/client.js`, client_manifest);
write_if_changed(`app/manifest/server.js`, server_manifest);
const client_manifest = generate_client(routes, path_to_routes, dev_port);
const server_manifest = generate_server(routes, path_to_routes);
write_if_changed(`${locations.app()}/manifest/client.js`, client_manifest);
write_if_changed(`${locations.app()}/manifest/server.js`, server_manifest);
}
export function create_serviceworker_manifest({ routes, client_files }: {
@@ -34,10 +36,10 @@ export function create_serviceworker_manifest({ routes, client_files }: {
export const routes = [\n\t${routes.filter((r: Route) => r.type === 'page' && !/^_[45]xx$/.test(r.id)).map((r: Route) => `{ pattern: ${r.pattern} }`).join(',\n\t')}\n];
`.replace(/^\t\t/gm, '').trim();
write_if_changed('app/manifest/service-worker.js', code);
write_if_changed(`${locations.app()}/manifest/service-worker.js`, code);
}
function generate_client(routes: Route[], dev_port?: number) {
function generate_client(routes: Route[], path_to_routes: string, dev_port?: number) {
let code = `
// This file is generated by Sapper — do not edit it!
export const routes = [
@@ -47,7 +49,7 @@ function generate_client(routes: Route[], dev_port?: number) {
return `{ pattern: ${route.pattern}, ignore: true }`;
}
const file = posixify(`../../routes/${route.file}`);
const file = posixify(`${path_to_routes}/${route.file}`);
if (route.id === '_4xx' || route.id === '_5xx') {
return `{ error: '${route.id.slice(1)}', load: () => import(/* webpackChunkName: "${route.id}" */ '${file}') }`;
@@ -79,12 +81,12 @@ function generate_client(routes: Route[], dev_port?: number) {
return code;
}
function generate_server(routes: Route[]) {
function generate_server(routes: Route[], path_to_routes: string) {
let code = `
// This file is generated by Sapper — do not edit it!
${routes
.map(route => {
const file = posixify(`../../routes/${route.file}`);
const file = posixify(`${path_to_routes}/${route.file}`);
return route.type === 'page'
? `import ${route.id} from '${file}';`
: `import * as ${route.id} from '${file}';`;

View File

@@ -6,7 +6,7 @@ export default {
client: {
entry: () => {
return {
main: './app/client'
main: `${locations.app()}/client`
};
},
@@ -23,7 +23,7 @@ export default {
server: {
entry: () => {
return {
server: './app/server'
server: `${locations.app()}/server`
};
},
@@ -40,7 +40,7 @@ export default {
serviceworker: {
entry: () => {
return {
'service-worker': './app/service-worker'
'service-worker': `${locations.app()}/service-worker`
};
},