Skip to content
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

perf(turbopack): Use ResolvedVc for turbopack-nodejs #73200

Merged
merged 26 commits into from
Nov 26, 2024
Merged
10 changes: 5 additions & 5 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,17 +633,17 @@ impl Project {

#[turbo_tasks::function]
pub(super) async fn execution_context(self: Vc<Self>) -> Result<Vc<ExecutionContext>> {
let node_root = self.node_root();
let node_root = self.node_root().to_resolved().await?;
let next_mode = self.next_mode().await?;

let node_execution_chunking_context = Vc::upcast(
NodeJsChunkingContext::builder(
self.project_path(),
self.project_path().to_resolved().await?,
node_root,
node_root,
node_root.join("build/chunks".into()),
node_root.join("build/assets".into()),
node_build_environment(),
node_root.join("build/chunks".into()).to_resolved().await?,
node_root.join("build/assets".into()).to_resolved().await?,
node_build_environment().to_resolved().await?,
next_mode.runtime_type(),
)
.build(),
Expand Down
34 changes: 20 additions & 14 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,12 @@ pub fn get_server_runtime_entries(
#[turbo_tasks::function]
pub async fn get_server_chunking_context_with_client_assets(
mode: Vc<NextMode>,
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
asset_prefix: Vc<Option<RcStr>>,
environment: Vc<Environment>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
project_path: ResolvedVc<FileSystemPath>,
node_root: ResolvedVc<FileSystemPath>,
client_root: ResolvedVc<FileSystemPath>,
asset_prefix: ResolvedVc<Option<RcStr>>,
environment: ResolvedVc<Environment>,
module_id_strategy: ResolvedVc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<NodeJsChunkingContext>> {
let next_mode = mode.await?;
Expand All @@ -990,8 +990,14 @@ pub async fn get_server_chunking_context_with_client_assets(
project_path,
node_root,
client_root,
node_root.join("server/chunks/ssr".into()),
client_root.join("static/media".into()),
node_root
.join("server/chunks/ssr".into())
.to_resolved()
.await?,
client_root
.join("static/media".into())
.to_resolved()
.await?,
environment,
next_mode.runtime_type(),
)
Expand All @@ -1013,10 +1019,10 @@ pub async fn get_server_chunking_context_with_client_assets(
#[turbo_tasks::function]
pub async fn get_server_chunking_context(
mode: Vc<NextMode>,
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
environment: Vc<Environment>,
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
project_path: ResolvedVc<FileSystemPath>,
node_root: ResolvedVc<FileSystemPath>,
environment: ResolvedVc<Environment>,
module_id_strategy: ResolvedVc<Box<dyn ModuleIdStrategy>>,
turbo_minify: Vc<bool>,
) -> Result<Vc<NodeJsChunkingContext>> {
let next_mode = mode.await?;
Expand All @@ -1027,8 +1033,8 @@ pub async fn get_server_chunking_context(
project_path,
node_root,
node_root,
node_root.join("server/chunks".into()),
node_root.join("server/assets".into()),
node_root.join("server/chunks".into()).to_resolved().await?,
node_root.join("server/assets".into()).to_resolved().await?,
environment,
next_mode.runtime_type(),
)
Expand Down
22 changes: 16 additions & 6 deletions turbopack/crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ async fn build_internal(
browserslist_query: browserslist_query.clone(),
}
.resolved_cell(),
)));
)))
.to_resolved()
.await?;
let output_fs = output_fs(project_dir.clone());
let project_fs = project_fs(root_dir.clone());
let project_relative = project_dir.strip_prefix(&*root_dir).unwrap();
Expand All @@ -187,8 +189,12 @@ async fn build_internal(
.unwrap_or(project_relative)
.replace(MAIN_SEPARATOR, "/")
.into();
let project_path = project_fs.root().join(project_relative);
let build_output_root = output_fs.root().join("dist".into());
let project_path = project_fs
.root()
.join(project_relative)
.to_resolved()
.await?;
let build_output_root = output_fs.root().join("dist".into()).to_resolved().await?;

let node_env = NodeEnv::Production.cell();

Expand All @@ -211,9 +217,13 @@ async fn build_internal(

let compile_time_info = get_client_compile_time_info(browserslist_query, node_env);
let execution_context =
ExecutionContext::new(project_path, chunking_context, load_env(project_path));
let asset_context =
get_client_asset_context(project_path, execution_context, compile_time_info, node_env);
ExecutionContext::new(*project_path, chunking_context, load_env(*project_path));
let asset_context = get_client_asset_context(
*project_path,
execution_context,
compile_time_info,
node_env,
);

let entry_requests = (*entry_requests
.await?
Expand Down
32 changes: 21 additions & 11 deletions turbopack/crates/turbopack-cli/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async fn source(
entry_requests: TransientInstance<Vec<EntryRequest>>,
eager_compile: bool,
browserslist_query: RcStr,
) -> Vc<Box<dyn ContentSource>> {
) -> Result<Vc<Box<dyn ContentSource>>> {
let project_relative = project_dir.strip_prefix(&*root_dir).unwrap();
let project_relative: RcStr = project_relative
.strip_prefix(MAIN_SEPARATOR)
Expand All @@ -243,24 +243,34 @@ async fn source(

let output_fs = output_fs(project_dir);
let fs = project_fs(root_dir);
let project_path: Vc<turbo_tasks_fs::FileSystemPath> = fs.root().join(project_relative);
let project_path = fs.root().join(project_relative).to_resolved().await?;

let env = load_env(project_path);
let build_output_root = output_fs.root().join(".turbopack/build".into());
let env = load_env(*project_path);
let build_output_root = output_fs
.root()
.join(".turbopack/build".into())
.to_resolved()
.await?;

let build_chunking_context = NodeJsChunkingContext::builder(
project_path,
build_output_root,
build_output_root,
build_output_root.join("chunks".into()),
build_output_root.join("assets".into()),
node_build_environment(),
build_output_root
.join("chunks".into())
.to_resolved()
.await?,
build_output_root
.join("assets".into())
.to_resolved()
.await?,
node_build_environment().to_resolved().await?,
RuntimeType::Development,
)
.build();

let execution_context =
ExecutionContext::new(project_path, Vc::upcast(build_chunking_context), env);
ExecutionContext::new(*project_path, Vc::upcast(build_chunking_context), env);

let server_fs = Vc::upcast::<Box<dyn FileSystem>>(ServerFileSystem::new());
let server_root = server_fs.root();
Expand All @@ -283,7 +293,7 @@ async fn source(
.collect();

let web_source = create_web_entry_source(
project_path,
*project_path,
execution_context,
entry_requests,
server_root,
Expand All @@ -304,11 +314,11 @@ async fn source(
.cell(),
);
let main_source = Vc::upcast(main_source);
Vc::upcast(PrefixedRouterContentSource::new(
Ok(Vc::upcast(PrefixedRouterContentSource::new(
Default::default(),
vec![("__turbopack__".into(), introspect)],
main_source,
))
)))
}

pub fn register() {
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::{FxIndexMap, ValueToString, Vc};
use turbo_tasks::{FxIndexMap, ResolvedVc, ValueToString, Vc};
use turbo_tasks_hash::hash_xxh3_hash64;

use super::ModuleId;
Expand All @@ -18,6 +18,10 @@ impl DevModuleIdStrategy {
pub fn new() -> Vc<Self> {
DevModuleIdStrategy {}.cell()
}

pub fn new_resolved() -> ResolvedVc<Self> {
DevModuleIdStrategy {}.resolved_cell()
}
}

#[turbo_tasks::value_impl]
Expand Down
51 changes: 27 additions & 24 deletions turbopack/crates/turbopack-nodejs/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct NodeJsChunkingContextBuilder {
}

impl NodeJsChunkingContextBuilder {
pub fn asset_prefix(mut self, asset_prefix: Vc<Option<RcStr>>) -> Self {
pub fn asset_prefix(mut self, asset_prefix: ResolvedVc<Option<RcStr>>) -> Self {
self.chunking_context.asset_prefix = asset_prefix;
self
}
Expand Down Expand Up @@ -65,7 +65,10 @@ impl NodeJsChunkingContextBuilder {
self
}

pub fn module_id_strategy(mut self, module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>) -> Self {
pub fn module_id_strategy(
mut self,
module_id_strategy: ResolvedVc<Box<dyn ModuleIdStrategy>>,
) -> Self {
self.chunking_context.module_id_strategy = module_id_strategy;
self
}
Expand All @@ -82,19 +85,19 @@ impl NodeJsChunkingContextBuilder {
pub struct NodeJsChunkingContext {
/// This path get stripped off of chunk paths before generating output asset
/// paths.
context_path: Vc<FileSystemPath>,
context_path: ResolvedVc<FileSystemPath>,
/// This path is used to compute the url to request chunks or assets from
output_root: Vc<FileSystemPath>,
output_root: ResolvedVc<FileSystemPath>,
/// This path is used to compute the url to request chunks or assets from
client_root: Vc<FileSystemPath>,
client_root: ResolvedVc<FileSystemPath>,
/// Chunks are placed at this path
chunk_root_path: Vc<FileSystemPath>,
chunk_root_path: ResolvedVc<FileSystemPath>,
/// Static assets are placed at this path
asset_root_path: Vc<FileSystemPath>,
asset_root_path: ResolvedVc<FileSystemPath>,
/// Static assets requested from this url base
asset_prefix: Vc<Option<RcStr>>,
asset_prefix: ResolvedVc<Option<RcStr>>,
/// The environment chunks will be evaluated in.
environment: Vc<Environment>,
environment: ResolvedVc<Environment>,
/// The kind of runtime to include in the output.
runtime_type: RuntimeType,
/// Enable tracing for this chunking
Expand All @@ -104,20 +107,20 @@ pub struct NodeJsChunkingContext {
/// Whether to use manifest chunks for lazy compilation
manifest_chunks: bool,
/// The strategy to use for generating module ids
module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
module_id_strategy: ResolvedVc<Box<dyn ModuleIdStrategy>>,
/// Whether to use file:// uris for source map sources
should_use_file_source_map_uris: bool,
}

impl NodeJsChunkingContext {
/// Creates a new chunking context builder.
pub fn builder(
context_path: Vc<FileSystemPath>,
output_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
chunk_root_path: Vc<FileSystemPath>,
asset_root_path: Vc<FileSystemPath>,
environment: Vc<Environment>,
context_path: ResolvedVc<FileSystemPath>,
output_root: ResolvedVc<FileSystemPath>,
client_root: ResolvedVc<FileSystemPath>,
chunk_root_path: ResolvedVc<FileSystemPath>,
asset_root_path: ResolvedVc<FileSystemPath>,
environment: ResolvedVc<Environment>,
runtime_type: RuntimeType,
) -> NodeJsChunkingContextBuilder {
NodeJsChunkingContextBuilder {
Expand All @@ -127,14 +130,14 @@ impl NodeJsChunkingContext {
client_root,
chunk_root_path,
asset_root_path,
asset_prefix: Default::default(),
asset_prefix: ResolvedVc::cell(None),
enable_file_tracing: false,
environment,
runtime_type,
minify_type: MinifyType::NoMinify,
manifest_chunks: false,
should_use_file_source_map_uris: false,
module_id_strategy: Vc::upcast(DevModuleIdStrategy::new()),
module_id_strategy: ResolvedVc::upcast(DevModuleIdStrategy::new_resolved()),
},
}
}
Expand Down Expand Up @@ -164,7 +167,7 @@ impl NodeJsChunkingContext {

#[turbo_tasks::function]
pub fn asset_prefix(&self) -> Vc<Option<RcStr>> {
self.asset_prefix
*self.asset_prefix
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -197,17 +200,17 @@ impl ChunkingContext for NodeJsChunkingContext {

#[turbo_tasks::function]
fn context_path(&self) -> Vc<FileSystemPath> {
self.context_path
*self.context_path
}

#[turbo_tasks::function]
fn output_root(&self) -> Vc<FileSystemPath> {
self.output_root
*self.output_root
}

#[turbo_tasks::function]
fn environment(&self) -> Vc<Environment> {
self.environment
*self.environment
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -243,8 +246,8 @@ impl ChunkingContext for NodeJsChunkingContext {
ident: Vc<AssetIdent>,
extension: RcStr,
) -> Result<Vc<FileSystemPath>> {
let root_path = self.chunk_root_path;
let name = ident.output_name(self.context_path, extension).await?;
let root_path = *self.chunk_root_path;
let name = ident.output_name(*self.context_path, extension).await?;
Ok(root_path.join(name.clone_value()))
}

Expand Down
12 changes: 6 additions & 6 deletions turbopack/crates/turbopack-nodejs/src/ecmascript/node/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ use crate::NodeJsChunkingContext;
/// Production Ecmascript chunk targeting Node.js.
#[turbo_tasks::value(shared)]
pub(crate) struct EcmascriptBuildNodeChunk {
chunking_context: Vc<NodeJsChunkingContext>,
chunk: Vc<EcmascriptChunk>,
chunking_context: ResolvedVc<NodeJsChunkingContext>,
chunk: ResolvedVc<EcmascriptChunk>,
}

#[turbo_tasks::value_impl]
impl EcmascriptBuildNodeChunk {
/// Creates a new [`Vc<EcmascriptBuildNodeChunk>`].
#[turbo_tasks::function]
pub fn new(
chunking_context: Vc<NodeJsChunkingContext>,
chunk: Vc<EcmascriptChunk>,
chunking_context: ResolvedVc<NodeJsChunkingContext>,
chunk: ResolvedVc<EcmascriptChunk>,
) -> Vc<Self> {
EcmascriptBuildNodeChunk {
chunking_context,
Expand Down Expand Up @@ -57,7 +57,7 @@ impl EcmascriptBuildNodeChunk {
async fn own_content(self: Vc<Self>) -> Result<Vc<EcmascriptBuildNodeChunkContent>> {
let this = self.await?;
Ok(EcmascriptBuildNodeChunkContent::new(
this.chunking_context,
*this.chunking_context,
self,
this.chunk.chunk_content(),
))
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Introspectable for EcmascriptBuildNodeChunk {
#[turbo_tasks::function]
async fn children(&self) -> Result<Vc<IntrospectableChildren>> {
let mut children = FxIndexSet::default();
let introspectable_chunk = Vc::upcast::<Box<dyn Introspectable>>(self.chunk)
let introspectable_chunk = ResolvedVc::upcast::<Box<dyn Introspectable>>(self.chunk)
.resolve()
.await?;
children.insert((Vc::cell("chunk".into()), introspectable_chunk));
Expand Down
Loading
Loading