diff --git a/Cargo.lock b/Cargo.lock index 9b922f179ba2a..899b6ef1a0b06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1400,6 +1400,8 @@ checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" name = "oxc" version = "0.30.5" dependencies = [ + "napi", + "napi-derive", "oxc_allocator", "oxc_ast", "oxc_cfg", @@ -1790,12 +1792,8 @@ dependencies = [ "napi", "napi-build", "napi-derive", - "oxc_allocator", - "oxc_ast", - "oxc_diagnostics", + "oxc", "oxc_module_lexer", - "oxc_parser", - "oxc_span", "serde_json", ] diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index 5c542b9878939..c3047ff38dc27 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -44,6 +44,9 @@ oxc_span = { workspace = true } oxc_syntax = { workspace = true } oxc_transformer = { workspace = true, optional = true } +napi = { workspace = true, optional = true, features = ["async"] } +napi-derive = { workspace = true, optional = true } + [features] full = [ "codegen", @@ -62,6 +65,7 @@ minifier = ["oxc_mangler", "oxc_minifier"] codegen = ["oxc_codegen"] mangler = ["oxc_mangler"] cfg = ["oxc_cfg"] +isolated_declarations = ["oxc_isolated_declarations"] serialize = [ "oxc_ast/serialize", @@ -73,9 +77,8 @@ serialize = [ sourcemap = ["oxc_sourcemap"] sourcemap_concurrent = ["oxc_sourcemap/concurrent", "sourcemap"] -isolated_declarations = ["oxc_isolated_declarations"] - wasm = ["oxc_transformer/wasm"] +napi = ["dep:napi", "dep:napi-derive"] [package.metadata.docs.rs] all-features = true diff --git a/crates/oxc/src/lib.rs b/crates/oxc/src/lib.rs index b7cab5a3a756f..74f2aaffc247d 100644 --- a/crates/oxc/src/lib.rs +++ b/crates/oxc/src/lib.rs @@ -3,6 +3,9 @@ #[cfg(feature = "full")] mod compiler; +#[cfg(feature = "napi")] +pub mod napi; + #[cfg(feature = "full")] pub use compiler::{Compiler, CompilerInterface}; diff --git a/crates/oxc/src/napi/mod.rs b/crates/oxc/src/napi/mod.rs new file mode 100644 index 0000000000000..4f42021da88b5 --- /dev/null +++ b/crates/oxc/src/napi/mod.rs @@ -0,0 +1,3 @@ +mod parse; + +pub use parse::*; diff --git a/crates/oxc/src/napi/parse.rs b/crates/oxc/src/napi/parse.rs new file mode 100644 index 0000000000000..9f2c94dd685ad --- /dev/null +++ b/crates/oxc/src/napi/parse.rs @@ -0,0 +1,36 @@ +use napi_derive::napi; + +/// Babel Parser Options +/// +/// +#[napi(object)] +#[derive(Default)] +pub struct ParserOptions { + #[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")] + pub source_type: Option, + pub source_filename: Option, + /// Emit `ParenthesizedExpression` in AST. + /// + /// If this option is true, parenthesized expressions are represented by + /// (non-standard) `ParenthesizedExpression` nodes that have a single `expression` property + /// containing the expression inside parentheses. + /// + /// Default: true + pub preserve_parens: Option, +} + +#[napi(object)] +pub struct ParseResult { + pub program: String, + pub comments: Vec, + pub errors: Vec, +} + +#[napi(object)] +pub struct Comment { + #[napi(ts_type = "'Line' | 'Block'")] + pub r#type: &'static str, + pub value: String, + pub start: u32, + pub end: u32, +} diff --git a/napi/parser/Cargo.toml b/napi/parser/Cargo.toml index 75f05e39c7ddf..8edd893eef398 100644 --- a/napi/parser/Cargo.toml +++ b/napi/parser/Cargo.toml @@ -21,12 +21,8 @@ test = false doctest = false [dependencies] -oxc_allocator = { workspace = true } -oxc_ast = { workspace = true, features = ["serialize"] } -oxc_diagnostics = { workspace = true } -oxc_module_lexer = { path = "../../crates/oxc_module_lexer" } -oxc_parser = { workspace = true } -oxc_span = { workspace = true } +oxc = { workspace = true, features = ["napi", "serialize"] } +oxc_module_lexer = { workspace = true } napi = { workspace = true, features = ["async"] } napi-derive = { workspace = true } diff --git a/napi/parser/src/lib.rs b/napi/parser/src/lib.rs index 87b716ea43f32..5228fe2d0a241 100644 --- a/napi/parser/src/lib.rs +++ b/napi/parser/src/lib.rs @@ -4,49 +4,17 @@ use std::sync::Arc; use napi::{bindgen_prelude::AsyncTask, Task}; use napi_derive::napi; -use oxc_allocator::Allocator; -pub use oxc_ast::ast::Program; -use oxc_ast::CommentKind; -use oxc_diagnostics::{Error, NamedSource}; -use oxc_parser::{ParseOptions, Parser, ParserReturn}; -use oxc_span::SourceType; -pub use crate::module_lexer::*; - -/// Babel Parser Options -/// -/// -#[napi(object)] -#[derive(Default)] -pub struct ParserOptions { - #[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")] - pub source_type: Option, - pub source_filename: Option, - /// Emit `ParenthesizedExpression` in AST. - /// - /// If this option is true, parenthesized expressions are represented by - /// (non-standard) `ParenthesizedExpression` nodes that have a single `expression` property - /// containing the expression inside parentheses. - /// - /// Default: true - pub preserve_parens: Option, -} +use oxc::{ + allocator::Allocator, + ast::CommentKind, + diagnostics::{Error, NamedSource}, + napi::{Comment, ParseResult, ParserOptions}, + parser::{ParseOptions, Parser, ParserReturn}, + span::SourceType, +}; -#[napi(object)] -pub struct ParseResult { - pub program: String, - pub comments: Vec, - pub errors: Vec, -} - -#[napi(object)] -pub struct Comment { - #[napi(ts_type = "'Line' | 'Block'")] - pub r#type: &'static str, - pub value: String, - pub start: u32, - pub end: u32, -} +pub use crate::module_lexer::*; fn parse<'a>( allocator: &'a Allocator, diff --git a/napi/parser/src/module_lexer.rs b/napi/parser/src/module_lexer.rs index 765a7e8403123..ba262ae727ea0 100644 --- a/napi/parser/src/module_lexer.rs +++ b/napi/parser/src/module_lexer.rs @@ -1,6 +1,7 @@ use napi::{bindgen_prelude::AsyncTask, Task}; use napi_derive::napi; -use oxc_allocator::Allocator; + +use oxc::allocator::Allocator; use oxc_module_lexer::ImportType; use crate::{parse, ParserOptions};