Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Merge branch 'gh-83'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 18, 2018
2 parents 4ca8195 + 792ccf5 commit 25bdcf9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ function create_hot_update_server(port: number, interval = 10000) {
return { send };
}

export default function create_watcher(src: string, dir: string) {
export default async function dev(src: string, dir: string) {
rimraf.sync(dir);
mkdirp.sync(dir);

const chokidar = require('chokidar');

// initial build
const dev_port = await require('get-port')(10000);

const routes = create_routes({ src });
create_app({ routes, src, dev: true });
create_app({ routes, src, dev: true, dev_port });

const hot_update_server = create_hot_update_server(23456); // TODO robustify port selection
const hot_update_server = create_hot_update_server(dev_port);

// TODO watch the configs themselves?
const compilers = create_compilers();
Expand All @@ -93,7 +95,7 @@ export default function create_watcher(src: string, dir: string) {

watch_files('routes/**/*.+(html|js|mjs)', () => {
const routes = create_routes({ src });
create_app({ routes, src, dev: true });
create_app({ routes, src, dev: true, dev_port });
});

watch_files('app/template.html', () => {
Expand Down
11 changes: 5 additions & 6 deletions src/core/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import create_routes from './create_routes';
import { fudge_mtime, posixify, write } from './utils';
import { Route } from '../interfaces';

export default function create_app({ routes, src, dev }: {
export default function create_app({ routes, src, dev, dev_port }: {
routes: Route[];
src: string;
dev: boolean;
dev_port: number;
}) {
mkdirp.sync('app/manifest');

write('app/manifest/client.js', generate_client(routes, src, dev));
write('app/manifest/client.js', generate_client(routes, src, dev, dev_port));
write('app/manifest/server.js', generate_server(routes, src));
}

function generate_client(routes: Route[], src: string, dev: boolean) {
function generate_client(routes: Route[], src: string, dev: boolean, dev_port?: number) {
let code = `
// This file is generated by Sapper — do not edit it!
export const routes = [
Expand All @@ -43,13 +44,11 @@ function generate_client(routes: Route[], src: string, dev: boolean) {
path.resolve(__dirname, 'src/hmr-client.js')
);

const PORT = 23456; // TODO robustify this — needs to be controlled by the dev task

code += `
if (module.hot) {
import('${hmr_client}').then(client => {
client.connect(${PORT});
client.connect(${dev_port});
});
}`.replace(/^\t{3}/gm, '');
}
Expand Down

0 comments on commit 25bdcf9

Please sign in to comment.