-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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 .js typescript resource from a typescript file fails #3040
Comments
I'm sorry, but I can't reproduce this issue on my local machine. |
Ofcourse, here you go: https://github.com/Avocher/vite-bug |
@sodatea here's a code sandbox with the issue: https://codesandbox.io/s/fancy-fire-epcgr?file=/src/main.tsx |
seems like it can be resolved via the vite.config file as follows: import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: [
{
find:/^(.*)\.js$/,
replacement: '$1',
}
]
},
plugins: [reactRefresh()],
}) |
Works in development fine with
|
I added the rollup resolve plugin to the config as a temporary fix:
It works for both development and building |
I've just read several threads in the TypeScript repository to have a better understanding of this issue. The best one that summarized the underlying concerns is this one: microsoft/TypeScript#16577 (comment) In my opinion, I don't think vite should support this particular pattern: It is because unlike So conceptually, I believe that extensionless imports in TypeScript should be preferred. As for this use case:
Try the |
@sodatea I think it's a TS thing, if you want to work ESM you must specify the file extension, and TS requires you to use a |
@danielemesh No, you don't.
|
That's a very solid point, You'd have to know the innerworkings of Vite to understand that argument though (which I do now).
I'm a bit uncertain what the |
I think Node folks are undecided yet nodejs/modules#323 |
On the usage of the experimental feature: Because in production environments, you would have run Then, if you run |
My bad, I should probably have started by saying: we have a quite large library of code that we share between frontend and backend, that's the reason why we need both |
My 2 cents, as someone who works with both Vite, TSC, and Node.js, on the same frontend code. We test extensively, and part of the test suite is running the frontend code using JSDOM in Node.js. For that reason, we use Given that we are using Node.js ESM, and Given that other bundlers are adding functionality to enable this (admittedly) weird behavior in TS (whereby you specify a |
Would just like to add one more request for this. Tried to use Vite this weekend for the first time and encountered this problem. The need to write my TS in a form that is compatible with Vite makes it a no-go choice for me. I do not like to lock-in my code to specific bundler patterns. I should be able to remove Vite from my codebase without having to change every import across it. Given that other bundlers support this pattern in resolution Vite really needs to follow suit and I won't be adopting it until it can, which is a shame because it otherwise seemed great for my small side projects. |
FYI, in the latest team meeting, we've decided to add support for this feature:
|
Fantastic thank you and the team. Looking forward to trying it out! |
great news! |
Great, thank you! |
To align with `tsc` default behavior, mentioned in microsoft/TypeScript#46452 Fixes vitejs#3040
Describe the bug
Importing a .js TypeScript resource from a TypeScript file fails with:
Internal server error: Failed to resolve import
TypeScript wants users to write the same module specifiers as are used in the produced output if they want to use ES Modules. An extensive discussion about the topic can be found here: microsoft/TypeScript#16577. As a result esbuild supports the same feature. Rollup also supports this if you add
extensions: ['.js', '.ts']
to the resolve plugin config. Thus I would expect the same code to work in Vite.I personally would like to be able to run my
.ts
files through eithervite
ortsc
as needed in a"type": "module"
repository.Reproduction
hello.ts
index.ts
System Info
Output of
npx envinfo --system --npmPackages vite --binaries --browsers
:Used package manager: yarn
Logs
Before submitting the issue, please make sure you do the following
The text was updated successfully, but these errors were encountered: