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

Aggressive inlining causes double the optimal filesize #1447

Closed
mbkv opened this issue Sep 17, 2023 · 3 comments
Closed

Aggressive inlining causes double the optimal filesize #1447

mbkv opened this issue Sep 17, 2023 · 3 comments
Labels

Comments

@mbkv
Copy link

mbkv commented Sep 17, 2023

Bug report or Feature request?
sub optimal output

Version (complete output of terser -V or specific git commit)

terser 5.19.4

Complete CLI command or minify() options used

    const minified = await minifyJS(
      { "router.js": file },
      {
        sourceMap: {
          includeSources: true,
          url: "router.js.map",
        },
      },
    );

terser input

https://github.com/mbkv/mbkv.io/blob/4cc82a16037f418071d4df4072b064a53b54be89/markdown/router.js

terser output or error

https://pastebin.com/tCr6NkBT

Expected result

a prettified version of the pastebin shows that terser is inlining "pushHistory" which effectively causes the entire file size to double. Playing around with router.js, if you remove the inner function, and just do a try/catch, it stops it from trying to inline it which suggests it was not intended

the fix for me is to add module: true to the options

@fabiosantoscode
Copy link
Collaborator

I appreciate the report!

This is happening because inline and unused are sometimes out of sync. It seems that inline knows that this const variable will not assign to the global object like a var would, but unused doesn't.

The workaround is to enable the toplevel: true which makes Terser able to remove variables on the top level of the script (IE assume that var a = 1 doesn't create window.a)

@fabiosantoscode
Copy link
Collaborator

Minimal repro (default options):

const dropped_func = () => {
  console.log("PASS")
}

export const kept_func = () => {
    dropped_func()
}

@mbkv
Copy link
Author

mbkv commented Sep 20, 2023

Thanks for looking into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants