Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/prefixPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ export default function denoPrefixPlugin(
return await resolveViteSpecifier(id, cache, root, importer);
}
},
transform(code, id) {
if (!id.endsWith(".jsx") && !id.endsWith(".tsx")) return;

const match = code.match(
/\/\*\*\s*@jsxImportSource\s+npm:(@?[^@\s*]+)([^\s*]*)\s*\*\//,
);

if (match) {
return code.replace(match[0], `/** @jsxImportSource ${match[1]} */`);
}
},
};
}
10 changes: 10 additions & 0 deletions tests/fixture/jsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @jsxRuntime automatic */
/** @jsxImportSource npm:preact@^10.24.0 */

function App() {
return <div>hello world</div>;
}

App();

console.log("it works");
1 change: 1 addition & 0 deletions tests/fixture/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineConfig({
inlineNpm: "inlineNpm.ts",
inlineJsr: "inlineJsr.ts",
inlineHttp: "inlineHttp.ts",
jsx: "jsx.tsx",
resolveInRootDir: "resolveInRootDir.ts",
},
},
Expand Down
4 changes: 4 additions & 0 deletions tests/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ describe("Deno plugin", () => {
it("resolve to file in root dir", async () => {
await runTest(`resolveInRootDir.js`);
});

it("resolves @jsxImportSource with npm: prefix", async () => {
await runTest(`jsx.js`);
});
});