Skip to content

Commit

Permalink
feat: Module Federation, part 4, ConsumeSharedPlugin (#4783)
Browse files Browse the repository at this point in the history
* feat: Module Federation, part 4, ConsumeSharedPlugin

* works

* fix

* fix

* enable tests

* fix
  • Loading branch information
ahabhgk authored Nov 27, 2023
1 parent 4c9efbe commit 086e6b4
Show file tree
Hide file tree
Showing 119 changed files with 2,230 additions and 60 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const enum BuiltinPluginName {
ContainerReferencePlugin = 'ContainerReferencePlugin',
ModuleFederationRuntimePlugin = 'ModuleFederationRuntimePlugin',
ProvideSharedPlugin = 'ProvideSharedPlugin',
ConsumeSharedPlugin = 'ConsumeSharedPlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down Expand Up @@ -618,6 +619,19 @@ export interface RawChunkOptionNameCtx {
module: JsModule
}

export interface RawConsumeOptions {
key: string
import?: string
importResolved?: string
shareKey: string
shareScope: string
requiredVersion?: string | false | undefined
packageName?: string
strictVersion: boolean
singleton: boolean
eager: boolean
}

export interface RawContainerPluginOptions {
name: string
shareScope: string
Expand Down
13 changes: 11 additions & 2 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use napi::{
use napi_derive::napi;
use rspack_core::{
mf::{
container_plugin::ContainerPlugin, container_reference_plugin::ContainerReferencePlugin,
consume_shared_plugin::ConsumeSharedPlugin, container_plugin::ContainerPlugin,
container_reference_plugin::ContainerReferencePlugin,
module_federation_runtime_plugin::ModuleFederationRuntimePlugin,
provide_shared_plugin::ProvideSharedPlugin,
},
Expand Down Expand Up @@ -43,7 +44,7 @@ use rspack_plugin_swc_js_minimizer::SwcJsMinimizerRspackPlugin;
use rspack_plugin_wasm::enable_wasm_loading_plugin;
use rspack_plugin_web_worker_template::web_worker_template_plugin;

use self::raw_mf::{RawContainerReferencePluginOptions, RawProvideOptions};
use self::raw_mf::{RawConsumeOptions, RawContainerReferencePluginOptions, RawProvideOptions};
pub use self::{
raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions,
raw_html::RawHtmlRspackPluginOptions, raw_limit_chunk_count::RawLimitChunkCountPluginOptions,
Expand Down Expand Up @@ -83,6 +84,7 @@ pub enum BuiltinPluginName {
ContainerReferencePlugin,
ModuleFederationRuntimePlugin,
ProvideSharedPlugin,
ConsumeSharedPlugin,

// rspack specific plugins
HttpExternalsRspackPlugin,
Expand Down Expand Up @@ -224,6 +226,13 @@ impl RawOptionsApply for BuiltinPlugin {
provides.sort_unstable_by_key(|(k, _)| k.to_string());
plugins.push(ProvideSharedPlugin::new(provides).boxed())
}
BuiltinPluginName::ConsumeSharedPlugin => {
let consumes: Vec<_> = downcast_into::<Vec<RawConsumeOptions>>(self.options)?
.into_iter()
.map(Into::into)
.collect();
plugins.push(ConsumeSharedPlugin::new(consumes).boxed())
}

// rspack specific plugins
BuiltinPluginName::HttpExternalsRspackPlugin => {
Expand Down
57 changes: 51 additions & 6 deletions crates/rspack_binding_options/src/options/raw_builtins/raw_mf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use napi::Either;
use napi_derive::napi;
use rspack_core::mf::{
consume_shared_plugin::{ConsumeOptions, ConsumeVersion},
container_plugin::{ContainerPluginOptions, ExposeOptions},
container_reference_plugin::{ContainerReferencePluginOptions, RemoteOptions},
provide_shared_plugin::{ProvideOptions, ProvideVersion},
Expand Down Expand Up @@ -97,7 +98,7 @@ pub struct RawProvideOptions {
pub share_key: String,
pub share_scope: String,
#[napi(ts_type = "string | false | undefined")]
pub version: Option<RawProvideVersion>,
pub version: Option<RawVersion>,
pub eager: bool,
}

Expand All @@ -108,22 +109,66 @@ impl From<RawProvideOptions> for (String, ProvideOptions) {
ProvideOptions {
share_key: value.share_key,
share_scope: value.share_scope,
version: value.version.map(|v| RawProvideVersionWrapper(v).into()),
version: value.version.map(|v| RawVersionWrapper(v).into()),
eager: value.eager,
},
)
}
}

pub type RawProvideVersion = Either<String, bool>;
#[derive(Debug)]
#[napi(object)]
pub struct RawConsumeOptions {
pub key: String,
pub import: Option<String>,
pub import_resolved: Option<String>,
pub share_key: String,
pub share_scope: String,
#[napi(ts_type = "string | false | undefined")]
pub required_version: Option<RawVersion>,
pub package_name: Option<String>,
pub strict_version: bool,
pub singleton: bool,
pub eager: bool,
}

struct RawProvideVersionWrapper(RawProvideVersion);
impl From<RawConsumeOptions> for (String, ConsumeOptions) {
fn from(value: RawConsumeOptions) -> Self {
(
value.key,
ConsumeOptions {
import: value.import,
import_resolved: value.import_resolved,
share_key: value.share_key,
share_scope: value.share_scope,
required_version: value.required_version.map(|v| RawVersionWrapper(v).into()),
package_name: value.package_name,
strict_version: value.strict_version,
singleton: value.singleton,
eager: value.eager,
},
)
}
}

pub type RawVersion = Either<String, bool>;

impl From<RawProvideVersionWrapper> for ProvideVersion {
fn from(value: RawProvideVersionWrapper) -> Self {
struct RawVersionWrapper(RawVersion);

impl From<RawVersionWrapper> for ProvideVersion {
fn from(value: RawVersionWrapper) -> Self {
match value.0 {
Either::A(s) => ProvideVersion::Version(s),
Either::B(_) => ProvideVersion::False,
}
}
}

impl From<RawVersionWrapper> for ConsumeVersion {
fn from(value: RawVersionWrapper) -> Self {
match value.0 {
Either::A(s) => ConsumeVersion::Version(s),
Either::B(_) => ConsumeVersion::False,
}
}
}
6 changes: 4 additions & 2 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,10 @@ impl Compilation {
}

Some(values)
} else if matches!(dep.dependency_type(), DependencyType::ContainerExposed)
&& let Some(module) = self.module_graph.get_module(dep.id())
} else if matches!(
dep.dependency_type(),
DependencyType::ContainerExposed | DependencyType::ProvideModuleForShared
) && let Some(module) = self.module_graph.get_module(dep.id())
{
Some(vec![(module.identifier(), BailoutFlag::CONTAINER_EXPOSED)])
} else {
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/dependency/dependency_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum DependencyType {
ProvideSharedModule,
/// provide module for shared
ProvideModuleForShared,
/// consume shared fallback
ConsumeSharedFallback,
Custom(Box<str>), // TODO it will increase large layout size
}

Expand Down Expand Up @@ -116,6 +118,7 @@ impl DependencyType {
DependencyType::RemoteToExternal => Cow::Borrowed("remote to external"),
DependencyType::ProvideSharedModule => Cow::Borrowed("provide shared module"),
DependencyType::ProvideModuleForShared => Cow::Borrowed("provide module for shared"),
DependencyType::ConsumeSharedFallback => Cow::Borrowed("consume shared fallback"),
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum SourceType {
Asset,
Remote,
ShareInit,
ConsumeShared,
#[default]
Unknown,
}
Expand All @@ -111,6 +112,7 @@ impl std::fmt::Display for SourceType {
SourceType::Asset => write!(f, "asset"),
SourceType::Remote => write!(f, "remote"),
SourceType::ShareInit => write!(f, "share-init"),
SourceType::ConsumeShared => write!(f, "consume-shared"),
SourceType::Unknown => write!(f, "unknown"),
}
}
Expand Down Expand Up @@ -138,7 +140,8 @@ pub enum ModuleType {
Asset,
Runtime,
Remote,
Provide,
ProvideShared,
ConsumeShared,
}

impl ModuleType {
Expand Down Expand Up @@ -224,7 +227,8 @@ impl ModuleType {
ModuleType::AssetInline => "asset/inline",
ModuleType::Runtime => "runtime",
ModuleType::Remote => "remote-module",
ModuleType::Provide => "provide-module",
ModuleType::ProvideShared => "provide-module",
ModuleType::ConsumeShared => "consume-shared-module",
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId,
DependencyType, ModuleDependency,
};

#[derive(Debug, Clone)]
pub struct ConsumeSharedFallbackDependency {
id: DependencyId,
request: String,
}

impl ConsumeSharedFallbackDependency {
pub fn new(request: String) -> Self {
Self {
id: DependencyId::new(),
request,
}
}
}

impl Dependency for ConsumeSharedFallbackDependency {
fn dependency_debug_name(&self) -> &'static str {
"ConsumeSharedFallbackDependency"
}

fn id(&self) -> &DependencyId {
&self.id
}

fn dependency_type(&self) -> &DependencyType {
&DependencyType::ProvideModuleForShared
}

fn category(&self) -> &DependencyCategory {
&DependencyCategory::Esm
}
}

impl ModuleDependency for ConsumeSharedFallbackDependency {
fn request(&self) -> &str {
&self.request
}
}

impl AsContextDependency for ConsumeSharedFallbackDependency {}
impl AsDependencyTemplate for ConsumeSharedFallbackDependency {}
Loading

0 comments on commit 086e6b4

Please sign in to comment.