Skip to content

Commit

Permalink
refactor(next/turbo): consolidate turbo devserver logic (#42315)
Browse files Browse the repository at this point in the history
<!--
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)
  • Loading branch information
kwonoj committed Nov 2, 2022
1 parent 0c2404a commit 76fdd25
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 164 deletions.
5 changes: 0 additions & 5 deletions packages/next-swc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions packages/next-swc/crates/napi/Cargo.toml
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion packages/next-swc/crates/napi/src/lib.rs
Expand Up @@ -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)]

Expand Down
154 changes: 3 additions & 151 deletions packages/next-swc/crates/napi/src/turbopack.rs
@@ -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()
}
8 changes: 7 additions & 1 deletion packages/next/build/swc/index.js
Expand Up @@ -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 })),
},
Expand Down

0 comments on commit 76fdd25

Please sign in to comment.