compiler.plugin('after-compile', (compilation, callback) => { if (process.env.NODE_ENV === 'development') { clearRequireCache(entry); } let app; /* eslint global-require:0 */ try { app = require(entry); } catch (err) { callback(err); return; } if (typeof app !== 'function') { callback(new Error('server.entry MUST BE a function')); return; } if (app.length !== 2) { callback(new Error('server.entry MUST BE (uri, cb) => cb(err, html)')); return; } map(staticRendering, (uri, cb) => { const filepath = `${config.outputdir}${uri.slice(config.rootdir.length - 1)}`; compiler.outputFileSystem.mkdirp(path.dirname(filepath), (err) => { if (err) { cb(err); return; } app(uri, (apperr, html) => { if (apperr) { cb(apperr); return; } /* eslint no-param-reassign:0 */ compilation.assets[uri.slice(config.rootdir.length)] = { source: () => html, size: () => html.length, emitted: true, }; compiler.outputFileSystem.writeFile(filepath, html, cb); }); }); }, callback); });
const server = http.createServer((req, res) => { if (process.env.NODE_ENV === 'development') { const route = router.hash.get(req.url); const clearRequireCache = require('clear-require-cache'); if ([ `${config.rootdir}web`, `${config.rootdir}web.html`, `${config.rootdir}web/*`, `${config.rootdir}m`, `${config.rootdir}m.html`, `${config.rootdir}m/*`, `${config.rootdir}hare`, `${config.rootdir}hare.html`, `${config.rootdir}hare/*`, ].indexOf(route.src) !== -1) { clearRequireCache(routerpath); router = require(routerpath); } } router(req, res, {}, (err) => { /* eslint no-param-reassign:0 */ if (err) { console.error(req.url, err.stack || err.toString()); if (res.finished) { return; } res.statusCode = err.statusCode || 500; if (process.env.NODE_ENV === 'development') { sendHtml(req, res, `<pre>${err.stack}</pre>`); } else { sendHtml(req, res, err.toString()); } } }); });