expose main.js

This commit is contained in:
Rich Harris
2017-12-16 15:40:22 -05:00
parent dd1f2d79ff
commit bd7f6e2b1a
4 changed files with 20 additions and 23 deletions

View File

@@ -1,26 +1,26 @@
const fs = require('fs');
const path = require('path');
const { main_built } = require('../config.js');
const template = fs.readFileSync(path.resolve(__dirname, '../../templates/main.js'), 'utf-8');
const template = fs.readFileSync('templates/main.js', 'utf-8');
module.exports = function create_app(src, dest, routes, options) {
// TODO in dev mode, watch files
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(', ') } }`;
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(',\n\t');
return `{ pattern: ${route.pattern}, params: match => (${params}), load: () => import(/* webpackChunkName: "${route.id}" */ '${src}/${route.file}') }`
})
.join(', ')
}]`;
const main = template
.replace('__app__', path.resolve(__dirname, '../../runtime/app.js'))
.replace('__selector__', options.selector || 'main')
.replace('__routes__', code);
const main = template.replace('__routes__', code);
fs.writeFileSync(path.join(dest, 'main.js'), main);
fs.writeFileSync(main_built, main);
};