serve assets from memory, use caching

This commit is contained in:
Rich Harris
2017-12-15 17:20:24 -05:00
parent 892b18cf80
commit 80ae909b73
4 changed files with 90 additions and 66 deletions

View File

@@ -13,7 +13,7 @@ module.exports = function create_app(src, dest, routes, options) {
'{}' :
`{ ${route.dynamic.map((part, i) => `${part}: match[${i + 1}]`).join(', ') } }`;
return `{ pattern: ${route.pattern}, params: match => (${params}), load: () => import('${src}/${route.file}') }`
return `{ pattern: ${route.pattern}, params: match => (${params}), load: () => import(/* webpackChunkName: "${route.id}" */ '${src}/${route.file}') }`
})
.join(',\n\t');

View File

@@ -44,6 +44,11 @@ module.exports = function create_webpack_compiler(dest, routes, dev) {
compiler.client_main = `/client/${info.assetsByChunkName.main}`;
compiler.assets = info.assets.map(asset => `/client/${asset.name}`);
compiler.asset_cache = {};
compiler.assets.forEach(file => {
compiler.asset_cache[file] = fs.readFileSync(path.join(dest, file), 'utf-8');
});
fulfil();
});
}),
@@ -59,7 +64,6 @@ module.exports = function create_webpack_compiler(dest, routes, dev) {
}
compiler.chunks = info.assetsByChunkName;
fulfil();
});
})
@@ -73,22 +77,22 @@ module.exports = function create_webpack_compiler(dest, routes, dev) {
.join(', ')
}]`;
const service_worker = fs.readFileSync('templates/service-worker.js', 'utf-8')
compiler.service_worker = fs.readFileSync('templates/service-worker.js', 'utf-8')
.replace('__timestamp__', Date.now())
.replace('__assets__', JSON.stringify(assets))
.replace('__shell__', JSON.stringify(compiler.assets.concat('/index.html')))
.replace('__routes__', route_code);
fs.writeFileSync(path.resolve(dest, 'service-worker.js'), service_worker);
const shell = templates.render(200, {
compiler.shell = templates.render(200, {
styles: '',
head: '',
html: '<noscript>Please enable JavaScript!</noscript>',
main: compiler.client_main
});
fs.writeFileSync(path.resolve(dest, 'index.html'), shell);
// useful for debugging, but the files are served from memory
fs.writeFileSync(path.resolve(dest, 'service-worker.js'), compiler.service_worker);
fs.writeFileSync(path.resolve(dest, 'index.html'), compiler.shell);
});
compiler.get_chunk = async id => {