Skip to content

Commit 1dad3a6

Browse files
authored
Turbopack: make ModulePart a TaskInput (#75364)
No significant perf impact
1 parent 7fee0f7 commit 1dad3a6

File tree

16 files changed

+186
-228
lines changed

16 files changed

+186
-228
lines changed

crates/next-api/src/server_actions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ async fn parse_actions(module: Vc<Box<dyn Module>>) -> Result<Vc<OptionActionMap
246246
if let Some(module) = Vc::try_resolve_downcast_type::<EcmascriptModulePartAsset>(module).await?
247247
{
248248
if matches!(
249-
&*module.await?.part.await?,
249+
module.await?.part,
250250
ModulePart::Evaluation | ModulePart::Facade
251251
) {
252252
return Ok(Vc::cell(None));

crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_transition.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Transition for NextEcmascriptClientReferenceTransition {
5050
let part = match &*reference_type {
5151
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::ImportPart(
5252
part,
53-
)) => Some(*part),
53+
)) => Some(part),
5454
_ => None,
5555
};
5656

@@ -59,7 +59,7 @@ impl Transition for NextEcmascriptClientReferenceTransition {
5959
let this = self.await?;
6060

6161
let ident = match part {
62-
Some(part) => source.ident().with_part(*part),
62+
Some(part) => source.ident().with_part(part.clone()),
6363
None => source.ident(),
6464
};
6565
let ident_ref = ident.await?;

crates/next-core/src/next_image/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl CustomModuleType for StructuredImageModuleType {
7474
&self,
7575
source: Vc<Box<dyn Source>>,
7676
module_asset_context: Vc<ModuleAssetContext>,
77-
_part: Option<Vc<ModulePart>>,
77+
_part: Option<ModulePart>,
7878
) -> Vc<Box<dyn Module>> {
7979
StructuredImageModuleType::create_module(
8080
source,

turbopack/crates/turbopack-core/src/ident.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct AssetIdent {
2424
/// The modifiers of this asset (e.g. `client chunks`)
2525
pub modifiers: Vec<ResolvedVc<RcStr>>,
2626
/// The parts of the asset that are (ECMAScript) modules
27-
pub parts: Vec<ResolvedVc<ModulePart>>,
27+
pub parts: Vec<ModulePart>,
2828
/// The asset layer the asset was created from.
2929
pub layer: Option<ResolvedVc<RcStr>>,
3030
}
@@ -100,10 +100,9 @@ impl ValueToString for AssetIdent {
100100

101101
if !self.parts.is_empty() {
102102
for part in self.parts.iter() {
103-
let part = part.to_string().await?;
104-
// facade is not included in ident as switching between facade and non-facade
105-
// shouldn't change the ident
106-
if part.as_str() != "facade" {
103+
if !matches!(part, ModulePart::Facade) {
104+
// facade is not included in ident as switching between facade and non-facade
105+
// shouldn't change the ident
107106
write!(s, " <{}>", part)?;
108107
}
109108
}
@@ -149,7 +148,7 @@ impl AssetIdent {
149148
}
150149

151150
#[turbo_tasks::function]
152-
pub fn with_part(&self, part: ResolvedVc<ModulePart>) -> Vc<Self> {
151+
pub fn with_part(&self, part: ModulePart) -> Vc<Self> {
153152
let mut this = self.clone();
154153
this.parts.push(part);
155154
Self::new(Value::new(this))
@@ -262,25 +261,25 @@ impl AssetIdent {
262261
}
263262
for part in parts.iter() {
264263
4_u8.deterministic_hash(&mut hasher);
265-
match &*part.await? {
264+
match part {
266265
ModulePart::Evaluation => {
267266
1_u8.deterministic_hash(&mut hasher);
268267
}
269268
ModulePart::Export(export) => {
270269
2_u8.deterministic_hash(&mut hasher);
271-
export.await?.deterministic_hash(&mut hasher);
270+
export.deterministic_hash(&mut hasher);
272271
}
273272
ModulePart::RenamedExport {
274273
original_export,
275274
export,
276275
} => {
277276
3_u8.deterministic_hash(&mut hasher);
278-
original_export.await?.deterministic_hash(&mut hasher);
279-
export.await?.deterministic_hash(&mut hasher);
277+
original_export.deterministic_hash(&mut hasher);
278+
export.deterministic_hash(&mut hasher);
280279
}
281280
ModulePart::RenamedNamespace { export } => {
282281
4_u8.deterministic_hash(&mut hasher);
283-
export.await?.deterministic_hash(&mut hasher);
282+
export.deterministic_hash(&mut hasher);
284283
}
285284
ModulePart::Internal(id) => {
286285
5_u8.deterministic_hash(&mut hasher);

turbopack/crates/turbopack-core/src/reference_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub enum ImportWithType {
4343
#[turbo_tasks::value(serialization = "auto_for_input")]
4444
#[derive(Debug, Default, Clone, Hash)]
4545
pub enum EcmaScriptModulesReferenceSubType {
46-
ImportPart(ResolvedVc<ModulePart>),
46+
ImportPart(ModulePart),
4747
Import,
4848
ImportWithType(ImportWithType),
4949
DynamicImport,

turbopack/crates/turbopack-core/src/resolve/mod.rs

+47-57
Original file line numberDiff line numberDiff line change
@@ -3052,20 +3052,22 @@ async fn error_severity(resolve_options: Vc<ResolveOptions>) -> Result<ResolvedV
30523052
/// ModulePart represents a part of a module.
30533053
///
30543054
/// Currently this is used only for ESMs.
3055-
#[turbo_tasks::value]
3055+
#[derive(
3056+
Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, TraceRawVcs, TaskInput, NonLocalValue,
3057+
)]
30563058
pub enum ModulePart {
30573059
/// Represents the side effects of a module. This part is evaluated even if
30583060
/// all exports are unused.
30593061
Evaluation,
30603062
/// Represents an export of a module.
3061-
Export(ResolvedVc<RcStr>),
3063+
Export(RcStr),
30623064
/// Represents a renamed export of a module.
30633065
RenamedExport {
3064-
original_export: ResolvedVc<RcStr>,
3065-
export: ResolvedVc<RcStr>,
3066+
original_export: RcStr,
3067+
export: RcStr,
30663068
},
30673069
/// Represents a namespace object of a module exported as named export.
3068-
RenamedNamespace { export: ResolvedVc<RcStr> },
3070+
RenamedNamespace { export: RcStr },
30693071
/// A pointer to a specific part.
30703072
Internal(u32),
30713073
/// A pointer to a specific part, but with evaluation.
@@ -3079,76 +3081,64 @@ pub enum ModulePart {
30793081
Facade,
30803082
}
30813083

3082-
#[turbo_tasks::value_impl]
30833084
impl ModulePart {
3084-
#[turbo_tasks::function]
3085-
pub fn evaluation() -> Vc<Self> {
3086-
ModulePart::Evaluation.cell()
3085+
pub fn evaluation() -> Self {
3086+
ModulePart::Evaluation
30873087
}
3088-
#[turbo_tasks::function]
3089-
pub fn export(export: RcStr) -> Vc<Self> {
3090-
ModulePart::Export(ResolvedVc::cell(export)).cell()
3088+
3089+
pub fn export(export: RcStr) -> Self {
3090+
ModulePart::Export(export)
30913091
}
3092-
#[turbo_tasks::function]
3093-
pub fn renamed_export(original_export: RcStr, export: RcStr) -> Vc<Self> {
3092+
3093+
pub fn renamed_export(original_export: RcStr, export: RcStr) -> Self {
30943094
ModulePart::RenamedExport {
3095-
original_export: ResolvedVc::cell(original_export),
3096-
export: ResolvedVc::cell(export),
3095+
original_export,
3096+
export,
30973097
}
3098-
.cell()
30993098
}
3100-
#[turbo_tasks::function]
3101-
pub fn renamed_namespace(export: RcStr) -> Vc<Self> {
3102-
ModulePart::RenamedNamespace {
3103-
export: ResolvedVc::cell(export),
3104-
}
3105-
.cell()
3099+
3100+
pub fn renamed_namespace(export: RcStr) -> Self {
3101+
ModulePart::RenamedNamespace { export }
31063102
}
3107-
#[turbo_tasks::function]
3108-
pub fn internal(id: u32) -> Vc<Self> {
3109-
ModulePart::Internal(id).cell()
3103+
3104+
pub fn internal(id: u32) -> Self {
3105+
ModulePart::Internal(id)
31103106
}
3111-
#[turbo_tasks::function]
3112-
pub fn internal_evaluation(id: u32) -> Vc<Self> {
3113-
ModulePart::InternalEvaluation(id).cell()
3107+
3108+
pub fn internal_evaluation(id: u32) -> Self {
3109+
ModulePart::InternalEvaluation(id)
31143110
}
3115-
#[turbo_tasks::function]
3116-
pub fn locals() -> Vc<Self> {
3117-
ModulePart::Locals.cell()
3111+
3112+
pub fn locals() -> Self {
3113+
ModulePart::Locals
31183114
}
3119-
#[turbo_tasks::function]
3120-
pub fn exports() -> Vc<Self> {
3121-
ModulePart::Exports.cell()
3115+
3116+
pub fn exports() -> Self {
3117+
ModulePart::Exports
31223118
}
3123-
#[turbo_tasks::function]
3124-
pub fn facade() -> Vc<Self> {
3125-
ModulePart::Facade.cell()
3119+
3120+
pub fn facade() -> Self {
3121+
ModulePart::Facade
31263122
}
31273123
}
31283124

3129-
#[turbo_tasks::value_impl]
3130-
impl ValueToString for ModulePart {
3131-
#[turbo_tasks::function]
3132-
async fn to_string(&self) -> Result<Vc<RcStr>> {
3133-
Ok(Vc::cell(match self {
3134-
ModulePart::Evaluation => "module evaluation".into(),
3135-
ModulePart::Export(export) => format!("export {}", export.await?).into(),
3125+
impl Display for ModulePart {
3126+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
3127+
match self {
3128+
ModulePart::Evaluation => f.write_str("module evaluation"),
3129+
ModulePart::Export(export) => write!(f, "export {}", export),
31363130
ModulePart::RenamedExport {
31373131
original_export,
31383132
export,
3139-
} => {
3140-
let original_export = original_export.await?;
3141-
let export = export.await?;
3142-
format!("export {} as {}", original_export, export).into()
3143-
}
3133+
} => write!(f, "export {} as {}", original_export, export),
31443134
ModulePart::RenamedNamespace { export } => {
3145-
format!("export * as {}", export.await?).into()
3135+
write!(f, "export * as {}", export)
31463136
}
3147-
ModulePart::Internal(id) => format!("internal part {}", id).into(),
3148-
ModulePart::InternalEvaluation(id) => format!("internal part {}", id).into(),
3149-
ModulePart::Locals => "locals".into(),
3150-
ModulePart::Exports => "exports".into(),
3151-
ModulePart::Facade => "facade".into(),
3152-
}))
3137+
ModulePart::Internal(id) => write!(f, "internal part {}", id),
3138+
ModulePart::InternalEvaluation(id) => write!(f, "internal part {}", id),
3139+
ModulePart::Locals => f.write_str("locals"),
3140+
ModulePart::Exports => f.write_str("exports"),
3141+
ModulePart::Facade => f.write_str("facade"),
3142+
}
31533143
}
31543144
}

turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub struct EsmAssetReference {
117117
pub request: ResolvedVc<Request>,
118118
pub annotations: ImportAnnotations,
119119
pub issue_source: IssueSource,
120-
pub export_name: Option<ResolvedVc<ModulePart>>,
120+
pub export_name: Option<ModulePart>,
121121
pub import_externals: bool,
122122
}
123123

@@ -137,7 +137,7 @@ impl EsmAssetReference {
137137
request: ResolvedVc<Request>,
138138
issue_source: IssueSource,
139139
annotations: Value<ImportAnnotations>,
140-
export_name: Option<ResolvedVc<ModulePart>>,
140+
export_name: Option<ModulePart>,
141141
import_externals: bool,
142142
) -> Vc<Self> {
143143
Self::cell(EsmAssetReference {
@@ -166,20 +166,20 @@ impl ModuleReference for EsmAssetReference {
166166
let ty = if matches!(self.annotations.module_type(), Some("json")) {
167167
EcmaScriptModulesReferenceSubType::ImportWithType(ImportWithType::Json)
168168
} else if let Some(part) = &self.export_name {
169-
EcmaScriptModulesReferenceSubType::ImportPart(*part)
169+
EcmaScriptModulesReferenceSubType::ImportPart(part.clone())
170170
} else {
171171
EcmaScriptModulesReferenceSubType::Import
172172
};
173173

174174
if let Request::Module { module, .. } = &*self.request.await? {
175175
if module == TURBOPACK_PART_IMPORT_SOURCE {
176-
if let Some(part) = self.export_name {
176+
if let Some(part) = &self.export_name {
177177
let module: ResolvedVc<crate::EcmascriptModuleAsset> =
178178
ResolvedVc::try_downcast_type(self.origin)
179179
.expect("EsmAssetReference origin should be a EcmascriptModuleAsset");
180180

181181
return Ok(ModuleResolveResult::module(
182-
EcmascriptModulePartAsset::select_part(*module, *part)
182+
EcmascriptModulePartAsset::select_part(*module, part.clone())
183183
.to_resolved()
184184
.await?,
185185
)
@@ -199,21 +199,17 @@ impl ModuleReference for EsmAssetReference {
199199
)
200200
.await?;
201201

202-
if let Some(part) = self.export_name {
203-
let part = part.await?;
204-
if let &ModulePart::Export(export_name) = &*part {
205-
for &module in result.primary_modules().await? {
206-
if let Some(module) = ResolvedVc::try_downcast(module) {
207-
let export = export_name.await?;
208-
if *is_export_missing(*module, export.clone_value()).await? {
209-
InvalidExport {
210-
export: export_name,
211-
module,
212-
source: self.issue_source.clone(),
213-
}
214-
.resolved_cell()
215-
.emit();
202+
if let Some(ModulePart::Export(export_name)) = &self.export_name {
203+
for &module in result.primary_modules().await? {
204+
if let Some(module) = ResolvedVc::try_downcast(module) {
205+
if *is_export_missing(*module, export_name.clone()).await? {
206+
InvalidExport {
207+
export: export_name.clone(),
208+
module,
209+
source: self.issue_source.clone(),
216210
}
211+
.resolved_cell()
212+
.emit();
217213
}
218214
}
219215
}
@@ -413,7 +409,7 @@ fn var_decl_with_span(mut decl: Stmt, span: Span) -> Stmt {
413409

414410
#[turbo_tasks::value(shared)]
415411
pub struct InvalidExport {
416-
export: ResolvedVc<RcStr>,
412+
export: RcStr,
417413
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
418414
source: IssueSource,
419415
}
@@ -429,7 +425,7 @@ impl Issue for InvalidExport {
429425
async fn title(&self) -> Result<Vc<StyledString>> {
430426
Ok(StyledString::Line(vec![
431427
StyledString::Text("Export ".into()),
432-
StyledString::Code(self.export.await?.clone_value()),
428+
StyledString::Code(self.export.clone()),
433429
StyledString::Text(" doesn't exist in target module".into()),
434430
])
435431
.cell())
@@ -447,18 +443,17 @@ impl Issue for InvalidExport {
447443

448444
#[turbo_tasks::function]
449445
async fn description(&self) -> Result<Vc<OptionStyledString>> {
450-
let export = self.export.await?;
451446
let export_names = all_known_export_names(*self.module).await?;
452447
let did_you_mean = export_names
453448
.iter()
454-
.map(|s| (s, jaro(export.as_str(), s.as_str())))
449+
.map(|s| (s, jaro(self.export.as_str(), s.as_str())))
455450
.max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
456451
.map(|(s, _)| s);
457452
Ok(Vc::cell(Some(
458453
StyledString::Stack(vec![
459454
StyledString::Line(vec![
460455
StyledString::Text("The export ".into()),
461-
StyledString::Code(export.clone_value()),
456+
StyledString::Code(self.export.clone()),
462457
StyledString::Text(" was not found in module ".into()),
463458
StyledString::Strong(self.module.ident().to_string().await?.clone_value()),
464459
StyledString::Text(".".into()),

0 commit comments

Comments
 (0)