Skip to content

Commit

Permalink
fix(mf2): extra provide sharing data (#7408)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Aug 1, 2024
1 parent 6a924fc commit 9e235b0
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 68 deletions.
3 changes: 3 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,9 @@ export interface RawProvideOptions {
shareScope: string
version?: string | false | undefined
eager: boolean
singleton?: boolean
requiredVersion?: string | false | undefined
strictVersion?: boolean
}

export interface RawRegex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub struct RawProvideOptions {
#[napi(ts_type = "string | false | undefined")]
pub version: Option<RawVersion>,
pub eager: bool,
pub singleton: Option<bool>,
#[napi(ts_type = "string | false | undefined")]
pub required_version: Option<RawVersion>,
pub strict_version: Option<bool>,
}

impl From<RawProvideOptions> for (String, ProvideOptions) {
Expand All @@ -116,6 +120,9 @@ impl From<RawProvideOptions> for (String, ProvideOptions) {
share_scope: value.share_scope,
version: value.version.map(|v| RawVersionWrapper(v).into()),
eager: value.eager,
singleton: value.singleton,
required_version: value.required_version.map(|v| RawVersionWrapper(v).into()),
strict_version: value.strict_version,
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct ConsumeOptions {
pub eager: bool,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConsumeVersion {
Version(String),
False,
Expand Down
13 changes: 12 additions & 1 deletion crates/rspack_plugin_mf/src/sharing/provide_shared_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rspack_core::{
};

use super::provide_shared_plugin::ProvideVersion;
use crate::ConsumeVersion;

#[derive(Debug, Clone)]
pub struct ProvideSharedDependency {
Expand All @@ -13,24 +14,31 @@ pub struct ProvideSharedDependency {
pub name: String,
pub version: ProvideVersion,
pub eager: bool,
pub singleton: Option<bool>,
pub required_version: Option<ConsumeVersion>,
pub strict_version: Option<bool>,
resource_identifier: String,
}

impl ProvideSharedDependency {
#[allow(clippy::too_many_arguments)]
pub fn new(
share_scope: String,
name: String,
version: ProvideVersion,
request: String,
eager: bool,
singleton: Option<bool>,
required_version: Option<ConsumeVersion>,
strict_version: Option<bool>,
) -> Self {
let resource_identifier = format!(
"provide module ({}) {} as {} @ {} {}",
&share_scope,
&request,
&name,
&version,
eager.then_some("eager").unwrap_or_default()
eager.then_some("eager").unwrap_or_default(),
);
Self {
id: DependencyId::new(),
Expand All @@ -39,6 +47,9 @@ impl ProvideSharedDependency {
name,
version,
eager,
singleton,
required_version,
strict_version,
resource_identifier,
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::{
CodeGenerationDataShareInit, DataInitInfo, ProvideSharedInfo, ShareInitData,
},
};
use crate::ConsumeVersion;

#[impl_source_map_config]
#[derive(Debug)]
Expand All @@ -34,18 +35,25 @@ pub struct ProvideSharedModule {
version: ProvideVersion,
request: String,
eager: bool,
singleton: Option<bool>,
required_version: Option<ConsumeVersion>,
strict_version: Option<bool>,
factory_meta: Option<FactoryMeta>,
build_info: Option<BuildInfo>,
build_meta: Option<BuildMeta>,
}

impl ProvideSharedModule {
#[allow(clippy::too_many_arguments)]
pub fn new(
share_scope: String,
name: String,
version: ProvideVersion,
request: String,
eager: bool,
singleton: Option<bool>,
required_version: Option<ConsumeVersion>,
strict_version: Option<bool>,
) -> Self {
let identifier = format!(
"provide shared module ({}) {}@{} = {}",
Expand All @@ -62,6 +70,9 @@ impl ProvideSharedModule {
version,
request,
eager,
singleton,
required_version,
strict_version,
factory_meta: None,
build_info: None,
build_meta: None,
Expand Down Expand Up @@ -195,6 +206,9 @@ impl Module for ProvideSharedModule {
version: self.version.clone(),
factory,
eager: self.eager,
singleton: self.singleton,
strict_version: self.strict_version,
required_version: self.required_version.clone(),
}),
}],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ impl ModuleFactory for ProvideSharedModuleFactory {
dep.version.clone(),
dep.request().to_owned(),
dep.eager,
dep.singleton,
dep.required_version.clone(),
dep.strict_version,
),
)))
}
Expand Down
28 changes: 28 additions & 0 deletions crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::{
provide_shared_dependency::ProvideSharedDependency,
provide_shared_module_factory::ProvideSharedModuleFactory,
};
use crate::ConsumeVersion;

static RELATIVE_REQUEST: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(\/|[A-Za-z]:\\|\\\\|\.\.?(\/|$))").expect("Invalid regex"));
Expand All @@ -30,6 +31,9 @@ pub struct ProvideOptions {
pub share_scope: String,
pub version: Option<ProvideVersion>,
pub eager: bool,
pub singleton: Option<bool>,
pub required_version: Option<ConsumeVersion>,
pub strict_version: Option<bool>,
}

#[derive(Debug, Clone)]
Expand All @@ -38,6 +42,9 @@ pub struct VersionedProvideOptions {
pub share_scope: String,
pub version: ProvideVersion,
pub eager: bool,
pub singleton: Option<bool>,
pub required_version: Option<ConsumeVersion>,
pub strict_version: Option<bool>,
}

impl ProvideOptions {
Expand All @@ -47,6 +54,9 @@ impl ProvideOptions {
share_scope: self.share_scope.clone(),
version: self.version.clone().unwrap_or_default(),
eager: self.eager,
singleton: self.singleton,
required_version: self.required_version.clone(),
strict_version: self.strict_version,
}
}
}
Expand Down Expand Up @@ -94,6 +104,9 @@ impl ProvideSharedPlugin {
share_scope: &str,
version: Option<&ProvideVersion>,
eager: bool,
singleton: Option<bool>,
required_version: Option<ConsumeVersion>,
strict_version: Option<bool>,
resource: &str,
resource_data: &ResourceData,
mut add_diagnostic: impl FnMut(Diagnostic),
Expand All @@ -108,6 +121,9 @@ impl ProvideSharedPlugin {
share_scope: share_scope.to_string(),
version: version.to_owned(),
eager,
singleton,
strict_version,
required_version,
},
);
} else if let Some(description) = &resource_data.resource_description {
Expand All @@ -122,6 +138,9 @@ impl ProvideSharedPlugin {
share_scope: share_scope.to_string(),
version: ProvideVersion::Version(version.to_string()),
eager,
singleton,
strict_version,
required_version,
},
);
} else {
Expand Down Expand Up @@ -174,6 +193,9 @@ async fn finish_make(&self, compilation: &mut Compilation) -> Result<()> {
config.version.clone(),
resource.to_string(),
config.eager,
config.singleton,
config.required_version.clone(),
config.strict_version,
)),
EntryOptions {
name: None,
Expand Down Expand Up @@ -213,6 +235,9 @@ async fn normal_module_factory_module(
&config.share_scope,
config.version.as_ref(),
config.eager,
config.singleton,
config.required_version.clone(),
config.strict_version,
resource,
resource_data,
|d| data.diagnostics.push(d),
Expand All @@ -230,6 +255,9 @@ async fn normal_module_factory_module(
&config.share_scope,
config.version.as_ref(),
config.eager,
config.singleton,
config.required_version.clone(),
config.strict_version,
resource,
resource_data,
|d| data.diagnostics.push(d),
Expand Down
29 changes: 24 additions & 5 deletions crates/rspack_plugin_mf/src/sharing/share_runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rspack_core::{
use rustc_hash::FxHashMap;

use super::provide_shared_plugin::ProvideVersion;
use crate::utils::json_stringify;
use crate::{utils::json_stringify, ConsumeVersion};

#[impl_runtime_module]
#[derive(Debug)]
Expand Down Expand Up @@ -76,13 +76,29 @@ impl RuntimeModule for ShareRuntimeModule {
.map(|info| match info {
DataInitInfo::ExternalModuleId(Some(id)) => json_stringify(&id),
DataInitInfo::ProvideSharedInfo(info) => {
format!(
"{{ name: {}, version: {}, factory: {}, eager: {} }}",
let mut stage = format!(
"{{ name: {}, version: {}, factory: {}, eager: {}",
json_stringify(&info.name),
json_stringify(&info.version.to_string()),
info.factory,
if info.eager { "1" } else { "0" }
)
if info.eager { "1" } else { "0" },
);
if self.enhanced {
if let Some(singleton) = info.singleton {
stage += ", singleton: ";
stage += if singleton { "1" } else { "0" };
}
if let Some(required_version) = info.required_version {
stage += ", requiredVersion: ";
stage += &json_stringify(&required_version.to_string());
}
if let Some(strict_version) = info.strict_version {
stage += ", strictVersion: ";
stage += if strict_version { "1" } else { "0" };
}
}
stage += " }";
stage
}
_ => "".to_string(),
})
Expand Down Expand Up @@ -141,4 +157,7 @@ pub struct ProvideSharedInfo {
pub version: ProvideVersion,
pub factory: String,
pub eager: bool,
pub singleton: Option<bool>,
pub required_version: Option<ConsumeVersion>,
pub strict_version: Option<bool>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react";

export default () => {
return `App rendered with [${React()}]`;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
it("should load the component from container", () => {
return import("./App").then(({ default: App }) => {
expect(App()).toBe("App rendered with [This is react 0.1.2]");
const sharingReact = __webpack_require__.initializeSharingData.scopeToSharingDataMapping.default[0];
// name: "react", version: "0", eager: 0, singleton: 1, requiredVersion: "*", strictVersion: 0
expect(sharingReact.name).toBe("react");
expect(sharingReact.version).toBe("0");
expect(sharingReact.eager).toBe(0);
expect(sharingReact.singleton).toBe(1);
expect(sharingReact.requiredVersion).toBe("*");
expect(sharingReact.strictVersion).toBe(0);
const initOptionsReact = __webpack_require__.federation.initOptions.shared.react[0];
expect(initOptionsReact.version).toBe("0");
expect(initOptionsReact.eager).toBe(0);
expect(initOptionsReact.singleton).toBe(1);
expect(initOptionsReact.requiredVersion).toBe("*");
expect(initOptionsReact.strictVersion).toBe(0);
});
});

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { ModuleFederationPlugin } = require("@rspack/core").container;

/** @type {import("@rspack/core").Configuration[]} */
module.exports = {
output: {
filename: "[name].js",
uniqueName: "provide-sharing-extra-data"
},
plugins: [
new ModuleFederationPlugin({
name: "container-provide-sharing-extra-data",
shared: {
react: {
version: false,
requiredVersion: false,
singleton: true,
strictVersion: false,
}
}
})
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../..").TConfigCaseConfig} */
module.exports = {
findBundle: function (i, options) {
return "./main.js";
}
};
Loading

2 comments on commit 9e235b0

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ❌ failure
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-08-01 2f6696e) Current Change
10000_development-mode + exec 2.24 s ± 26 ms 2.25 s ± 26 ms +0.51 %
10000_development-mode_hmr + exec 699 ms ± 9.2 ms 717 ms ± 14 ms +2.46 %
10000_production-mode + exec 2.79 s ± 22 ms 2.81 s ± 27 ms +0.79 %
arco-pro_development-mode + exec 1.9 s ± 77 ms 1.87 s ± 69 ms -1.78 %
arco-pro_development-mode_hmr + exec 432 ms ± 3 ms 432 ms ± 2.3 ms +0.08 %
arco-pro_production-mode + exec 3.39 s ± 64 ms 3.41 s ± 116 ms +0.42 %
threejs_development-mode_10x + exec 1.77 s ± 19 ms 1.72 s ± 7.6 ms -2.92 %
threejs_development-mode_10x_hmr + exec 878 ms ± 10 ms 839 ms ± 6 ms -4.46 %
threejs_production-mode_10x + exec 5.49 s ± 37 ms 5.48 s ± 35 ms -0.29 %

Please sign in to comment.