Skip to content

Commit

Permalink
Added a GlobalInformation struct, which is made available through the…
Browse files Browse the repository at this point in the history
… ChunkingContext trait.

Constructing a new GlobalInformation object takes a Project parameter, giving the constructor access to global information
  • Loading branch information
Lichu Acuña committed Aug 1, 2024
1 parent d24b396 commit 544f700
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 2 deletions.
17 changes: 16 additions & 1 deletion crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::{bail, Context, Result};
use tracing::Instrument;
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{
availability_info::AvailabilityInfo,
chunk_group::{make_chunk_group, MakeChunkGroupResult},
global_information::OptionGlobalInformation,
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingContext,
EntryChunkGroupResult, EvaluatableAssets, MinifyType, ModuleId,
},
Expand Down Expand Up @@ -122,6 +123,8 @@ pub struct BrowserChunkingContext {
minify_type: MinifyType,
/// Whether to use manifest chunks for lazy compilation
manifest_chunks: bool,
/// Global information
global_information: Vc<OptionGlobalInformation>,
}

impl BrowserChunkingContext {
Expand All @@ -133,6 +136,7 @@ impl BrowserChunkingContext {
asset_root_path: Vc<FileSystemPath>,
environment: Vc<Environment>,
runtime_type: RuntimeType,
global_information: Vc<OptionGlobalInformation>,
) -> BrowserChunkingContextBuilder {
BrowserChunkingContextBuilder {
chunking_context: BrowserChunkingContext {
Expand All @@ -151,6 +155,7 @@ impl BrowserChunkingContext {
runtime_type,
minify_type: MinifyType::NoMinify,
manifest_chunks: false,
global_information,
},
}
}
Expand Down Expand Up @@ -238,6 +243,16 @@ impl BrowserChunkingContext {

#[turbo_tasks::value_impl]
impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
async fn chunk_item_id_from_ident(
self: Vc<Self>,
ident: Vc<AssetIdent>,
) -> Result<Vc<ModuleId>> {
let this = self.await?;
dbg!(this.global_information.dbg().await?);
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
}

#[turbo_tasks::function]
fn name(&self) -> Vc<RcStr> {
if let Some(name) = &self.name {
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async fn build_internal(
NodeEnv::Development => RuntimeType::Development,
NodeEnv::Production => RuntimeType::Production,
},
Vc::cell(None),
)
.minify_type(minify_type)
.build(),
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-cli/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ async fn source(
build_output_root.join("assets".into()),
node_build_environment(),
RuntimeType::Development,
Vc::cell(None),
)
.build();

Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn get_client_chunking_context(
server_root.join("/_assets".into()),
environment,
RuntimeType::Development,
Vc::cell(None),
)
.hot_module_replacement()
.build(),
Expand Down
25 changes: 25 additions & 0 deletions crates/turbopack-core/src/chunk/global_information.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::collections::HashMap;

use turbo_tasks::{RcStr, Vc};

use super::ModuleId;
use crate::ident::AssetIdent;

#[turbo_tasks::value]
#[derive(Clone, Debug)]
pub struct GlobalInformation {
pub test_str: Vc<RcStr>,
pub module_id_map: HashMap<AssetIdent, ModuleId>,
}

impl GlobalInformation {
pub fn get_module_id(&self, asset_ident: &AssetIdent) -> ModuleId {
self.module_id_map.get(asset_ident).cloned().expect(
"No module ID found for the given asset identifier. This is an internal Turbopack \
error. Please report it.",
)
}
}

#[turbo_tasks::value(transparent)]
pub struct OptionGlobalInformation(Option<GlobalInformation>);
1 change: 1 addition & 0 deletions crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod chunking_context;
pub(crate) mod containment_tree;
pub(crate) mod data;
pub(crate) mod evaluate;
pub mod global_information;
pub mod optimize;

use std::{
Expand Down
17 changes: 16 additions & 1 deletion crates/turbopack-nodejs/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use std::iter::once;

use anyhow::{bail, Context, Result};
use tracing::Instrument;
use turbo_tasks::{RcStr, Value, ValueToString, Vc};
use turbo_tasks::{debug::ValueDebug, RcStr, Value, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{
availability_info::AvailabilityInfo,
chunk_group::{make_chunk_group, MakeChunkGroupResult},
global_information::OptionGlobalInformation,
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingContext,
EntryChunkGroupResult, EvaluatableAssets, MinifyType, ModuleId,
},
Expand Down Expand Up @@ -84,6 +85,8 @@ pub struct NodeJsChunkingContext {
minify_type: MinifyType,
/// Whether to use manifest chunks for lazy compilation
manifest_chunks: bool,
/// Global information
global_information: Vc<OptionGlobalInformation>,
}

impl NodeJsChunkingContext {
Expand All @@ -96,6 +99,7 @@ impl NodeJsChunkingContext {
asset_root_path: Vc<FileSystemPath>,
environment: Vc<Environment>,
runtime_type: RuntimeType,
global_information: Vc<OptionGlobalInformation>,
) -> NodeJsChunkingContextBuilder {
NodeJsChunkingContextBuilder {
chunking_context: NodeJsChunkingContext {
Expand All @@ -109,6 +113,7 @@ impl NodeJsChunkingContext {
runtime_type,
minify_type: MinifyType::NoMinify,
manifest_chunks: false,
global_information,
},
}
}
Expand All @@ -131,6 +136,16 @@ impl NodeJsChunkingContext {

#[turbo_tasks::value_impl]
impl NodeJsChunkingContext {
#[turbo_tasks::function]
async fn chunk_item_id_from_ident(
self: Vc<Self>,
ident: Vc<AssetIdent>,
) -> Result<Vc<ModuleId>> {
let this = self.await?;
dbg!(this.global_information.dbg().await?);
Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell())
}

#[turbo_tasks::function]
fn new(this: Value<NodeJsChunkingContext>) -> Vc<Self> {
this.into_value().cell()
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-tests/tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ async fn run_test(prepared_test: Vc<PreparedTest>) -> Result<Vc<RunTestResult>>
static_root_path,
env,
RuntimeType::Development,
Vc::cell(None),
)
.build();

Expand Down
2 changes: 2 additions & 0 deletions crates/turbopack-tests/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ async fn run_test(resource: RcStr) -> Result<Vc<FileSystemPath>> {
static_root_path,
env,
options.runtime_type,
Vc::cell(None),
)
.build(),
),
Expand All @@ -331,6 +332,7 @@ async fn run_test(resource: RcStr) -> Result<Vc<FileSystemPath>> {
static_root_path,
env,
options.runtime_type,
Vc::cell(None),
)
.minify_type(options.minify_type)
.build(),
Expand Down

0 comments on commit 544f700

Please sign in to comment.