Skip to content

Draft: Support npm: specifiers in @jsxImportSource pragma#58

Closed
alexgleason wants to merge 1 commit into
denoland:mainfrom
alexgleason:jsx-pragma
Closed

Draft: Support npm: specifiers in @jsxImportSource pragma#58
alexgleason wants to merge 1 commit into
denoland:mainfrom
alexgleason:jsx-pragma

Conversation

@alexgleason

Copy link
Copy Markdown

Currently if you try to use a JSX component from a JSR package in Deno+Vite, you will get an error: Uncaught TypeError: jsx is not a function

This fixes the ability to import JSX components from JSR and use them with Vite. Currently JSR does not transpile JSX components and instead inserts pragma directives like /** @jsxImportSource npm:preact */, which Vite cannot resolve.

See: jsr-io/jsr#24 (comment)

I did a lot of experimentation, and found that when you have a pragma like this:

/** @jsxImportSource npm:preact@^10.24.0 */

it is never intercepted by the Vite plugin system. It's never passed to resolveId. The only way to make it work is to modify the source code file before Vite's JSX loader transforms it.

So I did a simple regex replace to just strip the npm: identifier and @ version because Vite doesn't utilize it anyway. It doesn't seem worth doing full module resolution here because Vite expects a plain npm package name (I'm not 100% sure about that, but I couldn't get it to work with an absolute path in my tests). So just stripping it seems like the best workaround for now.

@alexgleason alexgleason changed the title Support npm: specifiers in @jsxImportSource pragma Draft: Support npm: specifiers in @jsxImportSource pragma Apr 11, 2025
@alexgleason

Copy link
Copy Markdown
Author

Oh shit. I think the actual issue is npm:react@^19.1.0/jsx-runtime getting converted to npm:react because of this:

https://github.com/denoland/deno-vite-plugin/blob/main/src/prefixPlugin.ts#L26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant