Closed dashavoo closed 2 years ago
As the report says, Does the file exist?
You could choose one of the solutions here:
../constructors
or ../constructors.ts
instead../constructors.ts
to ../constructors.js
@zheeeng I feel like I might be missing something here but: no, it doesn't exist, and if I understand the PR I linked above correctly (and also the corresponding issue #3040) it shouldn't need to.
../constructors.ts
instead results in complaints from the type checker and is (strangely) not the done thing in Typescript../constructors
works fine, and is the workaround I am already usingInterestingly, if I remove the import of the constructors
file but keep the other import, it works fine. Maybe because it is just Type
s and Interface
s so it gets stripped altogether by that point?
So here's the reason: it's all about ESM support.
ESM imports need to end with a file extension. To support this in TypeScript, you can import other TS files using the .js
extension in the import and TypeScript is smart enough to know that you really mean the .ts file. When it builds the output, all the imports have .js extensions and the files themselves are JS at that point.
common patterns look like:
import mod from './my-typescript-module.js' // source is .ts
import mod from './my-explicitly-commonjs-typescript-module.cjs' //source is .cts
import mod from './my-explicitly-esm-typescirpt-module.mjs' // source is .mts
Edit: it looks like this was all covered in #3040 - I look forward to more official support!
This comment seems to indicate that support for this feature has been shipped? I'm using v2.8.3 and this issue is still present.
https://github.com/vitejs/vite/issues/5539#issuecomment-960271520
The issue is because #5510 handles cases where the importer is a TS file only, then it resolves the .js
/.ts
import check. In this case, because the importer is a Vue file (not a TS file), Vite incorrectly skips the import check, hence the error. To fix this, we need to provide a hint somewhere so that Vite knows the Vue file is written in TS, and do the extra import checks.
The issue is because #5510 handles cases where the importer is a TS file only, then it resolves the
.js
/.ts
import check. In this case, because the importer is a Vue file (not a TS file), Vite incorrectly skips the import check, hence the error. To fix this, we need to provide a hint somewhere so that Vite knows the Vue file is written in TS, and do the extra import checks.
it's also broken when used with typescript alias: tsconfig, vite config Error:
Error: [vite]: Rollup failed to resolve import "@src/components/App/index.js" from "src/main.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
Project for reference: https://github.com/wight554/blog-template/ (doesn't contain nodenext changes)
I have to do smth like this to make it work:
resolve: {
alias: [{ find: /^(@?src\/.*)\.js$/, replacement: '$1.ts' }],
},
Describe the bug
Importing from a Typescript file by referencing it with a ".js" file extension is failing with the error below. It is my understanding that it should work (based on looking at #5510).
To reproduce, clone the repository provided, run
pnpm install
andpnpm run dev
.This is a VueJS project, but after sitting in front of this bug report for a long time and agonising over the question, I think it is probably a Vite bug and not a vuejs/core bug. That, or I am making an elementary mistake (sorry).
Reproduction
https://github.com/dashavoo/vite-bug-report
System Info
Used Package Manager
pnpm
Logs
No response
Validations