Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mf1.5): add resolve alias for mf runtime #5239

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions packages/rspack/src/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import { ExternalsType, externalsType } from "../config";
import { Compiler } from "../Compiler";
import { type ModuleFederationPluginV1Options } from "./ModuleFederationPluginV1";
Expand All @@ -6,19 +7,26 @@ import { isValidate } from "../util/validate";

export interface ModuleFederationPluginOptions
extends Omit<ModuleFederationPluginV1Options, "enhanced"> {
runtimePlugins?: string[];
runtimePlugins?: RuntimePlugins;
implementation?: string;
}
export type RuntimePlugins = string[];

export class ModuleFederationPlugin {
constructor(private _options: ModuleFederationPluginOptions) {}

apply(compiler: Compiler) {
const { webpack } = compiler;
const paths = getPaths(this._options);
compiler.options.resolve.alias = {
"@module-federation/runtime-tools": paths.runtimeTools,
"@module-federation/runtime": paths.runtime,
...compiler.options.resolve.alias
};
compiler.hooks.afterPlugins.tap(ModuleFederationPlugin.name, () => {
new webpack.EntryPlugin(
compiler.context,
getDefaultEntryRuntime(this._options, compiler),
getDefaultEntryRuntime(paths, this._options, compiler),
{ name: undefined }
).apply(compiler);
});
Expand All @@ -29,6 +37,12 @@ export class ModuleFederationPlugin {
}
}

interface RuntimePaths {
runtimeTools: string;
bundlerRuntime: string;
runtime: string;
}

interface RemoteInfo {
alias: string;
name?: string;
Expand Down Expand Up @@ -123,18 +137,29 @@ function getRuntimePlugins(options: ModuleFederationPluginOptions) {
return options.runtimePlugins ?? [];
}

function getImplementation(options: ModuleFederationPluginOptions) {
return (
function getPaths(options: ModuleFederationPluginOptions): RuntimePaths {
const runtimeToolsPath =
options.implementation ??
require.resolve("@module-federation/runtime-tools/webpack-bundler-runtime")
require.resolve("@module-federation/runtime-tools");
const bundlerRuntimePath = require.resolve(
"@module-federation/webpack-bundler-runtime",
{ paths: [runtimeToolsPath] }
);
const runtimePath = require.resolve("@module-federation/runtime", {
paths: [runtimeToolsPath]
});
return {
runtimeTools: runtimeToolsPath,
bundlerRuntime: bundlerRuntimePath,
runtime: runtimePath
};
}

function getDefaultEntryRuntime(
paths: RuntimePaths,
options: ModuleFederationPluginOptions,
compiler: Compiler
) {
const implementationPath = getImplementation(options);
const runtimePlugins = getRuntimePlugins(options);
const remoteInfos = getRemoteInfos(options);
const runtimePluginImports = [];
Expand All @@ -147,8 +172,8 @@ function getDefaultEntryRuntime(
runtimePluginVars.push(`${runtimePluginVar}()`);
}
const content = [
`import __module_federation_implementation__ from ${JSON.stringify(
implementationPath
`import __module_federation_bundler_runtime__ from ${JSON.stringify(
paths.bundlerRuntime
)}`,
...runtimePluginImports,
`const __module_federation_runtime_plugins__ = [${runtimePluginVars.join(
Expand Down
28 changes: 14 additions & 14 deletions packages/rspack/src/container/default.runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
var __module_federation_implementation__,
var __module_federation_bundler_runtime__,
__module_federation_runtime_plugins__,
__module_federation_remote_infos__,
__module_federation_container_name__;
Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports = function () {
early(
__webpack_require__,
"federation",
() => __module_federation_implementation__
() => __module_federation_bundler_runtime__
);

early(
Expand Down Expand Up @@ -165,16 +165,16 @@ module.exports = function () {
override(
__webpack_require__,
"S",
__module_federation_implementation__.bundlerRuntime.S
__module_federation_bundler_runtime__.bundlerRuntime.S
);
if (__module_federation_implementation__.attachShareScopeMap) {
__module_federation_implementation__.attachShareScopeMap(
if (__module_federation_bundler_runtime__.attachShareScopeMap) {
__module_federation_bundler_runtime__.attachShareScopeMap(
__webpack_require__
);
}

override(__webpack_require__.f, "remotes", (chunkId, promises) =>
__module_federation_implementation__.bundlerRuntime.remotes({
__module_federation_bundler_runtime__.bundlerRuntime.remotes({
chunkId,
promises,
chunkMapping: remotesLoadingChunkMapping,
Expand All @@ -188,7 +188,7 @@ module.exports = function () {
})
);
override(__webpack_require__.f, "consumes", (chunkId, promises) =>
__module_federation_implementation__.bundlerRuntime.consumes({
__module_federation_bundler_runtime__.bundlerRuntime.consumes({
chunkId,
promises,
chunkMapping: consumesLoadingChunkMapping,
Expand All @@ -199,7 +199,7 @@ module.exports = function () {
})
);
override(__webpack_require__, "I", (name, initScope) =>
__module_federation_implementation__.bundlerRuntime.I({
__module_federation_bundler_runtime__.bundlerRuntime.I({
shareScopeName: name,
initScope,
initPromises: initializeSharingInitPromises,
Expand All @@ -208,7 +208,7 @@ module.exports = function () {
})
);
override(__webpack_require__, "initContainer", (shareScope, initScope) =>
__module_federation_implementation__.bundlerRuntime.initContainerEntry({
__module_federation_bundler_runtime__.bundlerRuntime.initContainerEntry({
shareScope,
initScope,
shareScopeKey: containerShareScope,
Expand All @@ -229,20 +229,20 @@ module.exports = function () {
return getScope;
});

__module_federation_implementation__.instance =
__module_federation_implementation__.runtime.init(
__module_federation_implementation__.initOptions
__module_federation_bundler_runtime__.instance =
__module_federation_bundler_runtime__.runtime.init(
__module_federation_bundler_runtime__.initOptions
);

if (__webpack_require__.consumesLoadingData?.initialConsumes) {
__module_federation_implementation__.bundlerRuntime.installInitialConsumes(
__module_federation_bundler_runtime__.bundlerRuntime.installInitialConsumes(
{
webpackRequire: __webpack_require__,
installedModules: consumesLoadinginstalledModules,
initialConsumes:
__webpack_require__.consumesLoadingData.initialConsumes,
moduleToHandlerMapping:
__module_federation_implementation__.consumesLoadingModuleToHandlerMapping
__module_federation_bundler_runtime__.consumesLoadingModuleToHandlerMapping
}
);
}
Expand Down
Loading