run webpack in watch mode during dev

This commit is contained in:
Rich Harris
2017-12-16 16:50:46 -05:00
parent 71ed3864b7
commit 5995b7ae6a
6 changed files with 54 additions and 31 deletions

View File

@@ -1,30 +1,51 @@
const fs = require('fs');
const path = require('path');
const { main_built } = require('../config.js');
const template = fs.readFileSync('templates/main.js', 'utf-8');
const { dest, main_built, server_routes } = require('../config.js');
module.exports = function create_app(src, dest, routes, options) {
// TODO in dev mode, watch files
function create_client_main() {
const template = fs.readFileSync('templates/main.js', 'utf-8');
const code = `[${
routes
.filter(route => route.type === 'page')
const code = `[${
routes
.filter(route => route.type === 'page')
.map(route => {
const params = route.dynamic.length === 0 ?
'{}' :
`{ ${route.dynamic.map((part, i) => `${part}: match[${i + 1}]`).join(', ') } }`;
return `{ pattern: ${route.pattern}, params: match => (${params}), load: () => import(/* webpackChunkName: "${route.id}" */ '${src}/${route.file}') }`
})
.join(', ')
}]`;
const main = template.replace('__routes__', code);
fs.writeFileSync(main_built, main);
// need to fudge the mtime, because webpack is soft in the head
const stats = fs.statSync(main_built);
fs.utimesSync(main_built, stats.atimeMs - 9999, stats.mtimeMs - 9999);
}
function create_server_routes() {
const imports = routes
.map(route => {
const params = route.dynamic.length === 0 ?
'{}' :
`{ ${route.dynamic.map((part, i) => `${part}: match[${i + 1}]`).join(', ') } }`;
return `{ pattern: ${route.pattern}, params: match => (${params}), load: () => import(/* webpackChunkName: "${route.id}" */ '${src}/${route.file}') }`
return route.type === 'page' ?
`import ${route.id} from '${src}/${route.file}';` :
`import * as ${route.id} from '${src}/${route.file}';`;
})
.join(', ')
}]`;
.join('\n');
const main = template.replace('__routes__', code);
const exports = `export { ${routes.map(route => route.id)} };`;
fs.writeFileSync(main_built, main);
fs.writeFileSync(server_routes, `${imports}\n\n${exports}`);
// need to fudge the mtime, because webpack is soft in the head
const stats = fs.statSync(main_built);
fs.utimesSync(main_built, stats.atimeMs - 9999, stats.mtimeMs - 9999);
const stats = fs.statSync(server_routes);
fs.utimesSync(server_routes, stats.atimeMs - 9999, stats.mtimeMs - 9999);
}
// TODO in dev mode, watch files
create_client_main();
create_server_routes();
};