Skip to content

Commit 544f700

Browse files
author
Lichu Acuña
committed
Added a GlobalInformation struct, which is made available through the ChunkingContext trait.
Constructing a new GlobalInformation object takes a Project parameter, giving the constructor access to global information
1 parent d24b396 commit 544f700

File tree

9 files changed

+64
-2
lines changed

9 files changed

+64
-2
lines changed

crates/turbopack-browser/src/chunking_context.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use anyhow::{bail, Context, Result};
22
use tracing::Instrument;
3-
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
3+
use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc};
44
use turbo_tasks_fs::FileSystemPath;
55
use turbopack_core::{
66
chunk::{
77
availability_info::AvailabilityInfo,
88
chunk_group::{make_chunk_group, MakeChunkGroupResult},
9+
global_information::OptionGlobalInformation,
910
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingContext,
1011
EntryChunkGroupResult, EvaluatableAssets, MinifyType, ModuleId,
1112
},
@@ -122,6 +123,8 @@ pub struct BrowserChunkingContext {
122123
minify_type: MinifyType,
123124
/// Whether to use manifest chunks for lazy compilation
124125
manifest_chunks: bool,
126+
/// Global information
127+
global_information: Vc<OptionGlobalInformation>,
125128
}
126129

127130
impl BrowserChunkingContext {
@@ -133,6 +136,7 @@ impl BrowserChunkingContext {
133136
asset_root_path: Vc<FileSystemPath>,
134137
environment: Vc<Environment>,
135138
runtime_type: RuntimeType,
139+
global_information: Vc<OptionGlobalInformation>,
136140
) -> BrowserChunkingContextBuilder {
137141
BrowserChunkingContextBuilder {
138142
chunking_context: BrowserChunkingContext {
@@ -151,6 +155,7 @@ impl BrowserChunkingContext {
151155
runtime_type,
152156
minify_type: MinifyType::NoMinify,
153157
manifest_chunks: false,
158+
global_information,
154159
},
155160
}
156161
}
@@ -238,6 +243,16 @@ impl BrowserChunkingContext {
238243

239244
#[turbo_tasks::value_impl]
240245
impl ChunkingContext for BrowserChunkingContext {
246+
#[turbo_tasks::function]
247+
async fn chunk_item_id_from_ident(
248+
self: Vc<Self>,
249+
ident: Vc<AssetIdent>,
250+
) -> Result<Vc<ModuleId>> {
251+
let this = self.await?;
252+
dbg!(this.global_information.dbg().await?);
253+
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
254+
}
255+
241256
#[turbo_tasks::function]
242257
fn name(&self) -> Vc<RcStr> {
243258
if let Some(name) = &self.name {

crates/turbopack-cli/src/build/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ async fn build_internal(
197197
NodeEnv::Development => RuntimeType::Development,
198198
NodeEnv::Production => RuntimeType::Production,
199199
},
200+
Vc::cell(None),
200201
)
201202
.minify_type(minify_type)
202203
.build(),

crates/turbopack-cli/src/dev/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ async fn source(
255255
build_output_root.join("assets".into()),
256256
node_build_environment(),
257257
RuntimeType::Development,
258+
Vc::cell(None),
258259
)
259260
.build();
260261

crates/turbopack-cli/src/dev/web_entry_source.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub fn get_client_chunking_context(
4545
server_root.join("/_assets".into()),
4646
environment,
4747
RuntimeType::Development,
48+
Vc::cell(None),
4849
)
4950
.hot_module_replacement()
5051
.build(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::collections::HashMap;
2+
3+
use turbo_tasks::{RcStr, Vc};
4+
5+
use super::ModuleId;
6+
use crate::ident::AssetIdent;
7+
8+
#[turbo_tasks::value]
9+
#[derive(Clone, Debug)]
10+
pub struct GlobalInformation {
11+
pub test_str: Vc<RcStr>,
12+
pub module_id_map: HashMap<AssetIdent, ModuleId>,
13+
}
14+
15+
impl GlobalInformation {
16+
pub fn get_module_id(&self, asset_ident: &AssetIdent) -> ModuleId {
17+
self.module_id_map.get(asset_ident).cloned().expect(
18+
"No module ID found for the given asset identifier. This is an internal Turbopack \
19+
error. Please report it.",
20+
)
21+
}
22+
}
23+
24+
#[turbo_tasks::value(transparent)]
25+
pub struct OptionGlobalInformation(Option<GlobalInformation>);

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub(crate) mod chunking_context;
66
pub(crate) mod containment_tree;
77
pub(crate) mod data;
88
pub(crate) mod evaluate;
9+
pub mod global_information;
910
pub mod optimize;
1011

1112
use std::{

crates/turbopack-nodejs/src/chunking_context.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use std::iter::once;
22

33
use anyhow::{bail, Context, Result};
44
use tracing::Instrument;
5-
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
5+
use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc};
66
use turbo_tasks_fs::FileSystemPath;
77
use turbopack_core::{
88
chunk::{
99
availability_info::AvailabilityInfo,
1010
chunk_group::{make_chunk_group, MakeChunkGroupResult},
11+
global_information::OptionGlobalInformation,
1112
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingContext,
1213
EntryChunkGroupResult, EvaluatableAssets, MinifyType, ModuleId,
1314
},
@@ -84,6 +85,8 @@ pub struct NodeJsChunkingContext {
8485
minify_type: MinifyType,
8586
/// Whether to use manifest chunks for lazy compilation
8687
manifest_chunks: bool,
88+
/// Global information
89+
global_information: Vc<OptionGlobalInformation>,
8790
}
8891

8992
impl NodeJsChunkingContext {
@@ -96,6 +99,7 @@ impl NodeJsChunkingContext {
9699
asset_root_path: Vc<FileSystemPath>,
97100
environment: Vc<Environment>,
98101
runtime_type: RuntimeType,
102+
global_information: Vc<OptionGlobalInformation>,
99103
) -> NodeJsChunkingContextBuilder {
100104
NodeJsChunkingContextBuilder {
101105
chunking_context: NodeJsChunkingContext {
@@ -109,6 +113,7 @@ impl NodeJsChunkingContext {
109113
runtime_type,
110114
minify_type: MinifyType::NoMinify,
111115
manifest_chunks: false,
116+
global_information,
112117
},
113118
}
114119
}
@@ -131,6 +136,16 @@ impl NodeJsChunkingContext {
131136

132137
#[turbo_tasks::value_impl]
133138
impl NodeJsChunkingContext {
139+
#[turbo_tasks::function]
140+
async fn chunk_item_id_from_ident(
141+
self: Vc<Self>,
142+
ident: Vc<AssetIdent>,
143+
) -> Result<Vc<ModuleId>> {
144+
let this = self.await?;
145+
dbg!(this.global_information.dbg().await?);
146+
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
147+
}
148+
134149
#[turbo_tasks::function]
135150
fn new(this: Value<NodeJsChunkingContext>) -> Vc<Self> {
136151
this.into_value().cell()

crates/turbopack-tests/tests/execution.rs

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ async fn run_test(prepared_test: Vc<PreparedTest>) -> Result<Vc<RunTestResult>>
322322
static_root_path,
323323
env,
324324
RuntimeType::Development,
325+
Vc::cell(None),
325326
)
326327
.build();
327328

crates/turbopack-tests/tests/snapshot.rs

+2
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ async fn run_test(resource: RcStr) -> Result<Vc<FileSystemPath>> {
319319
static_root_path,
320320
env,
321321
options.runtime_type,
322+
Vc::cell(None),
322323
)
323324
.build(),
324325
),
@@ -331,6 +332,7 @@ async fn run_test(resource: RcStr) -> Result<Vc<FileSystemPath>> {
331332
static_root_path,
332333
env,
333334
options.runtime_type,
335+
Vc::cell(None),
334336
)
335337
.minify_type(options.minify_type)
336338
.build(),

0 commit comments

Comments
 (0)