UNPKG

debug-server-next

Version:

Dev server for hippy-core.

42 lines (41 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.routeApp = void 0; const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const cors_1 = tslib_1.__importDefault(require("@koa/cors")); const koa_static_cache_1 = tslib_1.__importDefault(require("koa-static-cache")); const log_1 = require("@/utils/log"); const debug_targets_1 = require("@/router/debug-targets"); const log = new log_1.Logger('router'); const routeApp = (app) => { const { staticPath, entry } = global.debugAppArgv; app.use(cors_1.default()); app.use((ctx, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a, _b; try { yield next(); } catch (e) { log.error('koa error: %s', (_a = e) === null || _a === void 0 ? void 0 : _a.stack); return (ctx.body = (_b = e) === null || _b === void 0 ? void 0 : _b.stack); } })); const debugTargetsRouter = debug_targets_1.getDebugTargetsRouter(); app.use(debugTargetsRouter.routes()).use(debugTargetsRouter.allowedMethods()); let servePath; if (staticPath) servePath = path_1.default.resolve(staticPath); else servePath = path_1.default.resolve(path_1.default.dirname(entry)); log.info(`serve bundle: ${entry}; serve folder: ${servePath}`); const defaultStaicOption = { buffer: false, dynamic: true, }; // bundle 静态资源,禁用缓存 app.use(koa_static_cache_1.default(servePath, defaultStaicOption)); // devtools 静态资源,优先强缓存,强缓存过期后协商缓存 app.use(koa_static_cache_1.default(path_1.default.join(__dirname, '../public'), Object.assign(Object.assign({}, defaultStaicOption), { maxAge: 60 * 60 }))); }; exports.routeApp = routeApp;