brillout / vite-plugin-mdx

Vite Plugin for MDX
MIT License
113 stars 36 forks source link

feat: add tsx support for mdx #26

Closed xiaoxiangmoe closed 3 years ago

brillout commented 3 years ago

I'm curious; why do you want this?

TS without intellisense is mostly useless and TS cannot handle .mdx files, so I'm not sure about the benefits here?

xiaoxiangmoe commented 3 years ago

@brillout Because we can copy and paste the TSX code to mdx. And it helps a lot for TS users.

aleclarson commented 3 years ago

I'd be curious if performance (in the context of large .mdx files) is affected at all by this change, but we don't have benchmarks. Some cursory manual tests would probably be enough. Any volunteers? :P

aleclarson commented 3 years ago

Also, as I understand it, the (only?) benefit is that you don't have to strip type assertions when pasting TSX from another file.

silvenon commented 3 years ago

Also, as I understand it, the (only?) benefit is that you don't have to strip type assertions when pasting TSX from another file.

Probably also to be able to use stuff like enums.

silvenon commented 3 years ago

The error comes when @babel/parser tries to parse it somewhere after applying rehype plugins, I'm not sure where exactly, the stack trace doesn't seem to say.

I was going to suggest a rehype plugin that transpiles TSX into JSX, but I'm not sure if that's possible to do 😬 esbuild doesn't support this yet (https://github.com/evanw/esbuild/issues/735), as for Babel I'm not sure if it's possible to pass a custom TypeScript configuration so it doesn't take the project's one.

That would've been a really nice solution to hack MDX to support TSX because it would put you in control, so I'm sad that it didn't seem to go anywhere. 😢

I just wanted to write this to balance out my discouraging words above, so that you know I tried. 😄

aleclarson commented 3 years ago

esbuild doesn't support this yet

We can use sucrase to strip the types.

silvenon commented 3 years ago

But it needs a JSX transform, too, it can't only strip types.

aleclarson commented 3 years ago

@silvenon Esbuild does that

aleclarson commented 3 years ago

I think pasting TypeScript into MDX is rare enough that we won't support this.

Your best bet is making a Remark plugin.

silvenon commented 3 years ago

@aleclarson I meant that sucrase doesn't seem to be able to turn TSX to JSX, only TSX to JS. transforms: ["typescript"] doesn't work for me, I need to configure it with transforms: ["jsx", "typescript"].

But yeah, I agree that this plugin shouldn't be doing this.

brillout commented 3 years ago

👍