Skip to content

Commit

Permalink
refactor(turbopack-core): Migrate Sources type (and related types) …
Browse files Browse the repository at this point in the history
…to `ResolvedVc` (vercel#72382)

A hand refactor of some struct fields from `Vc` to `ResolvedVc` to unblock local tasks.

Closes PACK-3392
  • Loading branch information
bgw authored and stipsan committed Nov 6, 2024
1 parent f1831b2 commit 959f38c
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 138 deletions.
6 changes: 3 additions & 3 deletions crates/next-core/src/next_edge/unsupported.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use indoc::formatdoc;
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
Expand Down Expand Up @@ -62,9 +62,9 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
"#
};
let content = AssetContent::file(File::from(code).into());
let source = VirtualSource::new(root_path, content);
let source = VirtualSource::new(root_path, content).to_resolved().await?;
return Ok(ImportMapResult::Result(
ResolveResult::source(Vc::upcast(source)).resolved_cell(),
ResolveResult::source(ResolvedVc::upcast(source)).resolved_cell(),
)
.cell());
};
Expand Down
20 changes: 11 additions & 9 deletions crates/next-core/src/next_font/google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ impl NextFontGoogleReplacer {
.into(),
)
.cell()),
);
Ok(
ImportMapResult::Result(ResolveResult::source(Vc::upcast(js_asset)).resolved_cell())
.cell(),
).to_resolved().await?;
Ok(ImportMapResult::Result(
ResolveResult::source(ResolvedVc::upcast(js_asset)).resolved_cell(),
)
.cell())
}
}

Expand Down Expand Up @@ -267,7 +267,7 @@ impl NextFontGoogleCssModuleReplacer {
.await?;

Ok(ImportMapResult::Result(
ResolveResult::source(*ResolvedVc::upcast(css_asset)).resolved_cell(),
ResolveResult::source(ResolvedVc::upcast(css_asset)).resolved_cell(),
)
.cell())
}
Expand Down Expand Up @@ -386,12 +386,14 @@ impl ImportMappingReplacement for NextFontGoogleFontFileReplacer {
let font_source = VirtualSource::new(
font_virtual_path,
AssetContent::file(FileContent::Content(font.await?.0.as_slice().into()).cell()),
);
)
.to_resolved()
.await?;

Ok(
ImportMapResult::Result(ResolveResult::source(Vc::upcast(font_source)).resolved_cell())
.cell(),
Ok(ImportMapResult::Result(
ResolveResult::source(ResolvedVc::upcast(font_source)).resolved_cell(),
)
.cell())
}
}

Expand Down
22 changes: 14 additions & 8 deletions crates/next-core/src/next_font/local/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{bail, Context, Result};
use indoc::formatdoc;
use serde::{Deserialize, Serialize};
use turbo_tasks::{RcStr, Value, Vc};
use turbo_tasks::{RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{
glob::Glob, json::parse_json_with_source_context, FileContent, FileSystemPath,
};
Expand Down Expand Up @@ -174,10 +174,12 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
.into(),
),
AssetContent::file(FileContent::Content(file_content.into()).into()),
);
)
.to_resolved()
.await?;

Ok(ResolveResultOption::some(
ResolveResult::source(Vc::upcast(js_asset)).into(),
ResolveResult::source(ResolvedVc::upcast(js_asset)).cell(),
))
}
"@vercel/turbopack-next/internal/font/local/cssmodule.module.css" => {
Expand All @@ -202,11 +204,13 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {

let css_asset = VirtualSource::new(
css_virtual_path,
AssetContent::file(FileContent::Content(stylesheet.into()).into()),
);
AssetContent::file(FileContent::Content(stylesheet.into()).cell()),
)
.to_resolved()
.await?;

Ok(ResolveResultOption::some(
ResolveResult::source(Vc::upcast(css_asset)).into(),
ResolveResult::source(ResolvedVc::upcast(css_asset)).cell(),
))
}
"@vercel/turbopack-next/internal/font/local/font" => {
Expand All @@ -233,10 +237,12 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
let font_file = lookup_path.join(path.clone()).read();

let font_source =
VirtualSource::new(font_virtual_path, AssetContent::file(font_file));
VirtualSource::new(font_virtual_path, AssetContent::file(font_file))
.to_resolved()
.await?;

Ok(ResolveResultOption::some(
ResolveResult::source(Vc::upcast(font_source)).into(),
ResolveResult::source(ResolvedVc::upcast(font_source)).cell(),
))
}
_ => Ok(ResolveResultOption::none()),
Expand Down
12 changes: 9 additions & 3 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use anyhow::Result;
use lazy_static::lazy_static;
use turbo_tasks::{RcStr, Value, Vc};
use turbo_tasks::{RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{glob::Glob, FileSystemPath};
use turbopack_core::{
diagnostics::DiagnosticExt,
Expand Down Expand Up @@ -319,7 +319,10 @@ impl AfterResolvePlugin for NextNodeSharedRuntimeResolvePlugin {
.join(format!("{base}/{resource_request}").into());

Ok(Vc::cell(Some(
ResolveResult::source(Vc::upcast(FileSource::new(new_path))).into(),
ResolveResult::source(ResolvedVc::upcast(
FileSource::new(new_path).to_resolved().await?,
))
.cell(),
)))
}
}
Expand Down Expand Up @@ -418,7 +421,10 @@ impl AfterResolvePlugin for NextSharedRuntimeResolvePlugin {
let modified_path = raw_fs_path.path.replace("next/dist/esm/", "next/dist/");
let new_path = fs_path.root().join(modified_path.into());
Ok(Vc::cell(Some(
ResolveResult::source(Vc::upcast(FileSource::new(new_path))).into(),
ResolveResult::source(ResolvedVc::upcast(
FileSource::new(new_path).to_resolved().await?,
))
.cell(),
)))
}
}
32 changes: 15 additions & 17 deletions crates/next-core/src/transform_options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use turbo_tasks::Vc;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{self, FileJsonContent, FileSystemPath};
use turbopack::{
module_options::{
Expand All @@ -19,20 +19,18 @@ use crate::{mode::NextMode, next_config::NextConfig};

async fn get_typescript_options(
project_path: Vc<FileSystemPath>,
) -> Option<Vec<(Vc<FileJsonContent>, Vc<Box<dyn Source>>)>> {
) -> Result<Option<Vec<(Vc<FileJsonContent>, ResolvedVc<Box<dyn Source>>)>>> {
let tsconfig = find_context_file(project_path, tsconfig());
match *tsconfig.await.ok()? {
FindContextFileResult::Found(path, _) => Some(
read_tsconfigs(
path.read(),
Vc::upcast(FileSource::new(*path)),
node_cjs_resolve_options(path.root()),
)
.await
.ok()?,
),
FindContextFileResult::NotFound(_) => None,
}
Ok(match tsconfig.await.ok().as_deref() {
Some(FindContextFileResult::Found(path, _)) => read_tsconfigs(
path.read(),
ResolvedVc::upcast(FileSource::new(**path).to_resolved().await?),
node_cjs_resolve_options(path.root()),
)
.await
.ok(),
Some(FindContextFileResult::NotFound(_)) | None => None,
})
}

/// Build the transform options for specifically for the typescript's runtime
Expand All @@ -41,7 +39,7 @@ async fn get_typescript_options(
pub async fn get_typescript_transform_options(
project_path: Vc<FileSystemPath>,
) -> Result<Vc<TypescriptTransformOptions>> {
let tsconfig = get_typescript_options(project_path).await;
let tsconfig = get_typescript_options(project_path).await?;

let use_define_for_class_fields = if let Some(tsconfig) = tsconfig {
read_from_tsconfigs(&tsconfig, |json, _| {
Expand All @@ -66,7 +64,7 @@ pub async fn get_typescript_transform_options(
pub async fn get_decorators_transform_options(
project_path: Vc<FileSystemPath>,
) -> Result<Vc<DecoratorsOptions>> {
let tsconfig = get_typescript_options(project_path).await;
let tsconfig = get_typescript_options(project_path).await?;

let decorators_transform_options = if let Some(tsconfig) = tsconfig {
read_from_tsconfigs(&tsconfig, |json, _| {
Expand Down Expand Up @@ -126,7 +124,7 @@ pub async fn get_jsx_transform_options(
is_rsc_context: bool,
next_config: Vc<NextConfig>,
) -> Result<Vc<JsxTransformOptions>> {
let tsconfig = get_typescript_options(project_path).await;
let tsconfig = get_typescript_options(project_path).await?;

let enable_react_refresh = if let Some(resolve_options_context) = resolve_options_context {
assert_can_resolve_react_refresh(project_path, resolve_options_context)
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-core/src/reference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub async fn referenced_modules_and_affecting_sources(
.affecting_sources_iter()
.map(|source| async move {
Ok(ResolvedVc::upcast(
RawModule::new(source).to_resolved().await?,
RawModule::new(*source).to_resolved().await?,
))
})
.try_join()
Expand Down
Loading

0 comments on commit 959f38c

Please sign in to comment.