From bd7f6e2b1a37d7531d7ed187e67df07a9604c71a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 16 Dec 2017 15:40:22 -0500 Subject: [PATCH] expose main.js --- lib/config.js | 4 +++- lib/utils/create_app.js | 30 +++++++++++++++--------------- templates/main.js | 5 ----- webpack/config.js | 4 ++-- 4 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 templates/main.js diff --git a/lib/config.js b/lib/config.js index b2cde20..2376c07 100644 --- a/lib/config.js +++ b/lib/config.js @@ -9,4 +9,6 @@ exports.src = path.resolve(process.env.SAPPER_ROUTES || 'routes'); exports.dest = path.resolve( process.env.NOW ? '/tmp' : process.env.SAPPER_DEST || '.sapper' -); \ No newline at end of file +); + +exports.main_built = path.resolve('templates/.main.tmp.js'); \ No newline at end of file diff --git a/lib/utils/create_app.js b/lib/utils/create_app.js index f7a3fe9..024028c 100644 --- a/lib/utils/create_app.js +++ b/lib/utils/create_app.js @@ -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); }; \ No newline at end of file diff --git a/templates/main.js b/templates/main.js deleted file mode 100644 index 252b60f..0000000 --- a/templates/main.js +++ /dev/null @@ -1,5 +0,0 @@ -import app from '__app__'; - -app.init(document.querySelector('__selector__'), [ - __routes__ -]); \ No newline at end of file diff --git a/webpack/config.js b/webpack/config.js index f459a63..a718851 100644 --- a/webpack/config.js +++ b/webpack/config.js @@ -1,6 +1,6 @@ const path = require('path'); const route_manager = require('../lib/route_manager.js'); -const { src, dest, dev } = require('../lib/config.js'); +const { src, dest, dev, main_built } = require('../lib/config.js'); module.exports = { dev, @@ -8,7 +8,7 @@ module.exports = { client: { entry: () => { return { - main: `${dest}/main.js` + main: main_built }; },