"vite" rewrites "named import" of typescript enums as "default import" and breaks app #17878
Open
7 tasks done
Labels
feat: deps optimizer
Esbuild Dependencies Optimization
p3-minor-bug
An edge case that only affects very specific usage (priority)
Describe the bug
I try to use a named import (
import { Status } from 'ts-enum/status.ts'
), I expect the import to work (it works withyarn build
), butvite
rewrites it as a "default import" (cf below) which is wrong (cf error).vite
import rewrite:browser error (from dev console, both Firefox and Chrome):
Issues that might be related: #17727 ; #11231 ; #14018 ; #11783 ; #15434 ; #17619 ; #16742 ; #16293 ; #15620Reproduction
https://gist.github.com/superboum/1d59a67290a3224d83fbc5cb7f97fb03
Steps to reproduce
Create a simple typescript module:
Create a vite project with the typescript template:
yarn create vite vite-bug --template vanilla-ts cd vite-bug
Replace yarn PnP module resolution with the traditional
node-modules
yarn config set nodeLinker node-modules
Now add our dependency (problem also exists if the dependency is pulled from git or a npm registry):
Write the most straightforward javascript that will trigger the bug:
Building works perfectly, you can try with:
But
vite
breaks our app:yarn dev # go to http://localhost:5173 ; open the dev console ; see an import error
System Info
System: OS: Linux 6.6 NixOS 24.05 (Uakari) 24.05 (Uakari) CPU: (4) x64 Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz Memory: 9.71 GB / 15.53 GB Container: Yes Shell: 5.2.26 - /nix/store/pg0zqlym7y3cd8631b3x6cghxcmjl8xa-bash-interactive-5.2p26/bin/bash Binaries: Node: 18.20.4 - /run/current-system/sw/bin/node Yarn: 4.2.2 - /nix/store/wvalqmxg0275mf7alwyl0x0fvjbgcb8j-yarn-berry-4.2.2/bin/yarn npm: 10.7.0 - /run/current-system/sw/bin/npm Browsers: Chromium: 127.0.6533.88 Firefox: 129.0 npmPackages: vite: ^5.4.0 => 5.4.0
Used Package Manager
yarn
Logs
vite
andvite build
do not produce any error.But loading the website in a browser with
vite
will produce this error:Still, the output of
vite --debug
:Reveal output
Workaround
Excluding
ts-enum
(the dependency containing an enum) from optimization seems to fix the issue.It worked for me with the following
vite.config.js
on the reproduction project:And then the import is not broken anymore:
Validations
The text was updated successfully, but these errors were encountered: