Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve star export from external module #8217

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ mod t {
self.deps.push(dependency);
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.deps.retain(|dep| dep != &dependency);
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.deps
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ impl DependenciesBlock for ConcatenatedModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,10 @@ impl DependenciesBlock for ContextModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_core/src/dependencies_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub trait DependenciesBlock {

fn add_dependency_id(&mut self, dependency: DependencyId);

fn remove_dependency_id(&mut self, _dependency: DependencyId);

fn get_dependencies(&self) -> &[DependencyId];
}

Expand Down Expand Up @@ -194,14 +196,12 @@ impl DependenciesBlock for AsyncDependenciesBlock {
self.dependency_ids.push(dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependency_ids
fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependency_ids.retain(|dep| dep != &dependency);
}
}

impl AsyncDependenciesBlock {
pub fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependency_ids.retain(|dep| dep != &dependency);
fn get_dependencies(&self) -> &[DependencyId] {
&self.dependency_ids
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ impl DependenciesBlock for ExternalModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ mod test {
unreachable!()
}

fn remove_dependency_id(&mut self, _: DependencyId) {
unreachable!()
}

fn get_dependencies(&self) -> &[DependencyId] {
unreachable!()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ impl DependenciesBlock for NormalModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl DependenciesBlock for RawModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/self_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl DependenciesBlock for SelfModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_macros/src/runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub fn impl_runtime_module(
unreachable!()
}

fn remove_dependency_id(&mut self, _: ::rspack_core::DependencyId) {
unreachable!()
}

fn get_dependencies(&self) -> &[::rspack_core::DependencyId] {
unreachable!()
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_plugin_extract_css/src/css_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ impl DependenciesBlock for CssModule {
self.dependencies.push(dependency)
}

fn remove_dependency_id(&mut self, dependency: DependencyId) {
self.dependencies.retain(|d| d != &dependency)
}

fn get_dependencies(&self) -> &[DependencyId] {
&self.dependencies
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_plugin_lazy_compilation/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ impl DependenciesBlock for LazyCompilationProxyModule {
self.dependencies.push(dependency);
}

fn remove_dependency_id(&mut self, dependency: rspack_core::DependencyId) {
self.dependencies.retain(|d| d != &dependency);
}

fn get_dependencies(&self) -> &[rspack_core::DependencyId] {
&self.dependencies
}
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_plugin_library/src/modern_module/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod dependency;
mod import_dependency;
mod reexport_star_external_dependency;

pub use dependency::*;
pub use import_dependency::*;
pub use reexport_star_external_dependency::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use rspack_core::{
AsContextDependency, Dependency, InitFragmentExt, InitFragmentKey, InitFragmentStage,
NormalInitFragment,
};
use rspack_core::{Compilation, DependencyType, ExternalRequest, ExternalType, RuntimeSpec};
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate};
use rspack_core::{ModuleDependency, TemplateContext, TemplateReplaceSource};
use rspack_plugin_javascript::dependency::create_resource_identifier_for_esm_dependency;
use swc_core::ecma::atoms::Atom;

#[derive(Debug, Clone)]
pub struct ModernModuleReexportStarExternalDependency {
id: DependencyId,
request: Atom,
target_request: ExternalRequest,
external_type: ExternalType,
resource_identifier: String,
}

impl ModernModuleReexportStarExternalDependency {
pub fn new(request: Atom, target_request: ExternalRequest, external_type: ExternalType) -> Self {
let resource_identifier = create_resource_identifier_for_esm_dependency(request.as_str(), None);
Self {
request,
target_request,
external_type,
id: DependencyId::new(),
resource_identifier,
}
}
}

impl Dependency for ModernModuleReexportStarExternalDependency {
fn id(&self) -> &DependencyId {
&self.id
}

fn resource_identifier(&self) -> Option<&str> {
Some(&self.resource_identifier)
}

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

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

fn could_affect_referencing_module(&self) -> rspack_core::AffectType {
rspack_core::AffectType::True
}
}

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

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

fn set_request(&mut self, request: String) {
self.request = request.into();
}
}

impl DependencyTemplate for ModernModuleReexportStarExternalDependency {
fn apply(
&self,
_source: &mut TemplateReplaceSource,
code_generatable_context: &mut TemplateContext,
) {
let request = match &self.target_request {
ExternalRequest::Single(request) => Some(request),
ExternalRequest::Map(map) => map.get(&self.external_type),
};

if let Some(request) = request {
let chunk_init_fragments = code_generatable_context.chunk_init_fragments();
chunk_init_fragments.push(
NormalInitFragment::new(
format!(
"export * from {};\n",
serde_json::to_string(request.primary()).expect("invalid json to_string")
),
InitFragmentStage::StageESMImports,
0,
InitFragmentKey::Const(format!("modern_module_reexport_star_{}", self.request)),
None,
)
.boxed(),
);
}
}

fn dependency_id(&self) -> Option<DependencyId> {
Some(self.id)
}

fn update_hash(
&self,
_hasher: &mut dyn std::hash::Hasher,
_compilation: &Compilation,
_runtime: Option<&RuntimeSpec>,
) {
}
}

impl AsContextDependency for ModernModuleReexportStarExternalDependency {}
Loading
Loading