Skip to content

Commit

Permalink
Evaluation uses Node.js chunking context instead of browser one (verc…
Browse files Browse the repository at this point in the history
…el/turborepo#8402)

### Description

Evaluation uses Node.js chunking context instead of browser one.
Renaming

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra authored Jun 12, 2024
1 parent 510dfe3 commit 6475279
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 196 deletions.
15 changes: 13 additions & 2 deletions crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use turbopack_core::{
chunk::{
availability_info::AvailabilityInfo,
chunk_group::{make_chunk_group, MakeChunkGroupResult},
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingContext, EvaluatableAssets,
MinifyType, ModuleId,
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingContext,
EntryChunkGroupResult, EvaluatableAssets, MinifyType, ModuleId,
},
environment::Environment,
ident::AssetIdent,
Expand Down Expand Up @@ -468,6 +468,17 @@ impl ChunkingContext for BrowserChunkingContext {
.await
}

#[turbo_tasks::function]
fn entry_chunk_group(
self: Vc<Self>,
_path: Vc<FileSystemPath>,
_module: Vc<Box<dyn Module>>,
_evaluatable_assets: Vc<EvaluatableAssets>,
_availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<EntryChunkGroupResult>> {
bail!("Browser chunking context does not support entry chunk groups")
}

#[turbo_tasks::function]
async fn async_loader_chunk_item(
self: Vc<Self>,
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use turbopack_cli_utils::issue::{ConsoleUi, LogOptions};
use turbopack_core::{
asset::Asset,
chunk::{
availability_info::AvailabilityInfo, ChunkableModule, ChunkingContextExt,
availability_info::AvailabilityInfo, ChunkableModule, ChunkingContext, ChunkingContextExt,
EvaluatableAssets, MinifyType,
},
environment::{BrowserEnvironment, Environment, ExecutionEnvironment},
Expand Down
4 changes: 2 additions & 2 deletions crates/turbopack-cli/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use turbo_tasks_fs::FileSystem;
use turbo_tasks_malloc::TurboMalloc;
use turbo_tasks_memory::MemoryBackend;
use turbopack::evaluate_context::node_build_environment;
use turbopack_browser::BrowserChunkingContext;
use turbopack_cli_utils::issue::{ConsoleUi, LogOptions};
use turbopack_core::{
issue::{IssueReporter, IssueSeverity},
Expand All @@ -37,6 +36,7 @@ use turbopack_dev_server::{
use turbopack_ecmascript_runtime::RuntimeType;
use turbopack_env::dotenv::load_env;
use turbopack_node::execution_context::ExecutionContext;
use turbopack_nodejs::NodeJsChunkingContext;

use self::web_entry_source::create_web_entry_source;
use crate::{
Expand Down Expand Up @@ -247,7 +247,7 @@ async fn source(
let env = load_env(project_path);
let build_output_root = output_fs.root().join(".turbopack/build".into());

let build_chunking_context = BrowserChunkingContext::builder(
let build_chunking_context = NodeJsChunkingContext::builder(
project_path,
build_output_root,
build_output_root,
Expand Down
106 changes: 106 additions & 0 deletions crates/turbopack-core/src/chunk/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
chunk::{ChunkItem, ModuleId},
environment::Environment,
ident::AssetIdent,
module::Module,
output::{OutputAsset, OutputAssets},
};

Expand Down Expand Up @@ -40,6 +41,12 @@ pub struct ChunkGroupResult {
pub availability_info: AvailabilityInfo,
}

#[turbo_tasks::value(shared)]
pub struct EntryChunkGroupResult {
pub asset: Vc<Box<dyn OutputAsset>>,
pub availability_info: AvailabilityInfo,
}

/// A context for the chunking that influences the way chunks are created
#[turbo_tasks::value_trait]
pub trait ChunkingContext {
Expand Down Expand Up @@ -95,6 +102,18 @@ pub trait ChunkingContext {
availability_info: Value<AvailabilityInfo>,
) -> Vc<ChunkGroupResult>;

/// Generates an output chunk that:
/// * evaluates the given assets; and
/// * exports the result of evaluating the given module as a CommonJS
/// default export.
fn entry_chunk_group(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<EntryChunkGroupResult>>;

async fn chunk_item_id_from_ident(
self: Vc<Self>,
ident: Vc<AssetIdent>,
Expand Down Expand Up @@ -131,6 +150,34 @@ pub trait ChunkingContextExt {
where
Self: Send;

fn entry_chunk_group_asset(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
availability_info: Value<AvailabilityInfo>,
) -> Vc<Box<dyn OutputAsset>>
where
Self: Send;

fn root_entry_chunk_group(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Vc<EntryChunkGroupResult>
where
Self: Send;

fn root_entry_chunk_group_asset(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Vc<Box<dyn OutputAsset>>
where
Self: Send;

fn chunk_group_assets(
self: Vc<Self>,
module: Vc<Box<dyn ChunkableModule>>,
Expand Down Expand Up @@ -169,6 +216,51 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
)
}

fn entry_chunk_group_asset(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
availability_info: Value<AvailabilityInfo>,
) -> Vc<Box<dyn OutputAsset>> {
entry_chunk_group_asset(
path,
Vc::upcast(self),
module,
evaluatable_assets,
availability_info,
)
}

fn root_entry_chunk_group(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Vc<EntryChunkGroupResult> {
self.entry_chunk_group(
path,
module,
evaluatable_assets,
Value::new(AvailabilityInfo::Root),
)
}

fn root_entry_chunk_group_asset(
self: Vc<Self>,
path: Vc<FileSystemPath>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Vc<Box<dyn OutputAsset>> {
entry_chunk_group_asset(
path,
Vc::upcast(self),
module,
evaluatable_assets,
Value::new(AvailabilityInfo::Root),
)
}

fn chunk_group_assets(
self: Vc<Self>,
module: Vc<Box<dyn ChunkableModule>>,
Expand Down Expand Up @@ -199,6 +291,20 @@ async fn evaluated_chunk_group_assets(
.assets)
}

#[turbo_tasks::function]
async fn entry_chunk_group_asset(
path: Vc<FileSystemPath>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module: Vc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<Box<dyn OutputAsset>>> {
Ok(chunking_context
.entry_chunk_group(path, module, evaluatable_assets, availability_info)
.await?
.asset)
}

#[turbo_tasks::function]
async fn chunk_group_assets(
chunking_context: Vc<Box<dyn ChunkingContext>>,
Expand Down
4 changes: 3 additions & 1 deletion crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ use turbo_tasks_hash::DeterministicHash;

use self::{availability_info::AvailabilityInfo, available_chunk_items::AvailableChunkItems};
pub use self::{
chunking_context::{ChunkGroupResult, ChunkingContext, ChunkingContextExt, MinifyType},
chunking_context::{
ChunkGroupResult, ChunkingContext, ChunkingContextExt, EntryChunkGroupResult, MinifyType,
},
data::{ChunkData, ChunkDataOption, ChunksData},
evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
};
Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-ecmascript-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub use runtime_type::RuntimeType;
pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
turbopack::register();
turbopack_core::register();
turbopack_ecmascript::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
Expand Down
72 changes: 0 additions & 72 deletions crates/turbopack-node/src/bootstrap.rs

This file was deleted.

19 changes: 3 additions & 16 deletions crates/turbopack-node/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::{to_sys_path, File, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
chunk::{ChunkingContext, EvaluatableAsset, EvaluatableAssets},
chunk::{ChunkingContext, ChunkingContextExt, EvaluatableAsset, EvaluatableAssets},
context::AssetContext,
error::PrettyPrintError,
file_source::FileSource,
Expand All @@ -33,7 +33,6 @@ use turbopack_core::{
};

use crate::{
bootstrap::NodeJsBootstrapAsset,
embed_js::embed_file_path,
emit, emit_package_json, internal_assets_for_source_mapping,
pool::{FormattingMode, NodeJsOperation, NodeJsPool},
Expand Down Expand Up @@ -122,12 +121,6 @@ pub async fn get_evaluate_pool(
)
.module();

let Some(entry_module) =
Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(entry_module).await?
else {
bail!("Internal module is not evaluatable");
};

let (Some(cwd), Some(entrypoint)) = (to_sys_path(cwd).await?, to_sys_path(path).await?) else {
panic!("can only evaluate from a disk filesystem");
};
Expand Down Expand Up @@ -156,14 +149,8 @@ pub async fn get_evaluate_pool(
Vc::<EvaluatableAssets>::cell(entries)
};

let bootstrap = Vc::upcast(
NodeJsBootstrapAsset {
path,
chunking_context,
evaluatable_assets: runtime_entries.with_entry(entry_module),
}
.cell(),
);
let bootstrap =
chunking_context.root_entry_chunk_group_asset(path, entry_module, runtime_entries);

let output_root: Vc<FileSystemPath> = chunking_context.output_root();
let emit_package = emit_package_json(output_root);
Expand Down
Loading

0 comments on commit 6475279

Please sign in to comment.