mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 09:38:16 +03:00
feat(cli): Setup subcommands for the features (#3858)
This commit is contained in:
parent
04f3a8fbc9
commit
367a57df6d
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2823,6 +2823,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"swc",
|
||||
"swc_common",
|
||||
"swc_trace_macro",
|
||||
"tracing",
|
||||
"tracing-chrome",
|
||||
"tracing-futures",
|
||||
|
@ -23,6 +23,7 @@ relative-path = "1.6.1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = { version = "1", features = ["unbounded_depth"] }
|
||||
atty = "0.2.14"
|
||||
swc_trace_macro = {version = "0.1.0", path = "../swc_trace_macro"}
|
||||
tracing-chrome = "0.4.0"
|
||||
tracing-futures = "0.2.5"
|
||||
tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
|
||||
|
12
crates/swc_cli/src/commands/bundle.rs
Normal file
12
crates/swc_cli/src/commands/bundle.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use clap::Parser;
|
||||
use swc_trace_macro::swc_trace;
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct BundleOptions {}
|
||||
|
||||
#[swc_trace]
|
||||
impl super::CommandRunner for BundleOptions {
|
||||
fn execute(&self) -> anyhow::Result<()> {
|
||||
unimplemented!("Bundle command is not yet implemented")
|
||||
}
|
||||
}
|
@ -15,10 +15,11 @@ use swc::{
|
||||
try_with_handler, Compiler, TransformOutput,
|
||||
};
|
||||
use swc_common::{sync::Lazy, FileName, FilePathMapping, SourceMap};
|
||||
use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
|
||||
use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt};
|
||||
use swc_trace_macro::swc_trace;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::util::trace::init_trace;
|
||||
|
||||
/// Configuration option for transform files.
|
||||
#[derive(Parser)]
|
||||
pub struct CompileOptions {
|
||||
@ -259,26 +260,8 @@ fn collect_stdin_input() -> Option<String> {
|
||||
)
|
||||
}
|
||||
|
||||
fn init_trace(out_file: &Option<String>) -> Option<FlushGuard> {
|
||||
let layer = if let Some(trace_out_file) = out_file {
|
||||
ChromeLayerBuilder::new()
|
||||
.file(trace_out_file.clone())
|
||||
.include_args(true)
|
||||
} else {
|
||||
ChromeLayerBuilder::new().include_args(true)
|
||||
};
|
||||
|
||||
let (chrome_layer, guard) = layer.build();
|
||||
tracing_subscriber::registry()
|
||||
.with(chrome_layer)
|
||||
.try_init()
|
||||
.expect("Should able to register trace");
|
||||
|
||||
Some(guard)
|
||||
}
|
||||
|
||||
impl super::CommandRunner for CompileOptions {
|
||||
#[tracing::instrument(level = "trace", skip_all)]
|
||||
#[swc_trace]
|
||||
impl CompileOptions {
|
||||
fn execute_inner(&self) -> anyhow::Result<()> {
|
||||
let stdin_input = collect_stdin_input();
|
||||
|
||||
@ -362,7 +345,10 @@ impl super::CommandRunner for CompileOptions {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[swc_trace]
|
||||
impl super::CommandRunner for CompileOptions {
|
||||
fn execute(&self) -> anyhow::Result<()> {
|
||||
let guard = if self.experimental_trace {
|
||||
init_trace(&self.trace_out_file)
|
||||
@ -374,6 +360,7 @@ impl super::CommandRunner for CompileOptions {
|
||||
|
||||
if let Some(guard) = guard {
|
||||
guard.flush();
|
||||
drop(guard);
|
||||
}
|
||||
|
||||
ret
|
||||
|
12
crates/swc_cli/src/commands/lint.rs
Normal file
12
crates/swc_cli/src/commands/lint.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use clap::Parser;
|
||||
use swc_trace_macro::swc_trace;
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct LintOptions {}
|
||||
|
||||
#[swc_trace]
|
||||
impl super::CommandRunner for LintOptions {
|
||||
fn execute(&self) -> anyhow::Result<()> {
|
||||
unimplemented!("Lint command is not yet implemented")
|
||||
}
|
||||
}
|
12
crates/swc_cli/src/commands/minify.rs
Normal file
12
crates/swc_cli/src/commands/minify.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use clap::Parser;
|
||||
use swc_trace_macro::swc_trace;
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct MinifyOptions {}
|
||||
|
||||
#[swc_trace]
|
||||
impl super::CommandRunner for MinifyOptions {
|
||||
fn execute(&self) -> anyhow::Result<()> {
|
||||
unimplemented!("Minify command is not yet implemented")
|
||||
}
|
||||
}
|
@ -1,9 +1,15 @@
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
mod bundle;
|
||||
mod compile;
|
||||
mod lint;
|
||||
mod minify;
|
||||
mod plugin;
|
||||
|
||||
pub use bundle::*;
|
||||
pub use compile::*;
|
||||
pub use lint::*;
|
||||
pub use minify::*;
|
||||
pub use plugin::{PluginScaffoldOptions, PluginSubcommand};
|
||||
|
||||
// Set of subcommands supported by the `swc` command.
|
||||
@ -14,6 +20,9 @@ pub enum Command {
|
||||
Plugin(PluginSubcommand),
|
||||
/// Run SWC's transformer.
|
||||
Compile(Box<CompileOptions>),
|
||||
Bundle(BundleOptions),
|
||||
Minify(MinifyOptions),
|
||||
Lint(LintOptions),
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
@ -25,5 +34,4 @@ pub struct SwcCliOptions {
|
||||
|
||||
pub trait CommandRunner {
|
||||
fn execute(&self) -> anyhow::Result<()>;
|
||||
fn execute_inner(&self) -> anyhow::Result<()>;
|
||||
}
|
||||
|
@ -109,10 +109,6 @@ fn write_ignore_file(base_path: &Path) -> Result<()> {
|
||||
}
|
||||
|
||||
impl super::CommandRunner for PluginScaffoldOptions {
|
||||
fn execute_inner(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create a rust project for the plugin from template.
|
||||
/// This largely mimic https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/cargo_new.rs,
|
||||
/// but also thinner implementation based on some assumptions like skipping
|
||||
|
@ -2,6 +2,7 @@ use clap::Parser;
|
||||
use commands::{Command, CommandRunner, PluginSubcommand, SwcCliOptions};
|
||||
|
||||
mod commands;
|
||||
mod util;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let command = SwcCliOptions::parse().command;
|
||||
@ -9,5 +10,8 @@ fn main() -> anyhow::Result<()> {
|
||||
match &command {
|
||||
Command::Plugin(PluginSubcommand::New(options)) => options.execute(),
|
||||
Command::Compile(options) => options.execute(),
|
||||
Command::Minify(options) => options.execute(),
|
||||
Command::Bundle(options) => options.execute(),
|
||||
Command::Lint(options) => options.execute(),
|
||||
}
|
||||
}
|
||||
|
1
crates/swc_cli/src/util/mod.rs
Normal file
1
crates/swc_cli/src/util/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub(crate) mod trace;
|
23
crates/swc_cli/src/util/trace.rs
Normal file
23
crates/swc_cli/src/util/trace.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
|
||||
use tracing_subscriber::{
|
||||
filter, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
|
||||
};
|
||||
|
||||
/// Register a tracing subscriber generated event trace format output.
|
||||
pub(crate) fn init_trace(out_file: &Option<String>) -> Option<FlushGuard> {
|
||||
let mut layer = ChromeLayerBuilder::new().include_args(true);
|
||||
|
||||
if let Some(trace_out_file) = out_file {
|
||||
layer = layer.file(trace_out_file.clone());
|
||||
}
|
||||
|
||||
let (chrome_layer, guard) = layer.build();
|
||||
tracing_subscriber::registry()
|
||||
.with(chrome_layer.with_filter(filter::filter_fn(|metadata| {
|
||||
!metadata.target().contains("cranelift") && !metadata.name().contains("log ")
|
||||
})))
|
||||
.try_init()
|
||||
.expect("Should able to register trace");
|
||||
|
||||
Some(guard)
|
||||
}
|
Loading…
Reference in New Issue
Block a user