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

Release: stark-module-1.5.1 #507

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: add library export after execute module (#495) (#496)
* fix: add library export after execute module (#495)


Co-authored-by: jianleiHuang <[email protected]>
  • Loading branch information
maoxiaoke and moreJs authored Jan 5, 2022
commit bbfd8eb45916c4a9ea0270e2a6e98558d8c0d17b
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
English | [简体中文](https://ice-lab.github.io/icestark/)

# icestark

> Micro Frontends solution for large application. [Website Chinese docs](https://ice-lab.github.io/icestark/).
Expand Down
13 changes: 9 additions & 4 deletions packages/icestark-module/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ function shouldSkipProperty(p, globalWindow) {
|| isIE11 && globalWindow[p] && typeof window !== 'undefined' && globalWindow[p].parent === window;
}

export function getGlobalProp(globalWindow) {
let cnt = 0;

export function getGlobalProp(globalWindow, checker: (property: unknown) => boolean) {
let cnt = -1;
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)) { continue; }
if (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp) { return p; }
cnt++;
const isValidLibraryExport = checker(globalWindow[p]);
if (!isValidLibraryExport) {
continue;
}
if (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp) { return p; }
lastProp = p;
}
if (lastProp !== lastGlobalProp) {
Expand All @@ -31,7 +36,7 @@ export function getGlobalProp(globalWindow) {
// 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)) {
if (!noteGlobalKeys.includes(p) && checker(globalWindow[p])) {
lastProp = p;
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/icestark-module/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Sandbox from '@ice/sandbox';
import { getGlobalProp, noteGlobalProps } from './global';
import { StarkModule } from './modules';
import { isEsModule } from './utils';

export interface ImportTask {
[name: string]: Promise<string[]>;
Expand Down Expand Up @@ -69,13 +70,13 @@ export default class ModuleLoader {
(0, eval)(source);
}
if (lastScript) {
libraryExport = getGlobalProp(globalWindow);
libraryExport = getGlobalProp(globalWindow, isEsModule);
}
});
} catch (err) {
console.error(err);
}
const moduleInfo = libraryExport ? (globalWindow as any)[libraryExport] : ((globalWindow as any)[name] || {});
const moduleInfo = libraryExport ? (globalWindow as any)[libraryExport] : ((globalWindow as any)[name]);
// remove moduleInfo from globalWindow in case of excute multi module in globalWindow
if ((globalWindow as any)[libraryExport]) {
delete globalWindow[libraryExport];
Expand Down
10 changes: 10 additions & 0 deletions packages/icestark-module/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export const any2AnyArray = <T>(any: T | T[]): T[] => (Array.isArray(any) ? any : [any]);

export const isEsModule = (esModule) => {
try {
return esModule && esModule.__esModule;
} catch (error) {
// 在某些场景下,例如:iframe,访问属性会存在跨域问题,会直接报错
// 这种情况,直接忽略不做diff比较
return false;
}
};