Skip to content

Commit

Permalink
Prepare select_part
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 14, 2024
1 parent 157f51e commit d327fef
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 20 deletions.
46 changes: 37 additions & 9 deletions turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ use swc_core::{
},
quote, quote_expr,
};
use turbo_tasks::{trace::TraceRawVcs, RcStr, TryFlatJoinIterExt, ValueToString, Vc};
use turbo_tasks::{trace::TraceRawVcs, vdbg, RcStr, TryFlatJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{
chunk::ChunkingContext,
ident::AssetIdent,
issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, StyledString},
module::Module,
reference::ModuleReference,
resolve::ModulePart,
};

use super::base::ReferencedAsset;
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
code_gen::{CodeGenerateable, CodeGeneration, CodeGenerationHoistedStmt},
magic_identifier,
tree_shake::asset::EcmascriptModulePartAsset,
};

#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)]
Expand Down Expand Up @@ -126,10 +128,12 @@ pub async fn follow_reexports(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
export_name: RcStr,
side_effect_free_packages: Vc<Glob>,
ignore_side_effect_of_entry: Vc<bool>,
) -> Result<Vc<FollowExportsResult>> {
if !*module
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?
if !*ignore_side_effect_of_entry.await?
&& !*module
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?
{
return Ok(FollowExportsResult::cell(FollowExportsResult {
module,
Expand All @@ -140,6 +144,7 @@ pub async fn follow_reexports(
let mut module = module;
let mut export_name = export_name;
loop {
vdbg!(&*module.ident().to_string().await?);
let exports = module.get_exports().await?;
let EcmascriptExports::EsmExports(exports) = &*exports else {
return Ok(FollowExportsResult::cell(FollowExportsResult {
Expand All @@ -152,13 +157,20 @@ pub async fn follow_reexports(
// Try to find the export in the local exports
let exports_ref = exports.await?;
if let Some(export) = exports_ref.exports.get(&export_name) {
match handle_declared_export(module, export_name, export, side_effect_free_packages)
.await?
match handle_declared_export(
module,
export_name.clone(),
export,
side_effect_free_packages,
)
.await?
{
ControlFlow::Continue((m, n)) => {
module = m;
export_name = n;
continue;
if !is_module_part_with_no_real_export(m).await? {
module = m;
export_name = n;
continue;
}
}
ControlFlow::Break(result) => {
return Ok(result.cell());
Expand All @@ -171,6 +183,7 @@ pub async fn follow_reexports(
let result = get_all_export_names(module).await?;
if let Some(m) = result.esm_exports.get(&export_name) {
module = *m;
vdbg!("all_export_names", &*module.ident().to_string().await?);
continue;
}
return match &result.dynamic_exporting_modules[..] {
Expand Down Expand Up @@ -203,6 +216,21 @@ pub async fn follow_reexports(
}
}

async fn is_module_part_with_no_real_export(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
) -> Result<bool> {
if let Some(module) = Vc::try_resolve_downcast_type::<EcmascriptModulePartAsset>(module).await?
{
if matches!(
*module.await?.part.await?,
ModulePart::Internal(..) | ModulePart::Evaluation
) {
return Ok(true);
}
}
Ok(false)
}

async fn handle_declared_export(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
export_name: RcStr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use self::{
base::EsmAssetReference,
binding::EsmBinding,
dynamic::EsmAsyncAssetReference,
export::{EsmExport, EsmExports},
export::{EsmExport, EsmExports, FoundExportType},
meta::{ImportMetaBinding, ImportMetaRef},
module_item::EsmModuleItem,
url::{UrlAssetReference, UrlRewriteBehavior},
Expand Down
143 changes: 134 additions & 9 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use anyhow::{Context, Result};
use turbo_tasks::Vc;
use turbo_tasks::{vdbg, RcStr, ValueToString, Vc};
use turbo_tasks_fs::glob::Glob;
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, EvaluatableAsset},
context::AssetContext,
ident::AssetIdent,
module::Module,
reference::{ModuleReference, ModuleReferences, SingleModuleReference},
resolve::ModulePart,
resolve::{origin::ResolveOrigin, ModulePart},
};

use super::{
Expand All @@ -16,7 +18,10 @@ use super::{
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
parse::ParseResult,
references::analyse_ecmascript_module,
references::{
analyse_ecmascript_module, esm::FoundExportType, follow_reexports, FollowExportsResult,
},
tree_shake::Key,
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptParsable,
};
Expand Down Expand Up @@ -92,13 +97,66 @@ impl EcmascriptModulePartAsset {
module: Vc<EcmascriptModuleAsset>,
part: Vc<ModulePart>,
) -> Result<Vc<Box<dyn Module>>> {
let split_result = split_module(module).await?;
let SplitResult::Ok { entrypoints, .. } = &*split_module(module).await? else {
return Ok(Vc::upcast(module));
};

Ok(if matches!(&*split_result, SplitResult::Failed { .. }) {
Vc::upcast(module)
} else {
Vc::upcast(EcmascriptModulePartAsset::new(module, part))
})
// We follow reexports here
if let ModulePart::Export(export) = &*part.await? {
let export_name = export.await?.clone_value();

// If a local binding or reexport with the same name exists, we stop here.
// Side effects of the barrel file are preserved.
if entrypoints.contains_key(&Key::Export(export_name.clone())) {
return Ok(Vc::upcast(EcmascriptModulePartAsset::new(module, part)));
}

let side_effect_free_packages = module.asset_context().side_effect_free_packages();

// Exclude local bindings by using exports module part.
let source_module = if entrypoints.contains_key(&Key::Exports)
&& *module
.is_marked_as_side_effect_free(side_effect_free_packages)
.await?
{
Vc::upcast(EcmascriptModulePartAsset::new(
module,
ModulePart::exports(),
))
} else {
Vc::upcast(module)
};

let FollowExportsWithSideEffectsResult {
side_effects,

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / build-native / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / build / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test unit (18) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test unit (20) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / types and precompiled / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr integration / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused variable: `side_effects`

Check failure on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=true, --scenario=heavy-npm-deps-dev --page=homepage) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test next-swc wasm / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (1/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=false, --scenario=heavy-npm-deps-dev --page=homepage) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (1/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (2/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (3/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (dev) (4/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (4/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (1/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (2/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test cargo benches / Test

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test cargo benches / Test

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (3/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=false, --scenario=heavy-npm-deps-build --page=homepage) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test firefox and safari / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests for flakes (prod) (4/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (1/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (3/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Run devlow benchmarks (--turbopack=true, --scenario=heavy-npm-deps-build --page=homepage) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (5/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (2/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (11/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / lint / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (7/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (5/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (10/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (1/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (12/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (4/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (2/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (3/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (9/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack development integration (2/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (3/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (2/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (6/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (4/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (4/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (5/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (3/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (2/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (1/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (4/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (3/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test dev (3/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (1/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test dev (2/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (5/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr dev (4/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test dev (1/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test prod (4/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test dev (4/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production integration (2/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack production (5/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (4/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (8/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test prod (1/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (2/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test prod (3/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (1/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test integration (1/12) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test prod (5/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test prod (2/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test ppr prod (3/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / test turbopack dev (3/5) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (2/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (1/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (4/4) / build

unused variable: `side_effects`

Check warning on line 131 in turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs

View workflow job for this annotation

GitHub Actions / Test new tests when deployed (3/4) / build

unused variable: `side_effects`
result,
} = &*follow_reexports_with_side_effects(
source_module,
export_name.clone(),
side_effect_free_packages,
)
.await?;

let FollowExportsResult {
module: final_module,
export_name: new_export,
ty,
} = &*result.await?;

vdbg!(
*ty,
final_module.ident().to_string().await?,
new_export.clone()
);

if let Some(new_export) = new_export {
if *new_export == export_name {
return Ok(Vc::upcast(*final_module));
}
}
}

Ok(Vc::upcast(EcmascriptModulePartAsset::new(module, part)))
}

#[turbo_tasks::function]
Expand All @@ -114,6 +172,58 @@ impl EcmascriptModulePartAsset {
}
}

#[turbo_tasks::value]
struct FollowExportsWithSideEffectsResult {
side_effects: Vc<SideEffects>,
result: Vc<FollowExportsResult>,
}

#[turbo_tasks::value(transparent)]
struct SideEffects(Vec<Vc<Box<dyn EcmascriptChunkPlaceable>>>);

#[turbo_tasks::function]
async fn follow_reexports_with_side_effects(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
export_name: RcStr,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<FollowExportsWithSideEffectsResult>> {
let mut side_effects = vec![];

let mut current_module = module;
let mut current_export_name = export_name;
let result = loop {
// We ignore the side effect of the entry module here, because we need to proceed.
let result = follow_reexports(
current_module,
current_export_name.clone(),
side_effect_free_packages,
Vc::cell(true),
);
dbg!(&*current_module.ident().to_string().await?);

let FollowExportsResult {
module,
export_name,
ty,
} = &*result.await?;

match ty {
FoundExportType::SideEffects => {
side_effects.push(*module);
current_module = *module;
current_export_name = export_name.clone().unwrap_or(current_export_name);
}
_ => break result,
}
};

Ok(FollowExportsWithSideEffectsResult {
side_effects: Vc::cell(side_effects),
result,
}
.cell())
}

#[turbo_tasks::value_impl]
impl Module for EcmascriptModulePartAsset {
#[turbo_tasks::function]
Expand Down Expand Up @@ -200,6 +310,21 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset {
async fn get_exports(self: Vc<Self>) -> Result<Vc<EcmascriptExports>> {
Ok(self.analyze().await?.exports)
}

#[turbo_tasks::function]
async fn is_marked_as_side_effect_free(
self: Vc<Self>,
side_effect_free_packages: Vc<Glob>,
) -> Result<Vc<bool>> {
let this = self.await?;

match *this.part.await? {
ModulePart::Exports | ModulePart::Export(..) => Ok(Vc::cell(true)),
_ => Ok(this
.full_module
.is_marked_as_side_effect_free(side_effect_free_packages)),
}
}
}

#[turbo_tasks::value_impl]
Expand Down
8 changes: 7 additions & 1 deletion turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,13 @@ async fn apply_reexport_tree_shaking(
module: final_module,
export_name: new_export,
..
} = &*follow_reexports(module, export.clone_value(), side_effect_free_packages).await?;
} = &*follow_reexports(
module,
export.clone_value(),
side_effect_free_packages,
Vc::cell(false),
)
.await?;
let module = if let Some(new_export) = new_export {
if *new_export == *export {
Vc::upcast(*final_module)
Expand Down

0 comments on commit d327fef

Please sign in to comment.