rebundle when routes and templates change

This commit is contained in:
Rich Harris
2017-12-20 17:34:23 -05:00
parent cd91bf2ca4
commit fefb0d96d7
9 changed files with 257 additions and 73 deletions

View File

@@ -1,16 +1,30 @@
const glob = require('glob');
const chokidar = require('chokidar');
const create_routes = require('./utils/create_routes.js');
const { src } = require('./config.js');
const { src, dev } = require('./config.js');
const route_manager = {
routes: create_routes(
glob.sync('**/*.+(html|js|mjs)', { cwd: src })
),
const callbacks = [];
onchange(fn) {
// TODO in dev mode, keep this updated, and allow
// webpack compiler etc to hook into it
}
exports.onchange = fn => {
callbacks.push(fn);
};
module.exports = route_manager;
function update() {
exports.routes = create_routes(
glob.sync('**/*.+(html|js|mjs)', { cwd: src })
);
callbacks.forEach(fn => fn());
}
update();
if (dev) {
const watcher = chokidar.watch(`${src}/**/*.+(html|js|mjs)`, {
ignoreInitial: true
});
watcher.on('add', update);
watcher.on('change', update);
watcher.on('unlink', update);
}