Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate miniapp-renderer #226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/miniapp-renderer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Changelog

## 0.1.15

- [Chore] use `pageSource` as the render unique path when `route.pageSource` existed

## 0.1.14

- [Feat] support plugin situation without global getApp
- [Feat] Adjust the time of appending root to document.body

## 0.1.13

- [Fix] __pagesRenderInfo in window will be replaced in subPakcages shareMemory mode

## 0.1.12

- [Fix] miss default rootId

## 0.1.11

- [Fix] miniapp global instance bind

## 0.1.10

- [Chore] remove vendor check
## 0.1.9

- [Chore] compatible with the old version

## 0.1.8

- [Feat] dispatch document modify when set document
1 change: 1 addition & 0 deletions packages/miniapp-renderer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `miniapp-renderer`
31 changes: 31 additions & 0 deletions packages/miniapp-renderer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "miniapp-renderer",
"version": "0.1.15",
"description": "",
"author": "Rax Team",
"homepage": "https://github.com/raxjs/miniapp#readme",
"license": "MIT",
"main": "lib/index.js",
"dependencies": {
"universal-env": "^3.0.0"
},
"sideEffects": false,
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib",
"!lib/**/*.map"
],
"repository": {
"type": "http",
"url": "https://github.com/raxjs/miniapp/tree/master/packages/miniapp-renderer"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/raxjs/miniapp/issues"
}
}
3 changes: 3 additions & 0 deletions packages/miniapp-renderer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import miniappRenderer from './miniappRenderer';

export default miniappRenderer;
75 changes: 75 additions & 0 deletions packages/miniapp-renderer/src/miniappRenderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
function miniappRenderer(
{ appConfig = {} as any, createBaseApp, createHistory, staticConfig, pageProps, emitLifeCycles, ErrorBoundary },
{ mount, unmount, createElement, Component }
) {
const history = createHistory({ routes: staticConfig.routes });

const { runtime } = createBaseApp(appConfig);
const AppProvider = runtime?.composeAppProvider?.();

const { app = {} } = appConfig;
const { rootId = 'root', ErrorBoundaryFallback, onErrorBoundaryHander, onErrorBoundaryHandler, errorBoundary } = app;

if (onErrorBoundaryHander) {
console.error('Please use onErrorBoundaryHandler instead of onErrorBoundaryHander');
}
emitLifeCycles();
class App extends Component {
public render() {
const { Page, ...otherProps } = this.props;
const PageComponent = createElement(Page, {
...otherProps
});

let appInstance = PageComponent;

if (AppProvider) {
appInstance = createElement(AppProvider, null, appInstance);
}
if (errorBoundary) {
appInstance = createElement(ErrorBoundary, {
Fallback: ErrorBoundaryFallback,
onError: onErrorBoundaryHander || onErrorBoundaryHandler,
}, appInstance);
}
return appInstance;
}
}
const pagesRenderInfo = staticConfig.routes.map(({ source, component, pageSource }: any) => {
return {
path: pageSource || source,
render() {
const PageComponent = component()();
const rootEl = document.createElement('div');
rootEl.setAttribute('id', rootId);
const appInstance = mount(createElement(App, {
history,
location: history.location,
...pageProps,
Page: PageComponent
}), rootEl);
document.body.appendChild(rootEl);

(document as any).__unmount = unmount(appInstance, rootEl);
},
setDocument(value) {
// eslint-disable-next-line no-global-assign
document = value;
// getApp doesn't exist in plugin situation
// @ts-ignore
if (typeof getApp === 'function') {
// @ts-ignore
const MiniAppGlobalInstance = getApp();
const dispatchDocumentModify = MiniAppGlobalInstance._dispatchDocumentModify;
if (typeof dispatchDocumentModify === 'function') {
dispatchDocumentModify.call(MiniAppGlobalInstance, value);
}
}
}
};
});

(window as any).__pagesRenderInfo = ((window as any).__pagesRenderInfo || []).concat(pagesRenderInfo);
}

export default miniappRenderer;
8 changes: 8 additions & 0 deletions packages/miniapp-renderer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.settings.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"outDir": "lib"
},
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"files": [],
"references": [
{ "path": "packages/build-plugin-rax-miniapp-faas" },
{ "path": "packages/driver-miniapp" }
{ "path": "packages/driver-miniapp" },
{ "path": "packages/miniapp-renderer" }
]
}