mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 20:34:44 +00:00
shuffle things around
This commit is contained in:
129
lib/index.js
Normal file
129
lib/index.js
Normal file
@@ -0,0 +1,129 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
const rimraf = require('rimraf');
|
||||
const mkdirp = require('mkdirp');
|
||||
const create_routes = require('./utils/create_routes.js');
|
||||
const templates = require('./templates.js');
|
||||
const create_app = require('./utils/create_app.js');
|
||||
const create_compiler = require('./utils/create_compiler.js');
|
||||
const escape_html = require('escape-html');
|
||||
const { src, dest, dev } = require('./config.js');
|
||||
|
||||
module.exports = function connect(opts) {
|
||||
mkdirp(dest);
|
||||
rimraf.sync(path.join(dest, '**/*'));
|
||||
|
||||
let routes = create_routes(
|
||||
glob.sync('**/*.+(html|js|mjs)', { cwd: src })
|
||||
);
|
||||
|
||||
create_app(src, dest, routes, opts);
|
||||
|
||||
const compiler = create_compiler(
|
||||
dest,
|
||||
routes,
|
||||
dev
|
||||
);
|
||||
|
||||
return async function(req, res, next) {
|
||||
const url = req.url.replace(/\?.+/, '');
|
||||
|
||||
if (url === '/service-worker.js') {
|
||||
await compiler.ready;
|
||||
res.set({
|
||||
'Content-Type': 'application/javascript',
|
||||
'Cache-Control': dev ? 'no-cache' : 'max-age=600'
|
||||
});
|
||||
res.end(compiler.service_worker);
|
||||
}
|
||||
|
||||
else if (url === '/index.html') {
|
||||
await compiler.ready;
|
||||
res.set({
|
||||
'Content-Type': 'text/html',
|
||||
'Cache-Control': dev ? 'no-cache' : 'max-age=600'
|
||||
});
|
||||
res.end(compiler.shell);
|
||||
}
|
||||
|
||||
else if (url.startsWith('/client/')) {
|
||||
await compiler.ready;
|
||||
res.set({
|
||||
'Content-Type': 'application/javascript',
|
||||
'Cache-Control': 'max-age=31536000'
|
||||
});
|
||||
res.end(compiler.asset_cache[url]);
|
||||
}
|
||||
|
||||
else {
|
||||
// whatever happens, we're going to serve some HTML
|
||||
res.set({
|
||||
'Content-Type': 'text/html'
|
||||
});
|
||||
|
||||
try {
|
||||
for (const route of routes) {
|
||||
if (route.test(url)) {
|
||||
await compiler.ready;
|
||||
|
||||
req.params = route.exec(url);
|
||||
|
||||
const mod = require(compiler.server_routes)[route.id];
|
||||
|
||||
if (route.type === 'page') {
|
||||
let data = { params: req.params, query: req.query };
|
||||
if (mod.preload) data = Object.assign(data, await mod.preload(data));
|
||||
|
||||
const { html, head, css } = mod.render(data);
|
||||
|
||||
const page = templates.render(200, {
|
||||
main: compiler.client_main,
|
||||
html,
|
||||
head: `<noscript id='sapper-head-start'></noscript>${head}<noscript id='sapper-head-end'></noscript>`,
|
||||
styles: (css && css.code ? `<style>${css.code}</style>` : '')
|
||||
});
|
||||
|
||||
res.status(200);
|
||||
res.end(page);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
res.status(404).end(templates.render(404, {
|
||||
title: 'Not found',
|
||||
status: 404,
|
||||
method: req.method,
|
||||
url
|
||||
}));
|
||||
} catch(err) {
|
||||
res.status(500).end(templates.render(500, {
|
||||
title: (err && err.name) || 'Internal server error',
|
||||
url,
|
||||
error: escape_html(err && (err.details || err.message || err) || 'Unknown error'),
|
||||
stack: err && err.stack.split('\n').slice(1).join('\n')
|
||||
}));
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -2,6 +2,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
const webpack = require('webpack');
|
||||
const hot_middleware = require('webpack-hot-middleware');
|
||||
const { dev } = require('../config.js');
|
||||
const templates = require('../templates.js');
|
||||
|
||||
@@ -77,6 +78,8 @@ module.exports = function create_webpack_compiler(dest, routes, dev) {
|
||||
}
|
||||
|
||||
if (dev) {
|
||||
compiler.hot_middleware = hot_middleware(client);
|
||||
|
||||
let client_is_ready = false;
|
||||
let server_is_ready = false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user