Skip to content

Commit

Permalink
feat: Module Federation, part 3, ProvideSharedPlugin (#4778)
Browse files Browse the repository at this point in the history
* ok

* fix

* fix
  • Loading branch information
ahabhgk authored and JSerFeng committed Dec 1, 2023
1 parent 54aee61 commit 5afb21b
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 78 deletions.
10 changes: 10 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ 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;

<<<<<<< HEAD
use self::raw_mf::{RawConsumeOptions, RawContainerReferencePluginOptions, RawProvideOptions};
=======
use self::raw_mf::{RawContainerReferencePluginOptions, RawProvideOptions};
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
pub use self::{
raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions,
raw_html::RawHtmlRspackPluginOptions, raw_limit_chunk_count::RawLimitChunkCountPluginOptions,
Expand Down Expand Up @@ -84,7 +88,10 @@ pub enum BuiltinPluginName {
ContainerReferencePlugin,
ModuleFederationRuntimePlugin,
ProvideSharedPlugin,
<<<<<<< HEAD
ConsumeSharedPlugin,
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))

// rspack specific plugins
HttpExternalsRspackPlugin,
Expand Down Expand Up @@ -226,13 +233,16 @@ impl RawOptionsApply for BuiltinPlugin {
provides.sort_unstable_by_key(|(k, _)| k.to_string());
plugins.push(ProvideSharedPlugin::new(provides).boxed())
}
<<<<<<< HEAD
BuiltinPluginName::ConsumeSharedPlugin => {
let consumes: Vec<_> = downcast_into::<Vec<RawConsumeOptions>>(self.options)?
.into_iter()
.map(Into::into)
.collect();
plugins.push(ConsumeSharedPlugin::new(consumes).boxed())
}
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))

// rspack specific plugins
BuiltinPluginName::HttpExternalsRspackPlugin => {
Expand Down
20 changes: 20 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/raw_mf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ pub struct RawProvideOptions {
pub share_key: String,
pub share_scope: String,
#[napi(ts_type = "string | false | undefined")]
<<<<<<< HEAD
pub version: Option<RawVersion>,
=======
pub version: Option<RawProvideVersion>,
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
pub eager: bool,
}

Expand All @@ -109,13 +113,18 @@ impl From<RawProvideOptions> for (String, ProvideOptions) {
ProvideOptions {
share_key: value.share_key,
share_scope: value.share_scope,
<<<<<<< HEAD
version: value.version.map(|v| RawVersionWrapper(v).into()),
=======
version: value.version.map(|v| RawProvideVersionWrapper(v).into()),
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
eager: value.eager,
},
)
}
}

<<<<<<< HEAD
#[derive(Debug)]
#[napi(object)]
pub struct RawConsumeOptions {
Expand Down Expand Up @@ -157,12 +166,21 @@ struct RawVersionWrapper(RawVersion);

impl From<RawVersionWrapper> for ProvideVersion {
fn from(value: RawVersionWrapper) -> Self {
=======
pub type RawProvideVersion = Either<String, bool>;

struct RawProvideVersionWrapper(RawProvideVersion);

impl From<RawProvideVersionWrapper> for ProvideVersion {
fn from(value: RawProvideVersionWrapper) -> Self {
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
match value.0 {
Either::A(s) => ProvideVersion::Version(s),
Either::B(_) => ProvideVersion::False,
}
}
}
<<<<<<< HEAD

impl From<RawVersionWrapper> for ConsumeVersion {
fn from(value: RawVersionWrapper) -> Self {
Expand All @@ -172,3 +190,5 @@ impl From<RawVersionWrapper> for ConsumeVersion {
}
}
}
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
3 changes: 3 additions & 0 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,15 @@ impl<'me> CodeSplitter<'me> {
.entry(entrypoint.ukey)
.or_default()
.extend(included_modules);
<<<<<<< HEAD

if let Some(name) = entrypoint.name() {
self
.named_chunk_groups
.insert(name.to_string(), entrypoint.ukey);
}
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}

let mut runtime_chunks = HashSet::default();
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/dependency/dependency_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ pub enum DependencyType {
ProvideSharedModule,
/// provide module for shared
ProvideModuleForShared,
<<<<<<< HEAD
/// consume shared fallback
ConsumeSharedFallback,
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
Custom(Box<str>), // TODO it will increase large layout size
}

Expand Down Expand Up @@ -118,7 +121,10 @@ impl DependencyType {
DependencyType::RemoteToExternal => Cow::Borrowed("remote to external"),
DependencyType::ProvideSharedModule => Cow::Borrowed("provide shared module"),
DependencyType::ProvideModuleForShared => Cow::Borrowed("provide module for shared"),
<<<<<<< HEAD
DependencyType::ConsumeSharedFallback => Cow::Borrowed("consume shared fallback"),
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use swc_core::ecma::atoms::JsWord;

use crate::{
<<<<<<< HEAD
get_import_var, property_access, to_comment, AsyncDependenciesBlockIdentifier, Compilation,
=======
get_import_var, property_access, AsyncDependenciesBlockIdentifier, Compilation,
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
DependenciesBlock, DependencyId, ExportsType, FakeNamespaceObjectMode, InitFragmentExt,
InitFragmentKey, InitFragmentStage, ModuleGraph, ModuleIdentifier, NormalInitFragment,
RuntimeGlobals, TemplateContext,
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ pub enum ModuleType {
Asset,
Runtime,
Remote,
<<<<<<< HEAD
ProvideShared,
ConsumeShared,
=======
Provide,
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}

impl ModuleType {
Expand Down Expand Up @@ -227,8 +231,12 @@ impl ModuleType {
ModuleType::AssetInline => "asset/inline",
ModuleType::Runtime => "runtime",
ModuleType::Remote => "remote-module",
<<<<<<< HEAD
ModuleType::ProvideShared => "provide-module",
ModuleType::ConsumeShared => "consume-shared-module",
=======
ModuleType::Provide => "provide-module",
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/mf/sharing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<<<<<<< HEAD
pub mod consume_shared_fallback_dependency;
pub mod consume_shared_module;
pub mod consume_shared_plugin;
pub mod consume_shared_runtime_module;
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
pub mod provide_for_shared_dependency;
pub mod provide_shared_dependency;
pub mod provide_shared_module;
Expand Down
23 changes: 23 additions & 0 deletions crates/rspack_core/src/mf/sharing/provide_shared_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl ProvideSharedModule {
request: String,
eager: bool,
) -> Self {
<<<<<<< HEAD
let identifier = format!(
"provide shared module ({}) {}@{} = {}",
&share_scope, &name, &version, &request
Expand All @@ -50,6 +51,20 @@ impl ProvideSharedModule {
identifier: ModuleIdentifier::from(identifier.clone()),
lib_ident: format!("webpack/sharing/provide/{}/{}", &share_scope, &name),
readable_identifier: identifier,
=======
Self {
blocks: Vec::new(),
dependencies: Vec::new(),
identifier: ModuleIdentifier::from(format!(
"provide shared module ({}) {}@{} = {}",
&share_scope, &name, &version, &request
)),
lib_ident: format!("webpack/sharing/provide/{}/{}", &share_scope, &name),
readable_identifier: format!(
"provide shared module ({}) {}@{} = {}",
&share_scope, &name, &version, &request
),
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
name,
share_scope,
version,
Expand Down Expand Up @@ -90,7 +105,11 @@ impl Module for ProvideSharedModule {
}

fn module_type(&self) -> &ModuleType {
<<<<<<< HEAD
&ModuleType::ProvideShared
=======
&ModuleType::Provide
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}

fn source_types(&self) -> &[SourceType] {
Expand Down Expand Up @@ -123,7 +142,11 @@ impl Module for ProvideSharedModule {
if self.eager {
dependencies.push(dep as BoxDependency);
} else {
<<<<<<< HEAD
let mut block = AsyncDependenciesBlock::new(self.identifier, "", None);
=======
let mut block = AsyncDependenciesBlock::new(self.identifier, "");
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
block.add_dependency(dep);
blocks.push(block);
}
Expand Down
19 changes: 19 additions & 0 deletions crates/rspack_core/src/mf/sharing/provide_shared_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use std::fmt;
use async_trait::async_trait;
use once_cell::sync::Lazy;
use regex::Regex;
<<<<<<< HEAD
use rspack_error::Result;
=======
use rspack_error::{Error, InternalError, Result, Severity};
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
use rspack_loader_runner::ResourceData;
use rustc_hash::FxHashMap;
use tokio::sync::RwLock;
Expand Down Expand Up @@ -83,16 +87,24 @@ impl ProvideSharedPlugin {
#[allow(clippy::too_many_arguments)]
pub async fn provide_shared_module(
&self,
<<<<<<< HEAD
_key: &str,
=======
key: &str,
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
share_key: &str,
share_scope: &str,
version: Option<&ProvideVersion>,
eager: bool,
resource: &str,
resource_data: &ResourceData,
) -> Result<()> {
<<<<<<< HEAD
// TODO: emit warning
// let error_header = "No version specified and unable to automatically determine one.";
=======
let error_header = "No version specified and unable to automatically determine one.";
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
if let Some(version) = version {
self.resolved_provide_map.write().await.insert(
resource.to_string(),
Expand All @@ -118,10 +130,17 @@ impl ProvideSharedPlugin {
},
);
} else {
<<<<<<< HEAD
// return Err(Error::InternalError(InternalError::new(format!("{error_header} No version in description file (usually package.json). Add version to description file {}, or manually specify version in shared config. shared module {key} -> {resource}", description.path().display()), Severity::Warn)));
}
} else {
// return Err(Error::InternalError(InternalError::new(format!("{error_header} No description file (usually package.json) found. Add description file with name and version, or manually specify version in shared config. shared module {key} -> {resource}"), Severity::Warn)));
=======
return Err(Error::InternalError(InternalError::new(format!("{error_header} No version in description file (usually package.json). Add version to description file {}, or manually specify version in shared config. shared module {key} -> {resource}", description.path().display()), Severity::Warn)));
}
} else {
return Err(Error::InternalError(InternalError::new(format!("{error_header} No description file (usually package.json) found. Add description file with name and version, or manually specify version in shared config. shared module {key} -> {resource}"), Severity::Warn)));
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}
Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ impl NormalModuleFactory {
dependency_type: data.dependency.dependency_type().clone(),
resolve_data_request: dependency.request(),
resource_resolve_data: resource_data.clone(),
<<<<<<< HEAD
context: data.context.clone(),
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
};
let module = if let Some(module) = self
.plugin_driver
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/plugin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ pub struct NormalModuleCreateData<'a> {
pub dependency_type: DependencyType,
pub resolve_data_request: &'a str,
pub resource_resolve_data: ResourceData,
<<<<<<< HEAD
pub context: Context,
=======
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}

#[derive(Debug, Clone)]
Expand Down
5 changes: 5 additions & 0 deletions packages/rspack/src/builtin-plugin/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ export enum BuiltinPluginName {
OldSplitChunksPlugin = "OldSplitChunksPlugin",
ContainerPlugin = "ContainerPlugin",
ContainerReferencePlugin = "ContainerReferencePlugin",
<<<<<<< HEAD
ModuleFederationRuntimePlugin = "ModuleFederationRuntimePlugin"
>>>>>>> c30ae9213 (feat: Module Federation, part 2, ContainerReferencePlugin (#4735))
=======
ModuleFederationRuntimePlugin = "ModuleFederationRuntimePlugin",
ProvideSharedPlugin = "ProvideSharedPlugin"
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}

export abstract class RspackBuiltinPlugin implements RspackPluginInstance {
Expand Down
2 changes: 0 additions & 2 deletions packages/rspack/src/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export class ModuleFederationPlugin {
) {
compiler.options.output.enabledLibraryTypes!.push(library.type);
}
const mfRuntimePlugin = new ModuleFederationRuntimePlugin();
mfRuntimePlugin.apply(compiler);
compiler.hooks.afterPlugins.tap("ModuleFederationPlugin", () => {
if (
options.exposes &&
Expand Down
14 changes: 14 additions & 0 deletions packages/rspack/src/container/ModuleFederationRuntimePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const ModuleFederationRuntimePlugin2 = create(
() => undefined
);

<<<<<<< HEAD
const compilerToPlugins = new WeakMap<Compiler, Set<string>>();
=======
const compilerToPlugins = new WeakMap<Compiler, string[]>();
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))

export class ModuleFederationRuntimePlugin {
apply(compiler: Compiler) {
Expand All @@ -19,9 +23,13 @@ export class ModuleFederationRuntimePlugin {
const plugins = compilerToPlugins.get(compiler);
if (plugins) {
// TODO: move to rust side so don't depend on dataUrl?
<<<<<<< HEAD
const entry = [...plugins]
.map(p => `import ${JSON.stringify(p)};`)
.join("\n");
=======
const entry = plugins.map(p => `import "${p}";`).join("\n");
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
new EntryPlugin(compiler.context, `data:text/javascript,${entry}`, {
name: undefined
}).apply(compiler);
Expand All @@ -34,8 +42,14 @@ export class ModuleFederationRuntimePlugin {
static addPlugin(compiler: Compiler, plugin: string) {
let plugins = compilerToPlugins.get(compiler);
if (!plugins) {
<<<<<<< HEAD
compilerToPlugins.set(compiler, (plugins = new Set()));
}
plugins.add(plugin);
=======
compilerToPlugins.set(compiler, (plugins = []));
}
plugins.push(plugin);
>>>>>>> 24ebe5e86 (feat: Module Federation, part 3, ProvideSharedPlugin (#4778))
}
}
Loading

0 comments on commit 5afb21b

Please sign in to comment.