-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 1.x support load umd sub app (#176)
* feat: support load umd sub app * chore: lint * test: test case for umd loader * chore: version * fix: parseUrl * chore: typo * [1.x]feat: set loadMode when load micro app (#181) * feat: set loadMode when load micro app * chore: mode value
- Loading branch information
Showing
9 changed files
with
248 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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; | ||
let noteGlobalKeys = []; | ||
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; | ||
} else { | ||
// polyfill for UC browser which lastprops will alway be window | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const p in globalWindow) { | ||
if (!noteGlobalKeys.includes(p)) { | ||
lastProp = p; | ||
} | ||
} | ||
return lastProp; | ||
} | ||
} | ||
|
||
export function noteGlobalProps (globalWindow) { | ||
// alternatively Object.keys(global).pop() | ||
// but this may be faster (pending benchmarks) | ||
firstGlobalProp = undefined; | ||
secondGlobalProp = undefined; | ||
noteGlobalKeys = Object.keys(globalWindow); | ||
// 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Sandbox from '@ice/sandbox'; | ||
import { getGlobalProp, noteGlobalProps } from './global'; | ||
import { Asset, fetchScripts } from './handleAssets'; | ||
|
||
export interface StarkModule { | ||
cacheKey: string; | ||
jsList: Asset[]; | ||
mount?: (Component: any, targetNode: HTMLElement, props?: any) => void; | ||
unmount?: (targetNode: HTMLElement) => void; | ||
}; | ||
|
||
export interface ImportTask { | ||
[cacheKey: string]: Promise<string[]>; | ||
}; | ||
|
||
export type PromiseModule = Promise<Response>; | ||
|
||
export interface Fetch { | ||
(input: RequestInfo, init?: RequestInit): Promise<Response>; | ||
} | ||
|
||
export default class ModuleLoader { | ||
private importTask: ImportTask = {}; | ||
|
||
load(starkModule: StarkModule): Promise<string[]> { | ||
const { jsList, cacheKey } = starkModule; | ||
if (this.importTask[cacheKey]) { | ||
// return promise if current module is pending or resolved | ||
return this.importTask[cacheKey]; | ||
} | ||
const task = fetchScripts(jsList); | ||
this.importTask[cacheKey] = task; | ||
return task; | ||
} | ||
|
||
clearTask() { | ||
this.importTask = {}; | ||
} | ||
|
||
execModule(starkModule: StarkModule, sandbox?: Sandbox) { | ||
return this.load(starkModule).then((sources) => { | ||
const globalWindow = getGobalWindow(sandbox); | ||
const { cacheKey } = starkModule; | ||
let libraryExport = ''; | ||
// excute script in order | ||
try { | ||
sources.forEach((source, index) => { | ||
const lastScript = index === sources.length - 1; | ||
if (lastScript) { | ||
noteGlobalProps(globalWindow); | ||
} | ||
// check sandbox | ||
if (sandbox?.execScriptInSandbox) { | ||
sandbox.execScriptInSandbox(source); | ||
} else { | ||
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval | ||
// eslint-disable-next-line no-eval | ||
(0, eval)(source); | ||
} | ||
if (lastScript) { | ||
libraryExport = getGlobalProp(globalWindow); | ||
} | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
const moduleInfo = libraryExport ? (globalWindow as any)[libraryExport] : ((globalWindow as any)[cacheKey] || {}); | ||
// remove moduleInfo from globalWindow in case of excute multi module in globalWindow | ||
if ((globalWindow as any)[libraryExport]) { | ||
delete globalWindow[libraryExport]; | ||
} | ||
return moduleInfo; | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Get globalwindow | ||
* | ||
* @export | ||
* @param {Sandbox} [sandbox] | ||
* @returns | ||
*/ | ||
export function getGobalWindow(sandbox?: Sandbox) { | ||
if (sandbox?.getSandbox) { | ||
sandbox.createProxySandbox(); | ||
return sandbox.getSandbox(); | ||
} | ||
// FIXME: If run in Node environment | ||
return window; | ||
} |
Oops, something went wrong.