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

Importing a TS file from a JS file with using extension .js does not work #11813

Closed
7 tasks done
frank-weindel opened this issue Jan 25, 2023 · 6 comments
Closed
7 tasks done
Labels
duplicate This issue or pull request already exists

Comments

@frank-weindel
Copy link

frank-weindel commented Jan 25, 2023

Describe the bug

There are a number of previous issues/PRs that have solved various aspects of this:

This and the above issues all relate to being able to import a TS file by a .js file extension. The previous issues seem to have solved this issue when the source file is a .ts file or a .vue file (with a special meta indicator). My issue is that TypeScript also supports type checking/compiling .js files with the checkJs config option. In this case, I need Vite to also support looking for a .ts file even if I provide its import path with .js. But that is not happening right now.

The problem is that I don't seem to have any easy way to satisfy both what Vite wants AND what TypeScript wants.

Please see the reproduction and steps below.

Reproduction

https://stackblitz.com/edit/vitejs-vite-6frdv6?file=package.json

Steps to reproduce

  • Run npm install
  • Run npm run tsc
    • Notice that TSC compilation is successful.
  • Run npm run dev
    • Notice the error: [vite] Internal server error: Failed to resolve import "./myTsFile.js" from "main.js". Does the file exist?
  • Change the import line in main.ts to:
    • import { MyTsClass } from './myTsFile.ts'; (the extension is now .ts)
  • Run npm run tsc
    • Note that TSC reports an error: error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './myTsFile.js'?
  • Run npm run dev
    • Notice it builds and runs successfully.

To summarize:

  • tsc wants the extension of the import to be .js
  • vite wants the extension of the import to be .ts

System Info

System:
    OS: macOS 12.2.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 2.98 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.0 - ~/.nodenv/versions/16.14.0/bin/node
    Yarn: 1.22.15 - ~/.nodenv/versions/16.14.0/bin/yarn
    npm: 8.3.1 - ~/.nodenv/versions/16.14.0/bin/npm
  Browsers:
    Chrome: 109.0.5414.87
    Firefox: 108.0.1
    Safari: 15.3

Used Package Manager

npm

Logs

No response

Validations

@suguanYang
Copy link
Contributor

suguanYang commented Feb 2, 2023

I think the problem is that vite use moduleResolution that similar to node to resolve the imports, but the tsc use nodenext that specified in the tsconfig. @sapphi-red

@frank-weindel
Copy link
Author

Is anyone aware of any workaround that can be used to fix this at the moment? Something like this works if all JS import references have an equivalent TS file. But this is not the case in my project.

    resolve: {
      alias: [{ find: /((\/|^)(src|tests)\/.*)\.js$/, replacement: '$1.ts' }],
    },

@suguanYang
Copy link
Contributor

You have 2 solutions:

  1. use node as the moduleResolution, and change the import { MyTsClass } from './myTsFile.ts' to import { MyTsClass } from './myTsFile' , the behavior will keep same as vite
  2. use @rollup/plugin-typescript

@frank-weindel
Copy link
Author

Thank you @suguanYang!

I was just reading the TypeScript 5.0 beta announcement and there's yet a new moduleResolution option coming soon named "bundler" which looks like would be an even better choice than using "node".

@suguanYang
Copy link
Contributor

Thank you @suguanYang!

I was just reading the TypeScript 5.0 beta announcement and there's yet a new moduleResolution option coming soon named "bundler" which looks like would be an even better choice than using "node".

You could check this plugin if its support this new moduleSolution @rollup/plugin-typescript

@sapphi-red
Copy link
Member

Duplicate of #8993

@sapphi-red sapphi-red marked this as a duplicate of #8993 Mar 29, 2023
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Mar 29, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants