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

Tree shaking for cross-module inlined enum values does not work #10384

Closed
7 tasks done
shy-robin opened this issue Oct 9, 2022 · 10 comments
Closed
7 tasks done

Tree shaking for cross-module inlined enum values does not work #10384

shy-robin opened this issue Oct 9, 2022 · 10 comments
Labels
wontfix This will not be worked on

Comments

@shy-robin
Copy link

Describe the bug

I want to use const enums instead of enum to optimize transpiled code, but it only works in a single file. When I use ESM import/export syntax for cross-module inlining, it does not work.
I have tried to use rollup to implement this, and it can work. And now I want to know if this is a bug of Vite.

Reproduction

https://stackblitz.com/edit/vitejs-vite-7lxty9

Steps to reproduce

No response

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitejs/plugin-vue: ^3.1.2 => 3.1.2 
    vite: ^3.1.4 => 3.1.6

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

I have tried to use rollup to implement this, and it can work.

Would you provide a reproduction of this?

@shy-robin
Copy link
Author

@sapphi-red
Copy link
Member

Thanks for the repro!

This does not work because of the limitation of isolatedModules: true. Vite uses esbuild's transform that only supports isolatedModules: true.

If you set typescript({ compilerOptions: { isolatedModules: true } }) in rollup repro, you can see the enum not inlined.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Oct 9, 2022
@sapphi-red sapphi-red added wontfix This will not be worked on and removed pending triage labels Oct 9, 2022
@shy-robin
Copy link
Author

Thanks for your reply!
But it's not the key point. What I expect is that when I use ESM import/export syntax, const enum should be inlined by the use ofvite build. However, when I use rollup, const enum in cross-module such as using export const enum {} and then using import can be inlined.
I want to know if this is the problem of Vite.

image

@sapphi-red
Copy link
Member

Ah, I got it.

Non cross-module enums are inlined because esbuild specially supports it (evanw/esbuild#128).
Cross-module enums inlining is supported by esbuild only when bundling. Vite uses transform that doesn't supported it.

To support cross-module enums inlining, the tool needs to know both:

  1. TypeScript syntax
  2. module graph

But in Vite, TypeScript syntax is processed by esbuild and module graph is handled by rollup. So it's not possible to support.

@shy-robin
Copy link
Author

Cross-module enums inlining is supported by esbu

Thanks for your reply!
So for now, the method I can reduce the size of the output code is not use cross-module enums but replicate enums to another file. Right?

@sapphi-red
Copy link
Member

I guess you could also use define + declare enum.

@shy-robin
Copy link
Author

I guess you could also use define + declare enum.

emmm... Doesn't the type declaration file only work in the development process ?
I have tried to declare enum in a .d.ts file and use it in a .ts file, but enum doesn't be transpiled.

image

@sapphi-red
Copy link
Member

You'll need to add define in the config.
https://vitejs.dev/config/shared-options.html#define

@shy-robin
Copy link
Author

Oh, I got it. Thank you very much.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants