Skip to content

Commit 08a9e21

Browse files
alexander-akaitkdy1
andauthoredNov 20, 2022
feat(es/codegen): Skip whitespaces for comments in minify mode (#6465)
Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
1 parent 5a3ae50 commit 08a9e21

File tree

9 files changed

+96
-13
lines changed

9 files changed

+96
-13
lines changed
 

‎.github/workflows/CI.yml

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ jobs:
163163
profile: minimal
164164

165165
- name: Patch
166+
shell: bash
166167
run: |
167168
echo '[patch.crates-io]' >> bindings/Cargo.toml
168169
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
@@ -703,6 +704,7 @@ jobs:
703704
${{ !contains(github.event.head_commit.message, 'chore: ') }}
704705
runs-on: ${{ matrix.os }}
705706
strategy:
707+
fail-fast: false
706708
matrix:
707709
os:
708710
- ubuntu-latest
@@ -723,8 +725,10 @@ jobs:
723725
cache: "yarn"
724726

725727
- name: Patch
728+
shell: bash
726729
run: |
727730
echo '[patch.crates-io]' >> bindings/Cargo.toml
731+
./scripts/cargo/patch-section.sh
728732
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
729733
cd bindings && cargo update
730734
@@ -784,6 +788,7 @@ jobs:
784788
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
785789

786790
- name: Patch
791+
shell: bash
787792
run: |
788793
echo '[patch.crates-io]' >> bindings/Cargo.toml
789794
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml

‎crates/swc_ecma_codegen/src/comments.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ macro_rules! write_comments {
1212
for cmt in cmts.iter() {
1313
match cmt.kind {
1414
CommentKind::Line => {
15-
if $prefix_space {
15+
if $prefix_space && !$e.cfg.minify {
1616
$e.wr.write_comment(" ")?;
1717
}
18+
1819
srcmap!($e, cmt, true);
20+
1921
$e.wr.write_comment("//")?;
2022
$e.wr.write_comment(&cmt.text)?;
23+
2124
srcmap!($e, cmt, false);
25+
2226
$e.wr.write_line()?;
2327
}
2428
CommentKind::Block => {
25-
if $prefix_space {
29+
if $prefix_space && !$e.cfg.minify {
2630
$e.wr.write_comment(" ")?;
2731
}
2832

@@ -39,7 +43,9 @@ macro_rules! write_comments {
3943
}
4044
$e.wr.write_comment("*/")?;
4145

42-
$e.wr.write_space()?;
46+
if !$e.cfg.minify {
47+
$e.wr.write_space()?;
48+
}
4349
}
4450
}
4551
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<script type="text/ng-template">
11+
<!--test-->
12+
<div>
13+
<span> foobar </span>
14+
</div>
15+
</script>
16+
17+
<script type="text/ng-template">
18+
<!--test-->
19+
<div>
20+
<span> foobar </span>
21+
</div>
22+
</script>
23+
<script type="text/html">
24+
<!--test-->
25+
<div>
26+
<span> foobar </span>
27+
</div>
28+
</script>
29+
<script type="text/html">
30+
<!--test-->
31+
<div>
32+
<span> foobar </span>
33+
</div>
34+
</script>
35+
</body>
36+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html><html lang=en><meta charset=UTF-8><meta name=viewport content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"><title>Document</title><body><script type=text/ng-template>
2+
<!--test-->
3+
<div>
4+
<span> foobar </span>
5+
</div>
6+
</script><script type=text/ng-template>
7+
<!--test-->
8+
<div>
9+
<span> foobar </span>
10+
</div>
11+
</script><script type=text/html>
12+
<!--test-->
13+
<div>
14+
<span> foobar </span>
15+
</div>
16+
</script><script type=text/html>
17+
<!--test-->
18+
<div>
19+
<span> foobar </span>
20+
</div>
21+
</script>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html><html lang=en><title>Document</title><div>Script:</div>
2-
<script>/* Should mangle top level stuff */ var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>
2+
<script>/* Should mangle top level stuff */var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>
33

44
<div>Module:</div>
5-
<script type=module>/* Should mangle top level stuff */ var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>
5+
<script type=module>/* Should mangle top level stuff */var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>

‎crates/swc_html_minifier/tests/fixture/element/script-options/input.html

+11
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,16 @@
1313
// Keep comment
1414
console.log("test " + "😀" + " test");
1515
</script>
16+
<script>
17+
// Comment
18+
var a = "test";
19+
/* Comment */
20+
var b = "test";
21+
var /* Comment */ c /* Comment */ = /* Comment */ "test" /* Comment */;
22+
</script>
23+
<script>
24+
var a = "test" // test
25+
;
26+
</script>
1627
</body>
1728
</html>
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
<!doctype html><html lang=en><title>Document</title><body><script type=module>/* Keep comment */ debugger;// Keep comment
2-
console.log("test \u{1F600} test")</script>
1+
<!doctype html><html lang=en><title>Document</title><body><script type=module>/* Keep comment */debugger;// Keep comment
2+
console.log("test \u{1F600} test")</script><script>// Comment
3+
var a="test",b="test",/* Comment */c/* Comment */=/* Comment */"test"/* Comment */,a="test"// test
4+
</script>

‎node-swc/__tests__/minify_test.mjs

+4-4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe("should remove comments", () => {
191191
);
192192
});
193193

194-
it("should preserve licnese", async () => {
194+
it("should preserve license", async () => {
195195
const { code } = await swc.minify(
196196
`
197197
(function(){
@@ -213,10 +213,10 @@ describe("should remove comments", () => {
213213
expect(code).toMatchInlineSnapshot(`
214214
"(function(){/**
215215
* @license
216-
*/ const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
216+
*/const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
217217
`);
218218
});
219-
it("should remove comment near to licnese", async () => {
219+
it("should remove comment near to license", async () => {
220220
const { code } = await swc.minify(
221221
`
222222
(function(){
@@ -241,7 +241,7 @@ describe("should remove comments", () => {
241241
expect(code).toMatchInlineSnapshot(`
242242
"(function(){/**
243243
* @license
244-
*/ const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
244+
*/const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
245245
`);
246246
});
247247
});

‎scripts/cargo/patch-section.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ set -eu
55
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
66

77
function toLine {
8-
arr=(${1//\\\t/ })
8+
# >&2 echo "toLine: $@"
9+
arr=(${1//,/ })
10+
# >&2 echo "arr: ${arr[0]} ${arr[1]}"
911
dir="$(dirname ${arr[1]})"
1012
echo "${arr[0]} = { path = '$dir' }"
1113
}
1214

1315
export -f toLine
1416

15-
$SCRIPT_DIR/list-crates.sh | jq '[.name, .manifest_path] | @tsv' | xargs -L 1 -I {} bash -c 'toLine "$@"' _ {}
17+
$SCRIPT_DIR/list-crates.sh | jq '[.name, .manifest_path] | @csv' -r | xargs -I {} bash -c 'toLine "$@"' _ {}

1 commit comments

Comments
 (1)

github-actions[bot] commented on Nov 20, 2022

@github-actions[bot]

Benchmark

Benchmark suite Current: 08a9e21 Previous: 9154bbc Ratio
es/full/bugs-1 343924 ns/iter (± 22485) 389841 ns/iter (± 22054) 0.88
es/full/minify/libraries/antd 1843749333 ns/iter (± 35421072) 2094374257 ns/iter (± 82448426) 0.88
es/full/minify/libraries/d3 387498447 ns/iter (± 5737899) 502630299 ns/iter (± 27607030) 0.77
es/full/minify/libraries/echarts 1572904733 ns/iter (± 20004804) 1873070557 ns/iter (± 758763072) 0.84
es/full/minify/libraries/jquery 98456298 ns/iter (± 1250161) 125668872 ns/iter (± 7586107) 0.78
es/full/minify/libraries/lodash 118166720 ns/iter (± 2299627) 161579804 ns/iter (± 21023829) 0.73
es/full/minify/libraries/moment 59148603 ns/iter (± 1227695) 88708545 ns/iter (± 29765105) 0.67
es/full/minify/libraries/react 19805763 ns/iter (± 491219) 26280308 ns/iter (± 4046363) 0.75
es/full/minify/libraries/terser 294796142 ns/iter (± 8290835) 374533985 ns/iter (± 18064188) 0.79
es/full/minify/libraries/three 565727456 ns/iter (± 7292018) 652642002 ns/iter (± 31323109) 0.87
es/full/minify/libraries/typescript 3371443301 ns/iter (± 25883126) 4136959214 ns/iter (± 252717032) 0.81
es/full/minify/libraries/victory 813563327 ns/iter (± 9071427) 927628403 ns/iter (± 61741347) 0.88
es/full/minify/libraries/vue 149708038 ns/iter (± 2072901) 178428798 ns/iter (± 9735215) 0.84
es/full/codegen/es3 32100 ns/iter (± 588) 35629 ns/iter (± 8609) 0.90
es/full/codegen/es5 32174 ns/iter (± 943) 38103 ns/iter (± 6502) 0.84
es/full/codegen/es2015 32209 ns/iter (± 599) 36890 ns/iter (± 5849) 0.87
es/full/codegen/es2016 32185 ns/iter (± 691) 34100 ns/iter (± 6093) 0.94
es/full/codegen/es2017 32113 ns/iter (± 1559) 38119 ns/iter (± 7737) 0.84
es/full/codegen/es2018 32131 ns/iter (± 932) 37150 ns/iter (± 6840) 0.86
es/full/codegen/es2019 32293 ns/iter (± 1393) 36291 ns/iter (± 6934) 0.89
es/full/codegen/es2020 32442 ns/iter (± 1290) 36868 ns/iter (± 5595) 0.88
es/full/all/es3 186157363 ns/iter (± 4084748) 225387040 ns/iter (± 24353389) 0.83
es/full/all/es5 176933693 ns/iter (± 3280462) 213631035 ns/iter (± 17082855) 0.83
es/full/all/es2015 141729882 ns/iter (± 9827151) 169123795 ns/iter (± 9833735) 0.84
es/full/all/es2016 140943158 ns/iter (± 2932572) 171026993 ns/iter (± 15511849) 0.82
es/full/all/es2017 140890007 ns/iter (± 7364971) 169572869 ns/iter (± 12352684) 0.83
es/full/all/es2018 140345275 ns/iter (± 8031443) 164007453 ns/iter (± 10050642) 0.86
es/full/all/es2019 138207241 ns/iter (± 4187062) 164051983 ns/iter (± 11626394) 0.84
es/full/all/es2020 132581196 ns/iter (± 2728081) 161819590 ns/iter (± 14091917) 0.82
es/full/parser 693874 ns/iter (± 30441) 822178 ns/iter (± 111880) 0.84
es/full/base/fixer 25086 ns/iter (± 896) 28915 ns/iter (± 4716) 0.87
es/full/base/resolver_and_hygiene 88295 ns/iter (± 6324) 97233 ns/iter (± 11134) 0.91
serialization of ast node 212 ns/iter (± 9) 244 ns/iter (± 32) 0.87
serialization of serde 217 ns/iter (± 7) 228 ns/iter (± 28) 0.95

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.