mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-27 01:22:49 +00:00
66 lines
2.0 KiB
JavaScript
66 lines
2.0 KiB
JavaScript
import { ContainerManager } from "@module-federation/managers";
|
|
import logger from "./logger.mjs";
|
|
|
|
;// CONCATENATED MODULE: external "@module-federation/managers"
|
|
|
|
;// CONCATENATED MODULE: external "./logger.mjs"
|
|
|
|
;// CONCATENATED MODULE: ./src/RemoteEntryPlugin.ts
|
|
|
|
|
|
// @ts-ignore
|
|
const charMap = {
|
|
'<': '\\u003C',
|
|
'>': '\\u003E',
|
|
'/': '\\u002F',
|
|
'\\': '\\\\',
|
|
'\b': '\\b',
|
|
'\f': '\\f',
|
|
'\n': '\\n',
|
|
'\r': '\\r',
|
|
'\t': '\\t',
|
|
'\0': '\\0',
|
|
'\u2028': '\\u2028',
|
|
'\u2029': '\\u2029'
|
|
};
|
|
function escapeUnsafeChars(str) {
|
|
return str.replace(/[<>\b\f\n\r\t\0\u2028\u2029\\]/g, (x)=>charMap[x]);
|
|
}
|
|
class RemoteEntryPlugin {
|
|
static addPublicPathEntry(compiler, getPublicPath, name) {
|
|
let code;
|
|
const sanitizedPublicPath = escapeUnsafeChars(getPublicPath);
|
|
if (!getPublicPath.startsWith('function')) {
|
|
code = `${compiler.webpack.RuntimeGlobals.publicPath} = new Function(${JSON.stringify(sanitizedPublicPath)})()`;
|
|
} else {
|
|
code = `(${sanitizedPublicPath}())`;
|
|
}
|
|
const base64Code = Buffer.from(code, 'utf8').toString('base64');
|
|
const dataUrl = `data:text/javascript;base64,${base64Code}`;
|
|
compiler.hooks.afterPlugins.tap('VmokRemoteEntryPlugin', ()=>{
|
|
new compiler.webpack.EntryPlugin(compiler.context, dataUrl, {
|
|
name: name
|
|
}).apply(compiler);
|
|
});
|
|
}
|
|
apply(compiler) {
|
|
const { name, getPublicPath } = this._options;
|
|
if (!getPublicPath || !name) {
|
|
return;
|
|
}
|
|
const containerManager = new ContainerManager();
|
|
containerManager.init(this._options);
|
|
if (!containerManager.enable) {
|
|
logger.warn("Detect you don't set exposes, 'getPublicPath' will not have effect.");
|
|
return;
|
|
}
|
|
RemoteEntryPlugin.addPublicPathEntry(compiler, getPublicPath, name);
|
|
}
|
|
constructor(options){
|
|
this.name = 'VmokRemoteEntryPlugin';
|
|
this._options = options;
|
|
}
|
|
}
|
|
|
|
export { RemoteEntryPlugin };
|