@module-federation/rspack
Version:
97 lines (92 loc) • 3.46 kB
JavaScript
;
var pBtoa = require('btoa');
var managers = require('@module-federation/managers');
var sdk = require('@module-federation/sdk');
var logger = sdk.createLogger('[ Module Federation Rspack Plugin ]');
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
// @ts-ignore
var 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, function(x) {
return charMap[x];
});
}
var RemoteEntryPlugin = /*#__PURE__*/ function() {
function RemoteEntryPlugin(options) {
_class_call_check(this, RemoteEntryPlugin);
this.name = 'VmokRemoteEntryPlugin';
this._options = options;
}
_create_class(RemoteEntryPlugin, [
{
key: "apply",
value: function apply(compiler) {
var _this__options = this._options, name = _this__options.name, getPublicPath = _this__options.getPublicPath;
if (!getPublicPath || !name) {
return;
}
var containerManager = new managers.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);
}
}
], [
{
key: "addPublicPathEntry",
value: function addPublicPathEntry(compiler, getPublicPath, name) {
var code;
var sanitizedPublicPath = escapeUnsafeChars(getPublicPath);
if (!getPublicPath.startsWith('function')) {
code = "".concat(compiler.webpack.RuntimeGlobals.publicPath, " = new Function(").concat(JSON.stringify(sanitizedPublicPath), ")()");
} else {
code = "(".concat(sanitizedPublicPath, "())");
}
var base64Code = pBtoa(code);
var dataUrl = "data:text/javascript;base64,".concat(base64Code);
compiler.hooks.afterPlugins.tap('VmokRemoteEntryPlugin', function() {
new compiler.webpack.EntryPlugin(compiler.context, dataUrl, {
name: name
}).apply(compiler);
});
}
}
]);
return RemoteEntryPlugin;
}
();
exports.RemoteEntryPlugin = RemoteEntryPlugin;