UNPKG

@hippy/debug-server-next

Version:
100 lines (99 loc) 4 kB
"use strict"; /* * Tencent is pleased to support the open source community by making * Hippy available. * * Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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 koa_static_1 = tslib_1.__importDefault(require("koa-static")); const koa_bodyparser_1 = tslib_1.__importDefault(require("koa-bodyparser")); const file_1 = require("@debug-server-next/utils/file"); const log_1 = require("@debug-server-next/utils/log"); const debug_targets_1 = require("@debug-server-next/router/debug-targets"); const config_1 = require("@debug-server-next/config"); const log = new log_1.Logger('router'); const routeApp = (app) => { const { static: staticPath, entry } = global.debugAppArgv; app.use((0, koa_bodyparser_1.default)()); app.use((0, cors_1.default)()); app.use(async (ctx, next) => { try { await next(); } catch (e) { log.error('koa error: %s', e === null || e === void 0 ? void 0 : e.stack); return (ctx.body = e === null || e === void 0 ? void 0 : e.stack); } }); const debugTargetsRouter = (0, debug_targets_1.getDebugTargetsRouter)(); app.use(debugTargetsRouter.routes()).use(debugTargetsRouter.allowedMethods()); const defaultStaticOption = { buffer: false, dynamic: true, preload: false, }; // chrome devtools html files no cache const htmlList = [ 'devtools_app.html', 'hippy_tdf.html', 'hippy.html', 'inspector.html', 'js_app.html', 'ndb_app.html', 'node_app.html', 'tdf_core.html', ]; htmlList.forEach((file) => { app.use((0, koa_static_1.default)((0, file_1.getModuleFolder)('@hippy/chrome-devtools', `out/Version/gen/front_end/${file}`))); }); // chrome devtools resources with versionId(commit id), app.use((0, koa_static_cache_1.default)((0, file_1.getModuleFolder)('@hippy/chrome-devtools', 'out/Version/gen'), { ...defaultStaticOption, maxAge: 60 * 60 * 24 * 365, })); // vue devtools app.use((0, koa_static_cache_1.default)((0, file_1.getModuleFolder)('@hippy/vue-devtools', 'dist'), { ...defaultStaticOption, maxAge: 60 * 60, })); // react devtools app.use((0, koa_static_cache_1.default)((0, file_1.getModuleFolder)('@hippy/react-devtools', 'dist'), { ...defaultStaticOption, maxAge: 60 * 60, })); // chrome devtools extensions app.use((0, koa_static_cache_1.default)((0, file_1.getModuleFolder)('@hippy/chrome-devtools-extensions', 'dist'), { ...defaultStaticOption, maxAge: 60 * 60, })); // hmr resources app.use((0, koa_static_1.default)(config_1.config.hmrStaticPath)); // bundle resources 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}`); app.use((0, koa_static_1.default)(servePath)); }; exports.routeApp = routeApp;