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

feat: Module Federation, part 2, ContainerReferencePlugin #4735

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix
  • Loading branch information
ahabhgk committed Nov 24, 2023
commit 77a1258c41ff347487df7e2ee67f86cd4737b0cf
3 changes: 1 addition & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ export const enum BuiltinPluginName {
OldSplitChunksPlugin = 'OldSplitChunksPlugin',
ContainerPlugin = 'ContainerPlugin',
ContainerReferencePlugin = 'ContainerReferencePlugin',
ShareRuntimePlugin = 'ShareRuntimePlugin',
MFScopeRuntimePlugin = 'MFScopeRuntimePlugin',
ModuleFederationRuntimePlugin = 'ModuleFederationRuntimePlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down
10 changes: 4 additions & 6 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use napi_derive::napi;
use rspack_core::{
mf::{
container_plugin::ContainerPlugin, container_reference_plugin::ContainerReferencePlugin,
mf_scope_runtime_plugin::MFScopeRuntimePlugin, share_runtime_plugin::ShareRuntimePlugin,
module_federation_runtime_plugin::ModuleFederationRuntimePlugin,
},
BoxPlugin, Define, DefinePlugin, PluginExt, Provide, ProvidePlugin,
};
Expand Down Expand Up @@ -80,8 +80,7 @@ pub enum BuiltinPluginName {
OldSplitChunksPlugin,
ContainerPlugin,
ContainerReferencePlugin,
ShareRuntimePlugin,
MFScopeRuntimePlugin,
ModuleFederationRuntimePlugin,

// rspack specific plugins
HttpExternalsRspackPlugin,
Expand Down Expand Up @@ -212,9 +211,8 @@ impl RawOptionsApply for BuiltinPlugin {
.boxed(),
);
}
BuiltinPluginName::ShareRuntimePlugin => plugins.push(ShareRuntimePlugin::default().boxed()),
BuiltinPluginName::MFScopeRuntimePlugin => {
plugins.push(MFScopeRuntimePlugin::default().boxed())
BuiltinPluginName::ModuleFederationRuntimePlugin => {
plugins.push(ModuleFederationRuntimePlugin::default().boxed())
}

// rspack specific plugins
Expand Down
35 changes: 0 additions & 35 deletions crates/rspack_core/src/mf/container/mf_scope_runtime_module.rs

This file was deleted.

25 changes: 0 additions & 25 deletions crates/rspack_core/src/mf/container/mf_scope_runtime_plugin.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/rspack_core/src/mf/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod container_entry_module_factory;
pub mod container_exposed_dependency;
pub mod container_plugin;
pub mod container_reference_plugin;
pub mod mf_scope_runtime_module;
pub mod mf_scope_runtime_plugin;
pub mod module_federation_runtime_plugin;
pub mod remote_module;
pub mod remote_runtime_module;
pub mod remote_to_external_dependency;
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use rspack_identifier::Identifier;
use rspack_sources::{BoxSource, RawSource, SourceExt};

use crate::{
impl_runtime_module, mf::share_runtime_module::ShareRuntimeModule,
AdditionalChunkRuntimeRequirementsArgs, ChunkUkey, Compilation, Plugin,
PluginAdditionalChunkRuntimeRequirementsOutput, PluginContext, RuntimeGlobals, RuntimeModule,
};

#[derive(Debug, Default)]
pub struct ModuleFederationRuntimePlugin;

impl Plugin for ModuleFederationRuntimePlugin {
fn name(&self) -> &'static str {
"rspack.ModuleFederationRuntimePlugin"
}

fn runtime_requirements_in_tree(
&self,
_ctx: PluginContext,
args: &mut AdditionalChunkRuntimeRequirementsArgs,
) -> PluginAdditionalChunkRuntimeRequirementsOutput {
if args
.runtime_requirements
.contains(RuntimeGlobals::SHARE_SCOPE_MAP)
{
args
.compilation
.add_runtime_module(args.chunk, Box::<ModuleFederationRuntimeModule>::default());
args
.compilation
.add_runtime_module(args.chunk, Box::<ShareRuntimeModule>::default());
}
Ok(())
}
}

#[derive(Debug, Eq)]
pub struct ModuleFederationRuntimeModule {
id: Identifier,
chunk: Option<ChunkUkey>,
}

impl Default for ModuleFederationRuntimeModule {
fn default() -> Self {
Self {
id: Identifier::from("webpack/runtime/mf_scope"),
chunk: None,
}
}
}

impl RuntimeModule for ModuleFederationRuntimeModule {
fn name(&self) -> Identifier {
self.id
}

fn generate(&self, _: &Compilation) -> BoxSource {
RawSource::from(format!(r#"{}.MF = {{}};"#, RuntimeGlobals::REQUIRE)).boxed()
}

fn attach(&mut self, chunk: ChunkUkey) {
self.chunk = Some(chunk);
}
}

impl_runtime_module!(ModuleFederationRuntimeModule);
1 change: 0 additions & 1 deletion crates/rspack_core/src/mf/sharing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod share_runtime_module;
pub mod share_runtime_plugin;
25 changes: 0 additions & 25 deletions crates/rspack_core/src/mf/sharing/share_runtime_plugin.rs

This file was deleted.

3 changes: 1 addition & 2 deletions packages/rspack/src/builtin-plugin/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export enum BuiltinPluginName {
OldSplitChunksPlugin = "OldSplitChunksPlugin",
ContainerPlugin = "ContainerPlugin",
ContainerReferencePlugin = "ContainerReferencePlugin",
ShareRuntimePlugin = "ShareRuntimePlugin",
MFScopeRuntimePlugin = "MFScopeRuntimePlugin",
ModuleFederationRuntimePlugin = "ModuleFederationRuntimePlugin"
}

export abstract class RspackBuiltinPlugin implements RspackPluginInstance {
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/container/ContainerReferencePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ExternalsPlugin } from "../builtin-plugin/ExternalsPlugin";
import { ExternalsType } from "../config";
import { parseOptions } from "./options";
import { RemoteRuntimeSingletonPlugin } from "./RemoteRuntimeSingletonPlugin";
import { ShareRuntimeSingletonPlugin } from "../sharing/ShareRuntimeSingletonPlugin";

export type ContainerReferencePluginOptions = {
remoteType: ExternalsType;
Expand Down Expand Up @@ -65,6 +66,7 @@ export class ContainerReferencePlugin extends RspackBuiltinPlugin {
}
}
new ExternalsPlugin(remoteType, remoteExternals).apply(compiler);
new ShareRuntimeSingletonPlugin().apply(compiler);
new RemoteRuntimeSingletonPlugin().apply(compiler);

return {
Expand Down
17 changes: 0 additions & 17 deletions packages/rspack/src/container/MFScopeRuntimeSingletonPlugin.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/rspack/src/container/ModuleFederationRuntimePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Compiler } from "../Compiler";
import { BuiltinPluginName, create } from "../builtin-plugin/base";

const ModuleFederationRuntimePlugin2 = create(
BuiltinPluginName.ModuleFederationRuntimePlugin,
() => undefined
);

export class ModuleFederationRuntimePlugin {
apply(compiler: Compiler) {
// TODO: a hack to make sure this runtime is added after ContainerReferencePlugin
// remove afterPlugin once we make rust side runtime_requirements_in_tree "tapable"
compiler.hooks.afterPlugins.tap("ModuleFederationRuntimePlugin", () => {
new ModuleFederationRuntimePlugin2().apply(compiler);
});
}
}
7 changes: 3 additions & 4 deletions packages/rspack/src/container/RemoteRuntimeSingletonPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Compiler } from "../Compiler";
import { EntryPlugin } from "../builtin-plugin/EntryPlugin";
import { ShareRuntimeSingletonPlugin } from "../sharing/ShareRuntimeSingletonPlugin";
import { MFScopeRuntimeSingletonPlugin } from "./MFScopeRuntimeSingletonPlugin";

let added = false;

export class RemoteRuntimeSingletonPlugin {
apply(compiler: Compiler) {
new MFScopeRuntimeSingletonPlugin().apply(compiler);
new ShareRuntimeSingletonPlugin().apply(compiler);
if (added) return;
added = true;
new EntryPlugin(compiler.context, require.resolve("./remotesLoading.js"), {
name: undefined
}).apply(compiler);
compiler.hooks.done.tap(RemoteRuntimeSingletonPlugin.name, () => {
added = false;
});
}
}
3 changes: 3 additions & 0 deletions packages/rspack/src/rspackOptionsApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
SplitChunksPlugin,
OldSplitChunksPlugin
} from "./builtin-plugin";
import { ModuleFederationRuntimePlugin } from "./container/ModuleFederationRuntimePlugin";

export function optionsApply_compat(
compiler: Compiler,
Expand Down Expand Up @@ -173,6 +174,8 @@ export class RspackOptionsApply {
);
compiler.hooks.entryOption.call(options.context, options.entry);

new ModuleFederationRuntimePlugin().apply(compiler);

const { minimize, minimizer } = options.optimization;
if (minimize && minimizer) {
for (const item of minimizer) {
Expand Down
12 changes: 3 additions & 9 deletions packages/rspack/src/sharing/ShareRuntimeSingletonPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { Compiler } from "../Compiler";
import { EntryPlugin } from "../builtin-plugin/EntryPlugin";
import { BuiltinPluginName, create } from "../builtin-plugin/base";
import { MFScopeRuntimeSingletonPlugin } from "../container/MFScopeRuntimeSingletonPlugin";

const ShareRuntimePlugin = create(
BuiltinPluginName.ShareRuntimePlugin,
() => undefined
);

let added = false;

export class ShareRuntimeSingletonPlugin {
apply(compiler: Compiler) {
new MFScopeRuntimeSingletonPlugin().apply(compiler);
if (added) return;
added = true;
new ShareRuntimePlugin().apply(compiler);
new EntryPlugin(
compiler.context,
require.resolve("./initializeSharing.js"),
{ name: undefined }
).apply(compiler);
compiler.hooks.done.tap(ShareRuntimeSingletonPlugin.name, () => {
added = false;
});
}
}