Skip to content

Commit

Permalink
Add base path option to AssetGraphContentSourceVc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Jul 11, 2023
1 parent 9ccf7ed commit 637bf50
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
14 changes: 14 additions & 0 deletions crates/turbo-tasks/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ impl ValueToString for U64 {
#[turbo_tasks::value(transparent)]
pub struct OptionString(Option<std::string::String>);

#[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<std::string::String>);

Expand Down
41 changes: 35 additions & 6 deletions crates/turbopack-dev-server/src/source/asset_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -28,6 +31,7 @@ struct AssetsMap(HashMap<String, AssetVc>);
#[turbo_tasks::value(serialization = "none", eq = "manual", cell = "new")]
pub struct AssetGraphContentSource {
root_path: FileSystemPathVc,
base_path: OptionStringVc,
root_assets: AssetsSetVc,
expanded: Option<State<HashSet<AssetVc>>>,
}
Expand All @@ -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,
})
Expand All @@ -47,19 +56,29 @@ 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())),
})
}

/// 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,
})
Expand All @@ -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())),
})
Expand Down Expand Up @@ -166,12 +190,17 @@ struct UnresolvedAsset(AssetVc);
impl ContentSource for AssetGraphContentSource {
#[turbo_tasks::function]
async fn get_routes(self_vc: AssetGraphContentSourceVc) -> Result<RouteTreeVc> {
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,
Expand Down
15 changes: 7 additions & 8 deletions crates/turbopack-dev/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<String>,
chunk_base_path: OptionStringVc,
/// Layer name within this context
layer: Option<String>,
/// Enable HMR for this chunking
Expand All @@ -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,
Expand All @@ -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
}
}

Expand Down
7 changes: 2 additions & 5 deletions crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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?);
}
Expand Down
13 changes: 9 additions & 4 deletions crates/turbopack-node/src/render/rendered_source.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,6 +51,7 @@ pub fn create_node_rendered_source(
base_segments: Vec<BaseSegment>,
route_type: RouteType,
server_root: FileSystemPathVc,
base_path: OptionStringVc,
route_match: RouteMatcherVc,
pathname: StringVc,
entry: NodeEntryVc,
Expand All @@ -64,6 +65,7 @@ pub fn create_node_rendered_source(
base_segments,
route_type,
server_root,
base_path,
route_match,
pathname,
entry,
Expand Down Expand Up @@ -91,6 +93,7 @@ pub struct NodeRenderContentSource {
base_segments: Vec<BaseSegment>,
route_type: RouteType,
server_root: FileSystemPathVc,
base_path: OptionStringVc,
route_match: RouteMatcherVc,
pathname: StringVc,
entry: NodeEntryVc,
Expand Down Expand Up @@ -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())
}
}

Expand Down

0 comments on commit 637bf50

Please sign in to comment.