Skip to content

Commit

Permalink
chore(transformer): support passing babel options path in example (#7790
Browse files Browse the repository at this point in the history
)

Sometimes we want to use Babel test's options to test some code, If the example can get the TransformerOptions from Babel options, then we can easily do it
  • Loading branch information
Dunqing committed Dec 11, 2024
1 parent caa57f1 commit 4a524ed
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_codegen::CodeGenerator;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{EnvOptions, HelperLoaderMode, TransformOptions, Transformer};
use oxc_transformer::{BabelOptions, EnvOptions, HelperLoaderMode, TransformOptions, Transformer};
use pico_args::Arguments;

// Instruction:
Expand All @@ -15,6 +15,8 @@ use pico_args::Arguments;

fn main() {
let mut args = Arguments::from_env();
let babel_options_path: Option<String> =
args.opt_value_from_str("--babel-options").unwrap_or(None);
let targets: Option<String> = args.opt_value_from_str("--targets").unwrap_or(None);
let target: Option<String> = args.opt_value_from_str("--target").unwrap_or(None);
let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string());
Expand Down Expand Up @@ -55,7 +57,11 @@ fn main() {

let (symbols, scopes) = ret.semantic.into_symbol_table_and_scope_tree();

let mut transform_options = if let Some(query) = &targets {
let mut transform_options = if let Some(babel_options_path) = babel_options_path {
let babel_options_path = Path::new(&babel_options_path);
let babel_options = BabelOptions::from_test_path(babel_options_path);
TransformOptions::try_from(&babel_options).unwrap()
} else if let Some(query) = &targets {
TransformOptions {
env: EnvOptions::from_browserslist_query(query).unwrap(),
..TransformOptions::default()
Expand Down

0 comments on commit 4a524ed

Please sign in to comment.