Skip to content

Put context=module content right after the imports #6062

Closed
@dkzlv

Description

@dkzlv

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 :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions