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

fix(ecma/codegen): don't print whitespaces for comments in minify mode #6465

5 changes: 5 additions & 0 deletions .github/workflows/CI.yml
Expand Up @@ -163,6 +163,7 @@ jobs:
profile: minimal

- name: Patch
shell: bash
run: |
echo '[patch.crates-io]' >> bindings/Cargo.toml
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
Expand Down Expand Up @@ -703,6 +704,7 @@ jobs:
${{ !contains(github.event.head_commit.message, 'chore: ') }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
Expand All @@ -723,8 +725,10 @@ jobs:
cache: "yarn"

- name: Patch
shell: bash
run: |
echo '[patch.crates-io]' >> bindings/Cargo.toml
./scripts/cargo/patch-section.sh
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
cd bindings && cargo update

Expand Down Expand Up @@ -784,6 +788,7 @@ jobs:
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: Patch
shell: bash
run: |
echo '[patch.crates-io]' >> bindings/Cargo.toml
./scripts/cargo/patch-section.sh >> bindings/Cargo.toml
Expand Down
12 changes: 9 additions & 3 deletions crates/swc_ecma_codegen/src/comments.rs
Expand Up @@ -12,17 +12,21 @@ macro_rules! write_comments {
for cmt in cmts.iter() {
match cmt.kind {
CommentKind::Line => {
if $prefix_space {
if $prefix_space && !$e.cfg.minify {
$e.wr.write_comment(" ")?;
}

srcmap!($e, cmt, true);

$e.wr.write_comment("//")?;
$e.wr.write_comment(&cmt.text)?;

srcmap!($e, cmt, false);

$e.wr.write_line()?;
}
CommentKind::Block => {
if $prefix_space {
if $prefix_space && !$e.cfg.minify {
$e.wr.write_comment(" ")?;
}

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

$e.wr.write_space()?;
if !$e.cfg.minify {
$e.wr.write_space()?;
}
}
}
}
Expand Down
@@ -0,0 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<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>
</head>
<body>
<script type="text/ng-template">
<!--test-->
<div>
<span> foobar </span>
</div>
</script>

<script type="text/ng-template">
<!--test-->
<div>
<span> foobar </span>
</div>
</script>
<script type="text/html">
<!--test-->
<div>
<span> foobar </span>
</div>
</script>
<script type="text/html">
<!--test-->
<div>
<span> foobar </span>
</div>
</script>
</body>
</html>
@@ -0,0 +1,21 @@
<!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>
<!--test-->
<div>
<span> foobar </span>
</div>
</script><script type=text/ng-template>
<!--test-->
<div>
<span> foobar </span>
</div>
</script><script type=text/html>
<!--test-->
<div>
<span> foobar </span>
</div>
</script><script type=text/html>
<!--test-->
<div>
<span> foobar </span>
</div>
</script>
@@ -1,5 +1,5 @@
<!doctype html><html lang=en><title>Document</title><div>Script:</div>
<script>/* Should mangle top level stuff */ var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>
<script>/* Should mangle top level stuff */var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>

<div>Module:</div>
<script type=module>/* Should mangle top level stuff */ var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>
<script type=module>/* Should mangle top level stuff */var o=function(){let o="bar";o&&(o+="baz"),console.log(o)}</script>
Expand Up @@ -13,5 +13,16 @@
// Keep comment
console.log("test " + "😀" + " test");
</script>
<script>
// Comment
var a = "test";
/* Comment */
var b = "test";
var /* Comment */ c /* Comment */ = /* Comment */ "test" /* Comment */;
</script>
<script>
var a = "test" // test
;
</script>
</body>
</html>
@@ -1,2 +1,4 @@
<!doctype html><html lang=en><title>Document</title><body><script type=module>/* Keep comment */ debugger;// Keep comment
console.log("test \u{1F600} test")</script>
<!doctype html><html lang=en><title>Document</title><body><script type=module>/* Keep comment */debugger;// Keep comment
console.log("test \u{1F600} test")</script><script>// Comment
var a="test",b="test",/* Comment */c/* Comment */=/* Comment */"test"/* Comment */,a="test"// test
</script>
8 changes: 4 additions & 4 deletions node-swc/__tests__/minify_test.mjs
Expand Up @@ -191,7 +191,7 @@ describe("should remove comments", () => {
);
});

it("should preserve licnese", async () => {
it("should preserve license", async () => {
const { code } = await swc.minify(
`
(function(){
Expand All @@ -213,10 +213,10 @@ describe("should remove comments", () => {
expect(code).toMatchInlineSnapshot(`
"(function(){/**
* @license
*/ const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
*/const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
`);
});
it("should remove comment near to licnese", async () => {
it("should remove comment near to license", async () => {
const { code } = await swc.minify(
`
(function(){
Expand All @@ -241,7 +241,7 @@ describe("should remove comments", () => {
expect(code).toMatchInlineSnapshot(`
"(function(){/**
* @license
*/ const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
*/const o=Math.random()+\\"_\\"+Math.random();console.log(o)})();"
`);
});
});
6 changes: 4 additions & 2 deletions scripts/cargo/patch-section.sh
Expand Up @@ -5,11 +5,13 @@ set -eu
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

function toLine {
arr=(${1//\\\t/ })
# >&2 echo "toLine: $@"
arr=(${1//,/ })
# >&2 echo "arr: ${arr[0]} ${arr[1]}"
dir="$(dirname ${arr[1]})"
echo "${arr[0]} = { path = '$dir' }"
}

export -f toLine

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