-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[vite] Can't import TypeScript files using .js
extension
#2527
Comments
I think what you’re saying makes sense, but it requires further investigation. Is is possible to import |
If the code to change is in Vite, why not raise the issue with Vite? |
The code change is we set an option for Vite somewhere in our plugin. The OP just linked where Vite checks this option. |
Where do we set Vite options? Why can Vite users not pass options to it? |
Like, the sveltekit stuff also was done in vite: vitejs/vite#8969 |
Ok, so possibly |
The other file types in the HTML-like regex are SFCs, where you have |
This could totally be an upstream change! But I really wasn't sure, so I reported it here first. I really don't mind the "bad" import in our files for now, we've had it worked around for a month or two. Just wanted to let you know before it causes a real problem. In case it helps, I know esbuild fixed this here: evanw/esbuild@cc25614 But it felt like, given I'm using this as a Vite plugin, maybe this is Vite's job. I wasn't sure and I figured you'd know a lot better than me. |
I think this issue isn't related to scanning or prebundling, so we should probably focus on the path resolving part in Vite. But:
At the moment, Astro doesn't inject an esbuild plugin for the scanning part, but maybe it would in the future In practice, Vite should probably resolve However, that issue hasn't been prioritized much recently since the introduction of I think it's fair if mdx doesn't want to set a vite specific option (but you also could if you think it's harmless, like the linked code above). Otherwise I think the proper solution is to not use |
@bluwy What are the values of Also, context: we have a rollup plugin and we have an esbuild plugin. There’s no particular Vite support. If Vite now supports esbuild plugins, and esbuild supports this correctly, we can also start recommending using our esbuild plugin. |
Sorry, you'd set That value is only used in Vite here, so if you set anything that's not ts/mts/cts/tsx, then Vite will see that it's not typescript. It doesn't error if you set some invalid values there.
That's true, but I think it's unlikely for Vite to expand on that to mean actually TS anytime soon. Right now it's only for the
Vite doesn't use esbuild for bundling so an esbuild plugin likely won't work. The rollup plugin is probably the best way still. |
Based on the conversation, I'm think I'm sold that this is a Vite problem. Their extension fallback only kicks in if the importer is a TypeScript file, but that logic seems backwards. The whole point of importing from a There's no reason an MDX file—or an HTML file, or any other type of file—shouldn't be able to import a @bluwy - simultaneous post, sorry! Not sure if you'd agree with the above, but maybe it's a conversation worth continuing over at vitejs/vite#8993. |
I agree that Vite's implementation is backwards and we should fix that issue. But as mentioned, when you're using a bundler there's very less reason to use |
Initial checklist
Affected packages and versions
3.0.1
Link to runnable example
https://codesandbox.io/p/github/aaronadamsCA/mdx-issues/file-extension?file=/src/mdx.mdx
Steps to reproduce
In a
.mdx
file, try importing a.ts
or.tsx
file using a.js
extension.This is specifically supported by Vite: vitejs/vite#5510
And it is the correct file extension to use when importing from TypeScript files, even in a
noEmit
environment: https://www.typescriptlang.org/docs/handbook/modules/theory.htmlExpected behavior
The import should work using a
.js
extension.Actual behavior
The import fails when using a
.js
extension.You can work around this by importing using the
.ts
or.tsx
extension (and setting"allowImportingTsExtensions": true
in yourtsconfig.json
to quiet the corresponding warning), but this should work without this workaround.To fix it, I think your Vite plugin needs to figure out how to tell Vite that
options.isFromTsImporter
should betrue
here:https://github.com/vitejs/vite/blob/3b8f03d789ec3ef1a099c884759bd4e61b03ce7c/packages/vite/src/node/plugins/resolve.ts#L210-L220
It's entirely possible you'll need an upstream change for this though, I really have no idea. Just wanted to share my troubleshooting findings so far.
Runtime
No response
Package manager
No response
OS
No response
Build and bundle tools
No response
The text was updated successfully, but these errors were encountered: