Skip to content

Commit

Permalink
Merge a03105d into 0bb91d3
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz authored Jul 11, 2023
2 parents 0bb91d3 + a03105d commit 3ce691e
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 50 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
3 changes: 1 addition & 2 deletions crates/turbopack-build/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl BuildChunkingContextVc {
/// * exports the result of evaluating the given module as a CommonJS
/// default export.
#[turbo_tasks::function]
pub async fn generate_entry_chunk(
pub async fn entry_chunk(
self_vc: BuildChunkingContextVc,
path: FileSystemPathVc,
module: EcmascriptChunkPlaceableVc,
Expand All @@ -129,7 +129,6 @@ impl BuildChunkingContextVc {
let asset = EcmascriptBuildNodeEntryChunkVc::new(
path,
self_vc,
entry_chunk,
AssetsVc::cell(other_chunks),
evaluatable_assets,
module,
Expand Down
5 changes: 1 addition & 4 deletions crates/turbopack-build/src/ecmascript/node/entry/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use turbo_tasks::{primitives::StringVc, ValueToString, ValueToStringVc};
use turbo_tasks_fs::{File, FileSystemPathVc};
use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc, AssetsVc},
chunk::{ChunkVc, ChunkingContext, EvaluatableAssetsVc},
chunk::{ChunkingContext, EvaluatableAssetsVc},
code_builder::{CodeBuilder, CodeVc},
ident::AssetIdentVc,
reference::{AssetReferencesVc, SingleAssetReferenceVc},
Expand All @@ -28,7 +28,6 @@ use crate::BuildChunkingContextVc;
pub(crate) struct EcmascriptBuildNodeEntryChunk {
path: FileSystemPathVc,
chunking_context: BuildChunkingContextVc,
entry_chunk: ChunkVc,
other_chunks: AssetsVc,
evaluatable_assets: EvaluatableAssetsVc,
exported_module: EcmascriptChunkPlaceableVc,
Expand All @@ -41,15 +40,13 @@ impl EcmascriptBuildNodeEntryChunkVc {
pub fn new(
path: FileSystemPathVc,
chunking_context: BuildChunkingContextVc,
entry_chunk: ChunkVc,
other_chunks: AssetsVc,
evaluatable_assets: EvaluatableAssetsVc,
exported_module: EcmascriptChunkPlaceableVc,
) -> Self {
EcmascriptBuildNodeEntryChunk {
path,
chunking_context,
entry_chunk,
other_chunks,
evaluatable_assets,
exported_module,
Expand Down
3 changes: 2 additions & 1 deletion crates/turbopack-cli/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ async fn source(
)
.build();

let execution_context = ExecutionContextVc::new(project_path, build_chunking_context, env);
let execution_context =
ExecutionContextVc::new(project_path, build_chunking_context.into(), env);

let server_fs = ServerFileSystemVc::new().as_file_system();
let server_root = server_fs.root();
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ pub fn get_client_chunking_context(
)
.hot_module_replacement()
.build()
.into()
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -307,9 +308,9 @@ pub async fn create_web_entry_source(
let entry_asset = DevHtmlAssetVc::new(server_root.join("index.html"), entries).into();

let graph = if eager_compile {
AssetGraphContentSourceVc::new_eager(server_root, entry_asset)
AssetGraphContentSourceVc::new_eager(server_root, Default::default(), entry_asset)
} else {
AssetGraphContentSourceVc::new_lazy(server_root, entry_asset)
AssetGraphContentSourceVc::new_lazy(server_root, Default::default(), entry_asset)
}
.into();
Ok(graph)
Expand Down
9 changes: 5 additions & 4 deletions crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ use turbopack_core::{
asset::{Asset, AssetContentVc, AssetVc, AssetsVc},
chunk::{
availability_info::AvailabilityInfo, chunk_content, chunk_content_split, Chunk,
ChunkContentResult, ChunkItem, ChunkItemVc, ChunkVc, ChunkableModuleVc, ChunkingContext,
ChunkingContextVc, ChunksVc, FromChunkableModule, ModuleId, ModuleIdVc, ModuleIdsVc,
OutputChunk, OutputChunkRuntimeInfo, OutputChunkRuntimeInfoVc, OutputChunkVc,
ChunkContentResult, ChunkItem, ChunkItemVc, ChunkVc, ChunkableModule, ChunkableModuleVc,
ChunkingContext, ChunkingContextVc, ChunksVc, FromChunkableModule, ModuleId, ModuleIdVc,
ModuleIdsVc, OutputChunk, OutputChunkRuntimeInfo, OutputChunkRuntimeInfoVc, OutputChunkVc,
},
code_builder::{CodeBuilder, CodeVc},
ident::{AssetIdent, AssetIdentVc},
introspect::{
asset::{children_from_asset_references, content_to_details, IntrospectableAssetVc},
Introspectable, IntrospectableChildrenVc, IntrospectableVc,
},
module::{Module, ModuleVc},
reference::{AssetReference, AssetReferenceVc, AssetReferencesVc},
resolve::PrimaryResolveResult,
source_map::{GenerateSourceMap, GenerateSourceMapVc, OptionSourceMapVc},
Expand Down Expand Up @@ -457,7 +458,7 @@ impl CssChunkContextVc {
}

#[turbo_tasks::value_trait]
pub trait CssChunkPlaceable: Asset {
pub trait CssChunkPlaceable: ChunkableModule + Module + Asset {
fn as_chunk_item(&self, context: ChunkingContextVc) -> CssChunkItemVc;
}

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
20 changes: 17 additions & 3 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,6 +41,11 @@ impl DevChunkingContextBuilder {
self
}

pub fn chunk_base_path(mut self, chunk_base_path: OptionStringVc) -> Self {
self.context.chunk_base_path = chunk_base_path;
self
}

pub fn layer(mut self, layer: &str) -> Self {
self.context.layer = (!layer.is_empty()).then(|| layer.to_string());
self
Expand All @@ -61,8 +66,8 @@ impl DevChunkingContextBuilder {
self
}

pub fn build(self) -> ChunkingContextVc {
DevChunkingContextVc::new(Value::new(self.context)).into()
pub fn build(self) -> DevChunkingContextVc {
DevChunkingContextVc::new(Value::new(self.context))
}
}

Expand All @@ -87,6 +92,9 @@ pub struct DevChunkingContext {
reference_css_chunk_source_maps: bool,
/// Static assets are placed at this path
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: OptionStringVc,
/// Layer name within this context
layer: Option<String>,
/// Enable HMR for this chunking
Expand All @@ -113,6 +121,7 @@ impl DevChunkingContextVc {
reference_chunk_source_maps: true,
reference_css_chunk_source_maps: true,
asset_root_path,
chunk_base_path: Default::default(),
layer: None,
enable_hot_module_replacement: false,
environment,
Expand All @@ -130,6 +139,11 @@ impl DevChunkingContext {
pub fn runtime_type(&self) -> RuntimeType {
self.runtime_type
}

/// Returns the chunk public path.
pub fn chunk_base_path(&self) -> OptionStringVc {
self.chunk_base_path
}
}

#[turbo_tasks::value_impl]
Expand Down
8 changes: 6 additions & 2 deletions crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl EcmascriptDevEvaluateChunkVc {
#[turbo_tasks::function]
async fn code(self) -> Result<CodeVc> {
let this = self.await?;
let chunking_context = this.chunking_context.await?;
let environment = this.chunking_context.environment();

let output_root = this.chunking_context.output_root().await?;
Expand Down Expand Up @@ -137,9 +138,12 @@ impl EcmascriptDevEvaluateChunkVc {
StringifyJs(&params),
)?;

match this.chunking_context.await?.runtime_type() {
match chunking_context.runtime_type() {
RuntimeType::Default => {
let runtime_code = turbopack_ecmascript_runtime::get_dev_runtime_code(environment);
let runtime_code = turbopack_ecmascript_runtime::get_dev_runtime_code(
environment,
chunking_context.chunk_base_path(),
);
code.push_code(&*runtime_code.await?);
}
#[cfg(feature = "test")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ pub struct ClientDirectiveTransformer {
}

impl ClientDirectiveTransformer {
pub fn new(transition_name: &StringVc) -> Self {
Self {
transition_name: *transition_name,
}
pub fn new(transition_name: StringVc) -> Self {
Self { transition_name }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ChunkUpdateProvider = {
push: (registration: [ChunkPath, UpdateCallback]) => void;
};

declare var CHUNK_BASE_PATH: string;
declare var TURBOPACK: ChunkRegistry | ChunkRegistration[] | undefined;
declare var TURBOPACK_CHUNK_LISTS: ChunkListProvider | ChunkList[] | undefined;
declare var TURBOPACK_CHUNK_UPDATE_LISTENERS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function commonJsRequireContext(
if (chunkPath.endsWith(".css")) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = `/${chunkPath}`;
link.href = `/${CHUNK_BASE_PATH}${chunkPath}`;
link.onerror = () => {
resolver.reject();
};
Expand All @@ -209,7 +209,7 @@ function commonJsRequireContext(
document.body.appendChild(link);
} else if (chunkPath.endsWith(".js")) {
const script = document.createElement("script");
script.src = `/${chunkPath}`;
script.src = `/${CHUNK_BASE_PATH}${chunkPath}`;
// We'll only mark the chunk as loaded once the script has been executed,
// which happens in `registerChunk`. Hence the absence of `resolve()` in
// this branch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function dynamicExport(module: Module, object: Record<string, any>) {
let reexportedObjects = module[REEXPORTED_OBJECTS];
if (!reexportedObjects) {
reexportedObjects = module[REEXPORTED_OBJECTS] = [];
module.namespaceObject = new Proxy(module.exports, {
module.exports = module.namespaceObject = new Proxy(module.exports, {
get(target, prop) {
if (
hasOwnProperty.call(target, prop) ||
Expand Down
Loading

0 comments on commit 3ce691e

Please sign in to comment.