From f7e019104c5b96f64cb2a5b992b24371d7c75f36 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:59:51 +0100 Subject: [PATCH] Turbopack: resolve some to-resolved-in-loop (#73794) --- .../transforms/swc_ecma_transform_plugins.rs | 89 ++++++++++--------- .../crates/turbopack-core/src/code_builder.rs | 1 + turbopack/crates/turbopack-core/src/rebase.rs | 21 ++--- .../crates/turbopack-core/src/resolve/mod.rs | 35 +++++--- .../crates/turbopack-css/src/chunk/mod.rs | 21 +++-- .../src/references/mod.rs | 1 + .../src/tree_shake/side_effect_module.rs | 26 +++--- .../src/typescript/mod.rs | 23 +++-- 8 files changed, 130 insertions(+), 87 deletions(-) diff --git a/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs b/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs index 53457dfac68ca..f877ba4245bff 100644 --- a/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs +++ b/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs @@ -36,7 +36,7 @@ pub async fn get_swc_ecma_transform_rule_impl( enable_mdx_rs: bool, ) -> Result> { use anyhow::{bail, Context}; - use turbo_tasks::Value; + use turbo_tasks::{TryJoinIterExt, Value}; use turbo_tasks_fs::FileContent; use turbopack::{resolve_options, resolve_options_context::ResolveOptionsContext}; use turbopack_core::{ @@ -50,56 +50,59 @@ pub async fn get_swc_ecma_transform_rule_impl( use crate::next_shared::transforms::get_ecma_transform_rule; - let mut plugins = vec![]; - for (name, config) in plugin_configs.iter() { - // [TODO]: SWC's current experimental config supports - // two forms of plugin path, - // one for implicit package name resolves to node_modules, - // and one for explicit path to a .wasm binary. - // Current resolve will fail with latter. - let request = Request::parse(Value::new(Pattern::Constant(name.as_str().into()))); - let resolve_options = resolve_options( - *project_path, - ResolveOptionsContext { - enable_node_modules: Some(project_path.root().to_resolved().await?), - enable_node_native_modules: true, - ..Default::default() - } - .cell(), - ); - - let plugin_wasm_module_resolve_result = handle_resolve_error( - resolve( + let plugins = plugin_configs + .iter() + .map(|(name, config)| async move { + // [TODO]: SWC's current experimental config supports + // two forms of plugin path, + // one for implicit package name resolves to node_modules, + // and one for explicit path to a .wasm binary. + // Current resolve will fail with latter. + let request = Request::parse(Value::new(Pattern::Constant(name.as_str().into()))); + let resolve_options = resolve_options( *project_path, + ResolveOptionsContext { + enable_node_modules: Some(project_path.root().to_resolved().await?), + enable_node_native_modules: true, + ..Default::default() + } + .cell(), + ); + + let plugin_wasm_module_resolve_result = handle_resolve_error( + resolve( + *project_path, + Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)), + request, + resolve_options, + ) + .as_raw_module_result(), Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)), + *project_path, request, resolve_options, + false, + None, ) - .as_raw_module_result(), - Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)), - *project_path, - request, - resolve_options, - false, - None, - ) - .await?; - let plugin_module = plugin_wasm_module_resolve_result - .first_module() - .await? - .context("Expected to find module")?; + .await?; + let plugin_module = plugin_wasm_module_resolve_result + .first_module() + .await? + .context("Expected to find module")?; - let content = &*plugin_module.content().file_content().await?; + let content = &*plugin_module.content().file_content().await?; - let FileContent::Content(file) = content else { - bail!("Expected file content for plugin module"); - }; + let FileContent::Content(file) = content else { + bail!("Expected file content for plugin module"); + }; - plugins.push(( - SwcPluginModule::new(name, file.content().to_bytes()?.to_vec()).resolved_cell(), - config.clone(), - )); - } + Ok(( + SwcPluginModule::new(name, file.content().to_bytes()?.to_vec()).resolved_cell(), + config.clone(), + )) + }) + .try_join() + .await?; Ok(Some(get_ecma_transform_rule( Box::new(SwcEcmaTransformPluginsTransformer::new(plugins)), diff --git a/turbopack/crates/turbopack-core/src/code_builder.rs b/turbopack/crates/turbopack-core/src/code_builder.rs index 99f4530fee408..167d81323a88f 100644 --- a/turbopack/crates/turbopack-core/src/code_builder.rs +++ b/turbopack/crates/turbopack-core/src/code_builder.rs @@ -164,6 +164,7 @@ impl GenerateSourceMap for Code { let mut sections = Vec::with_capacity(self.mappings.len()); let mut read = self.code.read(); + // ast-grep-ignore: to-resolved-in-loop for (byte_pos, map) in &self.mappings { let mut want = byte_pos - last_byte_pos; while want > 0 { diff --git a/turbopack/crates/turbopack-core/src/rebase.rs b/turbopack/crates/turbopack-core/src/rebase.rs index 95cc096bedf33..77b08ea5e82e2 100644 --- a/turbopack/crates/turbopack-core/src/rebase.rs +++ b/turbopack/crates/turbopack-core/src/rebase.rs @@ -1,7 +1,7 @@ use std::hash::Hash; use anyhow::Result; -use turbo_tasks::{ResolvedVc, Vc}; +use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks_fs::FileSystemPath; use crate::{ @@ -51,17 +51,18 @@ impl OutputAsset for RebasedAsset { #[turbo_tasks::function] async fn references(&self) -> Result> { - let mut references = Vec::new(); - for &module in referenced_modules_and_affecting_sources(*self.source) + let references = referenced_modules_and_affecting_sources(*self.source) .await? .iter() - { - references.push(ResolvedVc::upcast( - RebasedAsset::new(*module, *self.input_dir, *self.output_dir) - .to_resolved() - .await?, - )); - } + .map(|module| async move { + Ok(ResolvedVc::upcast( + RebasedAsset::new(**module, *self.input_dir, *self.output_dir) + .to_resolved() + .await?, + )) + }) + .try_join() + .await?; Ok(Vc::cell(references)) } } diff --git a/turbopack/crates/turbopack-core/src/resolve/mod.rs b/turbopack/crates/turbopack-core/src/resolve/mod.rs index 305db0248c4f9..f20169da6de51 100644 --- a/turbopack/crates/turbopack-core/src/resolve/mod.rs +++ b/turbopack/crates/turbopack-core/src/resolve/mod.rs @@ -1069,11 +1069,18 @@ async fn type_exists( refs: &mut Vec>>, ) -> Result>> { let result = fs_path.resolve().await?.realpath_with_links().await?; - for path in result.symlinks.iter() { - refs.push(ResolvedVc::upcast( - FileSource::new(**path).to_resolved().await?, - )); - } + refs.extend( + result + .symlinks + .iter() + .map(|path| async move { + Ok(ResolvedVc::upcast( + FileSource::new(**path).to_resolved().await?, + )) + }) + .try_join() + .await?, + ); let path = result.path; Ok(if *path.get_type().await? == ty { Some(path) @@ -1087,11 +1094,18 @@ async fn any_exists( refs: &mut Vec>>, ) -> Result)>> { let result = fs_path.resolve().await?.realpath_with_links().await?; - for path in result.symlinks.iter() { - refs.push(ResolvedVc::upcast( - FileSource::new(**path).to_resolved().await?, - )); - } + refs.extend( + result + .symlinks + .iter() + .map(|path| async move { + Ok(ResolvedVc::upcast( + FileSource::new(**path).to_resolved().await?, + )) + }) + .try_join() + .await?, + ); let path = result.path; let ty = *path.get_type().await?; Ok( @@ -2263,6 +2277,7 @@ async fn apply_in_package( fragment: Vc, ) -> Result>> { // Check alias field for module aliases first + // ast-grep-ignore: to-resolved-in-loop for in_package in options_value.in_package.iter() { // resolve_module_request is called when importing a node // module, not a PackageInternal one, so the imports field diff --git a/turbopack/crates/turbopack-css/src/chunk/mod.rs b/turbopack/crates/turbopack-css/src/chunk/mod.rs index 3284750f7bd13..eb80b1ef89f77 100644 --- a/turbopack/crates/turbopack-css/src/chunk/mod.rs +++ b/turbopack/crates/turbopack-css/src/chunk/mod.rs @@ -450,12 +450,21 @@ impl Introspectable for CssChunk { let mut children = children_from_output_assets(OutputAsset::references(self)) .await? .clone_value(); - for &chunk_item in self.await?.content.await?.chunk_items.iter() { - children.insert(( - entry_module_key().to_resolved().await?, - IntrospectableModule::new(chunk_item.module()), - )); - } + children.extend( + self.await? + .content + .await? + .chunk_items + .iter() + .map(|chunk_item| async move { + Ok(( + entry_module_key().to_resolved().await?, + IntrospectableModule::new(chunk_item.module()), + )) + }) + .try_join() + .await?, + ); Ok(Vc::cell(children)) } } diff --git a/turbopack/crates/turbopack-ecmascript/src/references/mod.rs b/turbopack/crates/turbopack-ecmascript/src/references/mod.rs index ce395debcf7c3..dd60b03350ca5 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/mod.rs @@ -598,6 +598,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( let mut evaluation_references = Vec::new(); + // ast-grep-ignore: to-resolved-in-loop for (i, r) in eval_context.imports.references().enumerate() { let r = EsmAssetReference::new( *origin, diff --git a/turbopack/crates/turbopack-ecmascript/src/tree_shake/side_effect_module.rs b/turbopack/crates/turbopack-ecmascript/src/tree_shake/side_effect_module.rs index 71a93e9c18678..1027451c25147 100644 --- a/turbopack/crates/turbopack-ecmascript/src/tree_shake/side_effect_module.rs +++ b/turbopack/crates/turbopack-ecmascript/src/tree_shake/side_effect_module.rs @@ -1,6 +1,6 @@ use anyhow::Result; use turbo_rcstr::RcStr; -use turbo_tasks::{ResolvedVc, Value, Vc}; +use turbo_tasks::{ResolvedVc, TryJoinIterExt, Value, Vc}; use turbo_tasks_fs::glob::Glob; use turbopack_core::{ asset::{Asset, AssetContent}, @@ -76,16 +76,22 @@ impl Module for SideEffectsModule { async fn references(&self) -> Result> { let mut references = vec![]; - for &side_effect in self.side_effects.iter() { - references.push(ResolvedVc::upcast( - SingleChunkableModuleReference::new( - *ResolvedVc::upcast(side_effect), - Vc::cell(RcStr::from("side effect")), - ) - .to_resolved() + references.extend( + self.side_effects + .iter() + .map(|side_effect| async move { + Ok(ResolvedVc::upcast( + SingleChunkableModuleReference::new( + *ResolvedVc::upcast(*side_effect), + Vc::cell(RcStr::from("side effect")), + ) + .to_resolved() + .await?, + )) + }) + .try_join() .await?, - )); - } + ); references.push(ResolvedVc::upcast( SingleChunkableModuleReference::new( diff --git a/turbopack/crates/turbopack-ecmascript/src/typescript/mod.rs b/turbopack/crates/turbopack-ecmascript/src/typescript/mod.rs index 0dc4356aced7e..81f703d97dcbc 100644 --- a/turbopack/crates/turbopack-ecmascript/src/typescript/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/src/typescript/mod.rs @@ -1,7 +1,7 @@ use anyhow::Result; use serde_json::Value as JsonValue; use turbo_rcstr::RcStr; -use turbo_tasks::{ResolvedVc, Value, ValueToString, Vc}; +use turbo_tasks::{ResolvedVc, TryJoinIterExt, Value, ValueToString, Vc}; use turbo_tasks_fs::DirectoryContent; use turbopack_core::{ asset::{Asset, AssetContent}, @@ -59,13 +59,20 @@ impl Module for TsConfigModuleAsset { ))), ) .await?; - for (_, config_asset) in configs[1..].iter() { - references.push(ResolvedVc::upcast( - TsExtendsReference::new(**config_asset) - .to_resolved() - .await?, - )); - } + references.extend( + configs[1..] + .iter() + .map(|(_, config_asset)| async move { + Ok(ResolvedVc::upcast( + TsExtendsReference::new(**config_asset) + .to_resolved() + .await?, + )) + }) + .try_join() + .await?, + ); + // ts-node options { let compiler = read_from_tsconfigs(&configs, |json, source| {