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
Added option to keep_imports #1423
base: master
Are you sure you want to change the base?
Conversation
Would appreciate a test there. You can use this linked test as a template https://github.com/terser/terser/blob/master/test/compress/mangleprops-computed.js#L1-L22 Use You can run these locally with |
I don't think you'll need to add something to cli.js, as it just parses options and passes them on. But TypeScript users will appreciate a change here |
Also I'd like to know what you're working on! For some small number of import usages I can see gzip treating straight-up imports more efficiently than a single letter plus |
I’ll aim to make those other changes by early next week. I’m working on a gui library that has a large number of imports and the byte shavings off the production build are not insignificant, though a more clever gzip for js files sounds like it could be a good idea.
… On Aug 4, 2023, at 9:38 AM, Fábio Santos ***@***.***> wrote:
Also I'd like to know what you're working on! For some small number of import usages I can see gzip treating straight-up imports more efficiently than a single letter plus as x. But I can imagine someone building a more clever gzip specifically for compressing JS files. I'm wondering if that's what you're working on.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.
|
But TypeScript users will appreciate a change [here](https://github.com/terser/terser/blob/master/tools/terser.d.ts#L79-L89)
Looking at the TypeScript definitions, I noticed that keep_classnames and keep_fnames can be booleans or RegExps. Would it make sense for me to implement keep_imports so that it uses the same types and logic versus the implementation in the pull request, which is boolean only?
|
@@ -168,7 +168,8 @@ class SymbolDef { | |||
|| this.orig[0] instanceof AST_SymbolDefun) && keep_name(options.keep_fnames, this.orig[0].name) | |||
|| this.orig[0] instanceof AST_SymbolMethod | |||
|| (this.orig[0] instanceof AST_SymbolClass | |||
|| this.orig[0] instanceof AST_SymbolDefClass) && keep_name(options.keep_classnames, this.orig[0].name); | |||
|| this.orig[0] instanceof AST_SymbolDefClass) && keep_name(options.keep_classnames, this.orig[0].name) | |||
|| (this.orig[0] instanceof AST_SymbolImport && options.keep_imports); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per your comment, maybe what's needed to solve the problem is a heuristic such as keeping imports that are used less than 2 or 3 times. this.references.length
should contain this information.
I can take it or leave it :) if someone needs a regex |
When adding the tests I am getting the same result for the output in both of these cases, but the actual output when running terser with the same settings is different and the imported bindings are aliased as expected. This test passes:
But the following test fails with the output
|
I think |
Looks good to merge! I can handle the docs. I'm leaving it up to you whether you'd like to experiment with checking how often an import is used, before deciding whether to mangle, or if you think this is good to merge. |
Great! I think it is good to merge, but I am also willing to do some more experimentation if you would prefer. It might just take me a few days to get to it.
… On Aug 5, 2023, at 7:33 AM, Fábio Santos ***@***.***> wrote:
Looks good to merge! I can handle the docs.
I'm leaving it up to you whether you'd like to experiment with checking how often an import is used, before deciding whether to mangle, or if you think this is good to merge.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.
|
I'm not in any sort of rush. If you have a project with many imports, you could find out whether gzip will like this or not and benefit everyone in the process :) so you could say I would prefer it if you're up to it! |
As per #1420, I have added a mangle option of
keep_imports
. I know the readme will need updating, but am unsure if anything needs to be changed with the CLI, or whether I should be adding any additional tests.