Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Dec 21, 2023
1 parent d5f3981 commit 648a04c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 83 deletions.
16 changes: 4 additions & 12 deletions packages/rspack/src/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,16 @@ function defaultImplementation(
});
}
}
const pluginImports = [];
const pluginVars = [];
const runtimePlugins = options.runtimePlugins ?? [];
for (let i = 0; i < runtimePlugins.length; i++) {
const pluginVar = `__MODULE_FEDERATION_RUNTIME_PLUGIN_${i}__`;
const pluginPath = JSON.stringify(runtimePlugins[i]);
pluginImports.push(
`const ${pluginVar} = getDefaultExport(require(${pluginPath}));`
);
pluginVars.push(`${pluginVar}()`);
}
const pluginImports = runtimePlugins.map(
p => `require(${JSON.stringify(p)})`
);
const implementationPath =
options.implementation ??
require.resolve("@module-federation/webpack-bundler-runtime");
let implementation = runtimeTemplate
.replace("$RUNTIME_PACKAGE_PATH$", JSON.stringify(implementationPath))
.replace("$ALL_REMOTES$", JSON.stringify(remotes))
.replace("$INITOPTIONS_PLUGIN_IMPORTS$", pluginImports.join("\n"))
.replace("$INITOPTIONS_PLUGINS$", `[${pluginVars.join(", ")}]`);
.replace("$INITOPTIONS_PLUGINS$", `[${pluginImports.join(", ")}]`);
return `data:text/javascript,${implementation}`;
}
123 changes: 52 additions & 71 deletions packages/rspack/src/container/default.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = function () {
const getDefaultExport = module =>
module.__esModule ? module.default : module;
const federation = require($RUNTIME_PACKAGE_PATH$);
$INITOPTIONS_PLUGIN_IMPORTS$;
const plugins = $INITOPTIONS_PLUGINS$.map(m => getDefaultExport(m)());
const allRemotes = $ALL_REMOTES$;

const scopeToInitDataMapping =
__webpack_require__.initializeSharingData?.scopeToSharingDataMapping ??
Expand All @@ -26,17 +27,6 @@ module.exports = function () {
}
}

const allRemotes = $ALL_REMOTES$;
__webpack_require__.federation = {};
__webpack_require__.federation.initOptions = {};
__webpack_require__.federation.initOptions.name =
__webpack_require__.initializeSharingData?.uniqueName;
__webpack_require__.federation.initOptions.remotes = allRemotes.filter(
remote => remote.externalType === "script"
);
__webpack_require__.federation.initOptions.shared = shared;
__webpack_require__.federation.initOptions.plugins = $INITOPTIONS_PLUGINS$;

const idToExternalAndNameMapping =
__webpack_require__.remotesLoadingData?.moduleIdToRemoteDataMapping ?? {};
const idToRemoteMap = {};
Expand All @@ -59,9 +49,9 @@ module.exports = function () {
const moduleToConsumeDataMapping =
__webpack_require__.consumesLoadingData?.moduleIdToConsumeDataMapping ??
{};
const moduleToHandlerMapping = {};
const consumesLoadingModuleToHandlerMapping = {};
for (let [moduleId, data] of Object.entries(moduleToConsumeDataMapping)) {
moduleToHandlerMapping[moduleId] = {
consumesLoadingModuleToHandlerMapping[moduleId] = {
getter: data.fallback,
shareInfo: {
shareConfig: {
Expand All @@ -77,23 +67,23 @@ module.exports = function () {
};
}

const installedModules = {};
const initPromises = [];
const initTokens = [];
const consumesLoadinginstalledModules = {};
const initializeSharingInitPromises = [];
const initializeSharingInitTokens = [];
const remotesLoadingChunkMapping =
__webpack_require__.remotesLoadingData?.chunkMapping ?? {};
const consumesLoadingChunkMapping =
__webpack_require__.consumesLoadingData?.chunkMapping ?? {};
const containerShareScope =
__webpack_require__.initializeExposesData?.containerShareScope;

__webpack_require__.federation.runtime = federation.runtime;
__webpack_require__.federation.instance = federation.instance;
__webpack_require__.federation.proxyShareScopeMap =
federation.proxyShareScopeMap;
__webpack_require__.federation.hasProxyShareScopeMap =
federation.hasProxyShareScopeMap;
__webpack_require__.federation.bundlerRuntimeOptions = {
federation.initOptions = {
name: __webpack_require__.initializeSharingData?.uniqueName,
remotes: allRemotes.filter(remote => remote.externalType === "script"),
shared: shared,
plugins: plugins
};
federation.bundlerRuntimeOptions = {
remotes: {
idToRemoteMap,
chunkMapping: remotesLoadingChunkMapping,
Expand All @@ -102,87 +92,78 @@ module.exports = function () {
}
};

__webpack_require__.federation.bundlerRuntime = {
remotes: (chunkId, promises) =>
if (__webpack_require__.f?.remotes) {
__webpack_require__.f.remotes = (chunkId, promises) =>
federation.bundlerRuntime.remotes({
chunkId,
promises,
chunkMapping: remotesLoadingChunkMapping,
idToExternalAndNameMapping,
idToRemoteMap,
webpackRequire: __webpack_require__
}),
consumes: (chunkId, promises) =>
});
}
if (__webpack_require__.f?.consumes) {
__webpack_require__.f.consumes = (chunkId, promises) =>
federation.bundlerRuntime.consumes({
chunkId,
promises,
chunkMapping: consumesLoadingChunkMapping,
moduleToHandlerMapping,
installedModules,
moduleToHandlerMapping: consumesLoadingModuleToHandlerMapping,
installedModules: consumesLoadinginstalledModules,
webpackRequire: __webpack_require__
}),
I: (name, initScope) =>
});
}
if (__webpack_require__.I) {
__webpack_require__.I = (name, initScope) =>
federation.bundlerRuntime.I({
shareScopeName: name,
initScope,
initPromises,
initTokens,
initPromises: initializeSharingInitPromises,
initTokens: initializeSharingInitTokens,
webpackRequire: __webpack_require__
}),
S: federation.bundlerRuntime.S,
installInitialConsumes: initialConsumes =>
federation.bundlerRuntime.installInitialConsumes({
webpackRequire: __webpack_require__,
installedModules,
initialConsumes,
moduleToHandlerMapping
}),
initContainerEntry: (shareScope, initScope) =>
});
}
if (__webpack_require__.S) {
__webpack_require__.S = federation.bundlerRuntime.S;
}
if (__webpack_require__.initContainer) {
__webpack_require__.initContainer = (shareScope, initScope) =>
federation.bundlerRuntime.initContainerEntry({
shareScope,
initScope,
shareScopeKey: containerShareScope,
webpackRequire: __webpack_require__
})
};

if (__webpack_require__.f?.remotes)
__webpack_require__.f.remotes =
__webpack_require__.federation.bundlerRuntime.remotes;
if (__webpack_require__.f?.consumes)
__webpack_require__.f.consumes =
__webpack_require__.federation.bundlerRuntime.consumes;
if (__webpack_require__.I)
__webpack_require__.I = __webpack_require__.federation.bundlerRuntime.I;
if (__webpack_require__.initContainer)
__webpack_require__.initContainer =
__webpack_require__.federation.bundlerRuntime.initContainerEntry;
if (__webpack_require__.getContainer)
});
}
if (__webpack_require__.getContainer) {
__webpack_require__.getContainer = (module, getScope) => {
var moduleMap = __webpack_require__.initializeExposesData.moduleMap;
__webpack_require__.R = getScope;
getScope = Object.prototype.hasOwnProperty.call(moduleMap, module)
? moduleMap[module]()
: Promise.resolve().then(() => {
{
throw new Error(
'Module "' + module + '" does not exist in container.'
);
}
throw new Error(
'Module "' + module + '" does not exist in container.'
);
});
__webpack_require__.R = undefined;
return getScope;
};
}

__webpack_require__.federation = federation;

__webpack_require__.federation.instance =
__webpack_require__.federation.runtime.init(
__webpack_require__.federation.initOptions
);
federation.instance = federation.runtime.init(federation.initOptions);

if (__webpack_require__.consumesLoadingData?.initialConsumes) {
__webpack_require__.federation.bundlerRuntime.installInitialConsumes(
__webpack_require__.consumesLoadingData.initialConsumes
);
federation.bundlerRuntime.installInitialConsumes({
webpackRequire: __webpack_require__,
installedModules: consumesLoadinginstalledModules,
initialConsumes:
__webpack_require__.consumesLoadingData.initialConsumes,
moduleToHandlerMapping: consumesLoadingModuleToHandlerMapping
});
}
}
};

0 comments on commit 648a04c

Please sign in to comment.