diff --git a/crates/oxc_wasm/.cargo/config.toml b/crates/oxc_wasm/.cargo/config.toml deleted file mode 100644 index e3787381f0cc2..0000000000000 --- a/crates/oxc_wasm/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[profile.release] -opt-level = "s" # Tell `rustc` to optimize for small code size. diff --git a/crates/oxc_wasm/package.json b/crates/oxc_wasm/package.json index dcfa9e4fd06c4..f05aa8a71adef 100644 --- a/crates/oxc_wasm/package.json +++ b/crates/oxc_wasm/package.json @@ -1,5 +1,5 @@ { - "name": "oxc_wasm", + "name": "@oxc/oxc_wasm", "type": "module", "collaborators": [ "Boshen ", @@ -12,9 +12,6 @@ "oxc_wasm.js", "oxc_wasm.d.ts" ], - "dependencies": { - "@oxc-project/types": "workspace:^" - }, "main": "oxc_wasm.js", "types": "oxc_wasm.d.ts", "sideEffects": [ @@ -29,5 +26,8 @@ ], "scripts": { "check": "tsc --lib es2020,dom ./oxc_wasm.d.ts" + }, + "dependencies": { + "@oxc-project/types": "workspace:^" } } diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 871d9b9e6d234..1305a6b854bb0 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -13,7 +13,6 @@ use oxc::{ allocator::Allocator, ast::{ast::Program, Comment as OxcComment, CommentKind, Visit}, codegen::{CodeGenerator, CodegenOptions}, - diagnostics::Error, minifier::{CompressOptions, Minifier, MinifierOptions}, parser::{ParseOptions, Parser, ParserReturn}, semantic::{ @@ -71,9 +70,11 @@ pub struct Oxc { #[wasm_bindgen(readonly, skip_typescript, js_name = "prettierIrText")] pub prettier_ir_text: String, + #[serde(skip)] comments: Vec, - diagnostics: RefCell>, + #[serde(skip)] + diagnostics: RefCell>, #[serde(skip)] serializer: serde_wasm_bindgen::Serializer, @@ -120,21 +121,24 @@ impl Oxc { .diagnostics .borrow() .iter() - .flat_map(|error| { - let Some(labels) = error.labels() else { return vec![] }; - labels - .map(|label| { - OxcDiagnostic { - start: label.offset(), - end: label.offset() + label.len(), - severity: format!("{:?}", error.severity().unwrap_or_default()), - message: format!("{error}"), - } - .serialize(&self.serializer) - .unwrap() + .flat_map(|error| match &error.labels { + Some(labels) => labels + .iter() + .map(|label| OxcDiagnostic { + start: label.offset(), + end: label.offset() + label.len(), + severity: format!("{:?}", error.severity), + message: format!("{error}"), }) - .collect::>() + .collect::>(), + None => vec![OxcDiagnostic { + start: 0, + end: 0, + severity: format!("{:?}", error.severity), + message: format!("{error}"), + }], }) + .map(|v| v.serialize(&self.serializer).unwrap()) .collect::>()) } @@ -169,7 +173,7 @@ impl Oxc { let _linter_options = linter_options.unwrap_or_default(); let minifier_options = minifier_options.unwrap_or_default(); let _codegen_options = codegen_options.unwrap_or_default(); - let _transform_options = transform_options.unwrap_or_default(); + let transform_options = transform_options.unwrap_or_default(); let control_flow_options = control_flow_options.unwrap_or_default(); let allocator = Allocator::default(); @@ -222,7 +226,7 @@ impl Oxc { }); if run_options.syntax.unwrap_or_default() { self.save_diagnostics( - errors.into_iter().chain(semantic_ret.errors).map(Error::from).collect::>(), + errors.into_iter().chain(semantic_ret.errors).collect::>(), ); } @@ -242,13 +246,23 @@ impl Oxc { } if run_options.transform.unwrap_or_default() { - let options = TransformOptions::enable_all(); + let options = transform_options + .target + .as_ref() + .and_then(|target| { + TransformOptions::from_target(target) + .map_err(|err| { + self.save_diagnostics(vec![oxc::diagnostics::OxcDiagnostic::error( + err, + )]); + }) + .ok() + }) + .unwrap_or_default(); let result = Transformer::new(&allocator, &path, &options) .build_with_symbols_and_scopes(symbols, scopes, &mut program); if !result.errors.is_empty() { - self.save_diagnostics( - result.errors.into_iter().map(Error::from).collect::>(), - ); + self.save_diagnostics(result.errors.into_iter().collect::>()); } } @@ -294,7 +308,7 @@ impl Oxc { .build(program); let semantic = Rc::new(semantic_ret.semantic); let linter_ret = Linter::default().run(path, Rc::clone(&semantic)); - let diagnostics = linter_ret.into_iter().map(|e| Error::from(e.error)).collect(); + let diagnostics = linter_ret.into_iter().map(|e| e.error).collect(); self.save_diagnostics(diagnostics); } } @@ -389,7 +403,7 @@ impl Oxc { writer.scope_text } - fn save_diagnostics(&self, diagnostics: Vec) { + fn save_diagnostics(&self, diagnostics: Vec) { self.diagnostics.borrow_mut().extend(diagnostics); } diff --git a/crates/oxc_wasm/src/options.rs b/crates/oxc_wasm/src/options.rs index 3c0f8bb32eee1..3f719795b9f2f 100644 --- a/crates/oxc_wasm/src/options.rs +++ b/crates/oxc_wasm/src/options.rs @@ -70,9 +70,10 @@ pub struct OxcLinterOptions {} #[derive(Debug, Default, Clone, Deserialize, Tsify)] #[tsify(from_wasm_abi)] #[serde(rename_all = "camelCase")] -// allow empty object for future compatibility -#[allow(clippy::empty_structs_with_brackets)] -pub struct OxcTransformerOptions {} +pub struct OxcTransformerOptions { + #[tsify(optional)] + pub target: Option, +} #[derive(Debug, Default, Clone, Deserialize, Tsify)] #[tsify(from_wasm_abi)] diff --git a/justfile b/justfile index a50791f21dd5d..f4675503bd80b 100755 --- a/justfile +++ b/justfile @@ -143,9 +143,9 @@ watch-wasm: just watch 'just build-wasm dev' build-wasm mode="release": - wasm-pack build --out-dir ../../npm/oxc-wasm --target web --{{mode}} --scope oxc crates/oxc_wasm - echo '*.wasm' > npm/oxc-wasm/.gitignore + wasm-pack build crates/oxc_wasm --mode no-install --no-pack --target web --scope oxc --out-dir ../../npm/oxc-wasm --{{mode}} cp crates/oxc_wasm/package.json npm/oxc-wasm/package.json + rm npm/oxc-wasm/.gitignore # Generate the JavaScript global variables. See `tasks/javascript_globals` javascript-globals: diff --git a/npm/oxc-wasm/.gitignore b/npm/oxc-wasm/.gitignore deleted file mode 100644 index 19e1bced9ad8a..0000000000000 --- a/npm/oxc-wasm/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.wasm diff --git a/npm/oxc-wasm/oxc_wasm.d.ts b/npm/oxc-wasm/oxc_wasm.d.ts index 12c93331c9ef4..80fb839a0037e 100644 --- a/npm/oxc-wasm/oxc_wasm.d.ts +++ b/npm/oxc-wasm/oxc_wasm.d.ts @@ -6,6 +6,32 @@ * @returns {any} */ export function browserslist(query: string, opts: any): any; + +import type { Program, Span } from "@oxc-project/types"; +export * from "@oxc-project/types"; + + +export interface Oxc { + ast: Program; + ir: string; + controlFlowGraph: string; + symbols: SymbolTable; + scopeText: string; + codegenText: string; + formattedText: string; + prettierFormattedText: string; + prettierIrText: string; +} + +export interface Comment { + type: CommentType; + value: string; + start: number; + end: number; +} + +export type CommentType = "Line" | "Block"; + export interface OxcOptions { run?: OxcRunOptions; parser?: OxcParserOptions; @@ -37,7 +63,9 @@ export interface OxcParserOptions { export interface OxcLinterOptions {} -export interface OxcTransformerOptions {} +export interface OxcTransformerOptions { + target?: string; +} export interface OxcCodegenOptions { indentation?: number; @@ -66,34 +94,6 @@ export interface OxcCompressOptions { } -import type { Program, Span } from "@oxc-project/types"; -export * from "@oxc-project/types"; - - -export interface Oxc { - ast: Program; - ir: string; - controlFlowGraph: string; - symbols: SymbolTable; - scopeText: string; - codegenText: string; - formattedText: string; - prettierFormattedText: string; - prettierIrText: string; - comments: Comment[]; - diagnostics: Error[]; -} - -export interface Comment { - type: CommentType; - value: string; - start: number; - end: number; -} - -export type CommentType = "Line" | "Block"; - - export type IndexVec = Array; export type CompactStr = string; diff --git a/npm/oxc-wasm/package.json b/npm/oxc-wasm/package.json index dcfa9e4fd06c4..f05aa8a71adef 100644 --- a/npm/oxc-wasm/package.json +++ b/npm/oxc-wasm/package.json @@ -1,5 +1,5 @@ { - "name": "oxc_wasm", + "name": "@oxc/oxc_wasm", "type": "module", "collaborators": [ "Boshen ", @@ -12,9 +12,6 @@ "oxc_wasm.js", "oxc_wasm.d.ts" ], - "dependencies": { - "@oxc-project/types": "workspace:^" - }, "main": "oxc_wasm.js", "types": "oxc_wasm.d.ts", "sideEffects": [ @@ -29,5 +26,8 @@ ], "scripts": { "check": "tsc --lib es2020,dom ./oxc_wasm.d.ts" + }, + "dependencies": { + "@oxc-project/types": "workspace:^" } }