From 637bf5079908f8b7b2dfeb3cd474bc85f98695c4 Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Tue, 11 Jul 2023 13:48:26 +0200 Subject: [PATCH] Add base path option to AssetGraphContentSourceVc --- crates/turbo-tasks/src/primitives.rs | 14 +++++++ .../src/source/asset_graph.rs | 41 ++++++++++++++++--- crates/turbopack-dev/src/chunking_context.rs | 15 ++++--- .../src/ecmascript/evaluate/chunk.rs | 7 +--- .../src/render/rendered_source.rs | 13 ++++-- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/crates/turbo-tasks/src/primitives.rs b/crates/turbo-tasks/src/primitives.rs index d631b90b17e7c9..19f8b53d72c493 100644 --- a/crates/turbo-tasks/src/primitives.rs +++ b/crates/turbo-tasks/src/primitives.rs @@ -37,6 +37,20 @@ impl ValueToString for U64 { #[turbo_tasks::value(transparent)] pub struct OptionString(Option); +#[turbo_tasks::value_impl] +impl OptionStringVc { + #[turbo_tasks::function] + pub fn none() -> Self { + Self::cell(None) + } +} + +impl Default for OptionStringVc { + fn default() -> Self { + Self::none() + } +} + #[turbo_tasks::value(transparent)] pub struct Strings(Vec); diff --git a/crates/turbopack-dev-server/src/source/asset_graph.rs b/crates/turbopack-dev-server/src/source/asset_graph.rs index 298975d53617e7..e2f2be13cc29c5 100644 --- a/crates/turbopack-dev-server/src/source/asset_graph.rs +++ b/crates/turbopack-dev-server/src/source/asset_graph.rs @@ -5,7 +5,10 @@ use std::{ use anyhow::Result; use indexmap::{indexset, IndexSet}; -use turbo_tasks::{primitives::StringVc, CompletionVc, State, Value, ValueToString}; +use turbo_tasks::{ + primitives::{OptionStringVc, StringVc}, + CompletionVc, State, Value, ValueToString, +}; use turbo_tasks_fs::{FileSystemPath, FileSystemPathVc}; use turbopack_core::{ asset::{Asset, AssetVc, AssetsSetVc}, @@ -28,6 +31,7 @@ struct AssetsMap(HashMap); #[turbo_tasks::value(serialization = "none", eq = "manual", cell = "new")] pub struct AssetGraphContentSource { root_path: FileSystemPathVc, + base_path: OptionStringVc, root_assets: AssetsSetVc, expanded: Option>>, } @@ -36,9 +40,14 @@ pub struct AssetGraphContentSource { impl AssetGraphContentSourceVc { /// Serves all assets references by root_asset. #[turbo_tasks::function] - pub fn new_eager(root_path: FileSystemPathVc, root_asset: AssetVc) -> Self { + pub fn new_eager( + root_path: FileSystemPathVc, + base_path: OptionStringVc, + root_asset: AssetVc, + ) -> Self { Self::cell(AssetGraphContentSource { root_path, + base_path, root_assets: AssetsSetVc::cell(indexset! { root_asset }), expanded: None, }) @@ -47,9 +56,14 @@ impl AssetGraphContentSourceVc { /// Serves all assets references by root_asset. Only serve references of an /// asset when it has served its content before. #[turbo_tasks::function] - pub fn new_lazy(root_path: FileSystemPathVc, root_asset: AssetVc) -> Self { + pub fn new_lazy( + root_path: FileSystemPathVc, + base_path: OptionStringVc, + root_asset: AssetVc, + ) -> Self { Self::cell(AssetGraphContentSource { root_path, + base_path, root_assets: AssetsSetVc::cell(indexset! { root_asset }), expanded: Some(State::new(HashSet::new())), }) @@ -57,9 +71,14 @@ impl AssetGraphContentSourceVc { /// Serves all assets references by all root_assets. #[turbo_tasks::function] - pub fn new_eager_multiple(root_path: FileSystemPathVc, root_assets: AssetsSetVc) -> Self { + pub fn new_eager_multiple( + root_path: FileSystemPathVc, + base_path: OptionStringVc, + root_assets: AssetsSetVc, + ) -> Self { Self::cell(AssetGraphContentSource { root_path, + base_path, root_assets, expanded: None, }) @@ -68,9 +87,14 @@ impl AssetGraphContentSourceVc { /// Serves all assets references by all root_assets. Only serve references /// of an asset when it has served its content before. #[turbo_tasks::function] - pub fn new_lazy_multiple(root_path: FileSystemPathVc, root_assets: AssetsSetVc) -> Self { + pub fn new_lazy_multiple( + root_path: FileSystemPathVc, + base_path: OptionStringVc, + root_assets: AssetsSetVc, + ) -> Self { Self::cell(AssetGraphContentSource { root_path, + base_path, root_assets, expanded: Some(State::new(HashSet::new())), }) @@ -166,12 +190,17 @@ struct UnresolvedAsset(AssetVc); impl ContentSource for AssetGraphContentSource { #[turbo_tasks::function] async fn get_routes(self_vc: AssetGraphContentSourceVc) -> Result { + let base_path = self_vc.await?.base_path.await?; let assets = self_vc.all_assets_map().strongly_consistent().await?; let routes = assets .iter() .map(|(path, asset)| { RouteTreeVc::new_route( - BaseSegment::from_static_pathname(path).collect(), + if let Some(base_path) = &*base_path { + BaseSegment::from_static_pathname(&format!("{base_path}{path}")).collect() + } else { + BaseSegment::from_static_pathname(path).collect() + }, RouteType::Exact, AssetGraphGetContentSourceContentVc::new( self_vc, diff --git a/crates/turbopack-dev/src/chunking_context.rs b/crates/turbopack-dev/src/chunking_context.rs index b087f5fea4ef8c..5203bc4dc3b232 100644 --- a/crates/turbopack-dev/src/chunking_context.rs +++ b/crates/turbopack-dev/src/chunking_context.rs @@ -2,7 +2,7 @@ use anyhow::Result; use indexmap::IndexSet; use turbo_tasks::{ graph::{AdjacencyMap, GraphTraversal}, - primitives::{BoolVc, StringVc}, + primitives::{BoolVc, OptionStringVc, StringVc}, TryJoinIterExt, Value, }; use turbo_tasks_fs::FileSystemPathVc; @@ -41,9 +41,8 @@ impl DevChunkingContextBuilder { self } - pub fn chunk_base_path(mut self, chunk_base_path: &str) -> Self { - self.context.chunk_base_path = - (!chunk_base_path.is_empty()).then(|| chunk_base_path.to_string()); + pub fn chunk_base_path(mut self, chunk_base_path: OptionStringVc) -> Self { + self.context.chunk_base_path = chunk_base_path; self } @@ -95,7 +94,7 @@ pub struct DevChunkingContext { asset_root_path: FileSystemPathVc, /// Base path that will be prepended to all chunk URLs when loading them. /// This path will not appear in chunk paths or chunk data. - chunk_base_path: Option, + chunk_base_path: OptionStringVc, /// Layer name within this context layer: Option, /// Enable HMR for this chunking @@ -122,7 +121,7 @@ impl DevChunkingContextVc { reference_chunk_source_maps: true, reference_css_chunk_source_maps: true, asset_root_path, - chunk_base_path: None, + chunk_base_path: Default::default(), layer: None, enable_hot_module_replacement: false, environment, @@ -142,8 +141,8 @@ impl DevChunkingContext { } /// Returns the chunk public path. - pub fn chunk_base_path(&self) -> Option<&String> { - self.chunk_base_path.as_ref() + pub fn chunk_base_path(&self) -> OptionStringVc { + self.chunk_base_path } } diff --git a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs index 3e2fad515ca755..3ba53f403a5339 100644 --- a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs @@ -3,10 +3,7 @@ use std::io::Write; use anyhow::{bail, Result}; use indoc::writedoc; use serde::Serialize; -use turbo_tasks::{ - primitives::{OptionStringVc, StringVc}, - TryJoinIterExt, Value, ValueToString, ValueToStringVc, -}; +use turbo_tasks::{primitives::StringVc, TryJoinIterExt, Value, ValueToString, ValueToStringVc}; use turbo_tasks_fs::File; use turbopack_core::{ asset::{Asset, AssetContentVc, AssetVc, AssetsVc}, @@ -145,7 +142,7 @@ impl EcmascriptDevEvaluateChunkVc { RuntimeType::Default => { let runtime_code = turbopack_ecmascript_runtime::get_dev_runtime_code( environment, - OptionStringVc::cell(chunking_context.chunk_base_path().cloned()), + chunking_context.chunk_base_path(), ); code.push_code(&*runtime_code.await?); } diff --git a/crates/turbopack-node/src/render/rendered_source.rs b/crates/turbopack-node/src/render/rendered_source.rs index 7dba3925a14b04..2a6d621719a9c5 100644 --- a/crates/turbopack-node/src/render/rendered_source.rs +++ b/crates/turbopack-node/src/render/rendered_source.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use indexmap::IndexSet; use turbo_tasks::{ - primitives::{JsonValueVc, StringVc}, + primitives::{JsonValueVc, OptionStringVc, StringVc}, Value, }; use turbo_tasks_env::ProcessEnvVc; @@ -51,6 +51,7 @@ pub fn create_node_rendered_source( base_segments: Vec, route_type: RouteType, server_root: FileSystemPathVc, + base_path: OptionStringVc, route_match: RouteMatcherVc, pathname: StringVc, entry: NodeEntryVc, @@ -64,6 +65,7 @@ pub fn create_node_rendered_source( base_segments, route_type, server_root, + base_path, route_match, pathname, entry, @@ -91,6 +93,7 @@ pub struct NodeRenderContentSource { base_segments: Vec, route_type: RouteType, server_root: FileSystemPathVc, + base_path: OptionStringVc, route_match: RouteMatcherVc, pathname: StringVc, entry: NodeEntryVc, @@ -145,10 +148,12 @@ impl GetContentSource for NodeRenderContentSource { .copied(), ) } - Ok( - AssetGraphContentSourceVc::new_lazy_multiple(self.server_root, AssetsSetVc::cell(set)) - .into(), + Ok(AssetGraphContentSourceVc::new_lazy_multiple( + self.server_root, + self.base_path, + AssetsSetVc::cell(set), ) + .into()) } }