-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildConfig.js
85 lines (79 loc) · 3.34 KB
/
buildConfig.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
const { dependencies: deps } = require("./package.json");
function generateBidragUiStaticUrl(appName) {
return `/static/${appName}/${getDeployEnv()}static`;
}
function getDeployEnv() {
if (!process.env.DEPLOY_ENV || process.env.DEPLOY_ENV === "prod") {
return "";
}
return process.env.DEPLOY_ENV + "/";
}
function initFedrationScript(appName, assetsUrl) {
return `promise new Promise((resolve) => {
// This part depends on how you plan on hosting and versioning your federated modules
let remoteUrl = "${assetsUrl}/remoteEntry.js";
const script = document.createElement("script");
script.src = remoteUrl;
script.onload = () => {
// the injected script has loaded and is available on window
// we can now resolve this Promise
const proxy = {
get: (request) => window.${appName}.get(request),
init: (arg) => {
try {
return window.${appName}.init(arg);
} catch (e) {
console.warn("App ${appName} already initialized");
}
},
};
resolve(proxy);
};
const logError = (message)=>{
fetch("/log", {
mode: "cors",
cache: "no-cache",
body: '{"appName": "${appName}", "message":"'+message+'", "level": "WARNING"}',
method: "POST",
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}).then(() => {});
}
//<div>
script.onerror = (error) => {
const proxy = {
get: (request) => {
const message = "Det skjedde en feil ved lasting av mikrofrontend app ${appName}"+request+" fra asset url ${assetsUrl} og remoteUrl " + remoteUrl
logError(message)
console.error(message, error)
fetch(remoteUrl).catch((err)=>logError("Feilmelding fra " + remoteUrl + ": " + err.message))
// If the service is down it will render this content
return Promise.resolve(() => () => "Det skjedde en feil");
},
init: (arg) => {
return;
},
};
resolve(proxy);
};
// inject this script with the src set to the versioned remoteEntry.js
document.head.appendChild(script);
})`;
}
module.exports = {
federationConfig: {
name: "bidrag_forsendelse_ui",
filename: "remoteEntry.js",
exposes: {
"./Forsendelse": "./src/app.tsx",
},
shared: {
react: { singleton: true, requiredVersion: deps.react },
"react-dom": { singleton: true, requiredVersion: deps.react },
},
},
configureRemoteApp: (appName, assetsUrl) => {
return initFedrationScript(appName, assetsUrl ?? generateBidragUiStaticUrl(appName));
},
};