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

Added option to keep_imports #1423

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

aleph1
Copy link

@aleph1 aleph1 commented Aug 4, 2023

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.

@fabiosantoscode
Copy link
Collaborator

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 options = { defaults: false } to disable compression, and mangle = { keep_imports: true }.

You can run these locally with npm run test:compress

@fabiosantoscode
Copy link
Collaborator

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

@fabiosantoscode
Copy link
Collaborator

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.

@aleph1
Copy link
Author

aleph1 commented Aug 4, 2023 via email

@aleph1
Copy link
Author

aleph1 commented Aug 4, 2023 via email

@@ -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);
Copy link
Collaborator

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.

@fabiosantoscode
Copy link
Collaborator

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?

I can take it or leave it :) if someone needs a regex keep_imports, I can ask for another PR.

@aleph1
Copy link
Author

aleph1 commented Aug 4, 2023

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 options = { defaults: false } to disable compression, and mangle = { keep_imports: true }.

You can run these locally with npm run test:compress

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:

keep_imports_true_named: {
    options = {
        defaults: false
    }
    mangle = {
        keep_imports: true,
    }
    input: {
        import {test} from 'test'
    }
    expect: {
        import {test} from 'test'
    }
}

But the following test fails with the output import{test}from"test";:

keep_imports_false_named: {
    options = {
        defaults: false
    }
    mangle = {
        keep_imports: false,
    }
    input: {
        import {test} from 'test'
    }
    expect: {
        import {test as a} from 'test'
    }
}

@fabiosantoscode
Copy link
Collaborator

I think toplevel: true should be added to the mangle options. My bad!

@fabiosantoscode
Copy link
Collaborator

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.

@aleph1
Copy link
Author

aleph1 commented Aug 5, 2023 via email

@fabiosantoscode
Copy link
Collaborator

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!

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

Successfully merging this pull request may close these issues.

None yet

2 participants