Skip to content

Commit

Permalink
feat: support umd js bundle (#159)
Browse files Browse the repository at this point in the history
* feat: 🎸 support umd js bundle

* 支持 umd 打包

* feat: 🎸 export load micro app

* fix: 🐛 fix lint

* feat: 🎸 support name
  • Loading branch information
maoxiaoke authored Sep 15, 2020
1 parent 2529dde commit bfbfef3
Show file tree
Hide file tree
Showing 6 changed files with 515 additions and 14 deletions.
39 changes: 39 additions & 0 deletions src/util/appLifeCycle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { setCache, getCache } from './cache';
import { resetCapturedEventListeners } from './capturedListeners';
import { exeuteUmdMount, executeUmdUnmount, executeUmdUpdate } from './handleAssets';
import getMountNode from './getMountNode';

export enum AppLifeCycleEnum {
AppEnter = 'appEnter',
Expand Down Expand Up @@ -50,3 +52,40 @@ export function callAppLeave() {
setCache(appLeaveKey, null);
}
}


/**
* call umd App when app enter
*
* @export
* @param {string} name
*/
export function callUmdAppEnter(name: string, rootId?: string, props?: object) {
const container = getMountNode(rootId);
return exeuteUmdMount(name, container, props);
}

/**
* call umd app when app unmount
*
* @export
* @param {string} name
* @returns
*/
export function callUmdAppLeave(name: string, rootId?: string, props?: object) {
const container = getMountNode(rootId);
return executeUmdUnmount(name, container, props);
}


/**
* call umd app when app update
*
* @export
* @param {string} name
* @returns
*/
export function callUmdAppUpdate(name: string, rootId?: string, props?: object) {
const container = getMountNode(rootId);
return executeUmdUpdate(name, container, props);
}
26 changes: 26 additions & 0 deletions src/util/assist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface PathData {
value: string;
exact?: boolean;
strict?: boolean;
sensitive?: boolean;
}

/**
* util function to covert path to string
*
* @export
* @param {(string | (string | PathData)[])} list
* @returns
*/
export function converArray2String(list: string | (string | PathData)[]) {
if (Array.isArray(list)) {
return list.map((item) => {
if (Object.prototype.toString.call(item) === '[object Object]') {
return Object.keys(item).map((key) => `${key}:${item[key]}`).join(',');
}
return item;
}).join(',');
}

return String(list);
}
27 changes: 27 additions & 0 deletions src/util/getMountNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getCache } from './cache';

export default function getMountNode(element?: any): any {
if (getCache('root')) {
return getCache('root');
}

if (element) {
// string treated as 'id'
if (typeof element === 'string') {
return document.querySelector(`#${element}`);
}

// function, return value
if (typeof element === 'function') {
return element();
}

return element;
}
const ICE_CONTAINER = document.querySelector('#ice-container');
if (!ICE_CONTAINER) {
throw new Error('Current page does not exist <div id="ice-container"></div> element.');
}

return ICE_CONTAINER;
}
52 changes: 52 additions & 0 deletions src/util/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// fork: https://github.com/systemjs/systemjs/blob/master/src/extras/global.js

// safari unpredictably lists some new globals first or second in object order
let firstGlobalProp;
let secondGlobalProp;
let lastGlobalProp;
const isIE11 = typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Trident') !== -1;

function shouldSkipProperty(p, globalWindow) {
// eslint-disable-next-line no-prototype-builtins
return !globalWindow.hasOwnProperty(p)
|| !isNaN(p) && p < (globalWindow as any).length
|| isIE11 && globalWindow[p] && typeof window !== 'undefined' && globalWindow[p].parent === window;
}

export function getGlobalProp (globalWindow) {
let cnt = 0;
let lastProp;
// eslint-disable-next-line no-restricted-syntax
for (const p in globalWindow) {
// do not check frames cause it could be removed during import
if (shouldSkipProperty(p, globalWindow))
// eslint-disable-next-line no-continue
continue;
if (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp)
return p;
cnt++;
lastProp = p;
}
if (lastProp !== lastGlobalProp)
return lastProp;
}

export function noteGlobalProps (globalWindow) {
// alternatively Object.keys(global).pop()
// but this may be faster (pending benchmarks)
firstGlobalProp = undefined;
secondGlobalProp = undefined;
// eslint-disable-next-line no-restricted-syntax
for (const p in globalWindow) {
// do not check frames cause it could be removed during import
if (shouldSkipProperty(p, globalWindow))
// eslint-disable-next-line no-continue
continue;
if (!firstGlobalProp)
firstGlobalProp = p;
else if (!secondGlobalProp)
secondGlobalProp = p;
lastGlobalProp = p;
}
return lastGlobalProp;
}
Loading

0 comments on commit bfbfef3

Please sign in to comment.