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
96 if let Some(diagnostic) = ret.diagnostics.first() {
97 anyhow::bail!("failed to parse JavaScript before minification: {diagnostic}");
98 }
99
100 let mut program = ret.program;
101 let options = MinifierOptions {
102 mangle: Some(MangleOptions {
103 top_level: Some(false),
104 ..Default::default()
105 }),
106 compress: Some(CompressOptions {
107 unused: CompressOptionsUnused::Keep,
108 keep_names: CompressOptionsKeepNames::all_false(),
109 ..CompressOptions::default()
110 }),
111 };
112
113 let ret = Minifier::new(options).minify(&allocator, &mut program);
114 Codegen::new()
115 .with_scoping(ret.scoping)
116 .build(&program)
117 .code
118 } else {
119 js_source
120 };
121
122 println!("componentize-qjs");
123 println!(" WIT: {}", args.wit.display());
124 println!(" JS: {}", args.js.display());
125 println!(" Output: {}", args.output.display());
126
127 let custom_runtime = args
128 .runtime
129 .as_ref()
130 .map(|file| fs::read(file).context("failed to read runtime file"))
131 .transpose()?;
132
133 let runtime = match custom_runtime.as_deref() {
134 Some(wasm) => Runtime::Custom(wasm),
135 None => Runtime::builtin(args.sync, args.opt_size),
136 };
137
138 if args.stub_wasi {
139 println!("Stubbing WASI imports...");
140 }
141
142 let component = componentize(&ComponentizeOpts {
143 wit_path: &args.wit,
144 js_source: &js_source,
145 js_path: Some(&args.js),
146 module_root: args.module_root.as_deref(),
147 world_name: args.world.as_deref(),
148 stub_wasi: args.stub_wasi,
149 disable_gc: args.disable_gc,
150 runtime,
151 })
152 .await?;
153
154 fs::write(&args.output, &component)
155 .with_context(|| format!("failed to write output to {}", args.output.display()))?;
156
157 println!("Component written to {}", args.output.display());
158 println!(" Size: {} bytes", component.len());
159
160 Ok(())
161}