Skip to content

Commit

Permalink
feat(es/minifier): Implement minifier partially (#1302)
Browse files Browse the repository at this point in the history
Co-authored-by: Fábio Santos <[email protected]>
  • Loading branch information
kdy1 and fabiosantoscode authored May 20, 2021
1 parent b6589af commit c6b22c5
Show file tree
Hide file tree
Showing 6,904 changed files with 65,882 additions and 47 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 4 additions & 0 deletions .github/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
1 change: 1 addition & 0 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
- swc_ecma_dep_graph
- swc_ecma_ext_transforms
- swc_ecma_loader
- swc_ecma_minifier
- swc_ecma_parser
- swc_ecma_preset_env
- swc_ecma_transforms
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[submodule "ecmascript/test262-parser-tests"]
path = ecmascript/parser/tests/test262-parser
url = https://github.com/tc39/test262-parser-tests.git
shallow = true
[submodule "vendor/terser"]
path = vendor/terser
url = https://github.com/terser/terser.git
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"useTabs": false
}
13 changes: 13 additions & 0 deletions common/src/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ impl EqIgnoreSpan for Span {
}
}

impl<T> EqIgnoreSpan for [T]
where
T: EqIgnoreSpan,
{
fn eq_ignore_span(&self, other: &Self) -> bool {
self.len() == other.len()
&& self
.iter()
.zip(other.iter())
.all(|(a, b)| a.eq_ignore_span(b))
}
}

impl<T> EqIgnoreSpan for Option<T>
where
T: EqIgnoreSpan,
Expand Down
2 changes: 2 additions & 0 deletions ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all-features = true
[features]
codegen = ["swc_ecma_codegen"]
dep_graph = ["swc_ecma_dep_graph"]
minifier = ["swc_ecma_minifier"]
parser = ["swc_ecma_parser"]
transforms = ["swc_ecma_transforms"]
utils = ["swc_ecma_utils"]
Expand All @@ -30,6 +31,7 @@ typescript = ["swc_ecma_transforms/typescript"]
swc_ecma_ast = {version = "0.45.0", path = "./ast"}
swc_ecma_codegen = {version = "0.55.0", path = "./codegen", optional = true}
swc_ecma_dep_graph = {version = "0.25.0", path = "./dep-graph", optional = true}
swc_ecma_minifier = {version = "0.1.0-beta.0", path = "./minifier", optional = true}
swc_ecma_parser = {version = "0.57.0", path = "./parser", optional = true}
swc_ecma_transforms = {version = "0.50.0", path = "./transforms", optional = true}
swc_ecma_utils = {version = "0.36.0", path = "./utils", optional = true}
Expand Down
35 changes: 35 additions & 0 deletions ecmascript/minifier/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
authors = ["강동윤 <[email protected]>"]
description = "Ecmascript code minifier."
documentation = "https://swc.rs/rustdoc/swc_ecma_minifier/"
edition = "2018"
include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_minifier"
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0-beta.0"

[dependencies]
fxhash = "0.2.1"
log = "0.4"
once_cell = "1.5.2"
pretty_assertions = "0.6.1"
regex = "1.5.3"
retain_mut = "0.1.2"
serde = {version = "1.0.118", features = ["derive"]}
serde_json = "1.0.61"
serde_regex = "1.1.0"
swc_atoms = {version = "0.2", path = "../../atoms"}
swc_common = {version = "0.10.8", path = "../../common"}
swc_ecma_ast = {version = "0.45.0", path = "../ast"}
swc_ecma_codegen = {version = "0.55.0", path = "../codegen"}
swc_ecma_transforms = {version = "0.50.0", path = "../transforms/", features = ["optimization"]}
swc_ecma_transforms_base = {version = "0.15.0", path = "../transforms/base"}
swc_ecma_utils = {version = "0.36.0", path = "../utils"}
swc_ecma_visit = {version = "0.31.0", path = "../visit"}

[dev-dependencies]
ansi_term = "0.12.1"
swc_ecma_parser = {version = "0.57.0", path = "../parser"}
testing = {version = "0.10.2", path = "../../testing"}
walkdir = "2.3.1"
7 changes: 7 additions & 0 deletions ecmascript/minifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Minifier

EcmaScript minifier for the swc project. This is basically a port of terser.

## Copying tests

Replace the content of `terser/test/compress.js` with it of `scripts/compress.js` and run `npm run test:compress`
15 changes: 15 additions & 0 deletions ecmascript/minifier/scripts/add-golden.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
#
# This script automatically add tests to golden.
# Note that this is append-only.
set -eu

cargo test --test compress \
| grep 'js .\.\. ok$' \
| sed -e 's!test fixture_terser__compress__!!' \
| sed -e 's! ... ok!!' \
| sed -e 's!__!/!g' \
| sed -e 's!_js!.js!' \
>> tests/golden.txt

./scripts/sort.sh
12 changes: 12 additions & 0 deletions ecmascript/minifier/scripts/ignore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eu

cargo test --test compress $1 \
| grep 'js .\.\. FAILED$' \
| sed -e 's!test fixture_terser__compress__!!' \
| sed -e 's! ... FAILED!!' \
| sed -e 's!__!/!g' \
| sed -e 's!_js!.js!' \
>> tests/ignored.txt

./scripts/sort.sh
47 changes: 47 additions & 0 deletions ecmascript/minifier/scripts/jsprops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Run this script to build jsprops.json

const fs = require('fs')

// Compatibility fix for some standard defined globals not defined on every js environment
var new_globals = ["Symbol", "Map", "Promise", "Proxy", "Reflect", "Set", "WeakMap", "WeakSet"];
var objects = {};
var global_ref = typeof global === "object" ? global : self;

new_globals.forEach(function (new_global) {
objects[new_global] = global_ref[new_global] || new Function();
});

const addedProps = new Set();

const add = propName => addedProps.add(propName);

[
"null",
"true",
"false",
"NaN",
"Infinity",
"-Infinity",
"undefined",
].forEach(add);
[ Object, Array, Function, Number,
String, Boolean, Error, Math,
Date, RegExp, objects.Symbol, ArrayBuffer,
DataView, decodeURI, decodeURIComponent,
encodeURI, encodeURIComponent, eval, EvalError,
Float32Array, Float64Array, Int8Array, Int16Array,
Int32Array, isFinite, isNaN, JSON, objects.Map, parseFloat,
parseInt, objects.Promise, objects.Proxy, RangeError, ReferenceError,
objects.Reflect, objects.Set, SyntaxError, TypeError, Uint8Array,
Uint8ClampedArray, Uint16Array, Uint32Array, URIError,
objects.WeakMap, objects.WeakSet
].forEach((ctor) => {
Object.getOwnPropertyNames(ctor).map(add);
if (ctor.prototype) {
Object.getOwnPropertyNames(ctor.prototype).map(add);
}
});

const propsJSON = JSON.stringify([...addedProps].sort(), null, 4)

fs.writeFileSync(__dirname + '/../src/lists/jsprops.json', propsJSON)
22 changes: 22 additions & 0 deletions ecmascript/minifier/scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
#
# This script exists to prevent regression.
#
# This script invoked tests two time.
# For first, it runs tests in no-regression mode.
# In the mode, only tests listed in tests/golden.txt will be tested.
#
# For second invokation, arguments are passed to cargo test so the user can
# filter tests.
#
set -eu

if [ -z "$@" ]; then
./scripts/sort.sh

export RUST_LOG=swc_ecma_minifier=trace

GOLDEN_ONLY=1 cargo test --test compress
fi

cargo test --test compress $@
11 changes: 11 additions & 0 deletions ecmascript/minifier/scripts/sort.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -eu

# Fix sorting differences between Linux and Mac
export LC_ALL=C

cat tests/golden.txt | awk NF | sort | uniq | awk '{$1=$1};1' | uniq | sort > tests/golden_sorted.txt
mv tests/golden_sorted.txt tests/golden.txt

cat tests/ignored.txt | awk NF | sort | uniq | awk '{$1=$1};1' | uniq | sort > tests/ignored_sorted.txt
mv tests/ignored_sorted.txt tests/ignored.txt
Loading

0 comments on commit c6b22c5

Please sign in to comment.