mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-20 06:15:15 +00:00
render with params
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const tmp = require('tmp');
|
|
||||||
|
|
||||||
const template = fs.readFileSync(path.resolve(__dirname, '../templates/main.js'), 'utf-8');
|
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 => {
|
.map(matcher => {
|
||||||
const condition = matcher.dynamic.length === 0 ?
|
const condition = matcher.dynamic.length === 0 ?
|
||||||
`url.pathname === '/${matcher.parts.join('/')}'` :
|
`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 `
|
return `
|
||||||
if (${condition}) {
|
if (${condition}) {
|
||||||
// TODO set params, if applicable
|
${lines.join(`\n\t\t\t\t\t`)}
|
||||||
import('${routes}/${matcher.file}').then(render);
|
|
||||||
}
|
}
|
||||||
`.replace(/^\t{3}/gm, '').trim();
|
`.replace(/^\t{3}/gm, '').trim();
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user