Skip to content

Commit

Permalink
feat: support __webpack_exports_info__ (#7205)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Jul 18, 2024
1 parent 8f941c5 commit 748a372
Show file tree
Hide file tree
Showing 28 changed files with 414 additions and 158 deletions.
12 changes: 11 additions & 1 deletion crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rspack_sources::{BoxSource, ConcatSource, RawSource, SourceExt};
use rspack_util::{fx_hash::FxIndexMap, json_stringify, source_map::SourceMapKind};
use rustc_hash::FxHashMap as HashMap;
use rustc_hash::FxHashSet as HashSet;
use swc_core::atoms::Atom;

use crate::{
block_promise, contextify, get_exports_type_with_strict, impl_module_meta_info,
Expand Down Expand Up @@ -141,6 +142,7 @@ pub struct ContextOptions {
pub replaces: Vec<(String, u32, u32)>,
pub start: u32,
pub end: u32,
pub referenced_exports: Option<Vec<Atom>>,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -1074,7 +1076,7 @@ impl ContextModule {
context: options.resource.clone().into(),
options: options.context_options.clone(),
resource_identifier: format!("context{}|{}", &options.resource, path.to_string_lossy()),
referenced_exports: None,
referenced_exports: options.context_options.referenced_exports.clone(),
dependency_type: DependencyType::ContextElement(options.type_prefix),
});
})
Expand Down Expand Up @@ -1217,6 +1219,14 @@ fn create_identifier(options: &ContextModuleOptions) -> Identifier {
id += "|exclude: ";
id += &exclude.to_source_string();
}
if let Some(exports) = &options.context_options.referenced_exports {
id += "|referencedExports: ";
id += &format!(
"[{}]",
exports.iter().map(|x| format!(r#""{x}""#)).join(",")
);
}

if let Some(GroupOptions::ChunkGroup(group)) = &options.context_options.group_options {
if let Some(chunk_name) = &group.name {
id += "|chunkName: ";
Expand Down
12 changes: 9 additions & 3 deletions crates/rspack_core/src/dependency/context_element_dependency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use itertools::Itertools;
use swc_core::ecma::atoms::Atom;

use crate::{AsContextDependency, AsDependencyTemplate, Context};
use crate::{create_exports_object_referenced, AsContextDependency, AsDependencyTemplate, Context};
use crate::{ContextMode, ContextOptions, Dependency};
use crate::{DependencyCategory, DependencyId, DependencyType};
use crate::{ExtendedReferencedExport, ModuleDependency};
Expand Down Expand Up @@ -47,9 +48,14 @@ impl Dependency for ContextElementDependency {
_runtime: Option<&RuntimeSpec>,
) -> Vec<ExtendedReferencedExport> {
if let Some(referenced_exports) = &self.referenced_exports {
vec![ReferencedExport::new(referenced_exports.clone(), false).into()]
referenced_exports
.iter()
.map(|export| {
ExtendedReferencedExport::Export(ReferencedExport::new(vec![export.clone()], false))
})
.collect_vec()
} else {
vec![ExtendedReferencedExport::Array(vec![])]
create_exports_object_referenced()
}
}
}
Expand Down
20 changes: 0 additions & 20 deletions crates/rspack_core/src/dependency/import_dependency_trait.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/rspack_core/src/dependency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod dependency_template;
mod dependency_trait;
mod dependency_type;
mod entry;
mod import_dependency_trait;
mod loader_import;
mod module_dependency;
mod runtime_requirements_dependency;
Expand All @@ -29,7 +28,6 @@ pub use dependency_template::*;
pub use dependency_trait::*;
pub use dependency_type::DependencyType;
pub use entry::*;
pub use import_dependency_trait::ImportDependencyTrait;
pub use loader_import::*;
pub use module_dependency::*;
pub use runtime_requirements_dependency::RuntimeRequirementsDependency;
Expand Down
12 changes: 9 additions & 3 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,11 +740,17 @@ impl ExportsInfo {
}
UsedName::Vec(value) => {
if value.is_empty() {
let other_export_info = module_graph.get_export_info_by_id(&self.other_exports_info);
return other_export_info.get_used(runtime);
return self
.other_exports_info
.get_export_info(module_graph)
.get_used(runtime);
}
let info = self.id.get_read_only_export_info(&value[0], module_graph);
if let Some(exports_info) = info.get_exports_info(module_graph) {
if let Some(exports_info) = info
.exports_info
.map(|id| id.get_exports_info(module_graph))
&& value.len() > 1
{
return exports_info.get_used(
UsedName::Vec(value.iter().skip(1).cloned().collect::<Vec<_>>()),
runtime,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
use rspack_core::{module_namespace_promise, DependencyType, ErrorSpan, ImportDependencyTrait};
use rspack_core::{
create_exports_object_referenced, module_namespace_promise, DependencyType, ErrorSpan,
ExportsType, ExtendedReferencedExport, ModuleGraph, ReferencedExport,
};
use rspack_core::{AsContextDependency, Dependency};
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate};
use rspack_core::{ModuleDependency, TemplateContext, TemplateReplaceSource};
use swc_core::ecma::atoms::Atom;

pub fn create_import_dependency_referenced_exports(
dependency_id: &DependencyId,
referenced_exports: &Option<Vec<Atom>>,
mg: &ModuleGraph,
) -> Vec<ExtendedReferencedExport> {
if let Some(referenced_exports) = referenced_exports {
let mut refs = vec![];
for referenced_export in referenced_exports {
if referenced_export == "default" {
let Some(strict) = mg
.get_parent_module(dependency_id)
.and_then(|id| mg.module_by_identifier(id))
.and_then(|m| m.build_meta())
.map(|bm| bm.strict_harmony_module)
else {
return create_exports_object_referenced();
};
let Some(imported_module) = mg
.module_identifier_by_dependency_id(dependency_id)
.and_then(|id| mg.module_by_identifier(id))
else {
return create_exports_object_referenced();
};
let exports_type = imported_module.get_exports_type_readonly(mg, strict);
if matches!(
exports_type,
ExportsType::DefaultOnly | ExportsType::DefaultWithNamed
) {
return create_exports_object_referenced();
}
}
refs.push(ExtendedReferencedExport::Export(ReferencedExport::new(
vec![referenced_export.clone()],
false,
)));
}
refs
} else {
create_exports_object_referenced()
}
}

#[derive(Debug, Clone)]
pub struct ImportDependency {
start: u32,
Expand Down Expand Up @@ -49,6 +94,14 @@ impl Dependency for ImportDependency {
fn span(&self) -> Option<ErrorSpan> {
self.span
}

fn get_referenced_exports(
&self,
module_graph: &rspack_core::ModuleGraph,
_runtime: Option<&rspack_core::RuntimeSpec>,
) -> Vec<rspack_core::ExtendedReferencedExport> {
create_import_dependency_referenced_exports(&self.id, &self.referenced_exports, module_graph)
}
}

impl ModuleDependency for ImportDependency {
Expand All @@ -65,12 +118,6 @@ impl ModuleDependency for ImportDependency {
}
}

impl ImportDependencyTrait for ImportDependency {
fn referenced_exports(&self) -> Option<&Vec<Atom>> {
self.referenced_exports.as_ref()
}
}

impl DependencyTemplate for ImportDependency {
fn apply(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use rspack_core::{
module_namespace_promise, AsContextDependency, Dependency, DependencyCategory, DependencyId,
DependencyTemplate, DependencyType, ErrorSpan, ExtendedReferencedExport, ImportDependencyTrait,
ModuleDependency, ModuleGraph, ReferencedExport, RuntimeSpec, TemplateContext,
DependencyTemplate, DependencyType, ErrorSpan, ModuleDependency, TemplateContext,
TemplateReplaceSource,
};
use swc_core::ecma::atoms::Atom;

use super::import_dependency::create_import_dependency_referenced_exports;

#[derive(Debug, Clone)]
pub struct ImportEagerDependency {
start: u32,
Expand Down Expand Up @@ -54,14 +55,10 @@ impl Dependency for ImportEagerDependency {

fn get_referenced_exports(
&self,
_module_graph: &ModuleGraph,
_runtime: Option<&RuntimeSpec>,
) -> Vec<ExtendedReferencedExport> {
if let Some(referenced_exports) = &self.referenced_exports {
vec![ReferencedExport::new(referenced_exports.clone(), false).into()]
} else {
vec![ExtendedReferencedExport::Array(vec![])]
}
module_graph: &rspack_core::ModuleGraph,
_runtime: Option<&rspack_core::RuntimeSpec>,
) -> Vec<rspack_core::ExtendedReferencedExport> {
create_import_dependency_referenced_exports(&self.id, &self.referenced_exports, module_graph)
}
}

Expand All @@ -79,12 +76,6 @@ impl ModuleDependency for ImportEagerDependency {
}
}

impl ImportDependencyTrait for ImportEagerDependency {
fn referenced_exports(&self) -> Option<&Vec<Atom>> {
self.referenced_exports.as_ref()
}
}

impl DependencyTemplate for ImportEagerDependency {
fn apply(
&self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use itertools::Itertools;
use rspack_core::{module_raw, NormalInitFragment, UsedName};
use rspack_core::{
create_exports_object_referenced, module_raw, ExtendedReferencedExport, ModuleGraph,
NormalInitFragment, RuntimeSpec, UsedName,
};
use rspack_core::{AsContextDependency, Dependency, InitFragmentKey, InitFragmentStage};
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate};
use rspack_core::{DependencyType, ErrorSpan};
Expand Down Expand Up @@ -45,6 +48,18 @@ impl Dependency for ProvideDependency {
fn span(&self) -> Option<ErrorSpan> {
None
}

fn get_referenced_exports(
&self,
_module_graph: &ModuleGraph,
_runtime: Option<&RuntimeSpec>,
) -> Vec<ExtendedReferencedExport> {
if self.ids.is_empty() {
create_exports_object_referenced()
} else {
vec![ExtendedReferencedExport::Array(self.ids.clone())]
}
}
}

impl ModuleDependency for ProvideDependency {
Expand Down

This file was deleted.

Loading

2 comments on commit 748a372

@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-07-18 ebcd277) Current Change
10000_development-mode + exec 2.25 s ± 26 ms 2.27 s ± 27 ms +0.93 %
10000_development-mode_hmr + exec 697 ms ± 3.8 ms 701 ms ± 8.8 ms +0.55 %
10000_production-mode + exec 2.83 s ± 30 ms 2.82 s ± 18 ms -0.21 %
arco-pro_development-mode + exec 1.91 s ± 57 ms 1.93 s ± 73 ms +1.24 %
arco-pro_development-mode_hmr + exec 434 ms ± 3.2 ms 434 ms ± 1.1 ms -0.09 %
arco-pro_production-mode + exec 3.46 s ± 74 ms 3.51 s ± 68 ms +1.49 %
threejs_development-mode_10x + exec 1.75 s ± 20 ms 1.75 s ± 24 ms -0.05 %
threejs_development-mode_10x_hmr + exec 859 ms ± 7.4 ms 865 ms ± 7.3 ms +0.76 %
threejs_production-mode_10x + exec 5.73 s ± 41 ms 5.76 s ± 26 ms +0.46 %

@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 ✅ success
_selftest ✅ success
nx ✅ success
rspress ✅ success
rsbuild ❌ failure
examples ❌ failure

Please sign in to comment.