Open superboum opened 3 months ago
The issues you linked don't look relevant. Probably this is due to a false detection of cjs in optimizer and a similar issue I remember is this one:
After some digging, it looks like es-module-lexer
somehow manages to "succeed" parsing the ts file, but it's returning hasModuleSyntax: false
. If it were failing, then it'll probably get parsed properly as typescript and esm, but it didn't get that chance.
I made a simpler reproduction here
es-module-lexer requires the input to be a valid JS, so probably we should check the file extension before running parse
.
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 ; #15620~
Reproduction
https://gist.github.com/superboum/1d59a67290a3224d83fbc5cb7f97fb03
Steps to reproduce
Create a simple typescript module:
Create a vite project with the typescript template:
Replace yarn PnP module resolution with the traditional
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:System Info
Used Package Manager
yarn
Logs
vite
andvite build
do not produce any error. But loading the website in a browser withvite
will produce this error:Still, the output of
vite --debug
:Reveal output
``` [nix-shell:/tmp/vite-bug]$ yarn run vite --debug vite:config no config file found. +0ms vite:config using resolved config: { vite:config root: '/tmp/vite-bug', vite:config base: '/', vite:config mode: 'development', vite:config configFile: undefined, vite:config logLevel: undefined, vite:config clearScreen: undefined, vite:config optimizeDeps: { vite:config holdUntilCrawlEnd: true, vite:config force: undefined, vite:config esbuildOptions: { preserveSymlinks: false } vite:config }, vite:config server: { vite:config preTransformRequests: true, vite:config host: undefined, vite:config sourcemapIgnoreList: [Function: isInNodeModules$1], vite:config middlewareMode: false, vite:config fs: { vite:config strict: true, vite:config allow: [Array], vite:config deny: [Array], vite:config cachedChecks: undefined vite:config } vite:config }, vite:config configFileDependencies: [], vite:config inlineConfig: { vite:config root: undefined, vite:config base: undefined, vite:config mode: undefined, vite:config configFile: undefined, vite:config logLevel: undefined, vite:config clearScreen: undefined, vite:config optimizeDeps: { force: undefined }, vite:config server: { host: undefined } vite:config }, vite:config decodedBase: '/', vite:config rawBase: '/', vite:config resolve: { vite:config mainFields: [ 'browser', 'module', 'jsnext:main', 'jsnext' ], vite:config conditions: [], vite:config extensions: [ vite:config '.mjs', '.js', vite:config '.mts', '.ts', vite:config '.jsx', '.tsx', vite:config '.json' vite:config ], vite:config dedupe: [], vite:config preserveSymlinks: false, vite:config alias: [ [Object], [Object] ] vite:config }, vite:config publicDir: '/tmp/vite-bug/public', vite:config cacheDir: '/tmp/vite-bug/node_modules/.vite', vite:config command: 'serve', vite:config ssr: { vite:config target: 'node', vite:config optimizeDeps: { noDiscovery: true, esbuildOptions: [Object] } vite:config }, vite:config isWorker: false, vite:config mainConfig: null, vite:config bundleChain: [], vite:config isProduction: false, vite:config plugins: [ vite:config 'vite:optimized-deps', vite:config 'vite:watch-package-data', vite:config 'vite:pre-alias', vite:config 'alias', vite:config 'vite:modulepreload-polyfill', vite:config 'vite:resolve', vite:config 'vite:html-inline-proxy', vite:config 'vite:css', vite:config 'vite:esbuild', vite:config 'vite:json', vite:config 'vite:wasm-helper', vite:config 'vite:worker', vite:config 'vite:asset', vite:config 'vite:wasm-fallback', vite:config 'vite:define', vite:config 'vite:css-post', vite:config 'vite:worker-import-meta-url', vite:config 'vite:asset-import-meta-url', vite:config 'vite:dynamic-import-vars', vite:config 'vite:import-glob', vite:config 'vite:client-inject', vite:config 'vite:css-analysis', vite:config 'vite:import-analysis' vite:config ], vite:config css: { lightningcss: undefined }, vite:config esbuild: { jsxDev: true }, vite:config build: { vite:config target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ], vite:config cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ], vite:config outDir: 'dist', vite:config assetsDir: 'assets', vite:config assetsInlineLimit: 4096, vite:config cssCodeSplit: true, vite:config sourcemap: false, vite:config rollupOptions: {}, vite:config minify: 'esbuild', vite:config terserOptions: {}, vite:config write: true, vite:config emptyOutDir: null, vite:config copyPublicDir: true, vite:config manifest: false, vite:config lib: false, vite:config ssr: false, vite:config ssrManifest: false, vite:config ssrEmitAssets: false, vite:config reportCompressedSize: true, vite:config chunkSizeWarningLimit: 500, vite:config watch: null, vite:config commonjsOptions: { include: [Array], extensions: [Array] }, vite:config dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }, vite:config modulePreload: { polyfill: true }, vite:config cssMinify: true vite:config }, vite:config preview: { vite:config port: undefined, vite:config strictPort: undefined, vite:config host: undefined, vite:config https: undefined, vite:config open: undefined, vite:config proxy: undefined, vite:config cors: undefined, vite:config headers: undefined vite:config }, vite:config envDir: '/tmp/vite-bug', vite:config env: { BASE_URL: '/', MODE: 'development', DEV: true, PROD: false }, vite:config assetsInclude: [Function: assetsInclude], vite:config logger: { vite:config hasWarned: false, vite:config info: [Function: info], vite:config warn: [Function: warn], vite:config warnOnce: [Function: warnOnce], vite:config error: [Function: error], vite:config clearScreen: [Function: clearScreen], vite:config hasErrorLogged: [Function: hasErrorLogged] vite:config }, vite:config packageCache: Map(1) { vite:config 'fnpd_/tmp/vite-bug' => { vite:config dir: '/tmp/vite-bug', vite:config data: [Object], vite:config hasSideEffects: [Function: hasSideEffects], vite:config webResolvedImports: {}, vite:config nodeResolvedImports: {}, vite:config setResolvedCache: [Function: setResolvedCache], vite:config getResolvedCache: [Function: getResolvedCache] vite:config }, vite:config set: [Function (anonymous)] vite:config }, vite:config createResolver: [Function: createResolver], vite:config worker: { format: 'iife', plugins: '() => plugins', rollupOptions: {} }, vite:config appType: 'spa', vite:config experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false }, vite:config getSortedPlugins: [Function: getSortedPlugins], vite:config getSortedPluginHooks: [Function: getSortedPluginHooks] vite:config } +10ms vite:deps Hash is consistent. Skipping. Use --force to override. +0ms VITE v5.4.0 ready in 194 ms ➜ Local: http://localhost:5173/ ➜ Network: use --host to expose ➜ press h + enter to show help vite:html-fallback Rewriting GET / to /index.html +0ms vite:time 38.75ms /index.html +0ms vite:resolve 7.77ms /src/main.ts -> /tmp/vite-bug/src/main.ts +0ms vite:load 22.27ms [fs] /src/main.ts +0ms vite:resolve 0.56ms ts-enum/status -> /tmp/vite-bug/node_modules/.vite/deps/ts-enum_status.js?v=30357745 +75ms vite:import-analysis /node_modules/.vite/deps/ts-enum_status.js?v=30357745 needs interop +0ms vite:optimize-deps load /tmp/vite-bug/node_modules/.vite/deps/ts-enum_status.js +0ms vite:import-analysis 8.90ms [1 imports rewritten] src/main.ts +5ms vite:transform 55.90ms /src/main.ts +0ms vite:load 6.74ms [plugin] /node_modules/.vite/deps/ts-enum_status.js?v=30357745 +64ms vite:import-analysis 0.48ms [no imports] node_modules/.vite/deps/ts-enum_status.js?v=30357745 +7ms vite:transform 1.35ms /node_modules/.vite/deps/ts-enum_status.js?v=30357745 +7ms vite:resolve 0.98ms /@vite/client -> /tmp/vite-bug/node_modules/vite/dist/client/client.mjs +128ms vite:cache [memory] /src/main.ts +0ms vite:time 1.83ms /src/main.ts +215ms vite:load 6.60ms [fs] /@vite/client +125ms vite:resolve 0.45ms @vite/env -> /tmp/vite-bug/node_modules/vite/dist/client/env.mjs +15ms vite:import-analysis 7.85ms [1 imports rewritten] node_modules/vite/dist/client/client.mjs +133ms vite:transform 8.80ms /@vite/client +133ms vite:time 19.86ms /@vite/client +12ms vite:load 5.62ms [fs] /node_modules/vite/dist/client/env.mjs +15ms vite:import-analysis 0.10ms [no imports] node_modules/vite/dist/client/env.mjs +6ms vite:transform 0.49ms /node_modules/vite/dist/client/env.mjs +6ms vite:cache [memory] /node_modules/.vite/deps/ts-enum_status.js?v=30357745 +36ms vite:time 0.60ms /node_modules/.vite/deps/ts-enum_status.js?v=30357745 +23ms vite:cache [304] /node_modules/vite/dist/client/env.mjs +0ms vite:time 0.31ms /node_modules/vite/dist/client/env.mjs +60ms ```Workaround
Excluding
ts-enum
(the dependency containing an enum) from optimization seems to fix the issue. It worked for me with the followingvite.config.js
on the reproduction project:And then the import is not broken anymore:
Validations