fix(optimizer): transform TypeScript files before parsing with es-module-lexer #21273
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #17878
es-module-lexerexpects valid JavaScript but was receiving raw TypeScript. When parsing TypeScript enums, it succeeds without throwing but returnshasModuleSyntax: false, causing Vite to incorrectly apply CommonJS interop to ESM code.Before:
After:
TypeScript files are transformed to JavaScript before parsing, correctly identifying ESM syntax.
Changes
extractExportsData()before callinges-module-lexer.ts,.tsx,.mts,.cts) withtransformWithOxcbefore parsingerasableSyntaxOnlyinplayground/tsconfig.jsonto allow TypeScript enum usage in testsOriginal prompt
This section details on the original issue you should resolve
<issue_title>"vite" rewrites "named import" of typescript enums as "default import" and breaks app</issue_title>
<issue_description>### 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), butviterewrites it as a "default import" (cf below) which is wrong (cf error).viteimport 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-bugReplace yarn PnP module resolution with the traditional
node-modulesyarn config set nodeLinker node-modulesNow 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
vitebreaks our app:yarn dev # go to http://localhost:5173 ; open the dev console ; see an import errorSystem 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.0Used Package Manager
yarn
Logs
viteandvite builddo not produce any error.But loading the website in a browser with
vitewill produce this error:Still, the output of
vite --debug:Reveal output