-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calling turbopack from the next build CLI (#46602)
Close WEB-661
- Loading branch information
1 parent
17e1cc7
commit 509ed00
Showing
14 changed files
with
223 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,110 @@ | ||
use std::convert::TryFrom; | ||
|
||
use crate::util::MapErr; | ||
use napi::bindgen_prelude::*; | ||
use next_binding::turbo::next_dev::{devserver_options::DevServerOptions, start_server}; | ||
use next_binding::turbo::{ | ||
next_build::{next_build as turbo_next_build, NextBuildOptions}, | ||
next_dev::{devserver_options::DevServerOptions, start_server}, | ||
}; | ||
|
||
#[napi] | ||
pub async fn start_turbo_dev(options: Buffer) -> napi::Result<()> { | ||
let options: DevServerOptions = serde_json::from_slice(&options)?; | ||
start_server(&options).await.convert_err() | ||
} | ||
|
||
#[napi(object, object_to_js = false)] | ||
#[derive(Debug)] | ||
pub struct NextBuildContext { | ||
pub dir: Option<String>, | ||
pub app_dir: Option<String>, | ||
pub pages_dir: Option<String>, | ||
pub rewrites: Option<Rewrites>, | ||
pub original_rewrites: Option<Rewrites>, | ||
pub original_redirects: Option<Vec<Redirect>>, | ||
} | ||
|
||
#[napi(object, object_to_js = false)] | ||
#[derive(Debug)] | ||
pub struct Rewrites { | ||
pub fallback: Vec<Rewrite>, | ||
pub after_files: Vec<Rewrite>, | ||
pub before_files: Vec<Rewrite>, | ||
} | ||
|
||
#[napi(object, object_to_js = false)] | ||
#[derive(Debug)] | ||
pub struct Rewrite { | ||
pub source: String, | ||
pub destination: String, | ||
} | ||
|
||
#[napi(object, object_to_js = false)] | ||
#[derive(Debug)] | ||
pub struct Redirect { | ||
pub source: String, | ||
pub destination: String, | ||
pub permanent: Option<bool>, | ||
pub status_code: Option<u32>, | ||
pub has: Option<RouteHas>, | ||
pub missing: Option<RouteHas>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct RouteHas { | ||
pub r#type: RouteType, | ||
pub key: Option<String>, | ||
pub value: Option<String>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum RouteType { | ||
Header, | ||
Query, | ||
Cookie, | ||
Host, | ||
} | ||
|
||
impl TryFrom<String> for RouteType { | ||
type Error = napi::Error; | ||
|
||
fn try_from(value: String) -> Result<Self> { | ||
match value.as_str() { | ||
"header" => Ok(RouteType::Header), | ||
"query" => Ok(RouteType::Query), | ||
"cookie" => Ok(RouteType::Cookie), | ||
"host" => Ok(RouteType::Host), | ||
_ => Err(napi::Error::new( | ||
napi::Status::InvalidArg, | ||
"Invalid route type", | ||
)), | ||
} | ||
} | ||
} | ||
|
||
impl FromNapiValue for RouteHas { | ||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> { | ||
let object = Object::from_napi_value(env, napi_val)?; | ||
let r#type = object.get_named_property::<String>("type")?; | ||
Ok(RouteHas { | ||
r#type: RouteType::try_from(r#type)?, | ||
key: object.get("key")?, | ||
value: object.get("value")?, | ||
}) | ||
} | ||
} | ||
|
||
impl From<NextBuildContext> for NextBuildOptions { | ||
fn from(value: NextBuildContext) -> Self { | ||
Self { | ||
dir: value.dir, | ||
memory_limit: None, | ||
full_stats: None, | ||
} | ||
} | ||
} | ||
|
||
#[napi] | ||
pub async fn next_build(ctx: NextBuildContext) -> napi::Result<()> { | ||
turbo_next_build(ctx.into()).await.convert_err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "next-build" | ||
version = "0.1.0" | ||
description = "TBD" | ||
license = "MPL-2.0" | ||
edition = "2021" | ||
autobenches = false | ||
|
||
[features] | ||
next-font-local = ["next-core/next-font-local"] | ||
native-tls = ["next-core/native-tls"] | ||
rustls-tls = ["next-core/rustls-tls"] | ||
custom_allocator = ["turbo-malloc/custom_allocator"] | ||
|
||
[dependencies] | ||
anyhow = "1.0.47" | ||
next-core = { workspace = true } | ||
turbo-malloc = { workspace = true, default-features = false } | ||
turbo-tasks = { workspace = true } | ||
turbo-tasks-memory = { workspace = true } | ||
|
||
[build-dependencies] | ||
turbo-tasks-build = { workspace = true } | ||
vergen = { version = "7.3.2", default-features = false, features = [ | ||
"cargo", | ||
"build", | ||
] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use turbo_tasks_build::{generate_register, rerun_if_glob}; | ||
|
||
use vergen::{vergen, Config}; | ||
|
||
fn main() { | ||
generate_register(); | ||
|
||
rerun_if_glob("tests/integration/*/*", "tests/integration"); | ||
|
||
// Attempt to collect some build time env values but will skip if there are any | ||
// errors. | ||
let _ = vergen(Config::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use turbo_tasks::{NothingVc, StatsType, TurboTasks, TurboTasksBackendApi}; | ||
use turbo_tasks_memory::MemoryBackend; | ||
|
||
pub fn register() { | ||
turbo_tasks::register(); | ||
include!(concat!(env!("OUT_DIR"), "/register.rs")); | ||
} | ||
|
||
pub struct NextBuildOptions { | ||
pub dir: Option<String>, | ||
pub memory_limit: Option<usize>, | ||
pub full_stats: Option<bool>, | ||
} | ||
|
||
pub async fn next_build(options: NextBuildOptions) -> anyhow::Result<()> { | ||
register(); | ||
let tt = TurboTasks::new(MemoryBackend::new( | ||
options.memory_limit.map_or(usize::MAX, |l| l * 1024 * 1024), | ||
)); | ||
let stats_type = match options.full_stats { | ||
Some(true) => StatsType::Full, | ||
_ => StatsType::Essential, | ||
}; | ||
tt.set_stats_type(stats_type); | ||
let task = tt.spawn_root_task(move || { | ||
Box::pin(async move { | ||
// run next build here | ||
Ok(NothingVc::new().into()) | ||
}) | ||
}); | ||
tt.wait_task_completion(task, true).await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters