mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(oxc): add napi transform options (#6268)
This commit is contained in:
parent
37cbabbac4
commit
2f888ed871
13 changed files with 80 additions and 81 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
|
@ -1418,6 +1418,7 @@ dependencies = [
|
|||
"oxc_span",
|
||||
"oxc_syntax",
|
||||
"oxc_transformer",
|
||||
"rustc-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1974,16 +1975,7 @@ dependencies = [
|
|||
"napi",
|
||||
"napi-build",
|
||||
"napi-derive",
|
||||
"oxc_allocator",
|
||||
"oxc_ast",
|
||||
"oxc_codegen",
|
||||
"oxc_diagnostics",
|
||||
"oxc_isolated_declarations",
|
||||
"oxc_parser",
|
||||
"oxc_semantic",
|
||||
"oxc_sourcemap",
|
||||
"oxc_span",
|
||||
"oxc_transformer",
|
||||
"oxc",
|
||||
"rustc-hash",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -47,6 +47,8 @@ oxc_transformer = { workspace = true, optional = true }
|
|||
napi = { workspace = true, optional = true, features = ["async"] }
|
||||
napi-derive = { workspace = true, optional = true }
|
||||
|
||||
rustc-hash = { workspace = true, optional = true }
|
||||
|
||||
[features]
|
||||
full = [
|
||||
"codegen",
|
||||
|
|
@ -59,6 +61,7 @@ full = [
|
|||
"cfg",
|
||||
]
|
||||
|
||||
parser = [] # for napi
|
||||
semantic = ["oxc_semantic"]
|
||||
transformer = ["oxc_transformer"]
|
||||
minifier = ["oxc_mangler", "oxc_minifier"]
|
||||
|
|
@ -78,7 +81,7 @@ sourcemap = ["oxc_sourcemap"]
|
|||
sourcemap_concurrent = ["oxc_sourcemap/concurrent", "sourcemap"]
|
||||
|
||||
wasm = ["oxc_transformer/wasm"]
|
||||
napi = ["dep:napi", "dep:napi-derive"]
|
||||
napi = ["dep:napi", "dep:napi-derive", "dep:rustc-hash"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
|
|
|||
24
crates/oxc/src/napi/isolated_declarations.rs
Normal file
24
crates/oxc/src/napi/isolated_declarations.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use napi_derive::napi;
|
||||
|
||||
use super::source_map::SourceMap;
|
||||
|
||||
#[napi(object)]
|
||||
pub struct IsolatedDeclarationsResult {
|
||||
pub code: String,
|
||||
pub map: Option<SourceMap>,
|
||||
pub errors: Vec<String>,
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct IsolatedDeclarationsOptions {
|
||||
/// Do not emit declarations for code that has an @internal annotation in its JSDoc comment.
|
||||
/// This is an internal compiler option; use at your own risk, because the compiler does not check that the result is valid.
|
||||
///
|
||||
/// Default: `false`
|
||||
///
|
||||
/// See <https://www.typescriptlang.org/tsconfig/#stripInternal>
|
||||
pub strip_internal: Option<bool>,
|
||||
|
||||
pub sourcemap: Option<bool>,
|
||||
}
|
||||
|
|
@ -1,3 +1,11 @@
|
|||
mod parse;
|
||||
#[cfg(feature = "parser")]
|
||||
pub mod parse;
|
||||
|
||||
pub use parse::*;
|
||||
#[cfg(feature = "sourcemap")]
|
||||
pub mod source_map;
|
||||
|
||||
#[cfg(feature = "isolated_declarations")]
|
||||
pub mod isolated_declarations;
|
||||
|
||||
#[cfg(feature = "transformer")]
|
||||
pub mod transform;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
|
|||
|
||||
use oxc_transformer::{JsxRuntime, RewriteExtensionsMode};
|
||||
|
||||
use crate::IsolatedDeclarationsOptions;
|
||||
use super::isolated_declarations::IsolatedDeclarationsOptions;
|
||||
|
||||
/// Options for transforming a JavaScript or TypeScript file.
|
||||
///
|
||||
|
|
@ -21,7 +21,7 @@ test = false
|
|||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
oxc = { workspace = true, features = ["napi", "serialize"] }
|
||||
oxc = { workspace = true, features = ["napi", "serialize", "parser"] }
|
||||
oxc_module_lexer = { workspace = true }
|
||||
|
||||
napi = { workspace = true, features = ["async"] }
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use oxc::{
|
|||
allocator::Allocator,
|
||||
ast::CommentKind,
|
||||
diagnostics::{Error, NamedSource},
|
||||
napi::{Comment, ParseResult, ParserOptions},
|
||||
napi::parse::{Comment, ParseResult, ParserOptions},
|
||||
parser::{ParseOptions, Parser, ParserReturn},
|
||||
span::SourceType,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -21,16 +21,7 @@ test = false
|
|||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
oxc_allocator = { workspace = true }
|
||||
oxc_ast = { workspace = true }
|
||||
oxc_codegen = { workspace = true }
|
||||
oxc_diagnostics = { workspace = true }
|
||||
oxc_isolated_declarations = { workspace = true }
|
||||
oxc_parser = { workspace = true }
|
||||
oxc_semantic = { workspace = true }
|
||||
oxc_sourcemap = { workspace = true }
|
||||
oxc_span = { workspace = true }
|
||||
oxc_transformer = { workspace = true }
|
||||
oxc = { workspace = true, features = ["napi", "isolated_declarations", "transformer", "sourcemap", "codegen", "semantic"] }
|
||||
|
||||
rustc-hash = { workspace = true }
|
||||
|
||||
|
|
|
|||
|
|
@ -4,14 +4,15 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::{ast::Program, Trivias};
|
||||
use oxc_codegen::Codegen;
|
||||
use oxc_diagnostics::{Error, NamedSource, OxcDiagnostic};
|
||||
use oxc_parser::{Parser, ParserReturn};
|
||||
use oxc_span::SourceType;
|
||||
|
||||
use crate::{IsolatedDeclarationsOptions, TransformOptions};
|
||||
use oxc::{
|
||||
allocator::Allocator,
|
||||
ast::{ast::Program, Trivias},
|
||||
codegen::Codegen,
|
||||
diagnostics::{Error, NamedSource, OxcDiagnostic},
|
||||
napi::{isolated_declarations::IsolatedDeclarationsOptions, transform::TransformOptions},
|
||||
parser::{Parser, ParserReturn},
|
||||
span::SourceType,
|
||||
};
|
||||
|
||||
#[must_use]
|
||||
pub(crate) struct TransformContext<'a> {
|
||||
|
|
@ -21,13 +22,13 @@ pub(crate) struct TransformContext<'a> {
|
|||
|
||||
/// Generate source maps?
|
||||
source_map: bool,
|
||||
|
||||
/// Generate `.d.ts` files?
|
||||
///
|
||||
/// Used by [`crate::transform`].
|
||||
declarations: Option<IsolatedDeclarationsOptions>,
|
||||
|
||||
/// Path to the file being transformed.
|
||||
filename: &'a str,
|
||||
|
||||
/// Source text of the file being transformed.
|
||||
source_text: &'a str,
|
||||
source_type: SourceType,
|
||||
|
|
|
|||
|
|
@ -1,31 +1,16 @@
|
|||
use napi_derive::napi;
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_codegen::{CodegenReturn, CommentOptions};
|
||||
use oxc_isolated_declarations::IsolatedDeclarations;
|
||||
use oxc_span::SourceType;
|
||||
use oxc::{
|
||||
allocator::Allocator,
|
||||
codegen::{CodegenReturn, CommentOptions},
|
||||
isolated_declarations::IsolatedDeclarations,
|
||||
napi::{
|
||||
isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult},
|
||||
transform::TransformOptions,
|
||||
},
|
||||
span::SourceType,
|
||||
};
|
||||
|
||||
use crate::{context::TransformContext, SourceMap, TransformOptions};
|
||||
|
||||
#[napi(object)]
|
||||
pub struct IsolatedDeclarationsResult {
|
||||
pub code: String,
|
||||
pub map: Option<SourceMap>,
|
||||
pub errors: Vec<String>,
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct IsolatedDeclarationsOptions {
|
||||
/// Do not emit declarations for code that has an @internal annotation in its JSDoc comment.
|
||||
/// This is an internal compiler option; use at your own risk, because the compiler does not check that the result is valid.
|
||||
///
|
||||
/// Default: `false`
|
||||
///
|
||||
/// See <https://www.typescriptlang.org/tsconfig/#stripInternal>
|
||||
pub strip_internal: Option<bool>,
|
||||
|
||||
pub sourcemap: Option<bool>,
|
||||
}
|
||||
use crate::context::TransformContext;
|
||||
|
||||
/// TypeScript Isolated Declarations for Standalone DTS Emit
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
|
|
@ -62,7 +47,7 @@ pub(crate) fn build_declarations(
|
|||
ctx.allocator,
|
||||
ctx.source_text(),
|
||||
&ctx.trivias,
|
||||
oxc_isolated_declarations::IsolatedDeclarationsOptions {
|
||||
oxc::isolated_declarations::IsolatedDeclarationsOptions {
|
||||
strip_internal: options.strip_internal.unwrap_or(false),
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,6 @@
|
|||
// NOTE: the strange order of struct and `mod` statements is to establish the
|
||||
// desired order in generated `index.d.ts` code. We want options to be on top.
|
||||
// This is not only for aesthetics, but using declarations before they're parsed
|
||||
// breaks NAPI typegen.
|
||||
mod context;
|
||||
mod options;
|
||||
|
||||
pub use crate::options::*;
|
||||
|
||||
mod sourcemap;
|
||||
pub use crate::sourcemap::*;
|
||||
pub use oxc::napi::{isolated_declarations, transform};
|
||||
|
||||
mod isolated_declaration;
|
||||
pub use isolated_declaration::*;
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@ use napi::Either;
|
|||
use napi_derive::napi;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_codegen::CodegenReturn;
|
||||
use oxc_semantic::{ScopeTree, SemanticBuilder, SymbolTable};
|
||||
use oxc_span::SourceType;
|
||||
use oxc_transformer::{
|
||||
InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport, ReplaceGlobalDefines,
|
||||
ReplaceGlobalDefinesConfig, Transformer,
|
||||
use oxc::{
|
||||
allocator::Allocator,
|
||||
codegen::CodegenReturn,
|
||||
napi::{source_map::SourceMap, transform::TransformOptions},
|
||||
semantic::{ScopeTree, SemanticBuilder, SymbolTable},
|
||||
span::SourceType,
|
||||
transformer::{
|
||||
InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport, ReplaceGlobalDefines,
|
||||
ReplaceGlobalDefinesConfig, Transformer,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{context::TransformContext, isolated_declaration, SourceMap, TransformOptions};
|
||||
use crate::{context::TransformContext, isolated_declaration};
|
||||
|
||||
// NOTE: Use JSDoc syntax for all doc comments, not rustdoc.
|
||||
// NOTE: Types must be aligned with [@types/babel__core](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/babel__core/index.d.ts).
|
||||
|
|
@ -115,7 +118,7 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> C
|
|||
let define = options.as_mut().and_then(|options| options.define.take());
|
||||
let inject = options.as_mut().and_then(|options| options.inject.take());
|
||||
|
||||
let options = options.map(oxc_transformer::TransformOptions::from).unwrap_or_default();
|
||||
let options = options.map(oxc::transformer::TransformOptions::from).unwrap_or_default();
|
||||
|
||||
let (mut symbols, mut scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue