-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
391 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 100 additions & 3 deletions
103
crates/rspack_core/src/mf/container/container_reference_plugin.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("!") { | ||
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() | ||
} 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())); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.