refactor(next/turbo): consolidate turbo devserver logic (#42315)

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

This PR simplifies how `--turbo` is being invoked in its napi bindings.
After refactoring in turbopack's devserver, it is possible to reuse same
logic between napi bindings / standalone binaries.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
This commit is contained in:
OJ Kwon 2022-11-01 20:27:14 -07:00 committed by GitHub
parent 0c2404a406
commit 76fdd256cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 164 deletions

View file

@ -2314,7 +2314,6 @@ dependencies = [
"next-swc",
"node-file-trace",
"once_cell",
"owo-colors",
"sentry",
"serde",
"serde_json",
@ -2323,10 +2322,6 @@ dependencies = [
"tracing-chrome",
"tracing-futures",
"tracing-subscriber",
"turbo-tasks",
"turbo-tasks-memory",
"turbopack-core",
"turbopack-dev-server",
]
[[package]]

View file

@ -55,12 +55,7 @@ tracing = { version = "0.1.32", features = ["release_max_level_info"] }
tracing-futures = "0.2.5"
tracing-subscriber = "0.3.9"
tracing-chrome = "0.5.0"
owo-colors = "3"
turbo-tasks = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591" }
turbo-tasks-memory = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591" }
turbopack-core = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591" }
turbopack-dev-server = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591" }
next-dev = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591" }
next-dev = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591", features = ["serializable"] }
node-file-trace = { git = "https://github.com/vercel/turbo.git", rev = "a11422fdf6b1b3cde9072d90aab6d9eebfacb591", default-features = false, features = ["node-api"] }
# There are few build targets we can't use native-tls which default features rely on,
# allow to specify alternative (rustls) instead via features.

View file

@ -26,7 +26,6 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#![feature(future_join)]
#![recursion_limit = "2048"]
//#![deny(clippy::all)]

View file

@ -1,157 +1,9 @@
use std::{
future::join,
net::{IpAddr, Ipv4Addr},
path::PathBuf,
time::{Duration, Instant},
};
use crate::util::MapErr;
use napi::bindgen_prelude::*;
use next_dev::{register, NextDevServerBuilder};
use owo_colors::OwoColorize;
use serde::Deserialize;
use turbo_tasks::{util::FormatDuration, TurboTasks};
use turbo_tasks_memory::MemoryBackend;
use turbopack_core::issue::IssueSeverity;
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(unused)]
struct TurboDevServerOptions {
#[serde(default = "default_port")]
port: u16,
#[serde(default = "default_host")]
hostname: IpAddr,
#[serde(default)]
eager_compile: bool,
#[serde(default)]
log_level: Option<IssueSeverity>,
#[serde(default)]
show_all: bool,
#[serde(default)]
log_detail: bool,
#[serde(default = "default_dir")]
dir: PathBuf,
#[serde(default = "default_dir")]
root_dir: PathBuf,
#[serde(default)]
allow_retry: bool,
#[serde(default)]
dev: bool,
#[serde(default)]
is_next_dev_command: bool,
#[serde(default)]
server_components_external_packages: Vec<String>,
}
fn default_port() -> u16 {
3000
}
fn default_host() -> IpAddr {
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))
}
fn default_dir() -> PathBuf {
std::env::current_dir().expect("Current dir should be accessible")
}
async fn start_server(options: TurboDevServerOptions) -> napi::Result<()> {
let start = Instant::now();
register();
let tt = TurboTasks::new(MemoryBackend::new());
let tt_clone = tt.clone();
let dir = options
.dir
.canonicalize()
.expect("Cannot canonicalize project directory")
.to_str()
.expect("project directory contains invalid characters")
.to_string();
let root_dir = options
.root_dir
.canonicalize()
.expect("Cannot canonicalize project directory")
.to_str()
.expect("project directory contains invalid characters")
.to_string();
//server_component_external
let mut server = NextDevServerBuilder::new(tt, dir, root_dir)
.entry_request("src/index".into())
.eager_compile(options.eager_compile)
.hostname(options.hostname)
.port(options.port)
.log_detail(options.log_detail)
.show_all(options.show_all)
.log_level(
options
.log_level
.map_or_else(|| IssueSeverity::Warning, |l| l),
);
for package in options.server_components_external_packages {
server = server.server_component_external(package);
}
let server = server.build().await.convert_err()?;
let index_uri = if server.addr.ip().is_loopback() || server.addr.ip().is_unspecified() {
format!("http://localhost:{}", server.addr.port())
} else {
format!("http://{}", server.addr)
};
println!(
"{} - started server on {}:{}, url: {}",
"ready".green(),
server.addr.ip(),
server.addr.port(),
index_uri
);
let stats_future = async move {
println!(
"{event_type} - initial compilation {start}",
event_type = "event".purple(),
start = FormatDuration(start.elapsed()),
);
loop {
let (elapsed, _count) = tt_clone
.get_or_wait_update_info(Duration::from_millis(100))
.await;
println!(
"{event_type} - updated in {elapsed}",
event_type = "event".purple(),
elapsed = FormatDuration(elapsed),
);
}
};
join!(stats_future, async { server.future.await.unwrap() }).await;
Ok(())
}
use next_dev::{devserver_options::DevServerOptions, start_server};
#[napi]
pub async fn start_turbo_dev(options: Buffer) -> napi::Result<()> {
let options: TurboDevServerOptions = serde_json::from_slice(&options)?;
start_server(options).await
let options: DevServerOptions = serde_json::from_slice(&options)?;
start_server(&options).await.convert_err()
}

View file

@ -342,7 +342,13 @@ function loadNative() {
teardownTraceSubscriber: bindings.teardownTraceSubscriber,
teardownCrashReporter: bindings.teardownCrashReporter,
turbo: {
startDev: (options) => bindings.startTurboDev(toBuffer(options)),
startDev: (options) => {
const devOptions = {
...options,
noOpen: options.noOpen ?? true,
}
bindings.startTurboDev(toBuffer(devOptions))
},
startTrace: (options = {}) =>
bindings.runTurboTracing(toBuffer({ exact: true, ...options })),
},