Skip to content

Commit

Permalink
feat(playground): add transform and minify (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Oct 14, 2023
1 parent 801d78a commit 2e2b758
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 170 deletions.
11 changes: 4 additions & 7 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ oxc_parser = { version = "0.2.0", path = "crates/oxc_parser" }
oxc_semantic = { version = "0.2.0", path = "crates/oxc_semantic" }
oxc_span = { version = "0.2.0", path = "crates/oxc_span" }
oxc_syntax = { version = "0.2.0", path = "crates/oxc_syntax" }
oxc_transformer = { version = "0.2.0", path = "crates/oxc_transformer" }
oxc_codegen = { version = "0.2.0", path = "crates/oxc_codegen" }

# publish = false
oxc_macros = { path = "crates/oxc_macros" }
oxc_linter = { path = "crates/oxc_linter" }
oxc_type_synthesis = { path = "crates/oxc_type_synthesis" }
oxc_resolver = { path = "crates/oxc_resolver" }
oxc_query = { path = "crates/oxc_query" }
oxc_linter_plugin = { path = "crates/oxc_linter_plugin" }
oxc_transformer = { path = "crates/oxc_transformer" }
oxc_codegen = { path = "crates/oxc_codegen" }

# published by its own
oxc_resolver = { path = "crates/oxc_resolver" }

oxc_tasks_common = { path = "tasks/common" }
oxc_vscode = { path = "editor/vscode/server" }
Expand Down
13 changes: 9 additions & 4 deletions crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_semantic = { workspace = true, optional = true }
oxc_formatter = { workspace = true, optional = true }
# oxc_minifier = { workspace = true, optional = true }
oxc_transformer = { workspace = true, optional = true }
oxc_minifier = { workspace = true, optional = true }
oxc_codegen = { workspace = true, optional = true }

[features]
formatter = ["oxc_formatter"]
semantic = ["oxc_semantic"]
# minifier = ["oxc_minifier"]
serde = ["oxc_ast/serde"]
semantic = ["oxc_semantic"]
formatter = ["oxc_formatter"]
transformer = ["oxc_transformer"]
minifier = ["oxc_minifier"]
codegen = ["oxc_codegen"]
44 changes: 28 additions & 16 deletions crates/oxc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,52 @@ pub mod diagnostics {
pub use oxc_diagnostics::*;
}

#[cfg(feature = "formatter")]
pub mod formatter {
#[doc(inline)]
pub use oxc_formatter::*;
}

pub mod index {
#[doc(inline)]
pub use oxc_index::*;
}

// #[cfg(feature = "minifier")]
// pub mod minifier {
// #[doc(inline)]
// pub use oxc_minifier::*;
// }

pub mod parser {
#[doc(inline)]
pub use oxc_parser::*;
}

pub mod span {
#[doc(inline)]
pub use oxc_span::*;
}

pub mod syntax {
#[doc(inline)]
pub use oxc_syntax::*;
}

#[cfg(feature = "semantic")]
pub mod semantic {
#[doc(inline)]
pub use oxc_semantic::*;
}

pub mod span {
#[cfg(feature = "formatter")]
pub mod formatter {
#[doc(inline)]
pub use oxc_span::*;
pub use oxc_formatter::*;
}

pub mod syntax {
#[cfg(feature = "transformer")]
pub mod transformer {
#[doc(inline)]
pub use oxc_syntax::*;
pub use oxc_transformer::*;
}

#[cfg(feature = "minifier")]
pub mod minifier {
#[doc(inline)]
pub use oxc_minifier::*;
}

#[cfg(feature = "codegen")]
pub mod codegen {
#[doc(inline)]
pub use oxc_codegen::*;
}
2 changes: 1 addition & 1 deletion crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "oxc_codegen"
version = "0.2.0"
publish = false
publish = true
authors.workspace = true
description.workspace = true
edition.workspace = true
Expand Down
55 changes: 2 additions & 53 deletions crates/oxc_minifier/src/compressor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod ast_util;
mod fold;
mod options;
mod prepass;
mod util;

Expand All @@ -15,61 +16,9 @@ use oxc_syntax::{
NumberBase,
};

pub use self::options::CompressOptions;
use self::prepass::Prepass;

#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy)]
pub struct CompressOptions {
/// Various optimizations for boolean context, for example `!!a ? b : c` → `a ? b : c`.
///
/// Default `true`
pub booleans: bool,

/// Remove `debugger;` statements.
///
/// Default `true`
pub drop_debugger: bool,

/// Remove `console.*` statements.
///
/// Default `false`
pub drop_console: bool,

/// Attempt to evaluate constant expressions
///
/// Default `true`
pub evaluate: bool,

/// Join consecutive var statements.
///
/// Default `true`
pub join_vars: bool,

/// Optimizations for do, while and for loops when we can statically determine the condition
///
/// Default `true`
pub loops: bool,

/// Transforms `typeof foo == "undefined" into `foo === void 0`
///
/// Default `true`
pub typeofs: bool,
}

impl Default for CompressOptions {
fn default() -> Self {
Self {
booleans: true,
drop_debugger: true,
drop_console: false,
evaluate: true,
join_vars: true,
loops: true,
typeofs: true,
}
}
}

pub struct Compressor<'a> {
ast: AstBuilder<'a>,
options: CompressOptions,
Expand Down
78 changes: 78 additions & 0 deletions crates/oxc_minifier/src/compressor/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy)]
pub struct CompressOptions {
/// Various optimizations for boolean context, for example `!!a ? b : c` → `a ? b : c`.
///
/// Default `true`
pub booleans: bool,

/// Remove `debugger;` statements.
///
/// Default `true`
pub drop_debugger: bool,

/// Remove `console.*` statements.
///
/// Default `false`
pub drop_console: bool,

/// Attempt to evaluate constant expressions
///
/// Default `true`
pub evaluate: bool,

/// Join consecutive var statements.
///
/// Default `true`
pub join_vars: bool,

/// Optimizations for do, while and for loops when we can statically determine the condition
///
/// Default `true`
pub loops: bool,

/// Transforms `typeof foo == "undefined" into `foo === void 0`
///
/// Default `true`
pub typeofs: bool,
}

impl Default for CompressOptions {
fn default() -> Self {
Self {
booleans: true,
drop_debugger: true,
drop_console: false,
evaluate: true,
join_vars: true,
loops: true,
typeofs: true,
}
}
}

impl CompressOptions {
pub fn all_true() -> Self {
Self {
booleans: true,
drop_debugger: true,
drop_console: true,
evaluate: true,
join_vars: true,
loops: true,
typeofs: true,
}
}

pub fn all_false() -> Self {
Self {
booleans: false,
drop_debugger: false,
drop_console: false,
evaluate: false,
join_vars: false,
loops: false,
typeofs: false,
}
}
}
2 changes: 1 addition & 1 deletion crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "oxc_transformer"
version = "0.2.0"
publish = false
publish = true
authors.workspace = true
description.workspace = true
edition.workspace = true
Expand Down
18 changes: 6 additions & 12 deletions crates/oxc_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@ doctest = false
default = ["console_error_panic_hook"]

[dependencies]
oxc_allocator = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_ast = { workspace = true, features = ["serde"] }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
oxc = { workspace = true, features = ["serde", "semantic", "formatter", "transformer", "minifier", "codegen"] }

oxc_linter = { workspace = true }
oxc_formatter = { workspace = true }
oxc_type_synthesis = { workspace = true }
# oxc_minifier = { workspace = true }
oxc_span = { workspace = true }
oxc_query = { workspace = true }
serde_json = { workspace = true }
trustfall = { workspace = true }
serde = { workspace = true }
oxc_query = { workspace = true }
serde_json = { workspace = true }
trustfall = { workspace = true }
serde = { workspace = true }

wasm-bindgen = { version = "0.2" }
serde-wasm-bindgen = "0.6.0"
Expand Down
Loading

0 comments on commit 2e2b758

Please sign in to comment.