basic sapper dev task, with HMR

This commit is contained in:
Rich Harris
2018-02-17 17:44:57 -05:00
parent d95f52f8e9
commit ab1ca60363
9 changed files with 248 additions and 179 deletions

View File

@@ -18,7 +18,8 @@ export default function create_app({ routes, src, dev }: {
function generate_client(routes: Route[], src: string, dev: boolean) {
let code = `
// This file is generated by Sapper — do not edit it!\nexport const routes = [
// This file is generated by Sapper — do not edit it!
export const routes = [
${routes
.filter(route => route.type === 'page')
.map(route => {
@@ -34,10 +35,18 @@ function generate_client(routes: Route[], src: string, dev: boolean) {
if (dev) {
const hmr_client = posixify(
require.resolve(`webpack-hot-middleware/client`)
path.resolve(__dirname, 'src/hmr-client.js')
);
code += `\n\nimport('${hmr_client}?path=/__webpack_hmr&timeout=20000'); if (module.hot) module.hot.accept();`;
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});
});
}`.replace(/^\t{3}/gm, '');
}
return code;