diff --git a/src/cli/dev.ts b/src/cli/dev.ts index 4fe6ea6fb..421f1bd71 100644 --- a/src/cli/dev.ts +++ b/src/cli/dev.ts @@ -86,22 +86,23 @@ export default async function dev() { // TODO watch the configs themselves? const compilers = create_compilers(); - function watch_files(pattern: string, callback: () => void) { + function watch_files(pattern: string, events: string[], callback: () => void) { const watcher = chokidar.watch(pattern, { - persistent: false + persistent: true, + ignoreInitial: true }); - watcher.on('add', callback); - watcher.on('change', callback); - watcher.on('unlink', callback); + events.forEach(event => { + watcher.on(event, callback); + }); } - watch_files('routes/**/*.+(html|js|mjs)', () => { + watch_files('routes/**/*', ['add', 'unlink'], () => { const routes = create_routes(); create_app({ routes, dev_port }); }); - watch_files('app/template.html', () => { + watch_files('app/template.html', ['change'], () => { const template = create_template(); // TODO reload current page? }); diff --git a/src/core/create_app.ts b/src/core/create_app.ts index 38a02d19c..79165fff0 100644 --- a/src/core/create_app.ts +++ b/src/core/create_app.ts @@ -6,14 +6,28 @@ import { fudge_mtime, posixify, write } from './utils'; import { dev } from '../config'; import { Route } from '../interfaces'; +// in dev mode, we avoid touching the fs unnecessarily +let last_client_manifest: string = null; +let last_server_manifest: string = null; + export default function create_app({ routes, dev_port }: { routes: Route[]; dev_port: number; }) { mkdirp.sync('app/manifest'); - write('app/manifest/client.js', generate_client(routes, dev_port)); - write('app/manifest/server.js', generate_server(routes)); + const client_manifest = generate_client(routes, dev_port); + const server_manifest = generate_server(routes); + + if (client_manifest !== last_client_manifest) { + write(`app/manifest/client.js`, client_manifest); + last_client_manifest = client_manifest; + } + + if (server_manifest !== last_server_manifest) { + write(`app/manifest/server.js`, server_manifest); + last_server_manifest = server_manifest; + } } function generate_client(routes: Route[], dev_port?: number) {