aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.24k stars 43 forks source link

ESM import `~/lib/util.js` doesn't work. #59

Closed JuanM04 closed 1 year ago

JuanM04 commented 2 years ago

With the introduction of ES Modules in TypeScript 4.7, TypeScript needs every import to have an extension. For example, if I have a file util.ts, I would need to import it as ./utils.js (so that when transpiles to plain JavaScript, the import works as expected).

The addition of a .js to an aliased path by the plugin breaks the resolution. For example:

import "~/lib/util" // TS Error: module not found
import "../util.js" // ok
import "~/lib/util.js" // No TS Error, but Rollup throws "Rollup failed to resolve import"
aleclarson commented 2 years ago

Can you fork the plugin, setup a repro in the demo folder, and post link here? Thx

JuanM04 commented 2 years ago

https://github.com/JuanM04/vite-tsconfig-paths/tree/repro-59

The second line in main.jsx has been changed:

- import { Root } from '@/Root'
+ import { Root } from '@/Root.js'

With that setup, when you run pnpm tsc --noEmit that file doesn't throw an error (others do, but they don't matter in this example), but when you run pnpm build it throws. If you revert that change, TypeScript throws and Vite doesn't

aleclarson commented 1 year ago

Duplicate of #54 I think?