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

Put context=module content right after the imports #6062

Open
dkzlv opened this issue Mar 8, 2021 · 2 comments
Open

Put context=module content right after the imports #6062

dkzlv opened this issue Mar 8, 2021 · 2 comments

Comments

@dkzlv
Copy link
Contributor

dkzlv commented Mar 8, 2021

Is your feature request related to a problem? Please describe.
We don't have const enum in TS support due to isolatedModules setting (more on this here: sveltejs/svelte-preprocess#281).
But I want it so bad.

Describe the solution you'd like
A cool workaround would be to allow terser to inline const objects.
Here's the code:

const States = { veryLongKey: 1, unusedKey: 2 }

export function blablah() {
  return States.veryLongKey
}

If you run it in the Terser REPL, it will turn it into this:

const n=1;export function blablah(){return n}

, which is more or less what const enum does (in reality it inlines the stuff, so there's no variables at all, but that doesn't matter imo).

But here's the problem. If you copy the compiled code from this simple Svelte component, and run it through Terser, the States variable won't disappear! Here, it's highlighted in the image.
Снимок экрана 2021-03-08 в 12 03 02
And the reason, I believe, is that the content of context='module' in the compiled version of the code goes way down to the function instance. But if you move the declaration up so it's right below the imports, it will be inlined! (highlighted as well)
Снимок экрана 2021-03-08 в 12 05 54

As far as I understand it shouldn't break anything in the components/runtime, but will help us get a little bit more optimized code. Free optimization, a very low hanging fruit and a functional replacement for const enum!

Describe alternatives you've considered
None.

How important is this feature to you?
Very nice to have :)

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 27, 2021
@stale
Copy link

stale bot commented Dec 24, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants