@pega/constellation-dx-components-build-utils
Version:
This tool uses a 'v3' approach to group components in a library, create a component map, employ webpack, and load the library like Pega-generated components, constellation app-static.
76 lines (69 loc) • 2.82 kB
JavaScript
window.dynamicFetchScriptHook = (script, v3) => {
let emptyScript = document.createElement('script');
let src = script.src;
if(src.startsWith('blob')){
src = src.replace('blob:', '');
}
const regex = /^(?:https?:)?(?:\/\/)?[^/]+/;
src = src.replace(regex, ""); //component orign src= "/chunks/morecomplex.js"
if(v3){
const match = src.match(/([^/]+\.js)$/);
if(match && match[1])
PCore.getAssetLoader().loadSvcComponent(match[1]);
} else{
fetch(`${PCore.getAssetLoader().appStaticUrl}v100/componentlib/component/${PCore.getAssetLoader().ccV2LibId}${src}`, {headers: { Authorization: `Bearer ${PCore.getAssetLoader().b2sJWT}` }})
.then(response => response.blob())
.then(blob => {
script.src = URL.createObjectURL(blob);
script.onerror = (event) => {
URL.revokeObjectURL(script.src);
};
script.onload = (event) => {
URL.revokeObjectURL(script.src);
};
emptyScript.remove();
document.head.appendChild(script);
});
}
return emptyScript;
};
window.checkDevTools = () => {
if (window && window.cosmos && window.cosmos.instances && window.cosmos.instances.length > 1) {
try {
const cosmosVersionsMap = {}
window.cosmos.instances.forEach(cosmosVersion => {
const { version, mountedConfigs } = cosmosVersion;
if(version && mountedConfigs){
if(mountedConfigs.includes("DXComponents")){
cosmosVersionsMap["pega-cosmos"] = version;
} else if(cosmosVersionsMap["pega-cosmos"] !== version){
const pegaCosmosVersion = cosmosVersionsMap["pega-cosmos"].split('.');
const libCosmosVersion = version.split('.');
if(!(pegaCosmosVersion[0] === libCosmosVersion[0] && pegaCosmosVersion[1] === libCosmosVersion[1])){
cosmosVersionsMap["lib-cosmos"] = cosmosVersionsMap["lib-cosmos"] ? cosmosVersionsMap["lib-cosmos"]+", "+version : version;
}
}
}
});
if(Object.keys(cosmosVersionsMap).length > 1){
console.warn(`%c[Warning] : Please update your library's cosmos version: ${cosmosVersionsMap["lib-cosmos"]} to ${cosmosVersionsMap["pega-cosmos"]} `,"font-size:15px; color: orange; border-left: 3px solid;");
}
} catch(ex){
console.log(ex);
}
}
}
checkDevTools();
const mismatchMsgInt = setInterval(checkDevTools, 30000);
export const loadable = (importFn, componentName) => {
return async () => {
if (!PCore.getComponentsRegistry().lazyMap[componentName]) {
const component = await importFn();
PCore.getComponentsRegistry().lazyMap[componentName] = component.default;
}
};
};
/* START */
const ComponentMap = {};
/* END */
PCore.getComponentsRegistry().mergeComponentsMap(ComponentMap);