Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ oxc_span = { version = "0.66.0", path = "crates/oxc_span" }
oxc_syntax = { version = "0.66.0", path = "crates/oxc_syntax" }
oxc_transform_napi = { version = "0.66.0", path = "napi/transform" }
oxc_transformer = { version = "0.66.0", path = "crates/oxc_transformer" }
oxc_transformer_plugins = { version = "0.66.0", path = "crates/oxc_transformer_plugins" }
oxc_traverse = { version = "0.66.0", path = "crates/oxc_traverse" }

# publish = false
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ required-features = ["full"]
[dependencies]
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_parser = { workspace = true, features = [] }
oxc_regular_expression = { workspace = true, optional = true }

oxc_ast_visit = { workspace = true, optional = true }
oxc_cfg = { workspace = true, optional = true }
oxc_codegen = { workspace = true, optional = true }
oxc_diagnostics = { workspace = true }
oxc_isolated_declarations = { workspace = true, optional = true }
oxc_mangler = { workspace = true, optional = true }
oxc_minifier = { workspace = true, optional = true }
oxc_parser = { workspace = true, features = [] }
oxc_regular_expression = { workspace = true, optional = true }
oxc_semantic = { workspace = true, optional = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_transformer = { workspace = true, optional = true }
oxc_transformer_plugins = { workspace = true, optional = true }

[features]
default = ["regular_expression"]
Expand All @@ -59,7 +59,7 @@ full = [
]

semantic = ["oxc_semantic"]
transformer = ["oxc_transformer"]
transformer = ["oxc_transformer", "oxc_transformer_plugins"]
minifier = ["oxc_mangler", "oxc_minifier"]
codegen = ["oxc_codegen"]
mangler = ["oxc_mangler"]
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use oxc_minifier::{CompressOptions, Compressor};
use oxc_parser::{ParseOptions, Parser, ParserReturn};
use oxc_semantic::{Scoping, SemanticBuilder, SemanticBuilderReturn};
use oxc_span::SourceType;
use oxc_transformer::{
use oxc_transformer::{TransformOptions, Transformer, TransformerReturn};
use oxc_transformer_plugins::{
InjectGlobalVariables, InjectGlobalVariablesConfig, ReplaceGlobalDefines,
ReplaceGlobalDefinesConfig, TransformOptions, Transformer, TransformerReturn,
ReplaceGlobalDefinesConfig,
};

#[derive(Default)]
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ pub mod transformer {
pub use oxc_transformer::*;
}

#[cfg(feature = "transformer")]
pub mod transformer_plugins {
//! Transformer/Transpiler
//!
//! See the [`oxc_transformer_plugins` module-level documentation](oxc_transformer_plugins) for more
//! information.
#[doc(inline)]
pub use oxc_transformer_plugins::*;
}

#[cfg(feature = "minifier")]
pub mod minifier {
//! Source code minifier.
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@ sha1 = { workspace = true }
insta = { workspace = true }
oxc_codegen = { workspace = true }
oxc_parser = { workspace = true }
oxc_sourcemap = { workspace = true }
oxc_tasks_common = { workspace = true }
pico-args = { workspace = true }
similar = { workspace = true }
2 changes: 0 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ mod regexp;
mod typescript;

mod decorator;
mod plugins;

use common::Common;
use context::TransformCtx;
Expand Down Expand Up @@ -72,7 +71,6 @@ pub use crate::{
ESTarget, Engine, EngineTargets, EnvOptions, Module, TransformOptions,
babel::{BabelEnvOptions, BabelOptions},
},
plugins::*,
proposals::ProposalOptions,
typescript::{RewriteExtensionsMode, TypeScriptOptions},
};
Expand Down
11 changes: 0 additions & 11 deletions crates/oxc_transformer/src/utils/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,3 @@ pub fn create_property_access<'a>(
let property = ctx.ast.identifier_name(SPAN, ctx.ast.atom(property));
Expression::from(ctx.ast.member_expression_static(span, object, property, false))
}

/// `object` -> `object['a']`.
pub fn create_compute_property_access<'a>(
span: Span,
object: Expression<'a>,
property: &str,
ctx: &TraverseCtx<'a>,
) -> Expression<'a> {
let expression = ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(property), None);
Expression::from(ctx.ast.member_expression_computed(span, object, expression, false))
}
1 change: 0 additions & 1 deletion crates/oxc_transformer/tests/integrations/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod es_target;
mod plugins;
mod targets;

use std::path::Path;
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_transformer/tests/integrations/plugins/mod.rs

This file was deleted.

46 changes: 46 additions & 0 deletions crates/oxc_transformer_plugins/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "oxc_transformer_plugins"
version = "0.66.0"
authors.workspace = true
categories.workspace = true
edition.workspace = true
homepage.workspace = true
include = ["/examples", "/src"]
keywords.workspace = true
license.workspace = true
publish = true
repository.workspace = true
rust-version.workspace = true
description.workspace = true

[lints]
workspace = true

[lib]
test = true
doctest = false

[dependencies]
cow-utils = { workspace = true }
itoa = { workspace = true }
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_ast_visit = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_ecmascript = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true, features = ["to_js_string"] }
oxc_transformer = { workspace = true }
oxc_traverse = { workspace = true }
rustc-hash = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
oxc_codegen = { workspace = true }
oxc_parser = { workspace = true }
oxc_sourcemap = { workspace = true }
oxc_tasks_common = { workspace = true }
oxc_transformer = { workspace = true }
similar = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ use oxc_span::SPAN;
use oxc_syntax::identifier::is_identifier_name;
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx, traverse_mut};

use crate::utils::ast_builder::{
create_compute_property_access, create_member_callee, create_property_access,
};

#[derive(Debug, Default)]
pub struct ModuleRunnerTransform<'a> {
/// Uid for generating import binding names.
Expand Down Expand Up @@ -814,11 +810,42 @@ impl<'a> ModuleRunnerTransform<'a> {
}
}

/// `object` -> `object['a']`.
fn create_compute_property_access<'a>(
span: Span,
object: Expression<'a>,
property: &str,
ctx: &TraverseCtx<'a>,
) -> Expression<'a> {
let expression = ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(property), None);
Expression::from(ctx.ast.member_expression_computed(span, object, expression, false))
}

/// `object` -> `object.call`.
pub fn create_member_callee<'a>(
object: Expression<'a>,
property: &'static str,
ctx: &TraverseCtx<'a>,
) -> Expression<'a> {
let property = ctx.ast.identifier_name(SPAN, Atom::from(property));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}

/// `object` -> `object.a`.
pub fn create_property_access<'a>(
span: Span,
object: Expression<'a>,
property: &str,
ctx: &TraverseCtx<'a>,
) -> Expression<'a> {
let property = ctx.ast.identifier_name(SPAN, ctx.ast.atom(property));
Expression::from(ctx.ast.member_expression_static(span, object, property, false))
}

#[cfg(test)]
mod test {
use std::path::Path;

use oxc_ast::AstBuilder;
use rustc_hash::FxHashSet;
use similar::TextDiff;

Expand All @@ -829,9 +856,7 @@ mod test {
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_tasks_common::print_diff_in_terminal;
use oxc_traverse::traverse_mut;

use crate::{JsxOptions, JsxRuntime, TransformOptions, context::TransformCtx, jsx::Jsx};
use oxc_transformer::{JsxRuntime, TransformOptions, Transformer};

use super::ModuleRunnerTransform;

Expand All @@ -848,12 +873,11 @@ mod test {
let mut program = ret.program;
let mut scoping = SemanticBuilder::new().build(&program).semantic.into_scoping();
if is_jsx {
let mut jsx_options = JsxOptions::enable();
jsx_options.runtime = JsxRuntime::Classic;
jsx_options.jsx_plugin = true;
let ctx = TransformCtx::new(Path::new(""), &TransformOptions::default());
let mut jsx = Jsx::new(jsx_options, None, AstBuilder::new(&allocator), &ctx);
scoping = traverse_mut(&mut jsx, &allocator, &mut program, scoping);
let mut options = TransformOptions::default();
options.jsx.runtime = JsxRuntime::Classic;
let ret = Transformer::new(&allocator, Path::new(""), &options)
.build_with_scoping(scoping, &mut program);
scoping = ret.scoping;
}
let (deps, dynamic_deps) =
ModuleRunnerTransform::new().transform(&allocator, &mut program, scoping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport};

use crate::codegen;
use oxc_transformer_plugins::{InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport};

pub fn test(source_text: &str, expected: &str, config: InjectGlobalVariablesConfig) {
use super::codegen;

fn test(source_text: &str, expected: &str, config: InjectGlobalVariablesConfig) {
let source_type = SourceType::default();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_transformer_plugins/tests/integrations/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mod inject_global_variables;
mod replace_global_defines;

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;

pub fn codegen(source_text: &str, source_type: SourceType) -> String {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(&ret.program)
.code
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig};
use oxc_transformer_plugins::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig};

use crate::codegen;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/oxc_transformer_plugins/tests/integrations/replace_global_defines.rs
expression: snapshot
---
- test.js.map
(0:0) "1;\n" --> (0:0) "1;\n"
(1:0) "__OBJECT__;\n" --> (1:0) "({ 'hello': 'test' });\n"
(2:0) "2;\n" --> (2:0) "2;\n"
(3:0) "__STRING__;\n" --> (3:0) "'development';\n"
(4:0) "3;\n" --> (4:0) "3;\n"
(5:0) "log(__OBJECT__)" --> (5:0) "log({ 'hello': 'test' })"
(5:15) ";\n" --> (5:24) ";\n"
(6:0) "4;\n" --> (6:0) "4;\n"
(7:0) "log(__STRING__)" --> (7:0) "log('development')"
(7:15) ";\n" --> (7:18) ";\n"
(8:0) "5;\n" --> (8:0) "5;\n"
(9:0) "__OBJECT__." --> (9:0) "({ 'hello': 'test' })."
(9:11) "hello;\n" --> (9:22) "hello;\n"
(10:0) "6;\n" --> (10:0) "6;\n"
(11:0) "log(__MEMBER__)" --> (11:0) "log(xx.yy.zz)"
(11:15) ";\n" --> (11:13) ";\n"
(12:0) "7;\n" --> (12:0) "7;\n"
9 changes: 6 additions & 3 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ use oxc::{
semantic::{SemanticBuilder, SemanticBuilderReturn},
span::SourceType,
transformer::{
EnvOptions, HelperLoaderMode, HelperLoaderOptions, InjectGlobalVariablesConfig,
InjectImport, JsxRuntime, ModuleRunnerTransform, ProposalOptions,
ReplaceGlobalDefinesConfig, RewriteExtensionsMode,
EnvOptions, HelperLoaderMode, HelperLoaderOptions, JsxRuntime, ProposalOptions,
RewriteExtensionsMode,
},
transformer_plugins::{
InjectGlobalVariablesConfig, InjectImport, ModuleRunnerTransform,
ReplaceGlobalDefinesConfig,
},
};
use oxc_napi::OxcError;
Expand Down
2 changes: 1 addition & 1 deletion tasks/minsize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ oxc_minifier = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true }
oxc_transformer = { workspace = true }
oxc_transformer_plugins = { workspace = true }

flate2 = { workspace = true }
oxc_tasks_common = { workspace = true }
Expand Down
Loading
Loading