UNPKG

@modern-js/server-core

Version:

A Progressive React Framework for modern web development.

66 lines (65 loc) 2.19 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var route_exports = {}; __export(route_exports, { injectRoutePlugin: () => injectRoutePlugin }); module.exports = __toCommonJS(route_exports); var import_constants = require("@modern-js/utils/universal/constants"); var import_utils = require("../utils"); function injectRoute(route) { return async (c, next) => { if (route && !c.get("route")) { c.set("route", route); } await next(); }; } function getPageRoutes(routes) { return routes.filter((route) => route.entryName).sort(import_utils.sortRoutes); } const injectRoutePlugin = () => ({ name: "@modern-js/plugin-inject-route", setup(api) { return { async prepare() { const { middlewares, routes } = api.useAppContext(); if (!routes) { return; } const pageRoutes = getPageRoutes(routes); for (const route of pageRoutes) { const { urlPath: originUrlPath, entryName = import_constants.MAIN_ENTRY_NAME } = route; const urlPath = originUrlPath.endsWith("/") ? `${originUrlPath}*` : `${originUrlPath}/*`; middlewares.push({ name: "inject-route-info", path: urlPath, handler: injectRoute({ entryName }) }); } } }; } }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { injectRoutePlugin });