mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-19 05:45:27 +00:00
create all routes simultaneously, differentiate with type property
This commit is contained in:
112
connect.js
112
connect.js
@@ -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,81 +13,78 @@ 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);
|
||||||
|
|
||||||
const page = opts.template({
|
if (route.type === 'page') {
|
||||||
main,
|
const main = await webpack_compiler.client_main;
|
||||||
html: Component.render({
|
|
||||||
params,
|
|
||||||
query: req.query
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
res.status(200);
|
const page = opts.template({
|
||||||
res.set({
|
main,
|
||||||
// TODO etag stuff
|
html: mod.default.render({
|
||||||
'Content-Length': page.length,
|
params: req.params,
|
||||||
'Content-Type': 'text/html'
|
query: req.query
|
||||||
});
|
})
|
||||||
res.end(page);
|
});
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < server_route_matchers.length; i += 1) {
|
res.status(200);
|
||||||
const matcher = server_route_matchers[i];
|
res.set({
|
||||||
if (matcher.test(url)) {
|
// TODO etag stuff
|
||||||
req.params = matcher.exec(url);
|
'Content-Length': page.length,
|
||||||
const route = esmRequire(`${routes}/${matcher.file}`);
|
'Content-Type': 'text/html'
|
||||||
|
});
|
||||||
const handler = route[req.method.toLowerCase()];
|
res.end(page);
|
||||||
if (handler) {
|
|
||||||
if (handler.length === 2) {
|
|
||||||
handler(req, res);
|
|
||||||
} else {
|
|
||||||
const data = await handler(req);
|
|
||||||
|
|
||||||
// TODO headers, error handling
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
res.end(data);
|
|
||||||
} else {
|
|
||||||
res.end(JSON.stringify(data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
const handler = mod[req.method.toLowerCase()];
|
||||||
|
if (handler) {
|
||||||
|
if (handler.length === 2) {
|
||||||
|
handler(req, res);
|
||||||
|
} else {
|
||||||
|
const data = await handler(req);
|
||||||
|
|
||||||
|
// TODO headers, error handling
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
res.end(data);
|
||||||
|
} else {
|
||||||
|
res.end(JSON.stringify(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 `
|
||||||
|
|||||||
Reference in New Issue
Block a user