Skip to content
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
36 changes: 24 additions & 12 deletions crates/node_binding/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::ptr::NonNull;

use napi::Either;
use napi_derive::napi;
use rspack_core::{Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraph, RuntimeSpec};
use rspack_core::{
Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraph, PrefetchExportsInfoMode, RuntimeSpec,
};
use rspack_util::atom::Atom;

use crate::JsRuntimeSpec;

Expand Down Expand Up @@ -43,7 +46,12 @@ impl JsExportsInfo {
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
});
Ok(self.exports_info.is_used(&module_graph, runtime.as_ref()))
let exports_info = ExportsInfoGetter::prefetch(
&self.exports_info,
&module_graph,
PrefetchExportsInfoMode::AllExports,
);
Ok(ExportsInfoGetter::is_used(&exports_info, runtime.as_ref()))
}

#[napi(ts_args_type = "runtime: string | string[] | undefined")]
Expand All @@ -53,7 +61,11 @@ impl JsExportsInfo {
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
});
let exports_info = ExportsInfoGetter::prefetch(&self.exports_info, &module_graph, None);
let exports_info = ExportsInfoGetter::prefetch(
&self.exports_info,
&module_graph,
PrefetchExportsInfoMode::AllExports,
);
Ok(ExportsInfoGetter::is_module_used(
&exports_info,
runtime.as_ref(),
Expand Down Expand Up @@ -88,16 +100,16 @@ impl JsExportsInfo {
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
});
let used = match js_name {
Either::A(s) => self
.exports_info
.get_used(&module_graph, &[s.into()], runtime.as_ref()),
Either::B(v) => self.exports_info.get_used(
&module_graph,
v.into_iter().map(Into::into).collect::<Vec<_>>().as_slice(),
runtime.as_ref(),
),
let names = match js_name {
Either::A(s) => vec![Atom::from(s)],
Either::B(v) => v.into_iter().map(Into::into).collect::<Vec<_>>(),
};
let exports_info = ExportsInfoGetter::prefetch(
&self.exports_info,
&module_graph,
PrefetchExportsInfoMode::NamedNestedExports(&names),
);
let used = ExportsInfoGetter::get_used(&exports_info, &names, runtime.as_ref());
Ok(used as u32)
}
}
7 changes: 4 additions & 3 deletions crates/node_binding/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ptr::NonNull;

use napi::{Either, Env, JsString};
use napi_derive::napi;
use rspack_core::{Compilation, ModuleGraph, RuntimeSpec};
use rspack_core::{Compilation, ModuleGraph, PrefetchExportsInfoMode, RuntimeSpec};

use crate::{
DependencyObject, JsExportsInfo, ModuleGraphConnectionWrapper, ModuleObject, ModuleObjectRef,
Expand Down Expand Up @@ -89,8 +89,9 @@ impl JsModuleGraph {
runtime.extend(vec.iter().map(String::as_str).map(ustr::Ustr::from));
}
};
let used_exports =
module_graph.get_used_exports(&js_module.identifier, Some(&RuntimeSpec::new(runtime)));
let exports_info = module_graph
.get_prefetched_exports_info(&js_module.identifier, PrefetchExportsInfoMode::AllExports);
let used_exports = exports_info.get_used_exports(Some(&RuntimeSpec::new(runtime)));
Ok(match used_exports {
rspack_core::UsedExports::Unknown => None,
rspack_core::UsedExports::UsedNamespace(b) => Some(Either::A(b)),
Expand Down
30 changes: 19 additions & 11 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ use crate::{
CodeGenerationPublicPathAutoReplace, CodeGenerationResult, Compilation, ConcatenatedModuleIdent,
ConcatenationScope, ConditionalInitFragment, ConnectionState, Context, DependenciesBlock,
DependencyId, DependencyType, ErrorSpan, ExportInfoGetter, ExportProvided, ExportsArgument,
ExportsType, FactoryMeta, IdentCollector, InitFragment, InitFragmentStage, LibIdentOptions,
MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument, ModuleGraph, ModuleGraphConnection,
ModuleIdentifier, ModuleLayer, ModuleType, Resolve, RuntimeCondition, RuntimeGlobals,
RuntimeSpec, SourceType, SpanExt, Template, UsageState, DEFAULT_EXPORT, NAMESPACE_OBJECT_EXPORT,
ExportsInfoGetter, ExportsType, FactoryMeta, IdentCollector, InitFragment, InitFragmentStage,
LibIdentOptions, MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument, ModuleGraph,
ModuleGraphConnection, ModuleIdentifier, ModuleLayer, ModuleType, PrefetchExportsInfoMode,
Resolve, RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, Template,
UsageState, DEFAULT_EXPORT, NAMESPACE_OBJECT_EXPORT,
};

type ExportsDefinitionArgs = Vec<(String, String)>;
Expand Down Expand Up @@ -2196,10 +2197,13 @@ impl ConcatenatedModule {
}
}

let exports_info = mg.get_exports_info(&info.id());
let exports_info = mg.get_prefetched_exports_info(
&info.id(),
PrefetchExportsInfoMode::NamedNestedExports(&export_name),
);
// webpack use `get_exports_info` here, https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/optimize/ConcatenatedModule.js#L377-L377
// But in our arch, there is no way to modify module graph during code_generation phase, so we use `get_export_info_without_mut_module_graph` instead.`
let export_info = exports_info.get_export_info_without_mut_module_graph(mg, &export_name[0]);
let export_info = exports_info.get_export_info_without_mut_module_graph(&export_name[0]);
let export_info_hash_key = export_info.as_hash_key();

if already_visited.contains(&export_info_hash_key) {
Expand All @@ -2218,7 +2222,7 @@ impl ConcatenatedModule {
ModuleInfo::Concatenated(info) => {
let export_id = export_name.first().cloned();
if matches!(
export_info.provided(mg),
export_info.provided(),
Some(crate::ExportProvided::NotProvided)
) {
needed_namespace_objects.insert(info.module);
Expand All @@ -2237,7 +2241,9 @@ impl ConcatenatedModule {
if let Some(ref export_id) = export_id
&& let Some(direct_export) = info.export_map.as_ref().and_then(|map| map.get(export_id))
{
if let Some(used_name) = exports_info.get_used_name(mg, runtime, &export_name) {
if let Some(used_name) =
ExportsInfoGetter::get_used_name(&exports_info, runtime, &export_name)
{
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L402-L404
let used_name = used_name.to_used_name_vec();

Expand Down Expand Up @@ -2315,9 +2321,9 @@ impl ConcatenatedModule {

if info.namespace_export_symbol.is_some() {
// That's how webpack write https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L463-L471
let used_name = exports_info
.get_used_name(mg, runtime, &export_name)
let used_name = ExportsInfoGetter::get_used_name(&exports_info, runtime, &export_name)
.expect("should have export name");

let used_name = used_name.to_used_name_vec();
return Binding::Raw(RawBinding {
info_id: info.module,
Expand All @@ -2340,7 +2346,9 @@ impl ConcatenatedModule {
);
}
ModuleInfo::External(info) => {
if let Some(used_name) = exports_info.get_used_name(mg, runtime, &export_name) {
if let Some(used_name) =
ExportsInfoGetter::get_used_name(&exports_info, runtime, &export_name)
{
let used_name = used_name.to_used_name_vec();
let comment = if used_name == export_name {
String::new()
Expand Down
22 changes: 12 additions & 10 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use swc_core::ecma::atoms::Atom;
use crate::{
compile_boolean_matcher_from_lists, contextify, property_access, to_comment, to_normal_comment,
AsyncDependenciesBlockIdentifier, ChunkGraph, Compilation, CompilerOptions, DependenciesBlock,
DependencyId, Environment, ExportsArgument, ExportsType, FakeNamespaceObjectMode,
InitFragmentExt, InitFragmentKey, InitFragmentStage, Module, ModuleGraph, ModuleId,
ModuleIdentifier, NormalInitFragment, PathInfo, RuntimeCondition, RuntimeGlobals, RuntimeSpec,
TemplateContext,
DependencyId, Environment, ExportsArgument, ExportsInfoGetter, ExportsType,
FakeNamespaceObjectMode, InitFragmentExt, InitFragmentKey, InitFragmentStage, Module,
ModuleGraph, ModuleId, ModuleIdentifier, NormalInitFragment, PathInfo, PrefetchExportsInfoMode,
RuntimeCondition, RuntimeGlobals, RuntimeSpec, TemplateContext,
};

pub fn runtime_condition_expression(
Expand Down Expand Up @@ -208,12 +208,14 @@ pub fn export_from_import(
.as_deref()
.unwrap_or(export_name);
if !export_name.is_empty() {
let exports_info = compilation
.get_module_graph()
.get_exports_info(&module_identifier);
let Some(used_name) =
exports_info.get_used_name(&compilation.get_module_graph(), *runtime, export_name)
else {
let Some(used_name) = ExportsInfoGetter::get_used_name(
&compilation.get_module_graph().get_prefetched_exports_info(
&module_identifier,
PrefetchExportsInfoMode::NamedNestedExports(export_name),
),
*runtime,
export_name,
) else {
return format!(
"{} undefined",
to_normal_comment(&property_access(export_name, 0))
Expand Down
69 changes: 36 additions & 33 deletions crates/rspack_core/src/exports/export_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ use serde::Serialize;

use super::{
ExportInfoGetter, ExportInfoTargetValue, ExportProvided, ExportsInfo, ExportsInfoData,
FindTargetRetEnum, FindTargetRetValue, ResolvedExportInfoTarget,
ExportsInfoGetter, FindTargetRetEnum, FindTargetRetValue, ResolvedExportInfoTarget,
ResolvedExportInfoTargetWithCircular, TerminalBinding, UnResolvedExportInfoTarget, UsageState,
NEXT_EXPORT_INFO_UKEY,
};
use crate::{Compilation, DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec};
use crate::{
Compilation, DependencyId, ModuleGraph, ModuleIdentifier, PrefetchExportsInfoMode, RuntimeSpec,
};

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportInfo(Ukey);
Expand Down Expand Up @@ -88,13 +90,18 @@ impl ExportInfo {
return Some(TerminalBinding::ExportInfo(*self));
}
let target = info.get_target(mg)?;

let exports_info = mg.get_exports_info(&target.module);
let Some(export) = target.export else {
return Some(TerminalBinding::ExportsInfo(exports_info));
};
exports_info
.get_read_only_export_info_recursive(mg, &export)
.map(TerminalBinding::ExportInfo)
ExportsInfoGetter::prefetch(
&exports_info,
mg,
PrefetchExportsInfoMode::NamedNestedExports(&export),
)
.get_read_only_export_info_recursive(&export)
.map(|data| TerminalBinding::ExportInfo(data.id))
}

pub fn update_hash_with_visited(
Expand Down Expand Up @@ -234,11 +241,12 @@ impl ExportInfoData {
if valid_target_module_filter(&target.module) {
return FindTargetRetEnum::Value(target);
}
let exports_info = mg.get_exports_info(&target.module);
let export_info = exports_info.get_export_info_without_mut_module_graph(
mg,
&target.export.as_ref().expect("should have export")[0],
let name = &target.export.as_ref().expect("should have export")[0];
let exports_info = mg.get_prefetched_exports_info(
&target.module,
PrefetchExportsInfoMode::NamedExports(HashSet::from_iter([name])),
);
let export_info = exports_info.get_export_info_without_mut_module_graph(name);
let export_info_hash_key = export_info.as_hash_key();
if visited.contains(&export_info_hash_key) {
return FindTargetRetEnum::Undefined;
Expand Down Expand Up @@ -372,11 +380,11 @@ impl ExportInfoData {
// and the Static variant represents the most situation which FlagDependencyExportsPlugin can
// analyze the exports statically.
#[derive(Debug)]
pub enum MaybeDynamicTargetExportInfo {
Static(ExportInfo),
pub enum MaybeDynamicTargetExportInfo<'a> {
Static(&'a ExportInfoData),
Dynamic {
export_name: Atom,
other_export_info: ExportInfo,
other_export_info: &'a ExportInfoData,
data: ExportInfoData,
},
}
Expand All @@ -390,28 +398,26 @@ pub enum MaybeDynamicTargetExportInfoHashKey {
},
}

impl MaybeDynamicTargetExportInfo {
impl<'a> MaybeDynamicTargetExportInfo<'a> {
pub fn as_hash_key(&self) -> MaybeDynamicTargetExportInfoHashKey {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
MaybeDynamicTargetExportInfoHashKey::ExportInfo(*export_info)
MaybeDynamicTargetExportInfoHashKey::ExportInfo(export_info.id())
}
MaybeDynamicTargetExportInfo::Dynamic {
export_name,
other_export_info,
..
} => MaybeDynamicTargetExportInfoHashKey::TemporaryData {
export_name: export_name.clone(),
other_export_info: *other_export_info,
other_export_info: other_export_info.id(),
},
}
}

pub fn provided<'a>(&'a self, mg: &'a ModuleGraph) -> Option<&'a ExportProvided> {
pub fn provided(&'a self) -> Option<&'a ExportProvided> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
ExportInfoGetter::provided(export_info.as_data(mg))
}
MaybeDynamicTargetExportInfo::Static(export_info) => ExportInfoGetter::provided(export_info),
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data.provided.as_ref(),
}
}
Expand All @@ -432,8 +438,7 @@ impl MaybeDynamicTargetExportInfo {
) -> FindTargetRetEnum {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
let data = export_info.as_data(mg);
data.find_target_impl(mg, valid_target_module_filter, visited)
export_info.find_target_impl(mg, valid_target_module_filter, visited)
}
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => {
data.find_target_impl(mg, valid_target_module_filter, visited)
Expand Down Expand Up @@ -461,8 +466,7 @@ impl MaybeDynamicTargetExportInfo {
) -> Option<ResolvedExportInfoTargetWithCircular> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
let export_info_data = export_info.as_data(mg);
export_info_data.get_target_proxy(mg, resolve_filter, already_visited)
export_info.get_target_proxy(mg, resolve_filter, already_visited)
}
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => {
if !data.target_is_set || data.target.is_empty() {
Expand All @@ -478,14 +482,10 @@ impl MaybeDynamicTargetExportInfo {
}
}

fn get_max_target<'a>(
&'a self,
mg: &'a ModuleGraph,
) -> Cow<'a, HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
fn get_max_target(&self) -> Cow<HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
let data = export_info.as_data(mg);
ExportInfoGetter::get_max_target(data)
ExportInfoGetter::get_max_target(export_info)
}
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => ExportInfoGetter::get_max_target(data),
}
Expand All @@ -497,11 +497,11 @@ impl MaybeDynamicTargetExportInfo {
resolve_filter: ResolveFilterFnTy,
) -> Option<ResolvedExportInfoTarget> {
let data = match self {
MaybeDynamicTargetExportInfo::Static(export_info) => export_info.as_data(mg),
MaybeDynamicTargetExportInfo::Static(export_info) => *export_info,
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data,
};
let target = data.get_target_with_filter(mg, resolve_filter)?;
let max_target = self.get_max_target(mg);
let max_target = self.get_max_target();
let original_target = max_target
.values()
.next()
Expand Down Expand Up @@ -567,8 +567,11 @@ fn resolve_target(
return Some(ResolvedExportInfoTargetWithCircular::Target(target));
};

let exports_info = mg.get_exports_info(&target.module);
let export_info = exports_info.get_export_info_without_mut_module_graph(mg, name);
let exports_info = mg.get_prefetched_exports_info(
&target.module,
PrefetchExportsInfoMode::NamedExports(HashSet::from_iter([name])),
);
let export_info = exports_info.get_export_info_without_mut_module_graph(name);
let export_info_hash_key = export_info.as_hash_key();
if already_visited.contains(&export_info_hash_key) {
return Some(ResolvedExportInfoTargetWithCircular::Circular);
Expand Down
Loading
Loading