-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetMainSingleSPATemplate.js
225 lines (198 loc) · 7.32 KB
/
getMainSingleSPATemplate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const mainSingleSPATemplate = `
// @ts-nocheck
import { enableProdMode, NgZone } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { Router, NavigationStart } from '@angular/router';
import { singleSpaAngular, getSingleSpaExtraProviders } from 'single-spa-angular';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { singleSpaPropsSubject } from './single-spa/single-spa-props';
var imgObserver, styles = environment.styles.split(","), isMountStylesEnabled = environment.mountStyles;
if (environment.production) {
enableProdMode();
}
function mountStyle(styleSheetUrl) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = styleSheetUrl;
link.media = 'all';
head.appendChild(link);
}
function unmountStyle(styleSheet) {
styleSheet.disabled = true;
styleSheet.parentNode.removeChild(styleSheet);
}
function mountStyles() {
if(isMountStylesEnabled) {
styles.map(function(stylesheet) {
let sspaDepUrl = environment.sspaDeployUrl;
sspaDepUrl = (sspaDepUrl.slice(-1) === "/" ? sspaDepUrl.slice(0, -1) : sspaDepUrl)
mountStyle(sspaDepUrl + '/' + stylesheet);
});
}
}
function unmountStyles() {
if(isMountStylesEnabled) {
const styleSheets = document.querySelectorAll('link[href$="styles.css"]');
styleSheets.forEach(unmountStyle);
}
}
function mountWMAppProps(appName) {
let wmProps = sessionStorage.getItem(appName + "-wmProps");
if(wmProps) {
return new Promise((resolve, reject) => {
return resolve(JSON.parse(wmProps));
})
} else {
return new Promise((resolve, reject) => {
let script = document.createElement('script');
let depUrl = (environment.deployUrl.slice(-1) === "/" ? environment.deployUrl.slice(0, -1) : environment.deployUrl)
script.src = depUrl + "/services/application/wmProperties.js";
script.id = 'sspa-wm-script';
script.type = 'text/javascript';
script.async = false;
script.charset = 'utf-8';
script.onload = () => {
sessionStorage.setItem(appName + "-wmProps", JSON.stringify(_WM_APP_PROPERTIES));
return resolve(_WM_APP_PROPERTIES);
}
document.getElementsByTagName('head')[0].appendChild(script);
})
}
}
function unmountWMAppProps() {
if(window[_WM_APP_PROPERTIES]) {
window[_WM_APP_PROPERTIES] = null
}
var head = document.getElementsByTagName('head')[0];
var script = document.getElementById('sspa-wm-script');
if (script != null) {
head.removeChild(script);
}
}
function addMetaTag() {
let sspaDeployUrl = environment.sspaDeployUrl.slice(-1) === "/" ? environment.sspaDeployUrl : environment.sspaDeployUrl + "/";
//this is required to download all the component chunks
__webpack_require__.p = sspaDeployUrl;
//this is used to download all the static resources
let meta = document.createElement('meta');
meta.name = "cdnUrl";
meta.content = sspaDeployUrl;
document.head.appendChild(meta);
}
function deleteMetaTag() {
__webpack_require__.p = "";
document.querySelector("[name='cdnUrl']").remove()
}
function addToasterObserver() {
let observer = new MutationObserver(function(list) {
list.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if(node.className === 'overlay-container') {
if(node.childNodes[0].id === 'toast-container') {
node.classList.add("wm-app");
observer.disconnect();
}
}
});
});
});
observer.observe(document.body, { subtree: false, childList: true });
}
function addImgObserver(appName) {
imgObserver = new MutationObserver(function(list) {
list.forEach(function(mutation) {
if (mutation.type === 'attributes') {
let targetEle = mutation.target, src = targetEle.getAttribute("src"), poster = targetEle.getAttribute("poster");
if(src.startsWith("./")) {
targetEle.setAttribute("src", environment.deployUrl + src.slice(1));
}
if(poster && poster.startsWith("resources")) {
targetEle.setAttribute("poster", environment.deployUrl + "/" + poster);
}
}
});
});
imgObserver.observe(document.getElementById('single-spa-application:'+appName),
{ attributes: true, subtree: true, childList: true , attributeFilter: ['src', 'poster']});
}
function disconnectObservers() {
imgObserver.disconnect();
}
function addObservers(appName) {
addToasterObserver();
addImgObserver(appName);
}
function unloadModule(appName) {
if(System) {
try {
importMapOverrides.getDefaultMap().then((importMap) => {
System.delete(importMap.imports[appName])
});
} catch(e) {
console.error("Unloading the module for the app[" + appName + "] failed.", e);
}
} else {
console.error("SystemJs not found in the global namespace. Please unload the module for the app : ", appName)
}
}
const lifecycles = singleSpaAngular({
bootstrapFunction: (singleSpaProps) => {
addObservers(singleSpaProps.name);
singleSpaPropsSubject.next(singleSpaProps);
return platformBrowserDynamic(getSingleSpaExtraProviders()).bootstrapModule(AppModule);
},
template: '<app-root />',
Router,
NavigationStart,
NgZone,
});
export const bootstrap = [
async (singleSpaProps) => {
let appName = singleSpaProps.name;
if(typeof WM_APPS_META != 'undefined') {
environment.deployUrl = WM_APPS_META[appName]["deployUrl"]
environment.sspaDeployUrl = WM_APPS_META[appName]["sspaDeployUrl"]
}
addMetaTag();
await mountWMAppProps(singleSpaProps.name).then((props) => {
window._WM_APP_PROPERTIES = props;
window._WM_APP_PROPERTIES["appName"] = appName;
window._WM_APP_PROPERTIES["sspaDeployUrl"] = environment.sspaDeployUrl;
window._WM_APP_PROPERTIES["deployUrl"] = environment.deployUrl;
});
},
lifecycles.bootstrap
];
export const mount = [
async (singleSpaProps) => {
mountStyles();
},
lifecycles.mount
]
export const unmount = [
async (singleSpaProps) => {
singleSpaProps.singleSpa.unloadApplication(singleSpaProps.name, {waitForUnmount: true}).then(() => {
if(document.getElementById('single-spa-application:'+singleSpaProps.name)) {
document.getElementById('single-spa-application:'+singleSpaProps.name).innerHTML = "";
} else {
console.error("App with the name[" + singleSpaProps.name+ "] not found");
}
deleteMetaTag();
disconnectObservers();
unmountStyles();
unmountWMAppProps();
unloadModule(singleSpaProps.name)
})
},
lifecycles.unmount
]
`;
const getMainSingleSPATemplate = () => {
return mainSingleSPATemplate;
};
module.exports = {
getMainSingleSPATemplate
};