From 2f51435d93626bfb47bb42fd9140483d5f78e14d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 11 Dec 2017 17:05:01 -0500 Subject: [PATCH] render with params --- utils/create_app.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/utils/create_app.js b/utils/create_app.js index fcb7cd4..c80d400 100644 --- a/utils/create_app.js +++ b/utils/create_app.js @@ -1,6 +1,5 @@ const fs = require('fs'); const path = require('path'); -const tmp = require('tmp'); const template = fs.readFileSync(path.resolve(__dirname, '../templates/main.js'), 'utf-8'); @@ -11,12 +10,23 @@ module.exports = function create_app(routes, dest, matchers, dev) { .map(matcher => { const condition = matcher.dynamic.length === 0 ? `url.pathname === '/${matcher.parts.join('/')}'` : - `${matcher.pattern}.test(url.pathname)`; + `match = ${matcher.pattern}.exec(url.pathname)`; + + const lines = []; + + matcher.dynamic.forEach((part, i) => { + lines.push( + `params.${part} = match[${i + 1}];` + ); + }); + + lines.push( + `import('${routes}/${matcher.file}').then(render);` + ); return ` if (${condition}) { - // TODO set params, if applicable - import('${routes}/${matcher.file}').then(render); + ${lines.join(`\n\t\t\t\t\t`)} } `.replace(/^\t{3}/gm, '').trim(); })