Skip to content

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

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

Closed
wants to merge 1 commit into from

Conversation

alexgleason
Copy link

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
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