-
Notifications
You must be signed in to change notification settings - Fork 28.7k
Turbopack: correct export usage in EcmascriptModulePartReference #80526
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
Changes from all commits
5ced023
3c15feb
e6229c5
6e65ddd
3dece61
2a999ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
use anyhow::{Context, Result, bail}; | ||
use serde::{Deserialize, Serialize}; | ||
use swc_core::{common::DUMMY_SP, ecma::ast::Ident, quote}; | ||
use turbo_rcstr::{RcStr, rcstr}; | ||
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; | ||
use turbo_rcstr::RcStr; | ||
use turbo_tasks::{NonLocalValue, ResolvedVc, ValueToString, Vc, trace::TraceRawVcs}; | ||
use turbopack_core::{ | ||
chunk::{ | ||
ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption, | ||
|
@@ -21,17 +22,24 @@ use crate::{ | |
utils::module_id_to_lit, | ||
}; | ||
|
||
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, NonLocalValue, TraceRawVcs)] | ||
enum EcmascriptModulePartReferenceMode { | ||
Synthesize { remove_unused_exports: bool }, | ||
Normal, | ||
} | ||
|
||
/// A reference to the [EcmascriptModuleLocalsModule] variant of an original | ||
/// module. | ||
#[turbo_tasks::value] | ||
pub struct EcmascriptModulePartReference { | ||
pub module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, | ||
pub part: Option<ModulePart>, | ||
pub remove_unused_exports: bool, | ||
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, | ||
part: ModulePart, | ||
mode: EcmascriptModulePartReferenceMode, | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl EcmascriptModulePartReference { | ||
// Create new [EcmascriptModuleFacadeModule]s as necessary | ||
#[turbo_tasks::function] | ||
pub fn new_part( | ||
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, | ||
|
@@ -40,21 +48,24 @@ impl EcmascriptModulePartReference { | |
) -> Vc<Self> { | ||
EcmascriptModulePartReference { | ||
module, | ||
part: Some(part), | ||
remove_unused_exports, | ||
part, | ||
mode: EcmascriptModulePartReferenceMode::Synthesize { | ||
remove_unused_exports, | ||
}, | ||
} | ||
.cell() | ||
} | ||
|
||
// A reference to the given module, without any intermediary synthesized modules. | ||
#[turbo_tasks::function] | ||
pub fn new( | ||
pub fn new_normal( | ||
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, | ||
remove_unused_exports: bool, | ||
part: ModulePart, | ||
) -> Vc<Self> { | ||
EcmascriptModulePartReference { | ||
module, | ||
part: None, | ||
remove_unused_exports, | ||
part, | ||
mode: EcmascriptModulePartReferenceMode::Normal, | ||
} | ||
.cell() | ||
} | ||
|
@@ -63,51 +74,51 @@ impl EcmascriptModulePartReference { | |
#[turbo_tasks::value_impl] | ||
impl ValueToString for EcmascriptModulePartReference { | ||
#[turbo_tasks::function] | ||
async fn to_string(&self) -> Result<Vc<RcStr>> { | ||
Ok(match &self.part { | ||
Some(part) => Vc::cell(part.to_string().into()), | ||
None => Vc::cell(rcstr!("module")), | ||
}) | ||
async fn to_string(&self) -> Vc<RcStr> { | ||
Vc::cell(self.part.to_string().into()) | ||
} | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl ModuleReference for EcmascriptModulePartReference { | ||
#[turbo_tasks::function] | ||
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> { | ||
let module = if let Some(part) = &self.part { | ||
match part { | ||
ModulePart::Locals => { | ||
let Some(module) = ResolvedVc::try_downcast_type(self.module) else { | ||
let module = match self.mode { | ||
EcmascriptModulePartReferenceMode::Synthesize { | ||
remove_unused_exports, | ||
} => { | ||
match &self.part { | ||
ModulePart::Locals => { | ||
let Some(module) = ResolvedVc::try_downcast_type(self.module) else { | ||
bail!( | ||
"Expected EcmascriptModuleAsset for a \ | ||
EcmascriptModulePartReference with ModulePart::Locals" | ||
); | ||
}; | ||
Vc::upcast::<Box<dyn Module>>(EcmascriptModuleLocalsModule::new(*module)) | ||
} | ||
ModulePart::Exports | ||
| ModulePart::Evaluation | ||
| ModulePart::Facade | ||
| ModulePart::RenamedExport { .. } | ||
| ModulePart::RenamedNamespace { .. } => { | ||
Vc::upcast(EcmascriptModuleFacadeModule::new( | ||
*self.module, | ||
self.part.clone(), | ||
remove_unused_exports, | ||
)) | ||
} | ||
ModulePart::Export(..) | ModulePart::Internal(..) => { | ||
bail!( | ||
"Expected EcmascriptModuleAsset for a EcmascriptModulePartReference \ | ||
with ModulePart::Locals" | ||
"Unexpected ModulePart \"{}\" for EcmascriptModulePartReference", | ||
self.part | ||
); | ||
}; | ||
Vc::upcast::<Box<dyn Module>>(EcmascriptModuleLocalsModule::new(*module)) | ||
} | ||
ModulePart::Exports | ||
| ModulePart::Evaluation | ||
| ModulePart::Facade | ||
| ModulePart::RenamedExport { .. } | ||
| ModulePart::RenamedNamespace { .. } => { | ||
Vc::upcast(EcmascriptModuleFacadeModule::new( | ||
*self.module, | ||
part.clone(), | ||
self.remove_unused_exports, | ||
)) | ||
} | ||
ModulePart::Export(..) | ModulePart::Internal(..) => { | ||
bail!( | ||
"Unexpected ModulePart \"{}\" for EcmascriptModulePartReference", | ||
part | ||
); | ||
} | ||
} | ||
.to_resolved() | ||
.await? | ||
} | ||
.to_resolved() | ||
.await? | ||
} else { | ||
ResolvedVc::upcast(self.module) | ||
EcmascriptModulePartReferenceMode::Normal => ResolvedVc::upcast(self.module), | ||
}; | ||
|
||
Ok(*ModuleResolveResult::module(module)) | ||
|
@@ -127,8 +138,11 @@ impl ChunkableModuleReference for EcmascriptModulePartReference { | |
#[turbo_tasks::function] | ||
fn export_usage(&self) -> Vc<ExportUsage> { | ||
match &self.part { | ||
Some(ModulePart::Export(export)) => ExportUsage::named(export.clone()), | ||
Some(ModulePart::Evaluation) => ExportUsage::evaluation(), | ||
ModulePart::Export(export) => ExportUsage::named(export.clone()), | ||
ModulePart::RenamedExport { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there something similar we should do for
couldn't that be modeled as a named export too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So no, the export usage of the reference to `./module` would still be "all". |
||
original_export, .. | ||
} => ExportUsage::named(original_export.clone()), | ||
ModulePart::Evaluation => ExportUsage::evaluation(), | ||
_ => ExportUsage::all(), | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { a0 } from 'lib' | ||
|
||
console.log(a0) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"treeShakingMode": "reexports-only", | ||
"removeUnusedExports": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([ | ||
"output/2b719_intermediate-tree-shake_rename-side-effect-free-facade_input_index_e208a8b3.js", | ||
{}, | ||
{"otherChunks":["output/457d9_snapshot_intermediate-tree-shake_rename-side-effect-free-facade_input_71bae14d._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/intermediate-tree-shake/rename-side-effect-free-facade/input/index.js [test] (ecmascript)"]} | ||
]); | ||
// Dummy runtime |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.