@micro-zoe/micro-app 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -14
- package/README.zh-cn.md +16 -23
- package/lib/index.d.ts +10 -21
- package/lib/index.esm.js +532 -380
- package/lib/index.esm.js.map +1 -1
- package/lib/index.min.js +1 -1
- package/lib/index.min.js.map +1 -1
- package/lib/index.umd.js +1 -1
- package/lib/index.umd.js.map +1 -1
- package/package.json +1 -1
- package/typings/global.d.ts +6 -3
package/lib/index.esm.js
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
1
|
+
const version = '0.4.0';
|
|
2
|
+
const isBrowser = typeof window !== 'undefined';
|
|
3
|
+
const globalThis = (function () {
|
|
4
|
+
let gt;
|
|
5
|
+
if (typeof global !== 'undefined') {
|
|
6
|
+
gt = global;
|
|
7
|
+
}
|
|
8
|
+
else if (typeof self !== 'undefined') {
|
|
9
|
+
gt = self;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
try {
|
|
13
|
+
gt = Function('return this')();
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
throw new Error('global object is unavailable in this environment');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return gt;
|
|
20
|
+
})();
|
|
4
21
|
/**
|
|
5
22
|
* format error log
|
|
6
23
|
* @param msg message
|
|
@@ -81,7 +98,7 @@ function getEffectivePath(url) {
|
|
|
81
98
|
* @param baseURI base url(app.url)
|
|
82
99
|
*/
|
|
83
100
|
function CompletionPath(path, baseURI) {
|
|
84
|
-
if (/^((((ht|f)tps?)|file):)?\/\//.test(path))
|
|
101
|
+
if (/^((((ht|f)tps?)|file):)?\/\//.test(path) || /^(data|blob):/.test(path))
|
|
85
102
|
return path;
|
|
86
103
|
return new URL(path, getEffectivePath(addProtocol(baseURI))).toString();
|
|
87
104
|
}
|
|
@@ -149,7 +166,7 @@ function unique(array) {
|
|
|
149
166
|
}, Object.create(null));
|
|
150
167
|
}
|
|
151
168
|
// requestIdleCallback polyfill
|
|
152
|
-
const requestIdleCallback =
|
|
169
|
+
const requestIdleCallback = globalThis.requestIdleCallback ||
|
|
153
170
|
function (fn) {
|
|
154
171
|
const lastTime = Date.now();
|
|
155
172
|
return setTimeout(function () {
|
|
@@ -188,7 +205,7 @@ function isFunction(target) {
|
|
|
188
205
|
* Create pure elements
|
|
189
206
|
*/
|
|
190
207
|
function pureCreateElement(tagName, options) {
|
|
191
|
-
const element =
|
|
208
|
+
const element = document.createElement(tagName, options);
|
|
192
209
|
if (element.__MICRO_APP_NAME__)
|
|
193
210
|
delete element.__MICRO_APP_NAME__;
|
|
194
211
|
return element;
|
|
@@ -199,9 +216,10 @@ function pureCreateElement(tagName, options) {
|
|
|
199
216
|
* @param target Accept cloned elements
|
|
200
217
|
*/
|
|
201
218
|
function cloneNode(origin, target) {
|
|
202
|
-
|
|
219
|
+
target.innerHTML = '';
|
|
220
|
+
const clonedNode = origin.cloneNode(true);
|
|
203
221
|
const fragment = document.createDocumentFragment();
|
|
204
|
-
Array.from(
|
|
222
|
+
Array.from(clonedNode.childNodes).forEach((node) => {
|
|
205
223
|
fragment.appendChild(node);
|
|
206
224
|
});
|
|
207
225
|
target.appendChild(fragment);
|
|
@@ -233,6 +251,83 @@ var lifeCycles;
|
|
|
233
251
|
lifeCycles["ERROR"] = "error";
|
|
234
252
|
})(lifeCycles || (lifeCycles = {}));
|
|
235
253
|
|
|
254
|
+
const globalEnv = {};
|
|
255
|
+
function initGloalEnv() {
|
|
256
|
+
if (isBrowser) {
|
|
257
|
+
/**
|
|
258
|
+
* save patch raw methods
|
|
259
|
+
* pay attention to this binding
|
|
260
|
+
*/
|
|
261
|
+
const rawSetAttribute = Element.prototype.setAttribute;
|
|
262
|
+
const rawAppendChild = Node.prototype.appendChild;
|
|
263
|
+
const rawInsertBefore = Node.prototype.insertBefore;
|
|
264
|
+
const rawReplaceChild = Node.prototype.replaceChild;
|
|
265
|
+
const rawRemoveChild = Node.prototype.removeChild;
|
|
266
|
+
const rawAppend = Element.prototype.append;
|
|
267
|
+
const rawPrepend = Element.prototype.prepend;
|
|
268
|
+
const rawCreateElement = Document.prototype.createElement;
|
|
269
|
+
const rawCreateElementNS = Document.prototype.createElementNS;
|
|
270
|
+
const rawCreateDocumentFragment = Document.prototype.createDocumentFragment;
|
|
271
|
+
const rawQuerySelector = Document.prototype.querySelector;
|
|
272
|
+
const rawQuerySelectorAll = Document.prototype.querySelectorAll;
|
|
273
|
+
const rawGetElementById = Document.prototype.getElementById;
|
|
274
|
+
const rawGetElementsByClassName = Document.prototype.getElementsByClassName;
|
|
275
|
+
const rawGetElementsByTagName = Document.prototype.getElementsByTagName;
|
|
276
|
+
const rawGetElementsByName = Document.prototype.getElementsByName;
|
|
277
|
+
const rawWindow = Function('return window')();
|
|
278
|
+
const rawDocument = Function('return document')();
|
|
279
|
+
const supportModuleScript = isSupportModuleScript();
|
|
280
|
+
const templateStyle = rawDocument.body.querySelector('#micro-app-template-style');
|
|
281
|
+
/**
|
|
282
|
+
* save effect raw methods
|
|
283
|
+
* pay attention to this binding, especially setInterval, setTimeout, clearInterval, clearTimeout
|
|
284
|
+
*/
|
|
285
|
+
const rawWindowAddEventListener = rawWindow.addEventListener;
|
|
286
|
+
const rawWindowRemoveEventListener = rawWindow.removeEventListener;
|
|
287
|
+
const rawSetInterval = rawWindow.setInterval;
|
|
288
|
+
const rawSetTimeout = rawWindow.setTimeout;
|
|
289
|
+
const rawClearInterval = rawWindow.clearInterval;
|
|
290
|
+
const rawClearTimeout = rawWindow.clearTimeout;
|
|
291
|
+
const rawDocumentAddEventListener = rawDocument.addEventListener;
|
|
292
|
+
const rawDocumentRemoveEventListener = rawDocument.removeEventListener;
|
|
293
|
+
// mark current application as base application
|
|
294
|
+
window.__MICRO_APP_BASE_APPLICATION__ = true;
|
|
295
|
+
Object.assign(globalEnv, {
|
|
296
|
+
// source/patch
|
|
297
|
+
rawSetAttribute,
|
|
298
|
+
rawAppendChild,
|
|
299
|
+
rawInsertBefore,
|
|
300
|
+
rawReplaceChild,
|
|
301
|
+
rawRemoveChild,
|
|
302
|
+
rawAppend,
|
|
303
|
+
rawPrepend,
|
|
304
|
+
rawCreateElement,
|
|
305
|
+
rawCreateElementNS,
|
|
306
|
+
rawCreateDocumentFragment,
|
|
307
|
+
rawQuerySelector,
|
|
308
|
+
rawQuerySelectorAll,
|
|
309
|
+
rawGetElementById,
|
|
310
|
+
rawGetElementsByClassName,
|
|
311
|
+
rawGetElementsByTagName,
|
|
312
|
+
rawGetElementsByName,
|
|
313
|
+
// common global vars
|
|
314
|
+
rawWindow,
|
|
315
|
+
rawDocument,
|
|
316
|
+
supportModuleScript,
|
|
317
|
+
templateStyle,
|
|
318
|
+
// sandbox/effect
|
|
319
|
+
rawWindowAddEventListener,
|
|
320
|
+
rawWindowRemoveEventListener,
|
|
321
|
+
rawSetInterval,
|
|
322
|
+
rawSetTimeout,
|
|
323
|
+
rawClearInterval,
|
|
324
|
+
rawClearTimeout,
|
|
325
|
+
rawDocumentAddEventListener,
|
|
326
|
+
rawDocumentRemoveEventListener,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
236
331
|
// https://developer.mozilla.org/zh-CN/docs/Web/API/CSSRule
|
|
237
332
|
var CSSRuleType;
|
|
238
333
|
(function (CSSRuleType) {
|
|
@@ -283,7 +378,7 @@ function scopedStyleRule(rule, prefix) {
|
|
|
283
378
|
*/
|
|
284
379
|
function scopedHost(cssText, baseURI, textContent, linkpath) {
|
|
285
380
|
return cssText.replace(/url\(["']?([^)"']+)["']?\)/gm, (all, $1) => {
|
|
286
|
-
if (/^data:/.test($1)) {
|
|
381
|
+
if (/^(data|blob):/.test($1)) {
|
|
287
382
|
return all;
|
|
288
383
|
}
|
|
289
384
|
else if (/^(https?:)?\/\//.test($1)) {
|
|
@@ -360,7 +455,6 @@ function commonAction(templateStyle, styleElement, originContent, prefix, baseUR
|
|
|
360
455
|
}
|
|
361
456
|
styleElement.textContent = result;
|
|
362
457
|
}
|
|
363
|
-
let templateStyle = rawDocument.body.querySelector('#micro-app-template-style');
|
|
364
458
|
/**
|
|
365
459
|
* scopedCSS
|
|
366
460
|
* @param styleElement target style element
|
|
@@ -370,10 +464,11 @@ function scopedCSS(styleElement, appName) {
|
|
|
370
464
|
const app = appInstanceMap.get(appName);
|
|
371
465
|
if (app === null || app === void 0 ? void 0 : app.scopecss) {
|
|
372
466
|
const prefix = `${microApp.tagName}[name=${appName}]`;
|
|
467
|
+
let templateStyle = globalEnv.templateStyle;
|
|
373
468
|
if (!templateStyle) {
|
|
374
|
-
templateStyle = pureCreateElement('style');
|
|
469
|
+
globalEnv.templateStyle = templateStyle = pureCreateElement('style');
|
|
375
470
|
templateStyle.setAttribute('id', 'micro-app-template-style');
|
|
376
|
-
rawDocument.body.appendChild(templateStyle);
|
|
471
|
+
globalEnv.rawDocument.body.appendChild(templateStyle);
|
|
377
472
|
templateStyle.sheet.disabled = true;
|
|
378
473
|
}
|
|
379
474
|
if (styleElement.textContent) {
|
|
@@ -581,7 +676,6 @@ function formatHTMLStyleAfterUmdInit(tempHTML, appName) {
|
|
|
581
676
|
|
|
582
677
|
// Global scripts, reuse across apps
|
|
583
678
|
const globalScripts = new Map();
|
|
584
|
-
const supportModuleScript = isSupportModuleScript();
|
|
585
679
|
/**
|
|
586
680
|
* Extract script elements
|
|
587
681
|
* @param script script element
|
|
@@ -595,8 +689,12 @@ function extractScriptElement(script, parent, app, isDynamic = false) {
|
|
|
595
689
|
if (script.hasAttribute('exclude')) {
|
|
596
690
|
replaceComment = document.createComment('script element with exclude attribute ignored by micro-app');
|
|
597
691
|
}
|
|
598
|
-
else if ((
|
|
599
|
-
|
|
692
|
+
else if ((script.type && !['text/javascript', 'text/ecmascript', 'application/javascript', 'application/ecmascript', 'module'].includes(script.type)) ||
|
|
693
|
+
script.hasAttribute('ignore')) {
|
|
694
|
+
return null;
|
|
695
|
+
}
|
|
696
|
+
else if ((globalEnv.supportModuleScript && script.noModule) ||
|
|
697
|
+
(!globalEnv.supportModuleScript && script.type === 'module')) {
|
|
600
698
|
replaceComment = document.createComment(`${script.noModule ? 'noModule' : 'module'} script ignored by micro-app`);
|
|
601
699
|
}
|
|
602
700
|
else if (src) { // remote script
|
|
@@ -642,7 +740,7 @@ function extractScriptElement(script, parent, app, isDynamic = false) {
|
|
|
642
740
|
if (isDynamic) {
|
|
643
741
|
return { replaceComment };
|
|
644
742
|
}
|
|
645
|
-
else {
|
|
743
|
+
else if (replaceComment) {
|
|
646
744
|
return parent.replaceChild(replaceComment, script);
|
|
647
745
|
}
|
|
648
746
|
}
|
|
@@ -696,8 +794,9 @@ function fetchScriptSuccess(url, info, data) {
|
|
|
696
794
|
* Execute js in the mount lifecycle
|
|
697
795
|
* @param scriptList script list
|
|
698
796
|
* @param app app
|
|
797
|
+
* @param callback callback for umd mode
|
|
699
798
|
*/
|
|
700
|
-
function execScripts(scriptList, app) {
|
|
799
|
+
function execScripts(scriptList, app, callback) {
|
|
701
800
|
const scriptListEntries = Array.from(scriptList.entries());
|
|
702
801
|
const deferScriptPromise = [];
|
|
703
802
|
const deferScriptInfo = [];
|
|
@@ -721,12 +820,47 @@ function execScripts(scriptList, app) {
|
|
|
721
820
|
Promise.all(deferScriptPromise).then((res) => {
|
|
722
821
|
res.forEach((code, index) => {
|
|
723
822
|
const [url, info] = deferScriptInfo[index];
|
|
724
|
-
runScript(url, info.code = info.code || code, app, info.module, false);
|
|
823
|
+
runScript(url, info.code = info.code || code, app, info.module, false, callback);
|
|
725
824
|
});
|
|
825
|
+
callback();
|
|
726
826
|
}).catch((err) => {
|
|
727
827
|
logError(err);
|
|
828
|
+
callback();
|
|
728
829
|
});
|
|
729
830
|
}
|
|
831
|
+
else {
|
|
832
|
+
callback();
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* run code
|
|
837
|
+
* @param url script address
|
|
838
|
+
* @param code js code
|
|
839
|
+
* @param app app
|
|
840
|
+
* @param module type='module' of script
|
|
841
|
+
* @param isDynamic dynamically created script
|
|
842
|
+
* @param callback callback from execScripts for first exec
|
|
843
|
+
*/
|
|
844
|
+
function runScript(url, code, app, module, isDynamic, callback) {
|
|
845
|
+
var _a;
|
|
846
|
+
try {
|
|
847
|
+
code = bindScope(url, code, app);
|
|
848
|
+
if (app.inline) {
|
|
849
|
+
const scriptElement = pureCreateElement('script');
|
|
850
|
+
setInlinScriptContent(url, code, module, scriptElement, callback);
|
|
851
|
+
if (isDynamic)
|
|
852
|
+
return scriptElement;
|
|
853
|
+
(_a = app.container) === null || _a === void 0 ? void 0 : _a.querySelector('micro-app-body').appendChild(scriptElement);
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
856
|
+
Function(code)();
|
|
857
|
+
if (isDynamic)
|
|
858
|
+
return document.createComment('dynamic script extract by micro-app');
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
catch (e) {
|
|
862
|
+
console.error('[micro-app from runScript]', e);
|
|
863
|
+
}
|
|
730
864
|
}
|
|
731
865
|
/**
|
|
732
866
|
* Get dynamically created remote script
|
|
@@ -755,20 +889,18 @@ function runDynamicScript(url, info, app, originScript) {
|
|
|
755
889
|
else {
|
|
756
890
|
replaceElement = document.createComment(`dynamic script with src='${url}' extract by micro-app`);
|
|
757
891
|
}
|
|
758
|
-
fetchSource(url, app.name).then((
|
|
759
|
-
info.code =
|
|
892
|
+
fetchSource(url, app.name).then((code) => {
|
|
893
|
+
info.code = code;
|
|
760
894
|
app.source.scripts.set(url, info);
|
|
761
895
|
if (info.isGlobal)
|
|
762
|
-
globalScripts.set(url,
|
|
896
|
+
globalScripts.set(url, code);
|
|
763
897
|
try {
|
|
764
|
-
|
|
898
|
+
code = bindScope(url, code, app);
|
|
765
899
|
if (app.inline) {
|
|
766
|
-
|
|
767
|
-
replaceElement.setAttribute('type', 'module');
|
|
768
|
-
replaceElement.textContent = data;
|
|
900
|
+
setInlinScriptContent(url, code, info.module, replaceElement);
|
|
769
901
|
}
|
|
770
902
|
else {
|
|
771
|
-
Function(
|
|
903
|
+
Function(code)();
|
|
772
904
|
}
|
|
773
905
|
}
|
|
774
906
|
catch (e) {
|
|
@@ -782,34 +914,24 @@ function runDynamicScript(url, info, app, originScript) {
|
|
|
782
914
|
return replaceElement;
|
|
783
915
|
}
|
|
784
916
|
/**
|
|
785
|
-
*
|
|
917
|
+
* common handle for inline script
|
|
786
918
|
* @param url script address
|
|
787
919
|
* @param code js code
|
|
788
|
-
* @param app app
|
|
789
920
|
* @param module type='module' of script
|
|
790
|
-
* @param
|
|
921
|
+
* @param scriptElement target script element
|
|
922
|
+
* @param callback callback from execScripts for first exec
|
|
791
923
|
*/
|
|
792
|
-
function
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
script.textContent = code;
|
|
801
|
-
if (isDynamic)
|
|
802
|
-
return script;
|
|
803
|
-
(_a = app.container) === null || _a === void 0 ? void 0 : _a.querySelector('micro-app-body').appendChild(script);
|
|
804
|
-
}
|
|
805
|
-
else {
|
|
806
|
-
Function(code)();
|
|
807
|
-
if (isDynamic)
|
|
808
|
-
return document.createComment('dynamic script extract by micro-app');
|
|
809
|
-
}
|
|
924
|
+
function setInlinScriptContent(url, code, module, scriptElement, callback) {
|
|
925
|
+
if (module) {
|
|
926
|
+
// module script is async, transform it to a blob for subsequent operations
|
|
927
|
+
const blob = new Blob([code], { type: 'text/javascript;charset=utf-8' });
|
|
928
|
+
scriptElement.src = URL.createObjectURL(blob);
|
|
929
|
+
scriptElement.setAttribute('type', 'module');
|
|
930
|
+
scriptElement.setAttribute('originSrc', url);
|
|
931
|
+
callback && (scriptElement.onload = callback);
|
|
810
932
|
}
|
|
811
|
-
|
|
812
|
-
|
|
933
|
+
else {
|
|
934
|
+
scriptElement.textContent = code;
|
|
813
935
|
}
|
|
814
936
|
}
|
|
815
937
|
/**
|
|
@@ -823,8 +945,8 @@ function bindScope(url, code, app) {
|
|
|
823
945
|
code = usePlugins(url, code, app.name, microApp.plugins);
|
|
824
946
|
}
|
|
825
947
|
if (app.sandBox) {
|
|
826
|
-
rawWindow.
|
|
827
|
-
return `;(function(window, self){with(window){;${code}\n}}).call(window.
|
|
948
|
+
globalEnv.rawWindow.__MICRO_APP_PROXY_WINDOW__ = app.sandBox.proxyWindow;
|
|
949
|
+
return `;(function(window, self){with(window){;${code}\n}}).call(window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__, window.__MICRO_APP_PROXY_WINDOW__);`;
|
|
828
950
|
}
|
|
829
951
|
return code;
|
|
830
952
|
}
|
|
@@ -854,23 +976,6 @@ function usePlugins(url, code, appName, plugins) {
|
|
|
854
976
|
return code;
|
|
855
977
|
}
|
|
856
978
|
|
|
857
|
-
// save raw methods
|
|
858
|
-
const rawSetAttribute = Element.prototype.setAttribute;
|
|
859
|
-
const rawAppendChild = Node.prototype.appendChild;
|
|
860
|
-
const rawInsertBefore = Node.prototype.insertBefore;
|
|
861
|
-
const rawReplaceChild = Node.prototype.replaceChild;
|
|
862
|
-
const rawRemoveChild = Node.prototype.removeChild;
|
|
863
|
-
const rawAppend = Element.prototype.append;
|
|
864
|
-
const rawPrepend = Element.prototype.prepend;
|
|
865
|
-
const rawCreateElement = Document.prototype.createElement;
|
|
866
|
-
const rawCreateElementNS = Document.prototype.createElementNS;
|
|
867
|
-
const rawCreateDocumentFragment = Document.prototype.createDocumentFragment;
|
|
868
|
-
const rawQuerySelector = Document.prototype.querySelector;
|
|
869
|
-
const rawQuerySelectorAll = Document.prototype.querySelectorAll;
|
|
870
|
-
const rawGetElementById = Document.prototype.getElementById;
|
|
871
|
-
const rawGetElementsByClassName = Document.prototype.getElementsByClassName;
|
|
872
|
-
const rawGetElementsByTagName = Document.prototype.getElementsByTagName;
|
|
873
|
-
const rawGetElementsByName = Document.prototype.getElementsByName;
|
|
874
979
|
// Record element and map element
|
|
875
980
|
const dynamicElementInMicroAppMap = new WeakMap();
|
|
876
981
|
/**
|
|
@@ -886,7 +991,7 @@ function handleNewNode(parent, child, app) {
|
|
|
886
991
|
dynamicElementInMicroAppMap.set(child, replaceComment);
|
|
887
992
|
return replaceComment;
|
|
888
993
|
}
|
|
889
|
-
else if (app.scopecss) {
|
|
994
|
+
else if (app.scopecss && !child.hasAttribute('ignore')) {
|
|
890
995
|
return scopedCSS(child, app.name);
|
|
891
996
|
}
|
|
892
997
|
return child;
|
|
@@ -897,7 +1002,7 @@ function handleNewNode(parent, child, app) {
|
|
|
897
1002
|
dynamicElementInMicroAppMap.set(child, linkReplaceComment);
|
|
898
1003
|
return linkReplaceComment;
|
|
899
1004
|
}
|
|
900
|
-
else if (!app.scopecss) {
|
|
1005
|
+
else if (!app.scopecss || child.hasAttribute('ignore')) {
|
|
901
1006
|
return child;
|
|
902
1007
|
}
|
|
903
1008
|
const { url, info } = extractLinkFromHtml(child, parent, app, null, true);
|
|
@@ -915,7 +1020,7 @@ function handleNewNode(parent, child, app) {
|
|
|
915
1020
|
return child;
|
|
916
1021
|
}
|
|
917
1022
|
else if (child instanceof HTMLScriptElement) {
|
|
918
|
-
const { replaceComment, url, info } = extractScriptElement(child, parent, app, true);
|
|
1023
|
+
const { replaceComment, url, info } = extractScriptElement(child, parent, app, true) || {};
|
|
919
1024
|
if (url && info) {
|
|
920
1025
|
if (info.code) { // inline script
|
|
921
1026
|
const replaceElement = runScript(url, info.code, app, info.module, true);
|
|
@@ -928,10 +1033,11 @@ function handleNewNode(parent, child, app) {
|
|
|
928
1033
|
return replaceElement;
|
|
929
1034
|
}
|
|
930
1035
|
}
|
|
931
|
-
else {
|
|
1036
|
+
else if (replaceComment) {
|
|
932
1037
|
dynamicElementInMicroAppMap.set(child, replaceComment);
|
|
933
1038
|
return replaceComment;
|
|
934
1039
|
}
|
|
1040
|
+
return child;
|
|
935
1041
|
}
|
|
936
1042
|
return child;
|
|
937
1043
|
}
|
|
@@ -955,15 +1061,15 @@ function invokePrototypeMethod(app, rawMethod, parent, targetChild, passiveChild
|
|
|
955
1061
|
* 2. When removeChild, targetChild may not be in microAppHead or head
|
|
956
1062
|
*/
|
|
957
1063
|
if (passiveChild && !microAppHead.contains(passiveChild)) {
|
|
958
|
-
return rawAppendChild.call(microAppHead, targetChild);
|
|
1064
|
+
return globalEnv.rawAppendChild.call(microAppHead, targetChild);
|
|
959
1065
|
}
|
|
960
|
-
else if (rawMethod === rawRemoveChild && !microAppHead.contains(targetChild)) {
|
|
1066
|
+
else if (rawMethod === globalEnv.rawRemoveChild && !microAppHead.contains(targetChild)) {
|
|
961
1067
|
if (parent.contains(targetChild)) {
|
|
962
1068
|
return rawMethod.call(parent, targetChild);
|
|
963
1069
|
}
|
|
964
1070
|
return targetChild;
|
|
965
1071
|
}
|
|
966
|
-
else if (rawMethod === rawAppend || rawMethod === rawPrepend) {
|
|
1072
|
+
else if (rawMethod === globalEnv.rawAppend || rawMethod === globalEnv.rawPrepend) {
|
|
967
1073
|
return rawMethod.call(microAppHead, targetChild);
|
|
968
1074
|
}
|
|
969
1075
|
return rawMethod.call(microAppHead, targetChild, passiveChild);
|
|
@@ -971,20 +1077,20 @@ function invokePrototypeMethod(app, rawMethod, parent, targetChild, passiveChild
|
|
|
971
1077
|
else if (parent === document.body) {
|
|
972
1078
|
const microAppBody = app.container.querySelector('micro-app-body');
|
|
973
1079
|
if (passiveChild && !microAppBody.contains(passiveChild)) {
|
|
974
|
-
return rawAppendChild.call(microAppBody, targetChild);
|
|
1080
|
+
return globalEnv.rawAppendChild.call(microAppBody, targetChild);
|
|
975
1081
|
}
|
|
976
|
-
else if (rawMethod === rawRemoveChild && !microAppBody.contains(targetChild)) {
|
|
1082
|
+
else if (rawMethod === globalEnv.rawRemoveChild && !microAppBody.contains(targetChild)) {
|
|
977
1083
|
if (parent.contains(targetChild)) {
|
|
978
1084
|
return rawMethod.call(parent, targetChild);
|
|
979
1085
|
}
|
|
980
1086
|
return targetChild;
|
|
981
1087
|
}
|
|
982
|
-
else if (rawMethod === rawAppend || rawMethod === rawPrepend) {
|
|
1088
|
+
else if (rawMethod === globalEnv.rawAppend || rawMethod === globalEnv.rawPrepend) {
|
|
983
1089
|
return rawMethod.call(microAppBody, targetChild);
|
|
984
1090
|
}
|
|
985
1091
|
return rawMethod.call(microAppBody, targetChild, passiveChild);
|
|
986
1092
|
}
|
|
987
|
-
else if (rawMethod === rawAppend || rawMethod === rawPrepend) {
|
|
1093
|
+
else if (rawMethod === globalEnv.rawAppend || rawMethod === globalEnv.rawPrepend) {
|
|
988
1094
|
return rawMethod.call(parent, targetChild);
|
|
989
1095
|
}
|
|
990
1096
|
return rawMethod.call(parent, targetChild, passiveChild);
|
|
@@ -999,7 +1105,7 @@ function getMappingNode(node) {
|
|
|
999
1105
|
* @param parent parent node
|
|
1000
1106
|
* @param newChild new node
|
|
1001
1107
|
* @param passiveChild passive node
|
|
1002
|
-
* @param
|
|
1108
|
+
* @param rawMethodraw method
|
|
1003
1109
|
*/
|
|
1004
1110
|
function commonElementHander(parent, newChild, passiveChild, rawMethod) {
|
|
1005
1111
|
if (newChild === null || newChild === void 0 ? void 0 : newChild.__MICRO_APP_NAME__) {
|
|
@@ -1007,12 +1113,12 @@ function commonElementHander(parent, newChild, passiveChild, rawMethod) {
|
|
|
1007
1113
|
if (app === null || app === void 0 ? void 0 : app.container) {
|
|
1008
1114
|
return invokePrototypeMethod(app, rawMethod, parent, handleNewNode(parent, newChild, app), passiveChild && getMappingNode(passiveChild));
|
|
1009
1115
|
}
|
|
1010
|
-
else if (rawMethod === rawAppend || rawMethod === rawPrepend) {
|
|
1116
|
+
else if (rawMethod === globalEnv.rawAppend || rawMethod === globalEnv.rawPrepend) {
|
|
1011
1117
|
return rawMethod.call(parent, newChild);
|
|
1012
1118
|
}
|
|
1013
1119
|
return rawMethod.call(parent, newChild, passiveChild);
|
|
1014
1120
|
}
|
|
1015
|
-
else if (rawMethod === rawAppend || rawMethod === rawPrepend) {
|
|
1121
|
+
else if (rawMethod === globalEnv.rawAppend || rawMethod === globalEnv.rawPrepend) {
|
|
1016
1122
|
const appName = getCurrentAppName();
|
|
1017
1123
|
if (!(newChild instanceof Node) && appName) {
|
|
1018
1124
|
const app = appInstanceMap.get(appName);
|
|
@@ -1056,34 +1162,34 @@ function patchElementPrototypeMethods() {
|
|
|
1056
1162
|
this.__MICRO_APP_NAME__ &&
|
|
1057
1163
|
appInstanceMap.has(this.__MICRO_APP_NAME__)) {
|
|
1058
1164
|
const app = appInstanceMap.get(this.__MICRO_APP_NAME__);
|
|
1059
|
-
rawSetAttribute.call(this, key, CompletionPath(value, app.url));
|
|
1165
|
+
globalEnv.rawSetAttribute.call(this, key, CompletionPath(value, app.url));
|
|
1060
1166
|
}
|
|
1061
1167
|
else {
|
|
1062
|
-
rawSetAttribute.call(this, key, value);
|
|
1168
|
+
globalEnv.rawSetAttribute.call(this, key, value);
|
|
1063
1169
|
}
|
|
1064
1170
|
};
|
|
1065
1171
|
// prototype methods of add element👇
|
|
1066
1172
|
Node.prototype.appendChild = function appendChild(newChild) {
|
|
1067
|
-
return commonElementHander(this, newChild, null, rawAppendChild);
|
|
1173
|
+
return commonElementHander(this, newChild, null, globalEnv.rawAppendChild);
|
|
1068
1174
|
};
|
|
1069
1175
|
Node.prototype.insertBefore = function insertBefore(newChild, refChild) {
|
|
1070
|
-
return commonElementHander(this, newChild, refChild, rawInsertBefore);
|
|
1176
|
+
return commonElementHander(this, newChild, refChild, globalEnv.rawInsertBefore);
|
|
1071
1177
|
};
|
|
1072
1178
|
Node.prototype.replaceChild = function replaceChild(newChild, oldChild) {
|
|
1073
|
-
return commonElementHander(this, newChild, oldChild, rawReplaceChild);
|
|
1179
|
+
return commonElementHander(this, newChild, oldChild, globalEnv.rawReplaceChild);
|
|
1074
1180
|
};
|
|
1075
1181
|
Element.prototype.append = function append(...nodes) {
|
|
1076
1182
|
let i = 0;
|
|
1077
1183
|
const length = nodes.length;
|
|
1078
1184
|
while (i < length) {
|
|
1079
|
-
commonElementHander(this, nodes[i], null, rawAppend);
|
|
1185
|
+
commonElementHander(this, nodes[i], null, globalEnv.rawAppend);
|
|
1080
1186
|
i++;
|
|
1081
1187
|
}
|
|
1082
1188
|
};
|
|
1083
1189
|
Element.prototype.prepend = function prepend(...nodes) {
|
|
1084
1190
|
let i = nodes.length;
|
|
1085
1191
|
while (i > 0) {
|
|
1086
|
-
commonElementHander(this, nodes[i - 1], null, rawPrepend);
|
|
1192
|
+
commonElementHander(this, nodes[i - 1], null, globalEnv.rawPrepend);
|
|
1087
1193
|
i--;
|
|
1088
1194
|
}
|
|
1089
1195
|
};
|
|
@@ -1092,11 +1198,11 @@ function patchElementPrototypeMethods() {
|
|
|
1092
1198
|
if (oldChild === null || oldChild === void 0 ? void 0 : oldChild.__MICRO_APP_NAME__) {
|
|
1093
1199
|
const app = appInstanceMap.get(oldChild.__MICRO_APP_NAME__);
|
|
1094
1200
|
if (app === null || app === void 0 ? void 0 : app.container) {
|
|
1095
|
-
return invokePrototypeMethod(app, rawRemoveChild, this, getMappingNode(oldChild));
|
|
1201
|
+
return invokePrototypeMethod(app, globalEnv.rawRemoveChild, this, getMappingNode(oldChild));
|
|
1096
1202
|
}
|
|
1097
|
-
return rawRemoveChild.call(this, oldChild);
|
|
1203
|
+
return globalEnv.rawRemoveChild.call(this, oldChild);
|
|
1098
1204
|
}
|
|
1099
|
-
return rawRemoveChild.call(this, oldChild);
|
|
1205
|
+
return globalEnv.rawRemoveChild.call(this, oldChild);
|
|
1100
1206
|
};
|
|
1101
1207
|
}
|
|
1102
1208
|
/**
|
|
@@ -1112,17 +1218,18 @@ function markElement(element) {
|
|
|
1112
1218
|
}
|
|
1113
1219
|
// methods of document
|
|
1114
1220
|
function patchDocument() {
|
|
1221
|
+
const rawDocument = globalEnv.rawDocument;
|
|
1115
1222
|
// create element 👇
|
|
1116
1223
|
Document.prototype.createElement = function createElement(tagName, options) {
|
|
1117
|
-
const element = rawCreateElement.call(rawDocument, tagName, options);
|
|
1224
|
+
const element = globalEnv.rawCreateElement.call(rawDocument, tagName, options);
|
|
1118
1225
|
return markElement(element);
|
|
1119
1226
|
};
|
|
1120
1227
|
Document.prototype.createElementNS = function createElementNS(namespaceURI, name, options) {
|
|
1121
|
-
const element = rawCreateElementNS.call(rawDocument, namespaceURI, name, options);
|
|
1228
|
+
const element = globalEnv.rawCreateElementNS.call(rawDocument, namespaceURI, name, options);
|
|
1122
1229
|
return markElement(element);
|
|
1123
1230
|
};
|
|
1124
1231
|
Document.prototype.createDocumentFragment = function createDocumentFragment() {
|
|
1125
|
-
const element = rawCreateDocumentFragment.call(rawDocument);
|
|
1232
|
+
const element = globalEnv.rawCreateDocumentFragment.call(rawDocument);
|
|
1126
1233
|
return markElement(element);
|
|
1127
1234
|
};
|
|
1128
1235
|
// query element👇
|
|
@@ -1130,7 +1237,7 @@ function patchDocument() {
|
|
|
1130
1237
|
var _a, _b, _c;
|
|
1131
1238
|
const appName = getCurrentAppName();
|
|
1132
1239
|
if (!appName || selectors === 'head' || selectors === 'body' || selectors === 'html') {
|
|
1133
|
-
return rawQuerySelector.call(rawDocument, selectors);
|
|
1240
|
+
return globalEnv.rawQuerySelector.call(rawDocument, selectors);
|
|
1134
1241
|
}
|
|
1135
1242
|
return (_c = (_b = (_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.container) === null || _b === void 0 ? void 0 : _b.querySelector(selectors)) !== null && _c !== void 0 ? _c : null;
|
|
1136
1243
|
}
|
|
@@ -1138,7 +1245,7 @@ function patchDocument() {
|
|
|
1138
1245
|
var _a, _b, _c;
|
|
1139
1246
|
const appName = getCurrentAppName();
|
|
1140
1247
|
if (!appName || selectors === 'head' || selectors === 'body' || selectors === 'html') {
|
|
1141
|
-
return rawQuerySelectorAll.call(rawDocument, selectors);
|
|
1248
|
+
return globalEnv.rawQuerySelectorAll.call(rawDocument, selectors);
|
|
1142
1249
|
}
|
|
1143
1250
|
return (_c = (_b = (_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.container) === null || _b === void 0 ? void 0 : _b.querySelectorAll(selectors)) !== null && _c !== void 0 ? _c : [];
|
|
1144
1251
|
}
|
|
@@ -1148,14 +1255,14 @@ function patchDocument() {
|
|
|
1148
1255
|
Document.prototype.getElementById = function getElementById(key) {
|
|
1149
1256
|
const appName = getCurrentAppName();
|
|
1150
1257
|
if (!appName || /^\d/.test(key)) {
|
|
1151
|
-
return rawGetElementById.call(rawDocument, key);
|
|
1258
|
+
return globalEnv.rawGetElementById.call(rawDocument, key);
|
|
1152
1259
|
}
|
|
1153
1260
|
return querySelector(`#${key}`);
|
|
1154
1261
|
};
|
|
1155
1262
|
Document.prototype.getElementsByClassName = function getElementsByClassName(key) {
|
|
1156
1263
|
const appName = getCurrentAppName();
|
|
1157
1264
|
if (!appName || /^\d/.test(key)) {
|
|
1158
|
-
return rawGetElementsByClassName.call(rawDocument, key);
|
|
1265
|
+
return globalEnv.rawGetElementsByClassName.call(rawDocument, key);
|
|
1159
1266
|
}
|
|
1160
1267
|
return querySelectorAll(`.${key}`);
|
|
1161
1268
|
};
|
|
@@ -1167,40 +1274,40 @@ function patchDocument() {
|
|
|
1167
1274
|
/^head$/i.test(key) ||
|
|
1168
1275
|
/^html$/i.test(key) ||
|
|
1169
1276
|
(!((_a = appInstanceMap.get(appName)) === null || _a === void 0 ? void 0 : _a.inline) && /^script$/i.test(key))) {
|
|
1170
|
-
return rawGetElementsByTagName.call(rawDocument, key);
|
|
1277
|
+
return globalEnv.rawGetElementsByTagName.call(rawDocument, key);
|
|
1171
1278
|
}
|
|
1172
1279
|
return querySelectorAll(key);
|
|
1173
1280
|
};
|
|
1174
1281
|
Document.prototype.getElementsByName = function getElementsByName(key) {
|
|
1175
1282
|
const appName = getCurrentAppName();
|
|
1176
1283
|
if (!appName || /^\d/.test(key)) {
|
|
1177
|
-
return rawGetElementsByName.call(rawDocument, key);
|
|
1284
|
+
return globalEnv.rawGetElementsByName.call(rawDocument, key);
|
|
1178
1285
|
}
|
|
1179
1286
|
return querySelectorAll(`[name=${key}]`);
|
|
1180
1287
|
};
|
|
1181
1288
|
}
|
|
1182
1289
|
function releasePatchDocument() {
|
|
1183
|
-
Document.prototype.createElement = rawCreateElement;
|
|
1184
|
-
Document.prototype.createElementNS = rawCreateElementNS;
|
|
1185
|
-
Document.prototype.createDocumentFragment = rawCreateDocumentFragment;
|
|
1186
|
-
Document.prototype.querySelector = rawQuerySelector;
|
|
1187
|
-
Document.prototype.querySelectorAll = rawQuerySelectorAll;
|
|
1188
|
-
Document.prototype.getElementById = rawGetElementById;
|
|
1189
|
-
Document.prototype.getElementsByClassName = rawGetElementsByClassName;
|
|
1190
|
-
Document.prototype.getElementsByTagName = rawGetElementsByTagName;
|
|
1191
|
-
Document.prototype.getElementsByName = rawGetElementsByName;
|
|
1290
|
+
Document.prototype.createElement = globalEnv.rawCreateElement;
|
|
1291
|
+
Document.prototype.createElementNS = globalEnv.rawCreateElementNS;
|
|
1292
|
+
Document.prototype.createDocumentFragment = globalEnv.rawCreateDocumentFragment;
|
|
1293
|
+
Document.prototype.querySelector = globalEnv.rawQuerySelector;
|
|
1294
|
+
Document.prototype.querySelectorAll = globalEnv.rawQuerySelectorAll;
|
|
1295
|
+
Document.prototype.getElementById = globalEnv.rawGetElementById;
|
|
1296
|
+
Document.prototype.getElementsByClassName = globalEnv.rawGetElementsByClassName;
|
|
1297
|
+
Document.prototype.getElementsByTagName = globalEnv.rawGetElementsByTagName;
|
|
1298
|
+
Document.prototype.getElementsByName = globalEnv.rawGetElementsByName;
|
|
1192
1299
|
}
|
|
1193
1300
|
// release patch
|
|
1194
1301
|
function releasePatches() {
|
|
1195
1302
|
setCurrentAppName(null);
|
|
1196
1303
|
releasePatchDocument();
|
|
1197
|
-
Element.prototype.setAttribute = rawSetAttribute;
|
|
1198
|
-
Node.prototype.appendChild = rawAppendChild;
|
|
1199
|
-
Node.prototype.insertBefore = rawInsertBefore;
|
|
1200
|
-
Node.prototype.replaceChild = rawReplaceChild;
|
|
1201
|
-
Node.prototype.removeChild = rawRemoveChild;
|
|
1202
|
-
Element.prototype.append = rawAppend;
|
|
1203
|
-
Element.prototype.prepend = rawPrepend;
|
|
1304
|
+
Element.prototype.setAttribute = globalEnv.rawSetAttribute;
|
|
1305
|
+
Node.prototype.appendChild = globalEnv.rawAppendChild;
|
|
1306
|
+
Node.prototype.insertBefore = globalEnv.rawInsertBefore;
|
|
1307
|
+
Node.prototype.replaceChild = globalEnv.rawReplaceChild;
|
|
1308
|
+
Node.prototype.removeChild = globalEnv.rawRemoveChild;
|
|
1309
|
+
Element.prototype.append = globalEnv.rawAppend;
|
|
1310
|
+
Element.prototype.prepend = globalEnv.rawPrepend;
|
|
1204
1311
|
}
|
|
1205
1312
|
// Set the style of micro-app-head and micro-app-body
|
|
1206
1313
|
let hasRejectMicroAppStyle = false;
|
|
@@ -1210,7 +1317,7 @@ function rejectMicroAppStyle() {
|
|
|
1210
1317
|
const style = pureCreateElement('style');
|
|
1211
1318
|
style.setAttribute('type', 'text/css');
|
|
1212
1319
|
style.textContent = `\n${microApp.tagName}, micro-app-body { display: block; } \nmicro-app-head { display: none; }`;
|
|
1213
|
-
rawDocument.head.appendChild(style);
|
|
1320
|
+
globalEnv.rawDocument.head.appendChild(style);
|
|
1214
1321
|
}
|
|
1215
1322
|
}
|
|
1216
1323
|
|
|
@@ -1270,7 +1377,8 @@ function dispatchUnmountToMicroApp(appName) {
|
|
|
1270
1377
|
window.dispatchEvent(event);
|
|
1271
1378
|
}
|
|
1272
1379
|
|
|
1273
|
-
function
|
|
1380
|
+
function unmountNestedApp() {
|
|
1381
|
+
replaseUnmountOfNestedApp();
|
|
1274
1382
|
appInstanceMap.forEach(app => {
|
|
1275
1383
|
let element = app.container;
|
|
1276
1384
|
if (element) {
|
|
@@ -1281,237 +1389,273 @@ function unmountAppInline() {
|
|
|
1281
1389
|
element.disconnectedCallback();
|
|
1282
1390
|
}
|
|
1283
1391
|
});
|
|
1284
|
-
|
|
1392
|
+
if (!window.__MICRO_APP_UMD_MODE__)
|
|
1393
|
+
appInstanceMap.clear();
|
|
1394
|
+
if (elementInstanceMap.size) {
|
|
1395
|
+
elementInstanceMap.clear();
|
|
1396
|
+
releasePatches();
|
|
1397
|
+
}
|
|
1285
1398
|
}
|
|
1286
1399
|
// if micro-app run in micro application, delete all next generation application when unmount event received
|
|
1287
|
-
function
|
|
1400
|
+
function listenUmountOfNestedApp() {
|
|
1288
1401
|
if (window.__MICRO_APP_ENVIRONMENT__) {
|
|
1289
|
-
window.addEventListener('unmount',
|
|
1402
|
+
window.addEventListener('unmount', unmountNestedApp, false);
|
|
1290
1403
|
}
|
|
1291
1404
|
}
|
|
1292
1405
|
// release listener
|
|
1293
|
-
function
|
|
1406
|
+
function replaseUnmountOfNestedApp() {
|
|
1294
1407
|
if (window.__MICRO_APP_ENVIRONMENT__) {
|
|
1295
|
-
window.removeEventListener('unmount',
|
|
1408
|
+
window.removeEventListener('unmount', unmountNestedApp, false);
|
|
1296
1409
|
}
|
|
1297
1410
|
}
|
|
1298
1411
|
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1412
|
+
// record all micro-app elements
|
|
1413
|
+
const elementInstanceMap = new Map();
|
|
1414
|
+
/**
|
|
1415
|
+
* define element
|
|
1416
|
+
* @param tagName element name
|
|
1417
|
+
*/
|
|
1418
|
+
function defineElement(tagName) {
|
|
1419
|
+
class MicroAppElement extends HTMLElement {
|
|
1420
|
+
constructor() {
|
|
1421
|
+
super();
|
|
1422
|
+
this.appName = '';
|
|
1423
|
+
this.appUrl = '';
|
|
1424
|
+
this.version = version;
|
|
1312
1425
|
this.isWating = false;
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1426
|
+
this.cacheData = null;
|
|
1427
|
+
this.hasConnected = false;
|
|
1428
|
+
/**
|
|
1429
|
+
* handle for change of name an url after element inited
|
|
1430
|
+
*/
|
|
1431
|
+
this.handleAttributeUpdate = () => {
|
|
1432
|
+
var _a;
|
|
1433
|
+
this.isWating = false;
|
|
1434
|
+
const attrName = this.getAttribute('name');
|
|
1435
|
+
const attrUrl = formatURL(this.getAttribute('url'));
|
|
1436
|
+
if (this.legalAttribute('name', attrName) && this.legalAttribute('url', attrUrl)) {
|
|
1437
|
+
const existApp = appInstanceMap.get(attrName);
|
|
1438
|
+
if (attrName !== this.appName && existApp) {
|
|
1439
|
+
// handling of cached and non-prefetch apps
|
|
1440
|
+
if (appStatus.UNMOUNT !== existApp.getAppStatus() && !existApp.isPrefetch) {
|
|
1441
|
+
this.setAttribute('name', this.appName);
|
|
1442
|
+
return logError(`an app named ${attrName} already exists`);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
if (attrName !== this.appName || attrUrl !== this.appUrl) {
|
|
1446
|
+
this.handleUnmount(attrName === this.appName);
|
|
1447
|
+
this.appName = attrName;
|
|
1448
|
+
this.appUrl = attrUrl;
|
|
1449
|
+
((_a = this.shadowRoot) !== null && _a !== void 0 ? _a : this).innerHTML = '';
|
|
1450
|
+
/**
|
|
1451
|
+
* when existApp not undefined
|
|
1452
|
+
* if attrName and this.appName are equal, existApp has been unmounted
|
|
1453
|
+
* if attrName and this.appName are not equal, existApp is prefetch or unmounted
|
|
1454
|
+
*/
|
|
1455
|
+
if (existApp && existApp.url === attrUrl) {
|
|
1456
|
+
// mount app
|
|
1457
|
+
this.handleAppMount(existApp);
|
|
1458
|
+
}
|
|
1459
|
+
else {
|
|
1460
|
+
this.handleCreate();
|
|
1461
|
+
}
|
|
1322
1462
|
}
|
|
1323
1463
|
}
|
|
1324
|
-
if (attrName !== this.appName
|
|
1325
|
-
this.
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1464
|
+
else if (attrName !== this.appName) {
|
|
1465
|
+
this.setAttribute('name', this.appName);
|
|
1466
|
+
}
|
|
1467
|
+
};
|
|
1468
|
+
// cloned node of umd container also trigger constructor, we should skip
|
|
1469
|
+
if (!this.querySelector('micro-app-head')) {
|
|
1470
|
+
this.performWhenFirstCreated();
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
static get observedAttributes() {
|
|
1474
|
+
return ['name', 'url'];
|
|
1475
|
+
}
|
|
1476
|
+
// 👇 Configuration
|
|
1477
|
+
// shadowDom: use shadowDOM, default is false
|
|
1478
|
+
// destory: whether delete cache resources when unmount, default is false
|
|
1479
|
+
// inline: whether js runs in inline script mode, default is false
|
|
1480
|
+
// disableScopecss: whether disable css scoped, default is false
|
|
1481
|
+
// disableSandbox: whether disable sandbox, default is false
|
|
1482
|
+
// macro: used to solve the async render problem of vue3, default is false
|
|
1483
|
+
// baseRoute: route prefix, default is ''
|
|
1484
|
+
connectedCallback() {
|
|
1485
|
+
this.hasConnected = true;
|
|
1486
|
+
if (!elementInstanceMap.has(this)) {
|
|
1487
|
+
this.performWhenFirstCreated();
|
|
1488
|
+
}
|
|
1489
|
+
defer(() => dispatchLifecyclesEvent(this, this.appName, lifeCycles.CREATED));
|
|
1490
|
+
this.initialMount();
|
|
1491
|
+
}
|
|
1492
|
+
disconnectedCallback() {
|
|
1493
|
+
this.hasConnected = false;
|
|
1494
|
+
elementInstanceMap.delete(this);
|
|
1495
|
+
this.handleUnmount(this.getDisposeResult('destory'));
|
|
1496
|
+
if (elementInstanceMap.size === 0) {
|
|
1497
|
+
releasePatches();
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
attributeChangedCallback(attr, _oldVal, newVal) {
|
|
1501
|
+
if (this.legalAttribute(attr, newVal) &&
|
|
1502
|
+
this[attr === ObservedAttrName.NAME ? 'appName' : 'appUrl'] !== newVal) {
|
|
1503
|
+
if (attr === ObservedAttrName.URL && !this.appUrl) {
|
|
1504
|
+
newVal = formatURL(newVal);
|
|
1505
|
+
if (!newVal) {
|
|
1506
|
+
return logError('Invalid attribute url');
|
|
1337
1507
|
}
|
|
1338
|
-
|
|
1339
|
-
|
|
1508
|
+
this.appUrl = newVal;
|
|
1509
|
+
this.handleInitialNameAndUrl();
|
|
1510
|
+
}
|
|
1511
|
+
else if (attr === ObservedAttrName.NAME && !this.appName) {
|
|
1512
|
+
if (this.cacheData) {
|
|
1513
|
+
microApp.setData(newVal, this.cacheData);
|
|
1514
|
+
this.cacheData = null;
|
|
1340
1515
|
}
|
|
1516
|
+
this.appName = newVal;
|
|
1517
|
+
this.handleInitialNameAndUrl();
|
|
1518
|
+
}
|
|
1519
|
+
else if (!this.isWating) {
|
|
1520
|
+
this.isWating = true;
|
|
1521
|
+
defer(this.handleAttributeUpdate);
|
|
1341
1522
|
}
|
|
1342
1523
|
}
|
|
1343
|
-
else if (attrName !== this.appName) {
|
|
1344
|
-
this.setAttribute('name', this.appName);
|
|
1345
|
-
}
|
|
1346
|
-
};
|
|
1347
|
-
}
|
|
1348
|
-
static get observedAttributes() {
|
|
1349
|
-
return ['name', 'url'];
|
|
1350
|
-
}
|
|
1351
|
-
// 👇Configuration
|
|
1352
|
-
// shadowDom: use shadowDOM, default is false
|
|
1353
|
-
// destory: whether delete cache resources when unmount, default is false
|
|
1354
|
-
// inline: whether js runs in inline script mode, default is false
|
|
1355
|
-
// disableScopecss: whether disable css scoped, default is false
|
|
1356
|
-
// disableSandbox: whether disable sandbox, default is false
|
|
1357
|
-
// macro: used to solve the async render problem of vue3, default is false
|
|
1358
|
-
// baseUrl: route prefix, default is ''
|
|
1359
|
-
connectedCallback() {
|
|
1360
|
-
if (++MicroAppElement.microAppCount === 1) {
|
|
1361
|
-
patchElementPrototypeMethods();
|
|
1362
|
-
rejectMicroAppStyle();
|
|
1363
|
-
listenUmountAppInline();
|
|
1364
|
-
}
|
|
1365
|
-
defer(() => dispatchLifecyclesEvent(this, this.appName, lifeCycles.CREATED));
|
|
1366
|
-
if (!this.appName || !this.appUrl)
|
|
1367
|
-
return;
|
|
1368
|
-
if (this.getDisposeResult('shadowDOM') && !this.shadowRoot) {
|
|
1369
|
-
this.attachShadow({ mode: 'open' });
|
|
1370
1524
|
}
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
if (
|
|
1374
|
-
|
|
1375
|
-
this.handleAppMount(app);
|
|
1376
|
-
}
|
|
1377
|
-
else if (app.isPrefetch) {
|
|
1378
|
-
logError(`the url: ${this.appUrl} is different from prefetch url: ${app.url}`);
|
|
1525
|
+
// handle for connectedCallback run before attributeChangedCallback
|
|
1526
|
+
handleInitialNameAndUrl() {
|
|
1527
|
+
if (this.hasConnected) {
|
|
1528
|
+
this.initialMount();
|
|
1379
1529
|
}
|
|
1380
|
-
else {
|
|
1381
|
-
logError(`an app named ${this.appName} already exists`);
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
else {
|
|
1385
|
-
this.handleCreate();
|
|
1386
1530
|
}
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1531
|
+
// Perform global initialization when the element count is 1
|
|
1532
|
+
performWhenFirstCreated() {
|
|
1533
|
+
if (elementInstanceMap.set(this, true).size === 1) {
|
|
1534
|
+
patchElementPrototypeMethods();
|
|
1535
|
+
rejectMicroAppStyle();
|
|
1536
|
+
replaseUnmountOfNestedApp();
|
|
1537
|
+
listenUmountOfNestedApp();
|
|
1394
1538
|
}
|
|
1395
1539
|
}
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
if (
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1540
|
+
/**
|
|
1541
|
+
* first mount of this app
|
|
1542
|
+
*/
|
|
1543
|
+
initialMount() {
|
|
1544
|
+
if (!this.appName || !this.appUrl)
|
|
1545
|
+
return;
|
|
1546
|
+
if (this.getDisposeResult('shadowDOM') && !this.shadowRoot) {
|
|
1547
|
+
this.attachShadow({ mode: 'open' });
|
|
1548
|
+
}
|
|
1549
|
+
const app = appInstanceMap.get(this.appName);
|
|
1550
|
+
if (app) {
|
|
1551
|
+
if (app.url === this.appUrl && (app.isPrefetch ||
|
|
1552
|
+
app.getAppStatus() === appStatus.UNMOUNT)) {
|
|
1553
|
+
this.handleAppMount(app);
|
|
1404
1554
|
}
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
this.cacheData = null;
|
|
1555
|
+
else if (app.isPrefetch) {
|
|
1556
|
+
logError(`the url: ${this.appUrl} is different from prefetch url: ${app.url}`);
|
|
1557
|
+
}
|
|
1558
|
+
else {
|
|
1559
|
+
logError(`an app named ${this.appName} already exists`);
|
|
1411
1560
|
}
|
|
1412
|
-
this.appName = newVal;
|
|
1413
1561
|
}
|
|
1414
|
-
else
|
|
1415
|
-
this.
|
|
1416
|
-
defer(this.handleAttributeUpdate);
|
|
1562
|
+
else {
|
|
1563
|
+
this.handleCreate();
|
|
1417
1564
|
}
|
|
1418
1565
|
}
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1566
|
+
/**
|
|
1567
|
+
* judge the attribute is legal
|
|
1568
|
+
* @param name attribute name
|
|
1569
|
+
* @param val attribute value
|
|
1570
|
+
*/
|
|
1571
|
+
legalAttribute(name, val) {
|
|
1572
|
+
if (typeof val !== 'string' || !val) {
|
|
1573
|
+
logError(`unexpected attribute ${name}, please check again`);
|
|
1574
|
+
return false;
|
|
1575
|
+
}
|
|
1576
|
+
return true;
|
|
1429
1577
|
}
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
return app.mount((_a = this.shadowRoot) !== null && _a !== void 0 ? _a : this, this.getDisposeResult('inline'), (_b = this.getAttribute('baseurl')) !== null && _b !== void 0 ? _b : '');
|
|
1444
|
-
});
|
|
1445
|
-
}
|
|
1446
|
-
// create app instance
|
|
1447
|
-
handleCreate() {
|
|
1448
|
-
var _a, _b;
|
|
1449
|
-
const instance = new CreateApp({
|
|
1450
|
-
name: this.appName,
|
|
1451
|
-
url: this.appUrl,
|
|
1452
|
-
container: (_a = this.shadowRoot) !== null && _a !== void 0 ? _a : this,
|
|
1453
|
-
inline: this.getDisposeResult('inline'),
|
|
1454
|
-
scopecss: !(this.getDisposeResult('disableScopecss') || this.getDisposeResult('shadowDOM')),
|
|
1455
|
-
useSandbox: !this.getDisposeResult('disableSandbox'),
|
|
1456
|
-
macro: this.getDisposeResult('macro'),
|
|
1457
|
-
baseurl: (_b = this.getAttribute('baseurl')) !== null && _b !== void 0 ? _b : '',
|
|
1458
|
-
});
|
|
1459
|
-
appInstanceMap.set(this.appName, instance);
|
|
1460
|
-
}
|
|
1461
|
-
/**
|
|
1462
|
-
* unmount app
|
|
1463
|
-
* @param destory delete cache resources when unmount
|
|
1464
|
-
*/
|
|
1465
|
-
handleUnmount(destory) {
|
|
1466
|
-
const app = appInstanceMap.get(this.appName);
|
|
1467
|
-
if (app && appStatus.UNMOUNT !== app.getAppStatus())
|
|
1468
|
-
app.unmount(destory);
|
|
1469
|
-
}
|
|
1470
|
-
/**
|
|
1471
|
-
* Get configuration
|
|
1472
|
-
* Global setting is lowest priority
|
|
1473
|
-
* @param name Configuration item name
|
|
1474
|
-
*/
|
|
1475
|
-
getDisposeResult(name) {
|
|
1476
|
-
// @ts-ignore
|
|
1477
|
-
return (this.hasAttribute(name) || microApp[name]) && this.getAttribute(name) !== 'false';
|
|
1478
|
-
}
|
|
1479
|
-
/**
|
|
1480
|
-
* Data from the base application
|
|
1481
|
-
*/
|
|
1482
|
-
set data(value) {
|
|
1483
|
-
if (this.appName) {
|
|
1484
|
-
microApp.setData(this.appName, value);
|
|
1578
|
+
/**
|
|
1579
|
+
* mount app
|
|
1580
|
+
* some serious note before mount:
|
|
1581
|
+
* 1. is prefetch ?
|
|
1582
|
+
* 2. is remount in another container ?
|
|
1583
|
+
* 3. is remount with change properties of the container ?
|
|
1584
|
+
*/
|
|
1585
|
+
handleAppMount(app) {
|
|
1586
|
+
app.isPrefetch = false;
|
|
1587
|
+
defer(() => {
|
|
1588
|
+
var _a;
|
|
1589
|
+
return app.mount((_a = this.shadowRoot) !== null && _a !== void 0 ? _a : this, this.getDisposeResult('inline'), this.getBaseRouteCompatible());
|
|
1590
|
+
});
|
|
1485
1591
|
}
|
|
1486
|
-
|
|
1487
|
-
|
|
1592
|
+
// create app instance
|
|
1593
|
+
handleCreate() {
|
|
1594
|
+
var _a;
|
|
1595
|
+
const instance = new CreateApp({
|
|
1596
|
+
name: this.appName,
|
|
1597
|
+
url: this.appUrl,
|
|
1598
|
+
container: (_a = this.shadowRoot) !== null && _a !== void 0 ? _a : this,
|
|
1599
|
+
inline: this.getDisposeResult('inline'),
|
|
1600
|
+
scopecss: !(this.getDisposeResult('disableScopecss') || this.getDisposeResult('shadowDOM')),
|
|
1601
|
+
useSandbox: !this.getDisposeResult('disableSandbox'),
|
|
1602
|
+
macro: this.getDisposeResult('macro'),
|
|
1603
|
+
baseroute: this.getBaseRouteCompatible(),
|
|
1604
|
+
});
|
|
1605
|
+
appInstanceMap.set(this.appName, instance);
|
|
1488
1606
|
}
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1607
|
+
/**
|
|
1608
|
+
* unmount app
|
|
1609
|
+
* @param destory delete cache resources when unmount
|
|
1610
|
+
*/
|
|
1611
|
+
handleUnmount(destory) {
|
|
1612
|
+
const app = appInstanceMap.get(this.appName);
|
|
1613
|
+
if (app && appStatus.UNMOUNT !== app.getAppStatus())
|
|
1614
|
+
app.unmount(destory);
|
|
1496
1615
|
}
|
|
1497
|
-
|
|
1498
|
-
|
|
1616
|
+
/**
|
|
1617
|
+
* Get configuration
|
|
1618
|
+
* Global setting is lowest priority
|
|
1619
|
+
* @param name Configuration item name
|
|
1620
|
+
*/
|
|
1621
|
+
getDisposeResult(name) {
|
|
1622
|
+
// @ts-ignore
|
|
1623
|
+
return (this.hasAttribute(name) || microApp[name]) && this.getAttribute(name) !== 'false';
|
|
1624
|
+
}
|
|
1625
|
+
/**
|
|
1626
|
+
* 2021-09-08
|
|
1627
|
+
* get baseRoute
|
|
1628
|
+
* getAttribute('baseurl') is compatible writing of versions below 0.3.1
|
|
1629
|
+
*/
|
|
1630
|
+
getBaseRouteCompatible() {
|
|
1631
|
+
var _a, _b;
|
|
1632
|
+
return (_b = (_a = this.getAttribute('baseroute')) !== null && _a !== void 0 ? _a : this.getAttribute('baseurl')) !== null && _b !== void 0 ? _b : '';
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Data from the base application
|
|
1636
|
+
*/
|
|
1637
|
+
set data(value) {
|
|
1638
|
+
if (this.appName) {
|
|
1639
|
+
microApp.setData(this.appName, value);
|
|
1640
|
+
}
|
|
1641
|
+
else {
|
|
1642
|
+
this.cacheData = value;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
/**
|
|
1646
|
+
* get data only used in jsx-custom-event once
|
|
1647
|
+
*/
|
|
1648
|
+
get data() {
|
|
1649
|
+
if (this.appName) {
|
|
1650
|
+
return microApp.getData(this.appName, true);
|
|
1651
|
+
}
|
|
1652
|
+
else if (this.cacheData) {
|
|
1653
|
+
return this.cacheData;
|
|
1654
|
+
}
|
|
1655
|
+
return null;
|
|
1499
1656
|
}
|
|
1500
|
-
return null;
|
|
1501
|
-
}
|
|
1502
|
-
}
|
|
1503
|
-
MicroAppElement.microAppCount = 0;
|
|
1504
|
-
/**
|
|
1505
|
-
* define element
|
|
1506
|
-
* @param tagName element name
|
|
1507
|
-
*/
|
|
1508
|
-
function defineElement(tagName) {
|
|
1509
|
-
if (window.customElements.get(tagName)) {
|
|
1510
|
-
logWarn(`element ${tagName} is already defined`);
|
|
1511
|
-
return false;
|
|
1512
1657
|
}
|
|
1513
1658
|
window.customElements.define(tagName, MicroAppElement);
|
|
1514
|
-
return true;
|
|
1515
1659
|
}
|
|
1516
1660
|
|
|
1517
1661
|
class EventCenter {
|
|
@@ -1803,8 +1947,8 @@ class MicroApp extends EventCenterForBaseApp {
|
|
|
1803
1947
|
this.preFetch = preFetch;
|
|
1804
1948
|
}
|
|
1805
1949
|
start(options) {
|
|
1806
|
-
if (!
|
|
1807
|
-
return logError('
|
|
1950
|
+
if (!isBrowser || !window.customElements) {
|
|
1951
|
+
return logError('micro-app is not supported in this environment');
|
|
1808
1952
|
}
|
|
1809
1953
|
if (options === null || options === void 0 ? void 0 : options.tagName) {
|
|
1810
1954
|
if (/^micro-app(-\S+)?/.test(options.tagName)) {
|
|
@@ -1814,7 +1958,11 @@ class MicroApp extends EventCenterForBaseApp {
|
|
|
1814
1958
|
return logError(`${options.tagName} is invalid tagName`);
|
|
1815
1959
|
}
|
|
1816
1960
|
}
|
|
1817
|
-
if (
|
|
1961
|
+
if (window.customElements.get(this.tagName)) {
|
|
1962
|
+
return logWarn(`element ${this.tagName} is already defined`);
|
|
1963
|
+
}
|
|
1964
|
+
initGloalEnv();
|
|
1965
|
+
if (options && toString.call(options) === '[object Object]') {
|
|
1818
1966
|
this.shadowDOM = options.shadowDOM;
|
|
1819
1967
|
this.destory = options.destory;
|
|
1820
1968
|
this.inline = options.inline;
|
|
@@ -1838,6 +1986,7 @@ class MicroApp extends EventCenterForBaseApp {
|
|
|
1838
1986
|
getGlobalAssets(options.globalAssets);
|
|
1839
1987
|
}
|
|
1840
1988
|
}
|
|
1989
|
+
defineElement(this.tagName);
|
|
1841
1990
|
}
|
|
1842
1991
|
}
|
|
1843
1992
|
var microApp = new MicroApp();
|
|
@@ -1882,7 +2031,7 @@ function flatChildren(parent, app, microAppHead) {
|
|
|
1882
2031
|
if (dom.hasAttribute('exclude')) {
|
|
1883
2032
|
parent.replaceChild(document.createComment('link element with exclude attribute ignored by micro-app'), dom);
|
|
1884
2033
|
}
|
|
1885
|
-
else if (app.scopecss) {
|
|
2034
|
+
else if (app.scopecss && !dom.hasAttribute('ignore')) {
|
|
1886
2035
|
extractLinkFromHtml(dom, parent, app, microAppHead);
|
|
1887
2036
|
}
|
|
1888
2037
|
else if (dom.hasAttribute('href')) {
|
|
@@ -1893,7 +2042,7 @@ function flatChildren(parent, app, microAppHead) {
|
|
|
1893
2042
|
if (dom.hasAttribute('exclude')) {
|
|
1894
2043
|
parent.replaceChild(document.createComment('style element with exclude attribute ignored by micro-app'), dom);
|
|
1895
2044
|
}
|
|
1896
|
-
else if (app.scopecss) {
|
|
2045
|
+
else if (app.scopecss && !dom.hasAttribute('ignore')) {
|
|
1897
2046
|
microAppHead.appendChild(scopedCSS(dom, app.name));
|
|
1898
2047
|
}
|
|
1899
2048
|
}
|
|
@@ -2011,15 +2160,6 @@ function bindFunctionToRawWidow(rawWindow, value) {
|
|
|
2011
2160
|
return value;
|
|
2012
2161
|
}
|
|
2013
2162
|
|
|
2014
|
-
// save raw methods
|
|
2015
|
-
const rawWindowAddEventListener = window.addEventListener;
|
|
2016
|
-
const rawWindowRemoveEventListener = window.removeEventListener;
|
|
2017
|
-
const rawSetInterval = window.setInterval;
|
|
2018
|
-
const rawSetTimeout = window.setTimeout;
|
|
2019
|
-
const rawClearInterval = window.clearInterval;
|
|
2020
|
-
const rawClearTimeout = window.clearTimeout;
|
|
2021
|
-
const rawDocumentAddEventListener = document.addEventListener;
|
|
2022
|
-
const rawDocumentRemoveEventListener = document.removeEventListener;
|
|
2023
2163
|
// document.onclick binding list, the binding function of each application is unique
|
|
2024
2164
|
const documentClickListMap = new Map();
|
|
2025
2165
|
let hasRewriteDocumentOnClick = false;
|
|
@@ -2057,7 +2197,7 @@ function overwriteDocumentOnClick() {
|
|
|
2057
2197
|
}
|
|
2058
2198
|
if (!hasDocumentClickInited && typeof f === 'function') {
|
|
2059
2199
|
hasDocumentClickInited = true;
|
|
2060
|
-
rawDocumentAddEventListener.call(
|
|
2200
|
+
globalEnv.rawDocumentAddEventListener.call(globalEnv.rawDocument, 'click', onClickHandler, false);
|
|
2061
2201
|
}
|
|
2062
2202
|
}
|
|
2063
2203
|
});
|
|
@@ -2070,6 +2210,7 @@ function overwriteDocumentOnClick() {
|
|
|
2070
2210
|
*/
|
|
2071
2211
|
const documentEventListenerMap = new Map();
|
|
2072
2212
|
function effectDocumentEvent() {
|
|
2213
|
+
const { rawDocument, rawDocumentAddEventListener, rawDocumentRemoveEventListener, } = globalEnv;
|
|
2073
2214
|
if (!hasRewriteDocumentOnClick) {
|
|
2074
2215
|
overwriteDocumentOnClick();
|
|
2075
2216
|
}
|
|
@@ -2091,7 +2232,7 @@ function effectDocumentEvent() {
|
|
|
2091
2232
|
}
|
|
2092
2233
|
listener && (listener.__MICRO_MARK_OPTIONS__ = options);
|
|
2093
2234
|
}
|
|
2094
|
-
rawDocumentAddEventListener.call(
|
|
2235
|
+
rawDocumentAddEventListener.call(rawDocument, type, listener, options);
|
|
2095
2236
|
};
|
|
2096
2237
|
document.removeEventListener = function (type, listener, options) {
|
|
2097
2238
|
const appName = getCurrentAppName();
|
|
@@ -2104,13 +2245,13 @@ function effectDocumentEvent() {
|
|
|
2104
2245
|
}
|
|
2105
2246
|
}
|
|
2106
2247
|
}
|
|
2107
|
-
rawDocumentRemoveEventListener.call(
|
|
2248
|
+
rawDocumentRemoveEventListener.call(rawDocument, type, listener, options);
|
|
2108
2249
|
};
|
|
2109
2250
|
}
|
|
2110
2251
|
// Clear the document event agent
|
|
2111
2252
|
function releaseEffectDocumentEvent() {
|
|
2112
|
-
document.addEventListener = rawDocumentAddEventListener;
|
|
2113
|
-
document.removeEventListener = rawDocumentRemoveEventListener;
|
|
2253
|
+
document.addEventListener = globalEnv.rawDocumentAddEventListener;
|
|
2254
|
+
document.removeEventListener = globalEnv.rawDocumentRemoveEventListener;
|
|
2114
2255
|
}
|
|
2115
2256
|
/**
|
|
2116
2257
|
* Format event name
|
|
@@ -2132,6 +2273,7 @@ function effect(microWindow) {
|
|
|
2132
2273
|
const eventListenerMap = new Map();
|
|
2133
2274
|
const intervalIdMap = new Map();
|
|
2134
2275
|
const timeoutIdMap = new Map();
|
|
2276
|
+
const { rawWindow, rawDocument, rawWindowAddEventListener, rawWindowRemoveEventListener, rawSetInterval, rawSetTimeout, rawClearInterval, rawClearTimeout, rawDocumentRemoveEventListener, } = globalEnv;
|
|
2135
2277
|
// listener may be null, e.g test-passive
|
|
2136
2278
|
microWindow.addEventListener = function (type, listener, options) {
|
|
2137
2279
|
type = formatEventType(type, microWindow);
|
|
@@ -2143,7 +2285,7 @@ function effect(microWindow) {
|
|
|
2143
2285
|
eventListenerMap.set(type, new Set([listener]));
|
|
2144
2286
|
}
|
|
2145
2287
|
listener && (listener.__MICRO_MARK_OPTIONS__ = options);
|
|
2146
|
-
rawWindowAddEventListener.call(
|
|
2288
|
+
rawWindowAddEventListener.call(rawWindow, type, listener, options);
|
|
2147
2289
|
};
|
|
2148
2290
|
microWindow.removeEventListener = function (type, listener, options) {
|
|
2149
2291
|
type = formatEventType(type, microWindow);
|
|
@@ -2151,25 +2293,25 @@ function effect(microWindow) {
|
|
|
2151
2293
|
if ((listenerList === null || listenerList === void 0 ? void 0 : listenerList.size) && listenerList.has(listener)) {
|
|
2152
2294
|
listenerList.delete(listener);
|
|
2153
2295
|
}
|
|
2154
|
-
rawWindowRemoveEventListener.call(
|
|
2296
|
+
rawWindowRemoveEventListener.call(rawWindow, type, listener, options);
|
|
2155
2297
|
};
|
|
2156
2298
|
microWindow.setInterval = function (handler, timeout, ...args) {
|
|
2157
|
-
const intervalId = rawSetInterval(handler, timeout, ...args);
|
|
2299
|
+
const intervalId = rawSetInterval.call(rawWindow, handler, timeout, ...args);
|
|
2158
2300
|
intervalIdMap.set(intervalId, { handler, timeout, args });
|
|
2159
2301
|
return intervalId;
|
|
2160
2302
|
};
|
|
2161
2303
|
microWindow.setTimeout = function (handler, timeout, ...args) {
|
|
2162
|
-
const timeoutId = rawSetTimeout(handler, timeout, ...args);
|
|
2304
|
+
const timeoutId = rawSetTimeout.call(rawWindow, handler, timeout, ...args);
|
|
2163
2305
|
timeoutIdMap.set(timeoutId, { handler, timeout, args });
|
|
2164
2306
|
return timeoutId;
|
|
2165
2307
|
};
|
|
2166
2308
|
microWindow.clearInterval = function (intervalId) {
|
|
2167
2309
|
intervalIdMap.delete(intervalId);
|
|
2168
|
-
rawClearInterval(intervalId);
|
|
2310
|
+
rawClearInterval.call(rawWindow, intervalId);
|
|
2169
2311
|
};
|
|
2170
2312
|
microWindow.clearTimeout = function (timeoutId) {
|
|
2171
2313
|
timeoutIdMap.delete(timeoutId);
|
|
2172
|
-
rawClearTimeout(timeoutId);
|
|
2314
|
+
rawClearTimeout.call(rawWindow, timeoutId);
|
|
2173
2315
|
};
|
|
2174
2316
|
const umdWindowListenerMap = new Map();
|
|
2175
2317
|
const umdDocumentListenerMap = new Map();
|
|
@@ -2235,7 +2377,7 @@ function effect(microWindow) {
|
|
|
2235
2377
|
if (eventListenerMap.size) {
|
|
2236
2378
|
eventListenerMap.forEach((listenerList, type) => {
|
|
2237
2379
|
for (const listener of listenerList) {
|
|
2238
|
-
rawWindowRemoveEventListener.call(
|
|
2380
|
+
rawWindowRemoveEventListener.call(rawWindow, type, listener);
|
|
2239
2381
|
}
|
|
2240
2382
|
});
|
|
2241
2383
|
eventListenerMap.clear();
|
|
@@ -2243,13 +2385,13 @@ function effect(microWindow) {
|
|
|
2243
2385
|
// Clear timers
|
|
2244
2386
|
if (intervalIdMap.size) {
|
|
2245
2387
|
intervalIdMap.forEach((_, intervalId) => {
|
|
2246
|
-
rawClearInterval(intervalId);
|
|
2388
|
+
rawClearInterval.call(rawWindow, intervalId);
|
|
2247
2389
|
});
|
|
2248
2390
|
intervalIdMap.clear();
|
|
2249
2391
|
}
|
|
2250
2392
|
if (timeoutIdMap.size) {
|
|
2251
2393
|
timeoutIdMap.forEach((_, timeoutId) => {
|
|
2252
|
-
rawClearTimeout(timeoutId);
|
|
2394
|
+
rawClearTimeout.call(rawWindow, timeoutId);
|
|
2253
2395
|
});
|
|
2254
2396
|
timeoutIdMap.clear();
|
|
2255
2397
|
}
|
|
@@ -2260,7 +2402,7 @@ function effect(microWindow) {
|
|
|
2260
2402
|
if (documentAppListenersMap) {
|
|
2261
2403
|
documentAppListenersMap.forEach((listenerList, type) => {
|
|
2262
2404
|
for (const listener of listenerList) {
|
|
2263
|
-
rawDocumentRemoveEventListener.call(
|
|
2405
|
+
rawDocumentRemoveEventListener.call(rawDocument, type, listener);
|
|
2264
2406
|
}
|
|
2265
2407
|
});
|
|
2266
2408
|
documentAppListenersMap.clear();
|
|
@@ -2314,6 +2456,8 @@ class SandBox {
|
|
|
2314
2456
|
this.microWindow = {}; // Proxy target
|
|
2315
2457
|
this.injectedKeys = new Set(); // Properties newly added to microWindow
|
|
2316
2458
|
this.escapeKeys = new Set(); // Properties escape to rawWindow, cleared when unmount
|
|
2459
|
+
const rawWindow = globalEnv.rawWindow;
|
|
2460
|
+
const rawDocument = globalEnv.rawDocument;
|
|
2317
2461
|
const descriptorTargetMap = new Map();
|
|
2318
2462
|
const hasOwnProperty = (key) => this.microWindow.hasOwnProperty(key) || rawWindow.hasOwnProperty(key);
|
|
2319
2463
|
// get scopeProperties and escapeProperties from plugins
|
|
@@ -2349,10 +2493,11 @@ class SandBox {
|
|
|
2349
2493
|
return eval;
|
|
2350
2494
|
}
|
|
2351
2495
|
}
|
|
2352
|
-
if (
|
|
2496
|
+
if (Reflect.has(target, key)) {
|
|
2353
2497
|
return Reflect.get(target, key);
|
|
2354
2498
|
}
|
|
2355
|
-
if (
|
|
2499
|
+
if (this.scopeProperties.includes(key) ||
|
|
2500
|
+
(typeof key === 'string' && /^__MICRO_APP_/.test(key))) {
|
|
2356
2501
|
return Reflect.get(target, key);
|
|
2357
2502
|
}
|
|
2358
2503
|
const rawValue = Reflect.get(rawWindow, key);
|
|
@@ -2432,12 +2577,12 @@ class SandBox {
|
|
|
2432
2577
|
},
|
|
2433
2578
|
});
|
|
2434
2579
|
}
|
|
2435
|
-
start(
|
|
2580
|
+
start(baseroute) {
|
|
2436
2581
|
if (!this.active) {
|
|
2437
2582
|
this.active = true;
|
|
2438
|
-
this.microWindow.__MICRO_APP_BASE_URL__ =
|
|
2439
|
-
if (rawWindow._babelPolyfill)
|
|
2440
|
-
rawWindow._babelPolyfill = false;
|
|
2583
|
+
this.microWindow.__MICRO_APP_BASE_ROUTE__ = this.microWindow.__MICRO_APP_BASE_URL__ = baseroute;
|
|
2584
|
+
if (globalEnv.rawWindow._babelPolyfill)
|
|
2585
|
+
globalEnv.rawWindow._babelPolyfill = false;
|
|
2441
2586
|
if (++SandBox.activeCount === 1) {
|
|
2442
2587
|
effectDocumentEvent();
|
|
2443
2588
|
}
|
|
@@ -2454,7 +2599,7 @@ class SandBox {
|
|
|
2454
2599
|
});
|
|
2455
2600
|
this.injectedKeys.clear();
|
|
2456
2601
|
this.escapeKeys.forEach((key) => {
|
|
2457
|
-
Reflect.deleteProperty(rawWindow, key);
|
|
2602
|
+
Reflect.deleteProperty(globalEnv.rawWindow, key);
|
|
2458
2603
|
});
|
|
2459
2604
|
this.escapeKeys.clear();
|
|
2460
2605
|
if (--SandBox.activeCount === 0) {
|
|
@@ -2464,6 +2609,7 @@ class SandBox {
|
|
|
2464
2609
|
}
|
|
2465
2610
|
// record umd snapshot before the first execution of umdHookMount
|
|
2466
2611
|
recordUmdSnapshot() {
|
|
2612
|
+
this.microWindow.__MICRO_APP_UMD_MODE__ = true;
|
|
2467
2613
|
this.recordUmdEffect();
|
|
2468
2614
|
recordDataCenterSnapshot(this.microWindow.microApp);
|
|
2469
2615
|
this.recordUmdinjectedValues = new Map();
|
|
@@ -2523,8 +2669,8 @@ class SandBox {
|
|
|
2523
2669
|
microWindow.__MICRO_APP_NAME__ = appName;
|
|
2524
2670
|
microWindow.__MICRO_APP_PUBLIC_PATH__ = getEffectivePath(url);
|
|
2525
2671
|
microWindow.microApp = new EventCenterForMicroApp(appName);
|
|
2526
|
-
microWindow.rawWindow = rawWindow;
|
|
2527
|
-
microWindow.rawDocument = rawDocument;
|
|
2672
|
+
microWindow.rawWindow = globalEnv.rawWindow;
|
|
2673
|
+
microWindow.rawDocument = globalEnv.rawDocument;
|
|
2528
2674
|
microWindow.removeDomScope = removeDomScope;
|
|
2529
2675
|
}
|
|
2530
2676
|
}
|
|
@@ -2533,7 +2679,7 @@ SandBox.activeCount = 0; // number of active sandbox
|
|
|
2533
2679
|
// micro app instances
|
|
2534
2680
|
const appInstanceMap = new Map();
|
|
2535
2681
|
class CreateApp {
|
|
2536
|
-
constructor({ name, url, container, inline, scopecss, useSandbox, macro,
|
|
2682
|
+
constructor({ name, url, container, inline, scopecss, useSandbox, macro, baseroute }) {
|
|
2537
2683
|
this.status = appStatus.NOT_LOADED;
|
|
2538
2684
|
this.loadSourceLevel = 0;
|
|
2539
2685
|
this.umdHookMount = null;
|
|
@@ -2541,11 +2687,11 @@ class CreateApp {
|
|
|
2541
2687
|
this.isPrefetch = false;
|
|
2542
2688
|
this.container = null;
|
|
2543
2689
|
this.macro = false;
|
|
2544
|
-
this.
|
|
2690
|
+
this.baseroute = '';
|
|
2545
2691
|
this.sandBox = null;
|
|
2546
2692
|
this.container = container !== null && container !== void 0 ? container : null;
|
|
2547
2693
|
this.inline = inline !== null && inline !== void 0 ? inline : false;
|
|
2548
|
-
this.
|
|
2694
|
+
this.baseroute = baseroute !== null && baseroute !== void 0 ? baseroute : '';
|
|
2549
2695
|
// optional during init👆
|
|
2550
2696
|
this.name = name;
|
|
2551
2697
|
this.url = url;
|
|
@@ -2593,15 +2739,15 @@ class CreateApp {
|
|
|
2593
2739
|
* mount app
|
|
2594
2740
|
* @param container app container
|
|
2595
2741
|
* @param inline js runs in inline mode
|
|
2596
|
-
* @param
|
|
2742
|
+
* @param baseroute route prefix, default is ''
|
|
2597
2743
|
*/
|
|
2598
|
-
mount(container, inline,
|
|
2599
|
-
var _a, _b, _c
|
|
2744
|
+
mount(container, inline, baseroute) {
|
|
2745
|
+
var _a, _b, _c;
|
|
2600
2746
|
if (typeof inline === 'boolean' && inline !== this.inline) {
|
|
2601
2747
|
this.inline = inline;
|
|
2602
2748
|
}
|
|
2603
2749
|
this.container = (_a = this.container) !== null && _a !== void 0 ? _a : container;
|
|
2604
|
-
this.
|
|
2750
|
+
this.baseroute = baseroute !== null && baseroute !== void 0 ? baseroute : this.baseroute;
|
|
2605
2751
|
if (this.loadSourceLevel !== 2) {
|
|
2606
2752
|
this.status = appStatus.LOADING_SOURCE_CODE;
|
|
2607
2753
|
return;
|
|
@@ -2609,26 +2755,29 @@ class CreateApp {
|
|
|
2609
2755
|
dispatchLifecyclesEvent(this.container, this.name, lifeCycles.BEFOREMOUNT);
|
|
2610
2756
|
this.status = appStatus.MOUNTING;
|
|
2611
2757
|
cloneNode(this.source.html, this.container);
|
|
2612
|
-
(_b = this.sandBox) === null || _b === void 0 ? void 0 : _b.start(this.
|
|
2758
|
+
(_b = this.sandBox) === null || _b === void 0 ? void 0 : _b.start(this.baseroute);
|
|
2613
2759
|
if (!this.umdHookMount) {
|
|
2614
|
-
execScripts(this.source.scripts, this)
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2760
|
+
execScripts(this.source.scripts, this, () => {
|
|
2761
|
+
var _a;
|
|
2762
|
+
if (this.umdHookMount === null) {
|
|
2763
|
+
const { mount, unmount } = this.getUmdLibraryHooks();
|
|
2764
|
+
// if mount & unmount is function, the sub app is umd mode
|
|
2765
|
+
if (isFunction(mount) && isFunction(unmount)) {
|
|
2766
|
+
this.umdHookMount = mount;
|
|
2767
|
+
this.umdHookunMount = unmount;
|
|
2768
|
+
(_a = this.sandBox) === null || _a === void 0 ? void 0 : _a.recordUmdSnapshot();
|
|
2769
|
+
/**
|
|
2770
|
+
* TODO: Some UI frameworks insert and record container elements to micro-app-body, such as modal and notification. The DOM remounted is a cloned element, so the cached elements of UI frameworks are invalid, this may cause bug when remount app
|
|
2771
|
+
*/
|
|
2772
|
+
cloneNode(this.container, this.source.html);
|
|
2773
|
+
formatHTMLStyleAfterUmdInit(this.source.html, this.name);
|
|
2774
|
+
this.umdHookMount();
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2777
|
+
});
|
|
2629
2778
|
}
|
|
2630
2779
|
else {
|
|
2631
|
-
(
|
|
2780
|
+
(_c = this.sandBox) === null || _c === void 0 ? void 0 : _c.rebuildUmdSnapshot();
|
|
2632
2781
|
this.umdHookMount();
|
|
2633
2782
|
}
|
|
2634
2783
|
if (appStatus.UNMOUNT !== this.status) {
|
|
@@ -2677,7 +2826,7 @@ class CreateApp {
|
|
|
2677
2826
|
var _a, _b;
|
|
2678
2827
|
// after execScripts, the app maybe unmounted
|
|
2679
2828
|
if (appStatus.UNMOUNT !== this.status) {
|
|
2680
|
-
const global = ((_b = (_a = this.sandBox) === null || _a === void 0 ? void 0 : _a.proxyWindow) !== null && _b !== void 0 ? _b : rawWindow);
|
|
2829
|
+
const global = ((_b = (_a = this.sandBox) === null || _a === void 0 ? void 0 : _a.proxyWindow) !== null && _b !== void 0 ? _b : globalEnv.rawWindow);
|
|
2681
2830
|
const libraryName = (this.container instanceof ShadowRoot ? this.container.host : this.container).getAttribute('library') || `micro-app-${this.name}`;
|
|
2682
2831
|
return typeof global[libraryName] === 'object' ? global[libraryName] : {};
|
|
2683
2832
|
}
|
|
@@ -2717,6 +2866,9 @@ function filterPreFetchTarget(apps) {
|
|
|
2717
2866
|
* @param apps micro apps
|
|
2718
2867
|
*/
|
|
2719
2868
|
function preFetch(apps) {
|
|
2869
|
+
if (!isBrowser) {
|
|
2870
|
+
return logError('preFetch is only supported in browser environment');
|
|
2871
|
+
}
|
|
2720
2872
|
requestIdleCallback(() => {
|
|
2721
2873
|
if (typeof apps === 'function')
|
|
2722
2874
|
apps = apps();
|
|
@@ -2778,5 +2930,5 @@ function getGlobalAssets(assets) {
|
|
|
2778
2930
|
}
|
|
2779
2931
|
|
|
2780
2932
|
export default microApp;
|
|
2781
|
-
export { preFetch, pureCreateElement, removeDomScope, version };
|
|
2933
|
+
export { preFetch, pureCreateElement, removeDomScope, setCurrentAppName, version };
|
|
2782
2934
|
//# sourceMappingURL=index.esm.js.map
|