Skip to content

Commit

Permalink
Writing entrypoints to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Jul 13, 2023
1 parent 025e6dc commit c9893f3
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ turbopack-binding = { workspace = true, features = [
"__turbo_tasks_env",
"__turbo_tasks_fs",
"__turbopack",
"__turbopack_build",
"__turbopack_core",
"__turbopack_dev",
"__turbopack_env",
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod route;

pub fn register() {
next_core::register();
turbopack_binding::turbopack::build::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}
61 changes: 54 additions & 7 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use indexmap::IndexMap;
use next_core::{
create_page_loader_entry_module,
create_page_loader_entry_module, emit_all_assets, get_asset_path_from_pathname,
pages_structure::{
PagesDirectoryStructure, PagesDirectoryStructureVc, PagesStructure, PagesStructureItem,
PagesStructureVc,
},
};
use turbo_tasks::{primitives::StringVc, CompletionVc};
use turbo_tasks::{primitives::StringVc, CompletionVc, Value};
use turbopack_binding::{
turbo::tasks_fs::FileSystemPathVc,
turbopack::{
core::{
asset::AssetsVc,
chunk::{ChunkableModule, ChunkingContext},
context::AssetContext,
file_source::FileSourceVc,
reference_type::{EntryReferenceSubType, ReferenceType},
},
ecmascript::EcmascriptModuleAssetVc,
},
};

use crate::{
project::ProjectVc,
route::{Endpoint, EndpointVc, Route, RoutesVc, WrittenEndpointVc},
route::{Endpoint, EndpointVc, Route, RoutesVc, WrittenEndpoint, WrittenEndpointVc},
};

#[turbo_tasks::function]
Expand Down Expand Up @@ -120,9 +123,11 @@ impl PageHtmlEndpointVc {
impl Endpoint for PageHtmlEndpoint {
#[turbo_tasks::function]
async fn write_to_disk(&self) -> Result<WrittenEndpointVc> {
let source = FileSourceVc::new(self.path).into();

let client_module = create_page_loader_entry_module(
self.project.pages_client_module_context(),
FileSourceVc::new(self.path).into(),
source,
self.pathname,
);

Expand All @@ -142,9 +147,51 @@ impl Endpoint for PageHtmlEndpoint {
.with_entry(client_module.into()),
);

// TODO(alexkirsz) Needs to update the build manifest.
let reference_type = Value::new(ReferenceType::Entry(EntryReferenceSubType::Page));

todo!()
let ssr_module = self
.project
.pages_ssr_module_context()
.process(source, reference_type.clone());

let Some(ssr_module) = EcmascriptModuleAssetVc::resolve_from(ssr_module).await? else {
bail!("expected an ECMAScript module asset");
};

let asset_path = get_asset_path_from_pathname(&self.pathname.await?, ".js");

let ssr_entry_chunk_path_string = format!("server/pages/{asset_path}");
let ssr_entry_chunk_path = self.project.node_root().join(&ssr_entry_chunk_path_string);
let ssr_entry_chunk = self.project.ssr_chunking_context().entry_chunk(
ssr_entry_chunk_path,
ssr_module.into(),
self.project.pages_ssr_runtime_entries(),
);

let ssr_chunks = AssetsVc::cell(vec![ssr_entry_chunk]);

let ssr_emit = emit_all_assets(
ssr_chunks,
self.project.node_root(),
self.project.client_root().join("_next"),
self.project.node_root(),
);
let client_emit = emit_all_assets(
client_chunks,
self.project.node_root(),
self.project.client_root().join("_next"),
self.project.node_root(),
);

ssr_emit.await?;
client_emit.await?;

Ok(WrittenEndpoint {
server_entry_path: ssr_entry_chunk_path_string,
server_paths: vec![],
client_paths: vec![],
}
.cell())
}

#[turbo_tasks::function]
Expand Down
26 changes: 17 additions & 9 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ impl ProjectVc {
Ok(disk_fs.into())
}

#[turbo_tasks::function]
pub(super) fn node_root(self) -> FileSystemPathVc {
self.node_fs().root().join(".next")
}

#[turbo_tasks::function]
pub(super) fn client_root(self) -> FileSystemPathVc {
self.client_fs().root()
}

#[turbo_tasks::function]
fn project_root_path(self) -> FileSystemPathVc {
self.project_fs().root()
Expand Down Expand Up @@ -193,13 +203,13 @@ impl ProjectVc {
}

#[turbo_tasks::function]
async fn next_config(self) -> NextConfigVc {
todo!()
async fn next_config(self) -> Result<NextConfigVc> {
Ok(self.await?.next_config)
}

#[turbo_tasks::function]
fn execution_context(self) -> ExecutionContextVc {
let node_root = self.node_fs().root().join(".next");
let node_root = self.node_root();

let node_execution_chunking_context = DevChunkingContextVc::builder(
self.project_path(),
Expand Down Expand Up @@ -309,7 +319,7 @@ impl ProjectVc {
}

#[turbo_tasks::function]
fn pages_ssr_module_context(self) -> AssetContextVc {
pub(super) fn pages_ssr_module_context(self) -> AssetContextVc {
ModuleAssetContextVc::new(
self.pages_transitions(),
self.server_compile_time_info(),
Expand Down Expand Up @@ -364,7 +374,7 @@ impl ProjectVc {
}

#[turbo_tasks::function]
async fn pages_ssr_runtime_entries(self) -> Result<EvaluatableAssetsVc> {
pub(super) async fn pages_ssr_runtime_entries(self) -> Result<EvaluatableAssetsVc> {
let this = self.await?;
let ssr_runtime_entries = get_server_runtime_entries(
self.project_path(),
Expand All @@ -383,9 +393,7 @@ impl ProjectVc {
let this = self.await?;
Ok(get_client_chunking_context(
self.project_path(),
// TODO(alexkirsz) Is this correct? In Next build it's under .next but for dev it
// represents a pathname.
self.client_fs().root(),
self.client_root(),
self.client_compile_time_info().environment(),
this.mode,
))
Expand All @@ -395,7 +403,7 @@ impl ProjectVc {
pub(super) fn server_chunking_context(self) -> BuildChunkingContextVc {
get_server_chunking_context(
self.project_path(),
self.node_fs().root().join(".next"),
self.node_root(),
self.client_fs().root(),
self.server_compile_time_info().environment(),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait Endpoint {
fn changed(&self) -> CompletionVc;
}

#[turbo_tasks::value]
#[turbo_tasks::value(shared)]
#[derive(Debug)]
pub struct WrittenEndpoint {
/// Relative to the root_path
Expand Down
89 changes: 89 additions & 0 deletions packages/next-swc/crates/next-core/src/emit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use anyhow::Result;
use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal},
CompletionVc, CompletionsVc, TryJoinIterExt,
};
use turbo_tasks_fs::{rebase, FileSystemPathVc};
use turbopack_binding::turbopack::core::{
asset::{Asset, AssetVc, AssetsVc},
reference::AssetReference,
};

/// Emits all assets transitively reachable from the given chunks, that are
/// inside the node root or the client root.
#[turbo_tasks::function]
pub async fn emit_all_assets(
assets: AssetsVc,
node_root: FileSystemPathVc,
client_relative_path: FileSystemPathVc,
client_output_path: FileSystemPathVc,
) -> Result<CompletionVc> {
let all_assets = all_assets_from_entries(assets).await?;
Ok(CompletionsVc::all(
all_assets
.iter()
.copied()
.map(|asset| async move {
if asset.ident().path().await?.is_inside(&*node_root.await?) {
return Ok(emit(asset));
} else if asset
.ident()
.path()
.await?
.is_inside(&*client_relative_path.await?)
{
// Client assets are emitted to the client output path, which is prefixed with
// _next. We need to rebase them to remove that prefix.
return Ok(emit_rebase(asset, client_relative_path, client_output_path));
}

Ok(CompletionVc::immutable())
})
.try_join()
.await?,
))
}

#[turbo_tasks::function]
fn emit(asset: AssetVc) -> CompletionVc {
asset.content().write(asset.ident().path())
}

#[turbo_tasks::function]
fn emit_rebase(asset: AssetVc, from: FileSystemPathVc, to: FileSystemPathVc) -> CompletionVc {
asset
.content()
.write(rebase(asset.ident().path(), from, to))
}

/// Walks the asset graph from multiple assets and collect all referenced
/// assets.
#[turbo_tasks::function]
async fn all_assets_from_entries(entries: AssetsVc) -> Result<AssetsVc> {
Ok(AssetsVc::cell(
AdjacencyMap::new()
.skip_duplicates()
.visit(entries.await?.iter().copied(), get_referenced_assets)
.await
.completed()?
.into_inner()
.into_reverse_topological()
.collect(),
))
}

/// Computes the list of all chunk children of a given chunk.
async fn get_referenced_assets(asset: AssetVc) -> Result<impl Iterator<Item = AssetVc> + Send> {
Ok(asset
.references()
.await?
.iter()
.map(|reference| async move {
let primary_assets = reference.resolve_reference().primary_assets().await?;
Ok(primary_assets.clone_value())
})
.try_join()
.await?
.into_iter()
.flatten())
}
2 changes: 2 additions & 0 deletions packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod app_structure;
mod babel;
mod bootstrap;
mod embed_js;
mod emit;
pub mod env;
mod fallback;
pub mod loader_tree;
Expand Down Expand Up @@ -45,6 +46,7 @@ pub mod util;
mod web_entry_source;

pub use app_source::create_app_source;
pub use emit::emit_all_assets;
pub use next_app::unsupported_dynamic_metadata_issue::{
UnsupportedDynamicMetadataIssue, UnsupportedDynamicMetadataIssueVc,
};
Expand Down
7 changes: 1 addition & 6 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,7 @@ function bindingToApi(binding: any, wasm: boolean) {
}

async writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> {
const result = await binding.endpointWriteToDisk(this._nativeEndpoint)
return {
...result.result,
issues: result.issues,
diagnostics: result.diagnostics,
}
return await binding.endpointWriteToDisk(this._nativeEndpoint)
}

async changed(): Promise<AsyncIterableIterator<TurbopackResult>> {
Expand Down

0 comments on commit c9893f3

Please sign in to comment.