Unverified Commit 86d7b85d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: Next.rs API improvements (#52856)

### What?

* adds `Project.update` to update project options
* fix manifest paths to be under `server`
* pass `env` into project
* handle and expose issues in all methods
* expose server paths in WrittenEndpoint
parent 31453fa2
...@@ -5,14 +5,12 @@ use next_api::route::{Endpoint, WrittenEndpoint}; ...@@ -5,14 +5,12 @@ use next_api::route::{Endpoint, WrittenEndpoint};
use turbo_tasks::Vc; use turbo_tasks::Vc;
use turbopack_binding::turbopack::core::error::PrettyPrintError; use turbopack_binding::turbopack::core::error::PrettyPrintError;
use super::utils::{subscribe, NapiDiagnostic, NapiIssue, RootTask, VcArc}; use super::utils::{get_issues, subscribe, NapiIssue, RootTask, TurbopackResult, VcArc};
#[napi(object)] #[napi(object)]
pub struct NapiWrittenEndpoint { pub struct NapiWrittenEndpoint {
pub server_entry_path: String, pub server_entry_path: String,
pub server_paths: Vec<String>, pub server_paths: Vec<String>,
pub issues: Vec<NapiIssue>,
pub diagnostics: Vec<NapiDiagnostic>,
} }
impl From<&WrittenEndpoint> for NapiWrittenEndpoint { impl From<&WrittenEndpoint> for NapiWrittenEndpoint {
...@@ -20,8 +18,6 @@ impl From<&WrittenEndpoint> for NapiWrittenEndpoint { ...@@ -20,8 +18,6 @@ impl From<&WrittenEndpoint> for NapiWrittenEndpoint {
Self { Self {
server_entry_path: written_endpoint.server_entry_path.clone(), server_entry_path: written_endpoint.server_entry_path.clone(),
server_paths: written_endpoint.server_paths.clone(), server_paths: written_endpoint.server_paths.clone(),
issues: vec![],
diagnostics: vec![],
} }
} }
} }
...@@ -45,15 +41,24 @@ impl Deref for ExternalEndpoint { ...@@ -45,15 +41,24 @@ impl Deref for ExternalEndpoint {
#[napi] #[napi]
pub async fn endpoint_write_to_disk( pub async fn endpoint_write_to_disk(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, #[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
) -> napi::Result<NapiWrittenEndpoint> { ) -> napi::Result<TurbopackResult<NapiWrittenEndpoint>> {
let turbo_tasks = endpoint.turbo_tasks().clone(); let turbo_tasks = endpoint.turbo_tasks().clone();
let endpoint = ***endpoint; let endpoint = ***endpoint;
let written = turbo_tasks let (written, issues) = turbo_tasks
.run_once(async move { endpoint.write_to_disk().strongly_consistent().await }) .run_once(async move {
let write_to_disk = endpoint.write_to_disk();
let issues = get_issues(write_to_disk).await?;
let written = write_to_disk.strongly_consistent().await?;
Ok((written, issues))
})
.await .await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?; .map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
// TODO peek_issues and diagnostics // TODO diagnostics
Ok((&*written).into()) Ok(TurbopackResult {
result: NapiWrittenEndpoint::from(&*written),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: vec![],
})
} }
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
...@@ -69,10 +74,19 @@ pub fn endpoint_changed_subscribe( ...@@ -69,10 +74,19 @@ pub fn endpoint_changed_subscribe(
turbo_tasks, turbo_tasks,
func, func,
move || async move { move || async move {
endpoint.changed().await?; let changed = endpoint.changed();
// TODO peek_issues and diagnostics let issues = get_issues(changed).await?;
Ok(()) changed.await?;
// TODO diagnostics
Ok(issues)
},
|ctx| {
let issues = ctx.value;
Ok(vec![TurbopackResult {
result: (),
issues: issues.iter().map(|i| NapiIssue::from(&**i)).collect(),
diagnostics: vec![],
}])
}, },
|_ctx| Ok(vec![()]),
) )
} }
...@@ -3,7 +3,7 @@ use std::sync::Arc; ...@@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use napi::{bindgen_prelude::External, JsFunction}; use napi::{bindgen_prelude::External, JsFunction};
use next_api::{ use next_api::{
project::{Middleware, Project, ProjectOptions}, project::{Middleware, ProjectContainer, ProjectOptions},
route::{Endpoint, Route}, route::{Endpoint, Route},
}; };
use turbo_tasks::{TurboTasks, Vc}; use turbo_tasks::{TurboTasks, Vc};
...@@ -13,10 +13,18 @@ use turbopack_binding::{ ...@@ -13,10 +13,18 @@ use turbopack_binding::{
use super::{ use super::{
endpoint::ExternalEndpoint, endpoint::ExternalEndpoint,
utils::{serde_enum_to_string, subscribe, NapiDiagnostic, NapiIssue, RootTask, VcArc}, utils::{
get_issues, serde_enum_to_string, subscribe, NapiDiagnostic, NapiIssue, RootTask, VcArc,
},
}; };
use crate::register; use crate::register;
#[napi(object)]
pub struct NapiEnvVar {
pub name: String,
pub value: String,
}
#[napi(object)] #[napi(object)]
pub struct NapiProjectOptions { pub struct NapiProjectOptions {
/// A root path from which all files must be nested under. Trying to access /// A root path from which all files must be nested under. Trying to access
...@@ -32,6 +40,12 @@ pub struct NapiProjectOptions { ...@@ -32,6 +40,12 @@ pub struct NapiProjectOptions {
/// The contents of next.config.js, serialized to JSON. /// The contents of next.config.js, serialized to JSON.
pub next_config: String, pub next_config: String,
/// A map of environment variables to use when compiling code.
pub env: Vec<NapiEnvVar>,
}
#[napi(object)]
pub struct NapiTurboEngineOptions {
/// An upper bound of memory that turbopack will attempt to stay under. /// An upper bound of memory that turbopack will attempt to stay under.
pub memory_limit: Option<f64>, pub memory_limit: Option<f64>,
} }
...@@ -43,7 +57,11 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -43,7 +57,11 @@ impl From<NapiProjectOptions> for ProjectOptions {
project_path: val.project_path, project_path: val.project_path,
watch: val.watch, watch: val.watch,
next_config: val.next_config, next_config: val.next_config,
memory_limit: val.memory_limit.map(|m| m as _), env: val
.env
.into_iter()
.map(|NapiEnvVar { name, value }| (name, value))
.collect(),
} }
} }
} }
...@@ -51,17 +69,22 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -51,17 +69,22 @@ impl From<NapiProjectOptions> for ProjectOptions {
#[napi(ts_return_type = "{ __napiType: \"Project\" }")] #[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_new( pub async fn project_new(
options: NapiProjectOptions, options: NapiProjectOptions,
) -> napi::Result<External<VcArc<Vc<Project>>>> { turbo_engine_options: NapiTurboEngineOptions,
) -> napi::Result<External<VcArc<Vc<ProjectContainer>>>> {
register(); register();
let turbo_tasks = TurboTasks::new(MemoryBackend::new( let turbo_tasks = TurboTasks::new(MemoryBackend::new(
options turbo_engine_options
.memory_limit .memory_limit
.map(|m| m as usize) .map(|m| m as usize)
.unwrap_or(usize::MAX), .unwrap_or(usize::MAX),
)); ));
let options = options.into(); let options = options.into();
let project = turbo_tasks let project = turbo_tasks
.run_once(async move { Project::new(options).resolve().await }) .run_once(async move {
let project = ProjectContainer::new(options);
let project = project.resolve().await?;
Ok(project)
})
.await .await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?; .map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(External::new_with_size_hint( Ok(External::new_with_size_hint(
...@@ -70,6 +93,26 @@ pub async fn project_new( ...@@ -70,6 +93,26 @@ pub async fn project_new(
)) ))
} }
#[napi(ts_return_type = "{ __napiType: \"Project\" }")]
pub async fn project_update(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<
VcArc<Vc<ProjectContainer>>,
>,
options: NapiProjectOptions,
) -> napi::Result<()> {
let turbo_tasks = project.turbo_tasks().clone();
let options = options.into();
let project = **project;
turbo_tasks
.run_once(async move {
project.update(options).await?;
Ok(())
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(())
}
#[napi(object)] #[napi(object)]
#[derive(Default)] #[derive(Default)]
struct NapiRoute { struct NapiRoute {
...@@ -170,7 +213,9 @@ struct NapiEntrypoints { ...@@ -170,7 +213,9 @@ struct NapiEntrypoints {
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_entrypoints_subscribe( pub fn project_entrypoints_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<VcArc<Vc<Project>>>, #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<
VcArc<Vc<ProjectContainer>>,
>,
func: JsFunction, func: JsFunction,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
let turbo_tasks = project.turbo_tasks().clone(); let turbo_tasks = project.turbo_tasks().clone();
...@@ -180,12 +225,13 @@ pub fn project_entrypoints_subscribe( ...@@ -180,12 +225,13 @@ pub fn project_entrypoints_subscribe(
func, func,
move || async move { move || async move {
let entrypoints = project.entrypoints(); let entrypoints = project.entrypoints();
let issues = get_issues(entrypoints).await?;
let entrypoints = entrypoints.strongly_consistent().await?; let entrypoints = entrypoints.strongly_consistent().await?;
// TODO peek_issues and diagnostics // TODO peek_issues and diagnostics
Ok(entrypoints) Ok((entrypoints, issues))
}, },
move |ctx| { move |ctx| {
let entrypoints = ctx.value; let (entrypoints, issues) = ctx.value;
Ok(vec![NapiEntrypoints { Ok(vec![NapiEntrypoints {
routes: entrypoints routes: entrypoints
.routes .routes
...@@ -199,7 +245,10 @@ pub fn project_entrypoints_subscribe( ...@@ -199,7 +245,10 @@ pub fn project_entrypoints_subscribe(
.as_ref() .as_ref()
.map(|m| NapiMiddleware::from_middleware(m, &turbo_tasks)) .map(|m| NapiMiddleware::from_middleware(m, &turbo_tasks))
.transpose()?, .transpose()?,
issues: vec![], issues: issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect(),
diagnostics: vec![], diagnostics: vec![],
}]) }])
}, },
......
...@@ -4,12 +4,17 @@ use anyhow::{anyhow, Context, Result}; ...@@ -4,12 +4,17 @@ use anyhow::{anyhow, Context, Result};
use napi::{ use napi::{
bindgen_prelude::{External, ToNapiValue}, bindgen_prelude::{External, ToNapiValue},
threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode}, threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode},
JsFunction, JsObject, NapiRaw, NapiValue, Status, JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status,
}; };
use serde::Serialize; use serde::Serialize;
use turbo_tasks::{unit, TaskId, TurboTasks}; use turbo_tasks::{unit, ReadRef, TaskId, TurboTasks, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::tasks_memory::MemoryBackend, turbopack::core::error::PrettyPrintError, turbo::{tasks_fs::FileContent, tasks_memory::MemoryBackend},
turbopack::core::{
error::PrettyPrintError,
issue::{IssueContextExt, PlainIssue, PlainIssueSource, PlainSource},
source_pos::SourcePos,
},
}; };
/// A helper type to hold both a Vc operation and the TurboTasks root process. /// A helper type to hold both a Vc operation and the TurboTasks root process.
...@@ -71,8 +76,106 @@ pub fn root_task_dispose( ...@@ -71,8 +76,106 @@ pub fn root_task_dispose(
Ok(()) Ok(())
} }
pub async fn get_issues<T>(source: Vc<T>) -> Result<Vec<ReadRef<PlainIssue>>> {
let issues = source
.peek_issues_with_path()
.await?
.strongly_consistent()
.await?;
issues.get_plain_issues().await
}
#[napi(object)]
pub struct NapiIssue {
pub severity: String,
pub category: String,
pub context: String,
pub title: String,
pub description: String,
pub detail: String,
pub source: Option<NapiIssueSource>,
pub documentation_link: String,
pub sub_issues: Vec<NapiIssue>,
}
impl From<&PlainIssue> for NapiIssue {
fn from(issue: &PlainIssue) -> Self {
Self {
description: issue.description.clone(),
category: issue.category.clone(),
context: issue.context.clone(),
detail: issue.detail.clone(),
documentation_link: issue.documentation_link.clone(),
severity: issue.severity.as_str().to_string(),
source: issue.source.as_deref().map(|source| source.into()),
title: issue.title.clone(),
sub_issues: issue
.sub_issues
.iter()
.map(|issue| (&**issue).into())
.collect(),
}
}
}
#[napi(object)] #[napi(object)]
pub struct NapiIssue {} pub struct NapiIssueSource {
pub source: NapiSource,
pub start: NapiSourcePos,
pub end: NapiSourcePos,
}
impl From<&PlainIssueSource> for NapiIssueSource {
fn from(
PlainIssueSource {
asset: source,
start,
end,
}: &PlainIssueSource,
) -> Self {
Self {
source: (&**source).into(),
start: (*start).into(),
end: (*end).into(),
}
}
}
#[napi(object)]
pub struct NapiSource {
pub ident: String,
pub content: Option<String>,
}
impl From<&PlainSource> for NapiSource {
fn from(source: &PlainSource) -> Self {
Self {
ident: source.ident.to_string(),
content: match &*source.content {
FileContent::Content(content) => match content.content().to_str() {
Ok(str) => Some(str.into_owned()),
Err(_) => None,
},
FileContent::NotFound => None,
},
}
}
}
#[napi(object)]
pub struct NapiSourcePos {
pub line: u32,
pub column: u32,
}
impl From<SourcePos> for NapiSourcePos {
fn from(pos: SourcePos) -> Self {
Self {
line: pos.line as u32,
column: pos.column as u32,
}
}
}
#[napi(object)] #[napi(object)]
pub struct NapiDiagnostic {} pub struct NapiDiagnostic {}
...@@ -88,16 +191,20 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> { ...@@ -88,16 +191,20 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
env: napi::sys::napi_env, env: napi::sys::napi_env,
val: Self, val: Self,
) -> napi::Result<napi::sys::napi_value> { ) -> napi::Result<napi::sys::napi_value> {
let result = T::to_napi_value(env, val.result)?;
// let issues = ToNapiValue::to_napi_value(env, val.issues)?;
// let diagnostics = ToNapiValue::to_napi_value(env, val.diagnostics)?;
let result = JsObject::from_raw(env, result)?;
let mut obj = napi::Env::from_raw(env).create_object()?; let mut obj = napi::Env::from_raw(env).create_object()?;
for key in JsObject::keys(&result)? {
obj.set_named_property(&key, result.get_named_property(&key)?)?; let result = T::to_napi_value(env, val.result)?;
let result = JsUnknown::from_raw(env, result)?;
if matches!(result.get_type()?, napi::ValueType::Object) {
// SAFETY: We know that result is an object, so we can cast it to a JsObject
let result = unsafe { result.cast::<JsObject>() };
for key in JsObject::keys(&result)? {
let value: JsUnknown = result.get_named_property(&key)?;
obj.set_named_property(&key, value)?;
}
} }
obj.set_named_property("issues", val.issues)?; obj.set_named_property("issues", val.issues)?;
obj.set_named_property("diagnostics", val.diagnostics)?; obj.set_named_property("diagnostics", val.diagnostics)?;
......
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use next_core::{ use next_core::{
all_server_paths,
app_structure::{ app_structure::{
get_entrypoints, Entrypoint as AppEntrypoint, Entrypoints as AppEntrypoints, LoaderTree, get_entrypoints, Entrypoint as AppEntrypoint, Entrypoints as AppEntrypoints, LoaderTree,
}, },
...@@ -578,7 +579,7 @@ impl AppEndpoint { ...@@ -578,7 +579,7 @@ impl AppEndpoint {
}; };
let build_manifest_output = Vc::upcast(VirtualOutputAsset::new( let build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!( node_root.join(format!(
"app{original_name}/build-manifest.json", "server/app{original_name}/build-manifest.json",
original_name = app_entry.original_name original_name = app_entry.original_name
)), )),
AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()), AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()),
...@@ -616,20 +617,25 @@ impl Endpoint for AppEndpoint { ...@@ -616,20 +617,25 @@ impl Endpoint for AppEndpoint {
let output = output.await?; let output = output.await?;
let node_root_ref = node_root.await?; let node_root_ref = node_root.await?;
let node_root = this.app_project.project().node_root();
emit_all_assets( emit_all_assets(
output.output_assets, output.output_assets,
this.app_project.project().node_root(), node_root,
self.client_relative_path(), self.client_relative_path(),
this.app_project.project().node_root(), this.app_project.project().node_root(),
) )
.await?; .await?;
let server_paths = all_server_paths(output.output_assets, node_root)
.await?
.clone_value();
Ok(WrittenEndpoint { Ok(WrittenEndpoint {
server_entry_path: node_root_ref server_entry_path: node_root_ref
.get_path_to(&*output.rsc_chunk.ident().path().await?) .get_path_to(&*output.rsc_chunk.ident().path().await?)
.context("rsc chunk entry path must be inside the node root")? .context("rsc chunk entry path must be inside the node root")?
.to_string(), .to_string(),
server_paths: vec![], server_paths,
} }
.cell()) .cell())
} }
......
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
use next_core::{ use next_core::{
create_page_loader_entry_module, emit_all_assets, get_asset_path_from_pathname, all_server_paths, create_page_loader_entry_module, emit_all_assets,
get_asset_path_from_pathname,
mode::NextMode, mode::NextMode,
next_client::{ next_client::{
get_client_module_options_context, get_client_resolve_options_context, get_client_module_options_context, get_client_resolve_options_context,
...@@ -560,7 +561,7 @@ impl PageEndpoint { ...@@ -560,7 +561,7 @@ impl PageEndpoint {
}; };
Ok(Vc::upcast(VirtualOutputAsset::new( Ok(Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!( node_root.join(format!(
"pages{original_name}/build-manifest.json", "server/pages{original_name}/build-manifest.json",
original_name = this.original_name.await? original_name = this.original_name.await?
)), )),
AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()), AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()),
...@@ -629,14 +630,19 @@ impl Endpoint for PageEndpoint { ...@@ -629,14 +630,19 @@ impl Endpoint for PageEndpoint {
let this = self.await?; let this = self.await?;
let node_root = this.pages_project.project().node_root();
emit_all_assets( emit_all_assets(
*output_assets, *output_assets,
this.pages_project.project().node_root(), node_root,
this.pages_project.project().client_relative_path(), this.pages_project.project().client_relative_path(),
this.pages_project.project().node_root(), this.pages_project.project().node_root(),
) )
.await?; .await?;
let server_paths = all_server_paths(*output_assets, node_root)
.await?
.clone_value();
Ok(WrittenEndpoint { Ok(WrittenEndpoint {
server_entry_path: this server_entry_path: this
.pages_project .pages_project
...@@ -646,7 +652,7 @@ impl Endpoint for PageEndpoint { ...@@ -646,7 +652,7 @@ impl Endpoint for PageEndpoint {
.get_path_to(&*entry_chunk.ident().path().await?) .get_path_to(&*entry_chunk.ident().path().await?)
.context("ssr chunk entry path must be inside the node root")? .context("ssr chunk entry path must be inside the node root")?
.to_string(), .to_string(),
server_paths: vec![], server_paths,
} }
.cell()) .cell())
} }
......
...@@ -12,11 +12,11 @@ use next_core::{ ...@@ -12,11 +12,11 @@ use next_core::{
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{ use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, unit, TaskInput, TransientValue, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, unit, State, TaskInput, TransientValue, Vc,
}; };
use turbopack_binding::{ use turbopack_binding::{
turbo::{ turbo::{
tasks_env::ProcessEnv, tasks_env::{EnvMap, ProcessEnv},
tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, VirtualFileSystem}, tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath, VirtualFileSystem},
}, },
turbopack::{ turbopack::{
...@@ -27,7 +27,6 @@ use turbopack_binding::{ ...@@ -27,7 +27,6 @@ use turbopack_binding::{
}, },
dev::DevChunkingContext, dev::DevChunkingContext,
ecmascript::chunk::EcmascriptChunkingContext, ecmascript::chunk::EcmascriptChunkingContext,
env::dotenv::load_env,
node::execution_context::ExecutionContext, node::execution_context::ExecutionContext,
turbopack::evaluate_context::node_build_environment, turbopack::evaluate_context::node_build_environment,
}, },
...@@ -40,7 +39,7 @@ use crate::{ ...@@ -40,7 +39,7 @@ use crate::{
route::{Endpoint, Route}, route::{Endpoint, Route},
}; };
#[derive(Debug, Serialize, Deserialize, Clone, TaskInput)] #[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ProjectOptions { pub struct ProjectOptions {
/// A root path from which all files must be nested under. Trying to access /// A root path from which all files must be nested under. Trying to access
...@@ -53,11 +52,11 @@ pub struct ProjectOptions { ...@@ -53,11 +52,11 @@ pub struct ProjectOptions {
/// The contents of next.config.js, serialized to JSON. /// The contents of next.config.js, serialized to JSON.
pub next_config: String, pub next_config: String,
/// A map of environment variables to use when compiling code.
pub env: Vec<(String, String)>,
/// Whether to watch the filesystem for file changes. /// Whether to watch the filesystem for file changes.
pub watch: bool, pub watch: bool,
/// An upper bound of memory that turbopack will attempt to stay under.
pub memory_limit: Option<u64>,
} }
#[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat)] #[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat)]
...@@ -66,6 +65,53 @@ pub struct Middleware { ...@@ -66,6 +65,53 @@ pub struct Middleware {
pub config: NextSourceConfig, pub config: NextSourceConfig,
} }
#[turbo_tasks::value]
pub struct ProjectContainer {
state: State<ProjectOptions>,
}
#[turbo_tasks::value_impl]
impl ProjectContainer {
#[turbo_tasks::function]
pub fn new(options: ProjectOptions) -> Vc<Self> {
ProjectContainer {
state: State::new(options),
}
.cell()
}
#[turbo_tasks::function]
pub async fn update(self: Vc<Self>, options: ProjectOptions) -> Result<Vc<()>> {
self.await?.state.set(options);
Ok(unit())
}
#[turbo_tasks::function]
pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
let this = self.await?;
let options = this.state.get();
let next_config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
let env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect());
Ok(Project {
root_path: options.root_path.clone(),
project_path: options.project_path.clone(),
watch: options.watch,
next_config,
env: Vc::upcast(env),
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions"
.to_string(),
mode: NextMode::Development,
}
.cell())
}
#[turbo_tasks::function]
pub fn entrypoints(self: Vc<Self>) -> Vc<Entrypoints> {
self.project().entrypoints()
}
}
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct Project { pub struct Project {
/// A root path from which all files must be nested under. Trying to access /// A root path from which all files must be nested under. Trying to access
...@@ -81,6 +127,9 @@ pub struct Project { ...@@ -81,6 +127,9 @@ pub struct Project {
/// Next config. /// Next config.
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
/// A map of environment variables to use when compiling code.
env: Vc<Box<dyn ProcessEnv>>,
browserslist_query: String, browserslist_query: String,
mode: NextMode, mode: NextMode,
...@@ -88,22 +137,6 @@ pub struct Project { ...@@ -88,22 +137,6 @@ pub struct Project {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Project { impl Project {
#[turbo_tasks::function]
pub async fn new(options: ProjectOptions) -> Result<Vc<Self>> {
let next_config = NextConfig::from_string(options.next_config);
Ok(Project {
root_path: options.root_path,
project_path: options.project_path,
watch: options.watch,
next_config,
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions"
.to_string(),
mode: NextMode::Development,
}
.cell())
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn app_project(self: Vc<Self>) -> Result<Vc<OptionAppProject>> { async fn app_project(self: Vc<Self>) -> Result<Vc<OptionAppProject>> {
let this = self.await?; let this = self.await?;
...@@ -182,8 +215,8 @@ impl Project { ...@@ -182,8 +215,8 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn env(self: Vc<Self>) -> Vc<Box<dyn ProcessEnv>> { pub(super) async fn env(self: Vc<Self>) -> Result<Vc<Box<dyn ProcessEnv>>> {
load_env(self.project_path()) Ok(self.await?.env)
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -9,6 +9,29 @@ use turbopack_binding::turbopack::core::{ ...@@ -9,6 +9,29 @@ use turbopack_binding::turbopack::core::{
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
}; };
#[turbo_tasks::function]
pub async fn all_server_paths(
assets: Vc<OutputAssets>,
node_root: Vc<FileSystemPath>,
) -> Result<Vc<Vec<String>>> {
let all_assets = all_assets_from_entries(assets).await?;
let node_root = &node_root.await?;
Ok(Vc::cell(
all_assets
.iter()
.map(|&asset| async move {
Ok(node_root
.get_path_to(&*asset.ident().path().await?)
.map(|s| s.to_string()))
})
.try_join()
.await?
.into_iter()
.flatten()
.collect(),
))
}
/// Emits all assets transitively reachable from the given chunks, that are /// Emits all assets transitively reachable from the given chunks, that are
/// inside the node root or the client root. /// inside the node root or the client root.
/// ///
......
...@@ -49,7 +49,7 @@ pub mod util; ...@@ -49,7 +49,7 @@ pub mod util;
mod web_entry_source; mod web_entry_source;
pub use app_source::create_app_source; pub use app_source::create_app_source;
pub use emit::emit_all_assets; pub use emit::{all_server_paths, emit_all_assets};
pub use page_loader::create_page_loader_entry_module; pub use page_loader::create_page_loader_entry_module;
pub use page_source::create_page_source; pub use page_source::create_page_source;
pub use turbopack_binding::{turbopack::node::source_map, *}; pub use turbopack_binding::{turbopack::node::source_map, *};
......
...@@ -508,9 +508,10 @@ pub enum RemoveConsoleConfig { ...@@ -508,9 +508,10 @@ pub enum RemoveConsoleConfig {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl NextConfig { impl NextConfig {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn from_string(s: String) -> Result<Vc<Self>> { pub async fn from_string(string: Vc<String>) -> Result<Vc<Self>> {
let config: NextConfig = serde_json::from_str(&s) let string = string.await?;
.with_context(|| format!("failed to parse next.config.js: {}", s))?; let config: NextConfig = serde_json::from_str(&string)
.with_context(|| format!("failed to parse next.config.js: {}", string))?;
Ok(config.cell()) Ok(config.cell())
} }
......
...@@ -107,7 +107,10 @@ export interface Binding { ...@@ -107,7 +107,10 @@ export interface Binding {
compile: any compile: any
compileSync: any compileSync: any
} }
createProject: (options: ProjectOptions) => Promise<Project> createProject: (
options: ProjectOptions,
turboEngineOptions?: TurboEngineOptions
) => Promise<Project>
} }
minify: any minify: any
minifySync: any minifySync: any
...@@ -337,18 +340,42 @@ interface ProjectOptions { ...@@ -337,18 +340,42 @@ interface ProjectOptions {
*/ */
nextConfig: NextConfigComplete nextConfig: NextConfigComplete
/**
* A map of environment variables to use when compiling code.
*/
env: Record<string, string>
/** /**
* Whether to watch he filesystem for file changes. * Whether to watch he filesystem for file changes.
*/ */
watch: boolean watch: boolean
}
interface TurboEngineOptions {
/** /**
* An upper bound of memory that turbopack will attempt to stay under. * An upper bound of memory that turbopack will attempt to stay under.
*/ */
memoryLimit?: number memoryLimit?: number
} }
interface Issue {} interface Issue {
severity: string
category: string
context: string
title: string
description: string
detail: string
source?: {
source: {
ident: string
content?: string
}
start: { line: number; column: number }
end: { line: number; column: number }
}
documentationLink: string
subIssues: Issue[]
}
interface Diagnostics {} interface Diagnostics {}
...@@ -369,6 +396,7 @@ interface Entrypoints { ...@@ -369,6 +396,7 @@ interface Entrypoints {
} }
interface Project { interface Project {
update(options: ProjectOptions): Promise<void>
entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>> entrypointsSubscribe(): AsyncIterableIterator<TurbopackResult<Entrypoints>>
} }
...@@ -446,6 +474,14 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -446,6 +474,14 @@ function bindingToApi(binding: any, _wasm: boolean) {
throw new Error(`Invariant: ${computeMessage(never)}`) throw new Error(`Invariant: ${computeMessage(never)}`)
} }
async function withErrorCause<T>(fn: () => Promise<T>): Promise<T> {
try {
return await fn()
} catch (nativeError: any) {
throw new Error(nativeError.message, { cause: nativeError })
}
}
/** /**
* Calls a native function and streams the result. * Calls a native function and streams the result.
* If useBuffer is true, all values will be preserved, potentially buffered * If useBuffer is true, all values will be preserved, potentially buffered
...@@ -488,7 +524,7 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -488,7 +524,7 @@ function bindingToApi(binding: any, _wasm: boolean) {
} }
return (async function* () { return (async function* () {
const task = await nativeFunction(emitResult) const task = await withErrorCause(() => nativeFunction(emitResult))
try { try {
while (true) { while (true) {
if (buffer.length > 0) { if (buffer.length > 0) {
...@@ -508,6 +544,17 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -508,6 +544,17 @@ function bindingToApi(binding: any, _wasm: boolean) {
})() })()
} }
async function rustifyProjectOptions(options: ProjectOptions): Promise<any> {
return {
...options,
nextConfig: await serializeNextConfig(options.nextConfig),
env: Object.entries(options.env).map(([name, value]) => ({
name,
value,
})),
}
}
class ProjectImpl implements Project { class ProjectImpl implements Project {
private _nativeProject: { __napiType: 'Project' } private _nativeProject: { __napiType: 'Project' }
...@@ -515,6 +562,15 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -515,6 +562,15 @@ function bindingToApi(binding: any, _wasm: boolean) {
this._nativeProject = nativeProject this._nativeProject = nativeProject
} }
async update(options: ProjectOptions) {
await withErrorCause(async () =>
binding.projectUpdate(
this._nativeProject,
await rustifyProjectOptions(options)
)
)
}
entrypointsSubscribe() { entrypointsSubscribe() {
type NapiEndpoint = { __napiType: 'Endpoint' } type NapiEndpoint = { __napiType: 'Endpoint' }
...@@ -633,7 +689,9 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -633,7 +689,9 @@ function bindingToApi(binding: any, _wasm: boolean) {
} }
async writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> { async writeToDisk(): Promise<TurbopackResult<WrittenEndpoint>> {
return await binding.endpointWriteToDisk(this._nativeEndpoint) return await withErrorCause(() =>
binding.endpointWriteToDisk(this._nativeEndpoint)
)
} }
async changed(): Promise<AsyncIterableIterator<TurbopackResult>> { async changed(): Promise<AsyncIterableIterator<TurbopackResult>> {
...@@ -683,11 +741,16 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -683,11 +741,16 @@ function bindingToApi(binding: any, _wasm: boolean) {
} }
} }
async function createProject(options: ProjectOptions) { async function createProject(
const optionsForRust = options as any options: ProjectOptions,
optionsForRust.nextConfig = await serializeNextConfig(options.nextConfig) turboEngineOptions: TurboEngineOptions
) {
return new ProjectImpl(await binding.projectNew(optionsForRust)) return new ProjectImpl(
await binding.projectNew(
await rustifyProjectOptions(options),
turboEngineOptions || {}
)
)
} }
return createProject return createProject
......
...@@ -251,12 +251,16 @@ const nextDev: CliCommand = async (argv) => { ...@@ -251,12 +251,16 @@ const nextDev: CliCommand = async (argv) => {
projectPath: dir, projectPath: dir,
rootPath: dir, rootPath: dir,
nextConfig: config, nextConfig: config,
env: {
NEXT_PUBLIC_ENV_VAR: 'world',
},
watch: true, watch: true,
}) })
const iter = project.entrypointsSubscribe() const iter = project.entrypointsSubscribe()
try { try {
for await (const entrypoints of iter) { for await (const entrypoints of iter) {
Log.info(entrypoints)
for (const [pathname, route] of entrypoints.routes) { for (const [pathname, route] of entrypoints.routes) {
switch (route.type) { switch (route.type) {
case 'page': { case 'page': {
...@@ -288,9 +292,19 @@ const nextDev: CliCommand = async (argv) => { ...@@ -288,9 +292,19 @@ const nextDev: CliCommand = async (argv) => {
break break
} }
} }
Log.info('iteration done')
await project.update({
projectPath: dir,
rootPath: dir,
nextConfig: config,
env: {
NEXT_PUBLIC_ENV_VAR: 'hello',
},
watch: true,
})
} }
} catch (e) { } catch (e) {
console.error(e) console.dir(e)
} }
Log.error('Not supported yet') Log.error('Not supported yet')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment