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(html/minifier)/(ecma/minifier): bug with mangling #6455

Merged
merged 9 commits into from Nov 17, 2022

Conversation

alexander-akait
Copy link
Collaborator

@alexander-akait alexander-akait commented Nov 16, 2022

Description:

Here two things:

  • FIXED: bug when we don't apply compressor for script node
  • FIXED: bug with topLevel for Program::Script and keep top level stuff by default not mangled
  • FIXED: better default options for JS optimization

I have

<script>
 var topLevel = function test() {
            let foo = "bar";

            if (foo) {
                foo += "baz";
            }

            console.log(foo);
        };
</script>

For options I use: swc_ecma_minifier::option::MinifyOptions::default()

After minification I have:

<script>var topLevel=function test(){let foo="bar";if(foo){foo+="baz"}console.log(foo)}</script>

Yes, we keep topLevel, but foo is not mangling

Also if you use

swc_ecma_minifier::option::MinifyOptions {
                    compress: Some(swc_ecma_minifier::option::CompressOptions::default()),
                    mangle: Some(swc_ecma_minifier::option::MangleOptions {
                        top_level: false,
                        ..Default::default()
                    }),
                    ..Default::default()
                }

You will get (only for swc_ecma_ast::Program::Script, but if I changed it on swc_ecma_ast::Program::Module everything is fine):

<script>var o=function o(){let o="bar";if(o){o+="baz"}console.log(o)}</script>

i.e. top_level is not respected

BREAKING CHANGE:

No

Related issue (if exists):

No

@alexander-akait alexander-akait marked this pull request as ready for review November 17, 2022 02:30
@@ -0,0 +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>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here another bug with printing comments - space between /* Should mangle top level stuff */ and var o, we can should avoid it, because it increases size of bundle (I think this bug happens everywhere with comments), I will fix it in another PR

@alexander-akait alexander-akait changed the title fix(html/minifier): bug with mangling fix(html/minifier)/(ecma/minifier): bug with mangling Nov 17, 2022
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


swc-bump:

  • swc_ecma_minifier

@kdy1 kdy1 enabled auto-merge (squash) November 17, 2022 06:00
@kdy1 kdy1 added this to the Planned milestone Nov 17, 2022
@kdy1 kdy1 self-assigned this Nov 17, 2022
@kdy1 kdy1 merged commit 4d7b920 into swc-project:main Nov 17, 2022
@alexander-akait alexander-akait deleted the bug-with-mangling branch November 17, 2022 13:47
@kdy1 kdy1 modified the milestones: Planned, v1.3.19 Nov 20, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Dec 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants