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

TypeScript source resolution does not work with jsx #6866

Closed
7 tasks done
AndrewLeedham opened this issue Feb 11, 2022 · 1 comment · Fixed by #7005
Closed
7 tasks done

TypeScript source resolution does not work with jsx #6866

AndrewLeedham opened this issue Feb 11, 2022 · 1 comment · Fixed by #7005

Comments

@AndrewLeedham
Copy link

Describe the bug

A feature was added recently which looks for .ts/.tsx files based on imports ending in .js/.jsx. However, TypeScript does not export to .jsx unless the jsx mode is set to preserve. Therefore, in a project importing ./MyComponent.js it will not resolve to ./MyComponent.tsx it will try and look for ./MyComponent.ts which does not exist.

I believe the issue is here:

filename.replace(/\.([cm])?(js)(x?)(\?|$)/, '.$1ts$3')
as it will never look for .tsx unless the import has .jsx.

Reproduction

https://stackblitz.com/edit/vitejs-vite-rzmfpt?file=main.tsx

System Info

System:
    OS: macOS 11.2.3
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 36.96 MB / 16.00 GB
    Shell: 5.8 - /usr/local/bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Chrome: 98.0.4758.80
    Edge: 98.0.1108.43
    Firefox: 94.0.1
    Safari: 14.0.3
  npmPackages:
    vite: ^2.8.0 => 2.8.0

Used Package Manager

yarn

Logs

❯ npx vite build --debug
  vite:config no config file found. +0ms
  vite:config using resolved config: {
  vite:config   root: '/home/projects/vitejs-vite-rzmfpt',
  vite:config   base: '/',
  vite:config   mode: 'production',
  vite:config   configFile: undefined,
  vite:config   logLevel: undefined,
  vite:config   clearScreen: undefined,
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: 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   },
  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     build: {}
  vite:config   },
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/home/projects/vitejs-vite-rzmfpt/public',
  vite:config   cacheDir: '/home/projects/vitejs-vite-rzmfpt/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   isProduction: true,
  vite:config   plugins: [
  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',
  vite:config     'vite:worker',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  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   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  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(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config } +6ms
vite v2.8.1 building for production...
✓ 3 modules transformed.
Could not resolve './MyComponent.js' from main.tsx
error during build:
Error: Could not resolve './MyComponent.js' from main.tsx
    at error (/home/projects/vitejs-vite-rzmfpt/node_modules/rollup/dist/shared/rollup.js:160:30)
    at ModuleLoader.handleResolveId (/home/projects/vitejs-vite-rzmfpt/node_modules/rollup/dist/shared/rollup.js:22402:24)
    at eval (/home/projects/vitejs-vite-rzmfpt/node_modules/rollup/dist/shared/rollup.js:22365:26)
[!] Error: unfinished hook action(s) on exit:
(vite:load-fallback) load "/home/projects/vitejs-vite-rzmfpt/node_modules/react-dom/index.js"

Validations

@leebeydoun
Copy link
Contributor

I have a similar issue. In my case I have a monorepo setup and during development I point the packages to the src. In one my packages the index.ts is as follows:

export { List } from "./components/List.js"

// Remaining content truncated

When I try importing this from vite it looks like it tries every extension combination but .tsx. For example, I added a console.log statement to the getTsSrcPath call:

/Users/<full-path-part>/List.ts
/Users/<full-path-part>/List.js.ts
/Users/<full-path-part>/List.js.tsx
/Users/<full-path-part>/List.ts
/Users/<full-path-part>/List.ts
/Users/<full-path-part>/List.js.ts
/Users/<full-path-part>/List.js.tsx
/Users/<full-path-part>/List.ts

@github-actions github-actions bot locked and limited conversation to collaborators Mar 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants