diff --git a/package-lock.json b/package-lock.json index 910f8f5..e450cc6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@x-poppy/web-sdk", - "version": "0.0.6", + "version": "0.0.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@x-poppy/web-sdk", - "version": "0.0.6", + "version": "0.0.7", "license": "MIT", "dependencies": { "react": "^18.2.0", diff --git a/package.json b/package.json index f5d0e27..783a033 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@x-poppy/web-sdk", - "version": "0.0.6", + "version": "0.0.7", "description": "", "main": "./dist/main.js", "scripts": { diff --git a/src/react/components/PageShell/index.ts b/src/react/components/PageShell/index.ts deleted file mode 100644 index 4788fba..0000000 --- a/src/react/components/PageShell/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./PageShell"; -export { default } from "./PageShell"; diff --git a/src/react/components/PageShell/PageShell.module.css b/src/react/components/WebModule/WebModule.module.css similarity index 100% rename from src/react/components/PageShell/PageShell.module.css rename to src/react/components/WebModule/WebModule.module.css diff --git a/src/react/components/PageShell/PageShell.tsx b/src/react/components/WebModule/WebModule.tsx similarity index 61% rename from src/react/components/PageShell/PageShell.tsx rename to src/react/components/WebModule/WebModule.tsx index 1734638..7f52df0 100644 --- a/src/react/components/PageShell/PageShell.tsx +++ b/src/react/components/WebModule/WebModule.tsx @@ -1,8 +1,8 @@ import React, { PropsWithChildren } from 'react'; -import './PageShell.module.css'; +import './WebModule.module.css'; import { ConfigProvider } from 'antd'; -export function PageShell(props: PropsWithChildren) { +export function WebModule(props: PropsWithChildren) { return ( { props.children } @@ -10,4 +10,4 @@ export function PageShell(props: PropsWithChildren) { ); } -export default PageShell; +export default WebModule; diff --git a/src/react/components/WebModule/index.ts b/src/react/components/WebModule/index.ts new file mode 100644 index 0000000..b1a414e --- /dev/null +++ b/src/react/components/WebModule/index.ts @@ -0,0 +1,2 @@ +export * from "./WebModule"; +export { default } from "./WebModule"; diff --git a/src/react/startPageModule.tsx b/src/react/startPageModule.tsx deleted file mode 100644 index 4008a3a..0000000 --- a/src/react/startPageModule.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import React from 'react'; -import ReactDOM from 'react-dom/client'; -import { WebModuleFactory, startWebModule } from "../utils/webModule"; -import PageShell from './components/PageShell'; -import NotFoundPage from './components/NotFoundPage'; - -interface CreatePageFactoryOpts { - loadPageComponent(pageName: string): Promise>> -} - -export function createPageFactory(opts: CreatePageFactoryOpts): WebModuleFactory { - return async (context) => { - let PageComponent: React.ComponentType = NotFoundPage; - - let pageName = context?.params?.page; - if (!pageName) { - const urlSearchParams = new URLSearchParams(window.location.search); - pageName = urlSearchParams.get("page") ?? undefined; - } - - if (!pageName) { - pageName = "notfound"; - } - - try { - PageComponent = await opts.loadPageComponent(pageName); - } catch (err) { - PageComponent = NotFoundPage; - } - - const render = ReactDOM.createRoot(context?.container ?? document.getElementById('root') ?? document.body); - render.render( - - - - - - ); - - return () => { - render.unmount(); - } - } -} - -export function startPageModule (opts: CreatePageFactoryOpts) { - startWebModule(createPageFactory(opts)); -} diff --git a/src/react/webModule.tsx b/src/react/webModule.tsx new file mode 100644 index 0000000..0579102 --- /dev/null +++ b/src/react/webModule.tsx @@ -0,0 +1,38 @@ +import React, { ComponentType, ReactElement } from 'react'; +import ReactDOM from 'react-dom/client'; +import { WebModuleFactoryContext, startWebModule } from "../utils/webModule"; +import WebModule from './components/WebModule'; + +type LazyReactElementFactory = (context?: WebModuleFactoryContext) => Promise + +interface WebModuleOpts { + wrapper?: ComponentType + view?: ReactElement | LazyReactElementFactory +} + +export function startup(opts: WebModuleOpts) { + startWebModule(async (context) => { + let webModuleContent: ReactElement = <>; + if (React.isValidElement(opts.view)) { + webModuleContent = opts.view; + } else if (typeof opts.view === "function") { + webModuleContent = await opts.view(context); + } + + const Wrapper = opts.wrapper ?? React.Fragment; + const render = ReactDOM.createRoot(context?.container ?? document.getElementById('root') ?? document.body); + render.render( + + + { webModuleContent } + + + ); + + return () => { + render.unmount(); + } + }) +} + + diff --git a/src/utils/webModule.ts b/src/utils/webModule.ts index f392d19..2a2592d 100644 --- a/src/utils/webModule.ts +++ b/src/utils/webModule.ts @@ -165,25 +165,26 @@ async function loadWebModule(webModuleUrl: string) { } } -function get(webModuleUrlOrName: string) { - const webModuleName = webModuleAliasMap[webModuleUrlOrName] ?? webModuleUrlOrName; - return webModuleMap[webModuleName] ?? undefined; +export function get(webModuleUrlOrName: string) { + const webModuleUrl = webModuleAliasMap[webModuleUrlOrName] ?? webModuleUrlOrName; + return webModuleMap[webModuleUrl] ?? undefined; } -export async function importWebModule(webModuleUrl: string, webModuleName?: string): Promise { +export function alias(webModuleUrl: string, alias: string) { + webModuleAliasMap[alias] = webModuleUrl; +} + +export async function importWebModule(webModuleUrlOrName: string): Promise { + const webModuleUrl = webModuleAliasMap[webModuleUrlOrName] ?? webModuleUrlOrName; if (webModuleLoadedMap[webModuleUrl]) { await webModuleLoadedMap[webModuleUrl]; return get(webModuleUrl); } - if (webModuleName) { - webModuleAliasMap[webModuleName] = webModuleUrl; - } - const publicPath = baseURL(webModuleUrl); const webModule = { - moduleName: webModuleName ?? webModuleUrl, + moduleName: webModuleUrl, publicPath, factory: null, };