Skip to content

Commit

Permalink
Turbopack: Source trait (#52511)
Browse files Browse the repository at this point in the history
### What?

refactoring see vercel/turbo#5483

### Turbopack Changes

* vercel/turbo#5483 <!-- Tobias Koppers - Source
trait -->
  • Loading branch information
sokra committed Jul 11, 2023
1 parent 5fff528 commit 56fcd7a
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 253 deletions.
152 changes: 76 additions & 76 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ swc_core = { version = "0.79.13" }
testing = { version = "0.33.20" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230710.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230711.2" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230710.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230711.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230710.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230711.2" }

# General Deps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ use next_core::{
turbopack::core::{asset::AssetsVc, chunk::EvaluatableAssetsVc},
};
use turbopack_binding::{
turbo::{
tasks::{primitives::StringVc, Value},
tasks_fs::FileSystemPathVc,
},
turbo::{tasks::primitives::StringVc, tasks_fs::FileSystemPathVc},
turbopack::{
core::{
asset::AssetVc,
chunk::{ChunkableModule, ChunkingContext, ChunkingContextVc},
context::{AssetContext, AssetContextVc},
reference_type::ReferenceType,
source::SourceVc,
},
dev::DevChunkingContextVc,
ecmascript::EcmascriptModuleAssetVc,
Expand Down Expand Up @@ -61,20 +57,11 @@ impl PagesBuildClientContextVc {
}

#[turbo_tasks::function]
pub async fn client_chunk(
self,
asset: AssetVc,
pathname: StringVc,
reference_type: Value<ReferenceType>,
) -> Result<AssetsVc> {
pub async fn client_chunk(self, source: SourceVc, pathname: StringVc) -> Result<AssetsVc> {
let this = self.await?;

let client_asset_page = this.client_asset_context.process(asset, reference_type);
let client_asset_page = create_page_loader_entry_asset(
this.client_asset_context,
client_asset_page.into(),
pathname,
);
let client_asset_page =
create_page_loader_entry_asset(this.client_asset_context, source, pathname);

let Some(client_module_asset) =
EcmascriptModuleAssetVc::resolve_from(client_asset_page).await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use turbopack_binding::{
context::{AssetContext, AssetContextVc},
reference_type::{EntryReferenceSubType, ReferenceType},
resolve::{parse::RequestVc, pattern::QueryMapVc},
source::SourceVc,
},
ecmascript::chunk::EcmascriptChunkPlaceableVc,
},
Expand Down Expand Up @@ -91,15 +92,13 @@ impl PagesBuildNodeContextVc {
#[turbo_tasks::function]
pub async fn node_chunk(
self,
source_asset: AssetVc,
source: SourceVc,
original_path: StringVc,
reference_type: Value<ReferenceType>,
) -> Result<AssetVc> {
let this = self.await?;

let node_asset_page = this
.node_asset_context
.process(source_asset, reference_type);
let node_asset_page = this.node_asset_context.process(source, reference_type);

let Some(node_module_asset) =
EcmascriptChunkPlaceableVc::resolve_from(node_asset_page).await?
Expand Down
21 changes: 9 additions & 12 deletions packages/next-swc/crates/next-build/src/next_pages/page_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ use turbopack_binding::{
asset::AssetVc,
context::AssetContextVc,
environment::ServerAddrVc,
file_source::FileSourceVc,
reference_type::{EntryReferenceSubType, ReferenceType},
source_asset::SourceAssetVc,
source::SourceVc,
},
env::ProcessEnvAssetVc,
node::execution_context::ExecutionContextVc,
Expand Down Expand Up @@ -204,7 +205,7 @@ async fn get_page_chunks_for_root_directory(
chunks.push(get_page_chunk_for_file(
node_build_context,
client_build_context,
SourceAssetVc::new(app.project_path).into(),
FileSourceVc::new(app.project_path).into(),
next_router_root,
app.next_router_path,
app.original_path,
Expand All @@ -215,7 +216,7 @@ async fn get_page_chunks_for_root_directory(
chunks.push(get_page_chunk_for_file(
node_build_context,
client_build_context,
SourceAssetVc::new(document.project_path).into(),
FileSourceVc::new(document.project_path).into(),
next_router_root,
document.next_router_path,
document.original_path,
Expand All @@ -227,7 +228,7 @@ async fn get_page_chunks_for_root_directory(
chunks.push(get_page_chunk_for_file(
node_build_context,
client_build_context,
SourceAssetVc::new(error.project_path).into(),
FileSourceVc::new(error.project_path).into(),
next_router_root,
error.next_router_path,
error.original_path,
Expand Down Expand Up @@ -287,7 +288,7 @@ async fn get_page_chunks_for_directory(
chunks.push(get_page_chunk_for_file(
node_build_context,
client_build_context,
SourceAssetVc::new(project_path).into(),
FileSourceVc::new(project_path).into(),
next_router_root,
next_router_path,
original_path,
Expand Down Expand Up @@ -327,7 +328,7 @@ pub struct PageChunk {
async fn get_page_chunk_for_file(
node_build_context: PagesBuildNodeContextVc,
client_build_context: PagesBuildClientContextVc,
page_asset: AssetVc,
page_asset: SourceVc,
next_router_root: FileSystemPathVc,
next_router_path: FileSystemPathVc,
original_path: FileSystemPathVc,
Expand All @@ -339,12 +340,8 @@ async fn get_page_chunk_for_file(

Ok(PageChunk {
pathname,
node_chunk: node_build_context.node_chunk(
page_asset,
original_path,
reference_type.clone(),
),
client_chunks: client_build_context.client_chunk(page_asset, pathname, reference_type),
node_chunk: node_build_context.node_chunk(page_asset, original_path, reference_type),
client_chunks: client_build_context.client_chunk(page_asset, pathname),
}
.cell())
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-core/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"check": "tsc --noEmit"
},
"dependencies": {
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230710.2",
"@vercel/turbopack-node": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230710.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230711.2",
"@vercel/turbopack-node": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230711.2",
"anser": "^2.1.1",
"css.escape": "^1.5.1",
"next": "*",
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-core/src/app_segment_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use turbopack_binding::turbopack::{
core::{
asset::{Asset, AssetVc},
context::{AssetContext, AssetContextVc},
file_source::FileSourceVc,
ident::AssetIdentVc,
issue::{
Issue, IssueSeverity, IssueSeverityVc, IssueSourceVc, IssueVc, OptionIssueSourceVc,
},
module::ModuleVc,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
source_asset::SourceAssetVc,
},
ecmascript::{
analyzer::{graph::EvalContext, ConstantNumber, ConstantValue, JsValue},
Expand Down Expand Up @@ -392,7 +392,7 @@ pub async fn parse_segment_config_from_loader_tree(
{
config.apply_parent_config(
&*parse_segment_config_from_source(context.process(
SourceAssetVc::new(component).into(),
FileSourceVc::new(component).into(),
turbo_tasks::Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined,
)),
Expand Down
51 changes: 26 additions & 25 deletions packages/next-swc/crates/next-core/src/app_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ use turbopack_binding::{
},
turbopack::{
core::{
asset::{AssetVc, AssetsVc},
asset::AssetVc,
chunk::{EvaluatableAssetVc, EvaluatableAssetsVc},
compile_time_info::CompileTimeInfoVc,
context::AssetContext,
environment::ServerAddrVc,
file_source::FileSourceVc,
issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc},
reference_type::{
EcmaScriptModulesReferenceSubType, EntryReferenceSubType, InnerAssetsVc,
ReferenceType,
},
source_asset::SourceAssetVc,
virtual_asset::VirtualAssetVc,
source::SourcesVc,
virtual_source::VirtualSourceVc,
},
dev::DevChunkingContextVc,
dev_server::{
Expand All @@ -38,7 +39,7 @@ use turbopack_binding::{
},
ecmascript::{
magic_identifier,
text::TextContentSourceAssetVc,
text::TextContentFileSourceVc,
utils::{FormatIter, StringifyJs},
},
env::ProcessEnvAssetVc,
Expand Down Expand Up @@ -517,7 +518,7 @@ pub async fn create_app_source(
let env = CustomProcessEnvVc::new(env, next_config.env()).as_process_env();

let server_runtime_entries =
AssetsVc::cell(vec![
SourcesVc::cell(vec![
ProcessEnvAssetVc::new(project_path, injected_env).into()
]);

Expand Down Expand Up @@ -614,7 +615,7 @@ async fn create_global_metadata_source(
MetadataItem::Static { path } => {
let asset = FixedStaticAssetVc::new(
server_root.join(server_path),
SourceAssetVc::new(path).into(),
FileSourceVc::new(path).into(),
);
sources.push(AssetGraphContentSourceVc::new_eager(server_root, asset.into()).into())
}
Expand Down Expand Up @@ -646,7 +647,7 @@ async fn create_app_page_source_for_route(
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
server_root: FileSystemPathVc,
runtime_entries: AssetsVc,
runtime_entries: SourcesVc,
fallback_page: DevHtmlAssetVc,
intermediate_output_path_root: FileSystemPathVc,
render_data: JsonValueVc,
Expand Down Expand Up @@ -695,7 +696,7 @@ async fn create_app_not_found_page_source(
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
server_root: FileSystemPathVc,
runtime_entries: AssetsVc,
runtime_entries: SourcesVc,
fallback_page: DevHtmlAssetVc,
intermediate_output_path_root: FileSystemPathVc,
render_data: JsonValueVc,
Expand Down Expand Up @@ -740,7 +741,7 @@ async fn create_app_route_source_for_route(
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
server_root: FileSystemPathVc,
runtime_entries: AssetsVc,
runtime_entries: SourcesVc,
intermediate_output_path_root: FileSystemPathVc,
render_data: JsonValueVc,
) -> Result<ContentSourceVc> {
Expand Down Expand Up @@ -780,7 +781,7 @@ async fn create_app_route_source_for_route(
/// The renderer for pages in app directory
#[turbo_tasks::value]
struct AppRenderer {
runtime_entries: AssetsVc,
runtime_entries: SourcesVc,
app_dir: FileSystemPathVc,
context_ssr: ModuleAssetContextVc,
context: ModuleAssetContextVc,
Expand Down Expand Up @@ -876,7 +877,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
.context
.with_transition(state.rsc_transition)
.process(
SourceAssetVc::new(component).into(),
FileSourceVc::new(component).into(),
Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined,
)),
Expand Down Expand Up @@ -928,7 +929,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
state.inner_assets.insert(
inner_module_id,
StaticModuleAssetVc::new(
SourceAssetVc::new(path).into(),
FileSourceVc::new(path).into(),
state.context.into(),
)
.into(),
Expand Down Expand Up @@ -979,7 +980,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
state.inner_assets.insert(
inner_module_id,
StructuredImageModuleType::create_module(
SourceAssetVc::new(*path).into(),
FileSourceVc::new(*path).into(),
BlurPlaceholderMode::None,
state.context,
)
Expand Down Expand Up @@ -1008,8 +1009,8 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
state
.context
.process(
TextContentSourceAssetVc::new(
SourceAssetVc::new(*alt_path).into(),
TextContentFileSourceVc::new(
FileSourceVc::new(*alt_path).into(),
)
.into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::empty())),
Expand Down Expand Up @@ -1115,7 +1116,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
)?;

let file = File::from(result.build());
let asset = VirtualAssetVc::new(next_js_file_path("entry/app-entry.tsx"), file.into());
let asset = VirtualSourceVc::new(next_js_file_path("entry/app-entry.tsx"), file.into());

let chunking_context = DevChunkingContextVc::builder(
project_path,
Expand All @@ -1130,14 +1131,14 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";

let renderer_module = match runtime {
Some(NextRuntime::NodeJs) | None => context.process(
SourceAssetVc::new(next_js_file_path("entry/app-renderer.tsx")).into(),
FileSourceVc::new(next_js_file_path("entry/app-renderer.tsx")).into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::cell(indexmap! {
"APP_ENTRY".to_string() => context.with_transition(rsc_transition).process(
asset.into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::cell(inner_assets))),
).into(),
"APP_BOOTSTRAP".to_string() => context.with_transition("next-client").process(
SourceAssetVc::new(next_js_file_path("entry/app/hydrate.tsx")).into(),
FileSourceVc::new(next_js_file_path("entry/app/hydrate.tsx")).into(),
Value::new(ReferenceType::EcmaScriptModules(
EcmaScriptModulesReferenceSubType::Undefined,
)),
Expand All @@ -1146,7 +1147,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
),
Some(NextRuntime::Edge) =>
context.process(
SourceAssetVc::new(next_js_file_path("entry/app-edge-renderer.tsx")).into(),
FileSourceVc::new(next_js_file_path("entry/app-edge-renderer.tsx")).into(),
Value::new(ReferenceType::Internal(InnerAssetsVc::cell(indexmap! {
"INNER_EDGE_CHUNK_GROUP".to_string() => context.with_transition("next-edge-page").process(
asset.into(),
Expand All @@ -1165,7 +1166,7 @@ import {}, {{ chunks as {} }} from "COMPONENT_{}";
runtime_entries
.await?
.iter()
.map(|entry| EvaluatableAssetVc::from_asset(*entry, context.into()))
.map(|entry| EvaluatableAssetVc::from_source(*entry, context.into()))
.collect(),
),
module,
Expand Down Expand Up @@ -1196,7 +1197,7 @@ impl NodeEntry for AppRenderer {
/// The node.js renderer api routes in the app directory
#[turbo_tasks::value]
struct AppRoute {
runtime_entries: AssetsVc,
runtime_entries: SourcesVc,
context: ModuleAssetContextVc,
entry_path: FileSystemPathVc,
intermediate_output_path: FileSystemPathVc,
Expand All @@ -1223,9 +1224,9 @@ impl AppRouteVc {
.reference_chunk_source_maps(should_debug("app_source"))
.build();

let entry_source_asset = SourceAssetVc::new(this.entry_path);
let entry_file_source = FileSourceVc::new(this.entry_path);
let entry_asset = this.context.process(
entry_source_asset.into(),
entry_file_source.into(),
Value::new(ReferenceType::Entry(EntryReferenceSubType::AppRoute)),
);

Expand All @@ -1246,7 +1247,7 @@ impl AppRouteVc {
let internal_asset = next_asset("entry/app/edge-route.ts");

let entry = this.context.with_transition("next-edge-route").process(
entry_source_asset.into(),
entry_file_source.into(),
Value::new(ReferenceType::Entry(EntryReferenceSubType::AppRoute)),
);

Expand All @@ -1270,7 +1271,7 @@ impl AppRouteVc {
this.runtime_entries
.await?
.iter()
.map(|entry| EvaluatableAssetVc::from_asset(*entry, this.context.into()))
.map(|entry| EvaluatableAssetVc::from_source(*entry, this.context.into()))
.collect(),
),
module,
Expand Down

0 comments on commit 56fcd7a

Please sign in to comment.