UNPKG

@difizen/umi-plugin-mana

Version:

- 支持 decorator 所需的 babel 配置,在 typescript 4 环境生效 - 支持动态路由路径 - 支持 nodenext,在 import path 中允许增加扩展名 - 支持 mana 运行时能力,基于 slot 的路由等

116 lines (110 loc) 3.34 kB
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); // src/mana-runtime.ts var mana_runtime_exports = {}; __export(mana_runtime_exports, { DIR_NAME_IN_TMP: () => DIR_NAME_IN_TMP, default: () => mana_runtime_default, pageContent: () => pageContent, providerContent: () => providerContent }); module.exports = __toCommonJS(mana_runtime_exports); var DIR_NAME_IN_TMP = "plugin-mana"; var providerContent = `import React from 'react'; import { ManaAppPreset, ManaComponents } from '@difizen/mana-app'; import RootModule from '@/modules/app.module'; export default ({ children }: { children: React.ReactNode }) => { return ( <ManaComponents.Application modules={[ManaAppPreset, RootModule]} renderChildren={true}> {children} </ManaComponents.Application> ); }; `; var runtimeContent = `import React from 'react'; import Provider from './Provider'; export function dataflowProvider(container: React.ReactNode) { return React.createElement( Provider, null, container, ); } `; var pageContent = (slot) => `(async () => { const { Slot } = await import('@difizen/mana-app'); const { Outlet } = await import('umi'); const Page = ({ children, ...props }) => { return ( <Slot name="${slot}" viewProps={props}> <Outlet /> </Slot> ); }; return Page })() `; var getRoutePagePath = (route) => ({ writePath: `pages/${route.slot}.tsx`, routePath: `@@/${DIR_NAME_IN_TMP}/pages/${route.slot}` }); var writeRoutePage = (api, route) => { if (route.slot) { api.writeTmpFile({ path: getRoutePagePath(route).writePath, content: pageContent(route.slot) }); } }; var mana_runtime_default = (api) => { let cacheRouteMap = {}; api.addRuntimePlugin(() => "../plugin-mana/runtime"); api.addRuntimePluginKey(() => ["mana"]); api.onGenerateFiles(async () => { try { api.writeTmpFile({ path: "/Provider.tsx", content: providerContent }); api.writeTmpFile({ path: "/runtime.tsx", content: runtimeContent }); const keys = Object.keys(cacheRouteMap); keys.forEach((key) => writeRoutePage(api, cacheRouteMap[key])); } catch (e) { console.error(e); } }); api.modifyRoutes((routeMap) => { cacheRouteMap = routeMap; Object.keys(routeMap).forEach((id) => { const route = routeMap[id]; if (route.slot !== void 0) { route.file = pageContent(route.slot); } }); return routeMap; }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DIR_NAME_IN_TMP, pageContent, providerContent });