Skip to content

Commit

Permalink
refactor(next/dev): allow devserver args serializable (vercel/turbo#2446
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kwonoj committed Oct 28, 2022
1 parent b12e0d8 commit 0d12445
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/next-swc/crates/next-dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ name = "mod"
harness = false

[features]
default = ["cli"]
cli = []
serializable = []
tokio_console = [
"dep:console-subscriber",
"tokio/tracing",
Expand Down Expand Up @@ -59,7 +62,7 @@ regex = "1.6.0"
tempfile = "3.3.0"
test-generator = "0.3.0"
# sync with chromiumoxide's tungstenite requirement.
tungstenite = "0.17.3" # For matching on errors from chromiumoxide. Keep in
tungstenite = "0.17.3" # For matching on errors from chromiumoxide. Keep in
turbopack-create-test-app = { path = "../turbopack-create-test-app" }

[target.'cfg(unix)'.dev-dependencies]
Expand Down
71 changes: 58 additions & 13 deletions packages/next-swc/crates/next-dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,104 @@ use turbo_tasks_memory::MemoryBackend;
use turbopack_cli_utils::issue::IssueSeverityCliOption;
use turbopack_core::issue::IssueSeverity;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[derive(Debug)]
#[cfg_attr(feature = "cli", derive(Parser))]
#[cfg_attr(feature = "cli", clap(author, version, about, long_about = None))]
#[cfg_attr(feature = "serializable", derive(serde::Deserialize))]
#[cfg_attr(feature = "serializable", serde(rename_all = "camelCase"))]
pub struct DevServerOptions {
/// The directory of the Next.js application.
/// If no directory is provided, the current directory will be used.
#[clap(value_parser)]
#[cfg_attr(feature = "cli", clap(value_parser))]
#[cfg_attr(feature = "serializable", serde(default))]
dir: Option<PathBuf>,

/// The root directory of the project. Nothing outside of this directory can
/// be accessed. e. g. the monorepo root.
/// If no directory is provided, `dir` will be used.
#[clap(long, value_parser)]
#[cfg_attr(feature = "cli", clap(long, value_parser))]
#[cfg_attr(feature = "serializable", serde(default))]
root: Option<PathBuf>,

/// The port number on which to start the application
#[clap(short, long, value_parser, default_value_t = 3000)]
#[cfg_attr(
feature = "cli",
clap(short, long, value_parser, default_value_t = 3000)
)]
#[cfg_attr(feature = "serializable", serde(default = "default_port"))]
port: u16,

/// Hostname on which to start the application
#[clap(short = 'H', long, value_parser, default_value = "0.0.0.0")]
#[cfg_attr(
feature = "cli",
clap(short = 'H', long, value_parser, default_value = "0.0.0.0")
)]
#[cfg_attr(feature = "serializable", serde(default = "default_host"))]
hostname: IpAddr,

/// Compile all, instead of only compiling referenced assets when their
/// parent asset is requested
#[clap(long)]
#[cfg_attr(feature = "cli", clap(long))]
#[cfg_attr(feature = "serializable", serde(default))]
eager_compile: bool,

/// Don't open the browser automatically when the dev server has started.
#[clap(long)]
#[cfg_attr(feature = "cli", clap(long))]
#[cfg_attr(feature = "serializable", serde(default))]
no_open: bool,

#[clap(short, long)]
#[cfg_attr(feature = "cli", clap(short, long))]
#[cfg_attr(feature = "serializable", serde(default))]
/// Filter by issue severity.
log_level: Option<IssueSeverityCliOption>,

#[clap(long)]
#[cfg_attr(feature = "cli", clap(long))]
#[cfg_attr(feature = "serializable", serde(default))]
/// Show all log messages without limit.
show_all: bool,

#[clap(long)]
#[cfg_attr(feature = "cli", clap(long))]
#[cfg_attr(feature = "serializable", serde(default))]
/// Expand the log details.
log_detail: bool,

// Inherited options from next-dev, need revisit later.
// This is not supported by CLI yet.
#[cfg_attr(feature = "serializable", serde(default))]
allow_retry: bool,
#[cfg_attr(feature = "serializable", serde(default))]
dev: bool,
#[cfg_attr(feature = "serializable", serde(default))]
is_next_dev_command: bool,
#[cfg_attr(feature = "serializable", serde(default))]
server_components_external_packages: Vec<String>,
}

#[cfg(feature = "serializable")]
fn default_port() -> u16 {
3000
}

#[cfg(feature = "serializable")]
fn default_host() -> IpAddr {
IpAddr::V4(std::net::Ipv4Addr::new(0, 0, 0, 0))
}

#[cfg(not(feature = "cli"))]
fn main() -> Result<()> {
unimplemented!("Cannot run binary without CLI feature enabled");
}

#[tokio::main]
#[cfg(feature = "cli")]
async fn main() -> Result<()> {
let start = Instant::now();

#[cfg(feature = "tokio_console")]
console_subscriber::init();
register();

let args = Cli::parse();
let args = DevServerOptions::parse();

let dir = args
.dir
Expand Down

0 comments on commit 0d12445

Please sign in to comment.