componentize_qjs_cli/
cli.rs1use componentize_qjs::{ComponentizeOpts, Runtime, componentize};
2
3use anyhow::{Context, Result};
4use clap::Parser;
5use oxc_allocator::Allocator;
6use oxc_codegen::Codegen;
7use oxc_minifier::{
8 CompressOptions, CompressOptionsKeepNames, CompressOptionsUnused, MangleOptions, Minifier,
9 MinifierOptions,
10};
11use oxc_parser::Parser as OxcParser;
12use oxc_span::SourceType;
13
14use std::fs;
15
16#[derive(Parser)]
17#[command(name = "componentize-qjs")]
18#[command(about = "Convert JavaScript to WebAssembly components using QuickJS")]
19pub struct CliArgs {
20 #[arg(short, long)]
22 pub wit: std::path::PathBuf,
23
24 #[arg(short, long)]
26 pub js: std::path::PathBuf,
27
28 #[arg(long, value_name = "PATH")]
30 pub module_root: Option<std::path::PathBuf>,
31
32 #[arg(short, long, default_value = "output.wasm")]
34 pub output: std::path::PathBuf,
35
36 #[arg(short = 'n', long)]
38 pub world: Option<String>,
39
40 #[arg(long)]
42 pub stub_wasi: bool,
43
44 #[arg(short = 'm', long)]
46 pub minify: bool,
47
48 #[arg(long)]
50 pub disable_gc: bool,
51
52 #[arg(long, conflicts_with = "runtime")]
54 pub opt_size: bool,
55
56 #[arg(long, conflicts_with = "runtime")]
59 pub sync: bool,
60
61 #[arg(long, value_name = "PATH")]
63 pub runtime: Option<std::path::PathBuf>,
64}
65
66pub async fn run(args: Vec<String>) -> Result<()> {
68 let args =
69 CliArgs::try_parse_from(std::iter::once("componentize-qjs".to_string()).chain(args))?;
70
71 if !args.wit.exists() {
72 anyhow::bail!("WIT file/directory not found: {}", args.wit.display());
73 }
74 if !args.js.exists() {
75 anyhow::bail!("JavaScript file not found: {}", args.js.display());
76 }
77 if let Some(runtime_file) = &args.runtime
78 && !runtime_file.exists()
79 {
80 anyhow::bail!("Runtime file not found: {}", runtime_file.display());
81 }
82 if let Some(module_root) = &args.module_root
83 && !module_root.exists()
84 {
85 anyhow::bail!("Module root not found: {}", module_root.display());
86 }
87
88 let js_source = fs::read_to_string(&args.js)
89 .with_context(|| format!("failed to read JS file: {}", args.js.display()))?;
90
91 let js_source = if args.minify {
92 let allocator = Allocator::default();
93 let source_type = SourceType::mjs();
94 let ret = OxcParser::new(&allocator, &js_source, source_type).parse();
95 let mut program = ret.program;
96
97 let options = MinifierOptions {
98 mangle: Some(MangleOptions {
99 top_level: Some(false),
100 ..Default::default()
101 }),
102 compress: Some(CompressOptions {
103 unused: CompressOptionsUnused::Keep,
104 keep_names: CompressOptionsKeepNames::all_false(),
105 ..CompressOptions::default()
106 }),
107 };
108 let ret = Minifier::new(options).minify(&allocator, &mut program);
109 Codegen::new()
110 .with_scoping(ret.scoping)
111 .build(&program)
112 .code
113 } else {
114 js_source
115 };
116
117 println!("componentize-qjs");
118 println!(" WIT: {}", args.wit.display());
119 println!(" JS: {}", args.js.display());
120 println!(" Output: {}", args.output.display());
121
122 let runtime = match &args.runtime {
123 Some(file) => Runtime::Custom(&fs::read(file)?),
124 None => match (args.sync, args.opt_size) {
125 (true, true) => Runtime::OptSizeSync,
126 (true, false) => Runtime::DefaultSync,
127 (false, true) => Runtime::OptSize,
128 (false, false) => Runtime::default(),
129 },
130 };
131
132 if args.stub_wasi {
133 println!("Stubbing WASI imports...");
134 }
135
136 let component = componentize(&ComponentizeOpts {
137 wit_path: &args.wit,
138 js_source: &js_source,
139 js_path: Some(&args.js),
140 module_root: args.module_root.as_deref(),
141 world_name: args.world.as_deref(),
142 stub_wasi: args.stub_wasi,
143 disable_gc: args.disable_gc,
144 runtime,
145 })
146 .await?;
147
148 fs::write(&args.output, &component)
149 .with_context(|| format!("failed to write output to {}", args.output.display()))?;
150
151 println!("Component written to {}", args.output.display());
152 println!(" Size: {} bytes", component.len());
153
154 Ok(())
155}