Skip to content

Commit

Permalink
Turbopack: resolve some to-resolved-in-loop (vercel#73794)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Dec 12, 2024
1 parent 0b637c4 commit f7e0191
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
enable_mdx_rs: bool,
) -> Result<Option<ModuleRule>> {
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::{
Expand All @@ -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)),
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-core/src/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 11 additions & 10 deletions turbopack/crates/turbopack-core/src/rebase.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -51,17 +51,18 @@ impl OutputAsset for RebasedAsset {

#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<OutputAssets>> {
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))
}
}
Expand Down
35 changes: 25 additions & 10 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,11 +1069,18 @@ async fn type_exists(
refs: &mut Vec<ResolvedVc<Box<dyn Source>>>,
) -> Result<Option<ResolvedVc<FileSystemPath>>> {
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)
Expand All @@ -1087,11 +1094,18 @@ async fn any_exists(
refs: &mut Vec<ResolvedVc<Box<dyn Source>>>,
) -> Result<Option<(FileSystemEntryType, Vc<FileSystemPath>)>> {
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(
Expand Down Expand Up @@ -2263,6 +2277,7 @@ async fn apply_in_package(
fragment: Vc<RcStr>,
) -> Result<Option<Vc<ResolveResult>>> {
// 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
Expand Down
21 changes: 15 additions & 6 deletions turbopack/crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -76,16 +76,22 @@ impl Module for SideEffectsModule {
async fn references(&self) -> Result<Vc<ModuleReferences>> {
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(
Expand Down
23 changes: 15 additions & 8 deletions turbopack/crates/turbopack-ecmascript/src/typescript/mod.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit f7e0191

Please sign in to comment.