Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minifier #826

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions ecmascript/transforms/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ macro_rules! validate {

macro_rules! noop_fold_type {
($F:ty, $N:tt) => {
impl Fold<swc_ecma_ast::$N> for $F {
impl swc_common::Fold<swc_ecma_ast::$N> for $F {
#[inline]
fn fold(&mut self, node: swc_ecma_ast::$N) -> swc_ecma_ast::$N {
node
Expand Down Expand Up @@ -318,7 +318,7 @@ macro_rules! noop_fold_type {

macro_rules! noop_visit_type {
($F:ty, $N:tt) => {
impl Visit<swc_ecma_ast::$N> for $F {
impl swc_common::Visit<swc_ecma_ast::$N> for $F {
#[inline]
fn visit(&mut self, _: &swc_ecma_ast::$N) {}
}
Expand Down
1 change: 1 addition & 0 deletions ecmascript/transforms/src/optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub use self::{inline_globals::InlineGlobals, json_parse::JsonParse, simplify::s

mod inline_globals;
mod json_parse;
pub mod minify;
pub mod simplify;
1 change: 1 addition & 0 deletions ecmascript/transforms/src/optimization/minify/merge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

63 changes: 63 additions & 0 deletions ecmascript/transforms/src/optimization/minify/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::pass::Pass;
use fxhash::FxHashMap;
use smallvec::alloc::borrow::Cow;
use swc_atoms::JsWord;
use swc_common::{pass::CompilerPass, Fold, FoldWith, Mark};
use swc_ecma_ast::{Function, Ident};
use swc_ecma_utils::Id;

mod merge;
mod rename;

pub fn minifier(global_mark: Mark) -> impl CompilerPass + Pass + 'static {
Minifier {
global_mark,
scope: Scope {
parent: None,
..Default::default()
},
}
}

#[derive(Debug)]
struct Minifier<'a> {
/// Identifiers marked with the marker are preserved.
global_mark: Mark,
scope: Scope<'a>,
}

noop_fold_type!(Minifier<'_>);

impl CompilerPass for Minifier<'_> {
fn name() -> Cow<'static, str> {
Cow::Borrowed("minifier")
}
}

impl Minifier<'_> {
fn fold_with_child<T>(&mut self, node: T) -> T
where
T: for<'any> FoldWith<Minifier<'any>>,
{
let mut child = Minifier {
global_mark: self.global_mark,
scope: Scope {
parent: Some(&self.scope),
..Default::default()
},
};
node.fold_children(&mut child)
}
}

#[derive(Debug, Default)]
struct Scope<'a> {
parent: Option<&'a Scope<'a>>,
renamed: FxHashMap<Id, JsWord>,
}

impl Fold<Function> for Minifier<'_> {
fn fold(&mut self, f: Function) -> Function {
self.fold_with_child(f)
}
}
1 change: 1 addition & 0 deletions ecmascript/transforms/src/optimization/minify/rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

41 changes: 41 additions & 0 deletions ecmascript/transforms/tests/optimization_minify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![feature(box_syntax)]
#![feature(test)]
#![feature(box_patterns)]
#![feature(specialization)]

use swc_common::chain;
use swc_ecma_transforms::{optimization::simplifier, resolver};

#[macro_use]
mod common;

fn test(src: &str, expected: &str) {
test_transform!(
::swc_ecma_parser::Syntax::default(),
|_| chain!(resolver(), simplifier(Default::default())),
src,
expected,
true
)
}

fn test_same(src: &str) {
test(src, src)
}

macro_rules! to {
($name:ident, $src:expr, $expected:expr) => {
test!(
Default::default(),
|_| chain!(resolver(), simplifier(Default::default())),
$name,
$src,
$expected
);
};
}

#[test]
fn simple() {
test("{ let abc = 1;}", "{ let a = 1; }");
}