Skip to content

Commit

Permalink
Properly use star reexports
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Sep 24, 2024
1 parent 5a4f142 commit 2f167e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ impl Module for EcmascriptModuleFacadeModule {
references
}
ModulePart::StarReexports { .. } => {
vec![]
let Some(module) =
Vc::try_resolve_sidecast::<Box<dyn EcmascriptAnalyzable>>(self.module).await?
else {
bail!(
"Expected EcmascriptModuleAsset for a EcmascriptModuleFacadeModule with \
ModulePart::Evaluation"
);
};
let result = module.analyze().await?;
result.reexport_references.await?.clone_value()
}
ModulePart::Facade => {
vec![
Expand Down Expand Up @@ -201,7 +210,16 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule {
}
star_exports.extend(esm_exports.star_exports.iter().copied());
}
ModulePart::StarReexports => {}
ModulePart::StarReexports => {
let EcmascriptExports::EsmExports(esm_exports) = *self.module.get_exports().await?
else {
bail!(
"EcmascriptModuleFacadeModule must only be used on modules with EsmExports"
);
};
let esm_exports = esm_exports.await?;
star_exports.extend(esm_exports.star_exports.iter().copied());
}
ModulePart::Facade => {
// Reexport everything from the reexports module
// (including default export if any)
Expand Down
64 changes: 5 additions & 59 deletions turbopack/crates/turbopack-ecmascript/src/tree_shake/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use turbopack_core::{

use super::{
chunk_item::EcmascriptModulePartChunkItem, get_part_id, part_of_module, split, split_module,
Key, PartId, SplitResult,
PartId, SplitResult,
};
use crate::{
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
Expand Down Expand Up @@ -155,73 +155,19 @@ impl Module for EcmascriptModulePartAsset {

let analyze = analyze(self.full_module, self.part).await?;

let (deps, entrypoints) = match &*split_data {
SplitResult::Ok {
deps, entrypoints, ..
} => (deps, entrypoints),
let deps = match &*split_data {
SplitResult::Ok { deps, .. } => deps,
SplitResult::Failed { .. } => return Ok(analyze.references),
};

// Facade depends on evaluation and re-exports
if matches!(&*self.part.await?, ModulePart::Facade) {
let mut references = vec![];

let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::evaluation(),
)),
Vc::cell("ecmascript module evaluation".into()),
));

references.push(reference);

let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::exports(),
)),
Vc::cell("ecmascript reexports".into()),
));

references.push(reference);

references.extend(analyze.references.await?.iter().cloned());

return Ok(Vc::cell(references));
return Ok(analyze.references);
}

// ModulePart::Exports contains all reexports and a reexport of the Locals
if matches!(&*self.part.await?, ModulePart::Exports) {
let mut references = vec![];

for key in entrypoints.keys() {
if let Key::Export(e) = key {
let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::export(e.clone()),
)),
Vc::cell(format!("ecmascript export '{e}'").into()),
));

references.push(reference);
}
}

let reference = Vc::upcast(SingleModuleReference::new(
Vc::upcast(EcmascriptModulePartAsset::new(
self.full_module,
ModulePart::star_reexports(),
)),
Vc::cell("reexport".into()),
));

references.push(reference);

references.extend(analyze.references.await?.iter().cloned());

return Ok(Vc::cell(references));
return Ok(analyze.references);
}

if matches!(&*self.part.await?, ModulePart::StarReexports) {
Expand Down

0 comments on commit 2f167e8

Please sign in to comment.