Skip to content

Commit 358090b

Browse files
committed
Support npm: specifiers in @jsxImportSource pragma
1 parent 8ebb2d6 commit 358090b

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/prefixPlugin.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,16 @@ export default function denoPrefixPlugin(
3030
return await resolveViteSpecifier(id, cache, root, importer);
3131
}
3232
},
33+
transform(code, id) {
34+
if (!id.endsWith(".jsx") && !id.endsWith(".tsx")) return;
35+
36+
const match = code.match(
37+
/\/\*\*\s*@jsxImportSource\s+npm:(@?[^@\s*]+)([^\s*]*)\s*\*\//,
38+
);
39+
40+
if (match) {
41+
return code.replace(match[0], `/** @jsxImportSource ${match[1]} */`);
42+
}
43+
},
3344
};
3445
}

tests/fixture/jsx.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @jsxRuntime automatic */
2+
/** @jsxImportSource npm:preact@^10.24.0 */
3+
4+
function App() {
5+
return <div>hello world</div>;
6+
}
7+
8+
App();
9+
10+
console.log("it works");

tests/fixture/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default defineConfig({
2727
inlineNpm: "inlineNpm.ts",
2828
inlineJsr: "inlineJsr.ts",
2929
inlineHttp: "inlineHttp.ts",
30+
jsx: "jsx.tsx",
3031
resolveInRootDir: "resolveInRootDir.ts",
3132
},
3233
},

tests/plugin.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ describe("Deno plugin", () => {
6666
it("resolve to file in root dir", async () => {
6767
await runTest(`resolveInRootDir.js`);
6868
});
69+
70+
it("resolves @jsxImportSource with npm: prefix", async () => {
71+
await runTest(`jsx.js`);
72+
});
6973
});

0 commit comments

Comments
 (0)