Skip to content

Commit

Permalink
allow to give a chunking context a name (vercel/turborepo#7930)
Browse files Browse the repository at this point in the history
### Description

improves error message

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->


Closes PACK-2930
  • Loading branch information
sokra authored Apr 22, 2024
1 parent dffc6f3 commit 80ad27b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
16 changes: 16 additions & 0 deletions crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ pub struct BrowserChunkingContextBuilder {
}

impl BrowserChunkingContextBuilder {
pub fn name(mut self, name: String) -> Self {
self.chunking_context.name = Some(name);
self
}

pub fn hot_module_replacement(mut self) -> Self {
self.chunking_context.enable_hot_module_replacement = true;
self
Expand Down Expand Up @@ -85,6 +90,7 @@ impl BrowserChunkingContextBuilder {
#[turbo_tasks::value(serialization = "auto_for_input")]
#[derive(Debug, Clone, Hash, PartialOrd, Ord)]
pub struct BrowserChunkingContext {
name: Option<String>,
/// This path get stripped off of chunk paths before generating output asset
/// paths.
context_path: Vc<FileSystemPath>,
Expand Down Expand Up @@ -130,6 +136,7 @@ impl BrowserChunkingContext {
) -> BrowserChunkingContextBuilder {
BrowserChunkingContextBuilder {
chunking_context: BrowserChunkingContext {
name: None,
context_path,
output_root,
client_root,
Expand Down Expand Up @@ -231,6 +238,15 @@ impl BrowserChunkingContext {

#[turbo_tasks::value_impl]
impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
fn name(&self) -> Vc<String> {
if let Some(name) = &self.name {
Vc::cell(name.clone())
} else {
Vc::cell("unknown".to_string())
}
}

#[turbo_tasks::function]
fn context_path(&self) -> Vc<FileSystemPath> {
self.context_path
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-core/src/chunk/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct ChunkGroupResult {
/// A context for the chunking that influences the way chunks are created
#[turbo_tasks::value_trait]
pub trait ChunkingContext {
fn name(self: Vc<Self>) -> Vc<String>;
fn context_path(self: Vc<Self>) -> Vc<FileSystemPath>;
fn output_root(self: Vc<Self>) -> Vc<FileSystemPath>;

Expand Down
10 changes: 6 additions & 4 deletions crates/turbopack-ecmascript/src/references/esm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ impl CodeGenerateable for EsmAssetReference {
.await?
{
bail!(
"the chunking context does not support external modules (request: \
{})",
"the chunking context ({}) does not support external modules (esm \
request: {})",
chunking_context.name().await?,
request
);
}
Expand Down Expand Up @@ -282,8 +283,9 @@ impl CodeGenerateable for EsmAssetReference {
.await?
{
bail!(
"the chunking context does not support external modules (request: \
{})",
"the chunking context ({}) does not support external modules \
(request: {})",
chunking_context.name().await?,
request
);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/turbopack-nodejs/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ impl NodeJsChunkingContext {

#[turbo_tasks::value_impl]
impl ChunkingContext for NodeJsChunkingContext {
#[turbo_tasks::function]
fn name(&self) -> Vc<String> {
Vc::cell("unknown".to_string())
}

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

0 comments on commit 80ad27b

Please sign in to comment.