create all routes simultaneously, differentiate with type property

This commit is contained in:
Rich Harris
2017-12-12 11:42:06 -05:00
parent 2549477e05
commit 86292d119b
2 changed files with 64 additions and 65 deletions

View File

@@ -3,7 +3,8 @@ const esm = require('@std/esm');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const glob = require('glob'); const glob = require('glob');
const create_matchers = require('./utils/create_matchers.js'); const rimraf = require('rimraf');
const create_routes = require('./utils/create_routes.js');
const create_app = require('./utils/create_app.js'); const create_app = require('./utils/create_app.js');
const create_webpack_compiler = require('./utils/create_webpack_compiler.js'); const create_webpack_compiler = require('./utils/create_webpack_compiler.js');
@@ -12,43 +13,46 @@ const esmRequire = esm(module, {
}); });
module.exports = function connect(opts) { module.exports = function connect(opts) {
const routes = path.resolve('routes'); const src = path.resolve('routes');
const out = path.resolve('.sapper'); const dest = path.resolve(opts.tmpDir || '.sapper');
let pages = glob.sync('**/*.html', { cwd: routes }); rimraf.sync(dest);
let page_matchers = create_matchers(pages); fs.mkdirSync(dest);
let server_routes = glob.sync('**/*.+(js|mjs)', { cwd: routes }); let routes = create_routes(
let server_route_matchers = create_matchers(server_routes); glob.sync('**/*.+(html|js|mjs)', { cwd: src })
);
create_app(routes, out, page_matchers, opts); create_app(src, dest, routes, opts);
const webpack_compiler = create_webpack_compiler( const webpack_compiler = create_webpack_compiler(
path.join(out, 'main.js'), dest,
path.resolve('.sapper/webpack'), routes,
opts.dev opts.dev
); );
return async function(req, res, next) { return async function(req, res, next) {
const url = req.url.replace(/\?.+/, ''); const url = req.url.replace(/\?.+/, '');
if (url.startsWith('/webpack/')) { if (url.startsWith('/client/')) {
fs.createReadStream(path.resolve('.sapper' + url)).pipe(res); fs.createReadStream(`${dest}${url}`).pipe(res);
return; return;
} }
for (let i = 0; i < page_matchers.length; i += 1) { for (const route of routes) {
const matcher = page_matchers[i]; if (route.test(url)) {
if (matcher.test(url)) { req.params = route.exec(url);
const params = matcher.exec(url);
const Component = require(`${routes}/${matcher.file}`);
const main = await webpack_compiler.app; const chunk = await webpack_compiler.get_chunk(route.id);
const mod = require(chunk);
if (route.type === 'page') {
const main = await webpack_compiler.client_main;
const page = opts.template({ const page = opts.template({
main, main,
html: Component.render({ html: mod.default.render({
params, params: req.params,
query: req.query query: req.query
}) })
}); });
@@ -60,17 +64,10 @@ module.exports = function connect(opts) {
'Content-Type': 'text/html' 'Content-Type': 'text/html'
}); });
res.end(page); res.end(page);
return;
}
} }
for (let i = 0; i < server_route_matchers.length; i += 1) { else {
const matcher = server_route_matchers[i]; const handler = mod[req.method.toLowerCase()];
if (matcher.test(url)) {
req.params = matcher.exec(url);
const route = esmRequire(`${routes}/${matcher.file}`);
const handler = route[req.method.toLowerCase()];
if (handler) { if (handler) {
if (handler.length === 2) { if (handler.length === 2) {
handler(req, res); handler(req, res);
@@ -84,11 +81,12 @@ module.exports = function connect(opts) {
res.end(JSON.stringify(data)); res.end(JSON.stringify(data));
} }
} }
}
}
return; return;
} }
} }
}
next(); next();
}; };

View File

@@ -3,25 +3,26 @@ const path = require('path');
const template = fs.readFileSync(path.resolve(__dirname, '../templates/main.js'), 'utf-8'); const template = fs.readFileSync(path.resolve(__dirname, '../templates/main.js'), 'utf-8');
module.exports = function create_app(routes, dest, matchers, options) { module.exports = function create_app(src, dest, routes, options) {
// TODO in dev mode, watch files // TODO in dev mode, watch files
const code = matchers const code = routes
.map(matcher => { .filter(route => route.type === 'page')
const condition = matcher.dynamic.length === 0 ? .map(route => {
`url.pathname === '/${matcher.parts.join('/')}'` : const condition = route.dynamic.length === 0 ?
`match = ${matcher.pattern}.exec(url.pathname)`; `url.pathname === '/${route.parts.join('/')}'` :
`match = ${route.pattern}.exec(url.pathname)`;
const lines = []; const lines = [];
matcher.dynamic.forEach((part, i) => { route.dynamic.forEach((part, i) => {
lines.push( lines.push(
`params.${part} = match[${i + 1}];` `params.${part} = match[${i + 1}];`
); );
}); });
lines.push( lines.push(
`import('${routes}/${matcher.file}').then(render);` `import('${src}/${route.file}').then(render);`
); );
return ` return `