Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Nov 22, 2023
1 parent 0cadc2d commit 533c38f
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 12 deletions.
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 @@ -66,6 +66,8 @@ pub enum DependencyType {
ContainerExposed,
/// container entry,
ContainerEntry,
/// remote to external,
RemoteToExternal,
Custom(Box<str>), // TODO it will increase large layout size
}

Expand Down Expand Up @@ -107,6 +109,7 @@ impl DependencyType {
DependencyType::ImportMetaContext => Cow::Borrowed("import.meta context"),
DependencyType::ContainerExposed => Cow::Borrowed("container exposed"),
DependencyType::ContainerEntry => Cow::Borrowed("container entry"),
DependencyType::RemoteToExternal => Cow::Borrowed("remote to external"),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub enum SourceType {
Css,
Wasm,
Asset,
Remote,
ShareInit,
#[default]
Unknown,
}
Expand All @@ -106,6 +108,8 @@ impl std::fmt::Display for SourceType {
SourceType::Css => write!(f, "css"),
SourceType::Wasm => write!(f, "wasm"),
SourceType::Asset => write!(f, "asset"),
SourceType::Remote => write!(f, "remote"),
SourceType::ShareInit => write!(f, "share-init"),
SourceType::Unknown => write!(f, "unknown"),
}
}
Expand All @@ -132,6 +136,7 @@ pub enum ModuleType {
AssetSource,
Asset,
Runtime,
Remote,
}

impl ModuleType {
Expand Down Expand Up @@ -216,6 +221,7 @@ impl ModuleType {
ModuleType::AssetResource => "asset/resource",
ModuleType::AssetInline => "asset/inline",
ModuleType::Runtime => "runtime",
ModuleType::Remote => "remote-module",
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ impl ModuleDependency for ContainerEntryDependency {
fn request(&self) -> &str {
&self.resource_identifier
}

fn user_request(&self) -> &str {
&self.resource_identifier
}

fn set_request(&mut self, _request: String) {}
}

impl AsContextDependency for ContainerEntryDependency {}
Expand Down
7 changes: 4 additions & 3 deletions crates/rspack_core/src/mf/container/container_entry_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ pub struct ContainerEntryModule {
blocks: Vec<AsyncDependenciesBlockIdentifier>,
dependencies: Vec<DependencyId>,
identifier: ModuleIdentifier,
name: String,
lib_ident: String,
exposes: Vec<(String, ExposeOptions)>,
share_scope: String,
}

impl ContainerEntryModule {
pub fn new(name: String, exposes: Vec<(String, ExposeOptions)>, share_scope: String) -> Self {
let lib_ident = format!("webpack/container/entry/{}", &name);
Self {
blocks: Vec::new(),
dependencies: Vec::new(),
Expand All @@ -35,7 +36,7 @@ impl ContainerEntryModule {
share_scope,
serde_json::to_string(&exposes).expect("should able to json to_string")
)),
name,
lib_ident,
exposes,
share_scope,
}
Expand Down Expand Up @@ -89,7 +90,7 @@ impl Module for ContainerEntryModule {
}

fn lib_ident(&self, _options: LibIdentOptions) -> Option<Cow<str>> {
Some(format!("webpack/container/entry/{}", self.name).into())
Some(self.lib_ident.as_str().into())
}

async fn build(
Expand Down
103 changes: 100 additions & 3 deletions crates/rspack_core/src/mf/container/container_reference_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,103 @@
use crate::Plugin;
use async_trait::async_trait;

use super::{remote_module::RemoteModule, RemoteRuntimeModule};
use crate::{
AdditionalChunkRuntimeRequirementsArgs, ExternalType, FactorizeArgs, ModuleExt,
ModuleFactoryResult, NormalModuleFactoryContext, Plugin,
PluginAdditionalChunkRuntimeRequirementsOutput, PluginContext, PluginFactorizeHookOutput,
RuntimeGlobals,
};

#[derive(Debug)]
pub struct ContainerReferencePluginOptions {
pub remote_type: ExternalType,
pub remotes: Vec<(String, RemoteOptions)>,
pub share_scope: Option<String>,
}

#[derive(Debug)]
pub struct ContainerReferencePlugin;
pub struct RemoteOptions {
pub external: Vec<String>,
pub share_scope: String,
}

#[derive(Debug)]
pub struct ContainerReferencePlugin {
options: ContainerReferencePluginOptions,
}

#[async_trait]
impl Plugin for ContainerReferencePlugin {
async fn factorize(
&self,
_ctx: PluginContext,
args: FactorizeArgs<'_>,
_job_ctx: &mut NormalModuleFactoryContext,
) -> PluginFactorizeHookOutput {
let request = args.dependency.request();
if !request.contains("!") {

Check failure on line 38 in crates/rspack_core/src/mf/container/container_reference_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

single-character string constant used as pattern
for (key, config) in &self.options.remotes {
let key_len = key.len();
let internal_request = &request[key_len..];
if request.starts_with(key)
&& (request.len() == key_len || internal_request.starts_with('/'))
{
let remote = RemoteModule::new(
request.to_owned(),
config
.external
.iter()
.enumerate()
.map(|(i, e)| {
if e.starts_with("internal ") {
e[9..].to_string()

Check failure on line 53 in crates/rspack_core/src/mf/container/container_reference_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

stripping a prefix manually
} else {
format!(
"webpack/container/reference/{}",
(i > 0)
.then(|| format!("/fallback-{}", i))
.unwrap_or_default()
)
}
})
.collect(),
format!(".{}", internal_request),
config.share_scope.clone(),
)
.boxed();
return Ok(Some(ModuleFactoryResult::new(remote)));
}
}
}
Ok(None)
}

impl Plugin for ContainerReferencePlugin {}
fn runtime_requirements_in_tree(
&self,
_ctx: PluginContext,
args: &mut AdditionalChunkRuntimeRequirementsArgs,
) -> PluginAdditionalChunkRuntimeRequirementsOutput {
if args
.runtime_requirements
.contains(RuntimeGlobals::ENSURE_CHUNK_HANDLERS)
{
args.runtime_requirements.insert(RuntimeGlobals::MODULE);
args
.runtime_requirements
.insert(RuntimeGlobals::MODULE_FACTORIES_ADD_ONLY);
args
.runtime_requirements
.insert(RuntimeGlobals::HAS_OWN_PROPERTY);
args
.runtime_requirements
.insert(RuntimeGlobals::INITIALIZE_SHARING);
args
.runtime_requirements
.insert(RuntimeGlobals::SHARE_SCOPE_MAP);
args
.compilation
.add_runtime_module(args.chunk, Box::new(RemoteRuntimeModule::default()));

Check failure on line 99 in crates/rspack_core/src/mf/container/container_reference_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

`Box::new(_)` of default value
}
Ok(())
}
}
5 changes: 5 additions & 0 deletions crates/rspack_core/src/mf/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ mod container_entry_module_factory;
mod container_exposed_dependency;
mod container_plugin;
mod container_reference_plugin;
mod remote_module;
mod remote_runtime_module;
mod remote_to_external_dependency;

pub use container_entry_module_factory::ContainerEntryModuleFactory;
pub use container_plugin::{ContainerPlugin, ContainerPluginOptions, ExposeOptions};
pub use remote_runtime_module::RemoteRuntimeModule;
pub use remote_to_external_dependency::RemoteToExternalDependency;
Loading

0 comments on commit 533c38f

Please sign in to comment.