Skip to content

Commit 2c3c12f

Browse files
authored
Turbopack: refactor resolve_url_reference to avoid chunk_path (#77732)
### What? refactor resolve_url_reference to avoid chunk_path and use chunk_root_path instead
1 parent d5228b3 commit 2c3c12f

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

turbopack/crates/turbopack-browser/src/chunking_context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ impl ChunkingContext for BrowserChunkingContext {
329329
*self.environment
330330
}
331331

332+
#[turbo_tasks::function]
333+
async fn chunk_root_path(&self) -> Vc<FileSystemPath> {
334+
*self.chunk_root_path
335+
}
336+
332337
#[turbo_tasks::function]
333338
async fn chunk_path(
334339
&self,

turbopack/crates/turbopack-core/src/chunk/chunking_context.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,22 @@ pub struct ChunkingConfigs(FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingCon
123123
pub trait ChunkingContext {
124124
fn name(self: Vc<Self>) -> Vc<RcStr>;
125125
fn should_use_file_source_map_uris(self: Vc<Self>) -> Vc<bool>;
126-
// The root path of the project
126+
/// The root path of the project
127127
fn root_path(self: Vc<Self>) -> Vc<FileSystemPath>;
128-
// The output root path in the output filesystem
128+
/// The output root path in the output filesystem
129129
fn output_root(self: Vc<Self>) -> Vc<FileSystemPath>;
130-
// A relative path how to reach the root path from the output root. This is used to compute
131-
// original paths at runtime relative to the output files. e. g. import.meta.url needs that.
130+
/// A relative path how to reach the root path from the output root. This is used to compute
131+
/// original paths at runtime relative to the output files. e. g. import.meta.url needs that.
132132
fn output_root_to_root_path(self: Vc<Self>) -> Vc<RcStr>;
133133

134134
// TODO remove this, a chunking context should not be bound to a specific
135135
// environment since this can change due to transitions in the module graph
136136
fn environment(self: Vc<Self>) -> Vc<Environment>;
137137

138+
/// The path to the folder where all chunks are placed. This can be used to compute relative
139+
/// paths.
140+
fn chunk_root_path(self: Vc<Self>) -> Vc<FileSystemPath>;
141+
138142
// TODO(alexkirsz) Remove this from the chunking context. This should be at the
139143
// discretion of chunking context implementors. However, we currently use this
140144
// in a couple of places in `turbopack-css`, so we need to remove that

turbopack/crates/turbopack-css/src/references/url.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use turbo_rcstr::RcStr;
1111
use turbo_tasks::{ResolvedVc, Value, ValueToString, Vc};
1212
use turbopack_core::{
1313
chunk::{ChunkableModuleReference, ChunkingContext},
14-
ident::AssetIdent,
1514
issue::IssueSource,
1615
output::OutputAsset,
1716
reference::ModuleReference,
@@ -103,18 +102,9 @@ pub async fn resolve_url_reference(
103102
url: Vc<UrlAssetReference>,
104103
chunking_context: Vc<Box<dyn ChunkingContext>>,
105104
) -> Result<Vc<Option<RcStr>>> {
106-
let this = url.await?;
107-
// TODO(WEB-662) This is not the correct way to get the current chunk path. It
108-
// currently works as all chunks are in the same directory.
109-
let chunk_path = chunking_context.chunk_path(
110-
AssetIdent::from_path(this.origin.origin_path()),
111-
".css".into(),
112-
);
113-
let context_path = chunk_path.parent().await?;
105+
let context_path = chunking_context.chunk_root_path().await?;
114106

115107
if let ReferencedAsset::Some(asset) = &*url.get_referenced_asset(chunking_context).await? {
116-
// TODO(WEB-662) This is not the correct way to get the path of the asset.
117-
// `asset` is on module-level, but we need the output-level asset instead.
118108
let path = asset.path().await?;
119109
let relative_path = context_path
120110
.get_relative_path_to(&path)

turbopack/crates/turbopack-nodejs/src/chunking_context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ impl ChunkingContext for NodeJsChunkingContext {
268268
))
269269
}
270270

271+
#[turbo_tasks::function]
272+
async fn chunk_root_path(&self) -> Vc<FileSystemPath> {
273+
*self.chunk_root_path
274+
}
275+
271276
#[turbo_tasks::function]
272277
async fn chunk_path(
273278
&self,

0 commit comments

Comments
 (0)