mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-14 12:04:39 +00:00
start generating client-side bundle. WIP
This commit is contained in:
27
utils/create_app.js
Normal file
27
utils/create_app.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const tmp = require('tmp');
|
||||
|
||||
const template = fs.readFileSync(path.resolve(__dirname, '../templates/main.js'), 'utf-8');
|
||||
|
||||
module.exports = function create_app(routes, dest, matchers, dev) {
|
||||
// TODO in dev mode, watch files
|
||||
|
||||
const code = matchers
|
||||
.map(matcher => {
|
||||
const condition = matcher.dynamic.length === 0 ?
|
||||
`url.pathname === '/${matcher.parts.join('/')}'` :
|
||||
`${matcher.pattern}.test(url.pathname)`;
|
||||
|
||||
return `
|
||||
if (${condition}) {
|
||||
import('../routes/${matcher.file}').then(render);
|
||||
}
|
||||
`.replace(/^\t{3}/gm, '').trim();
|
||||
})
|
||||
.join(' else ');
|
||||
|
||||
const main = template.replace('// ROUTES', code);
|
||||
|
||||
fs.writeFileSync(path.join(dest, 'main.js'), main);
|
||||
};
|
||||
Reference in New Issue
Block a user