Skip to content

Commit 536c02e

Browse files
committed
Fix parsing npm: specifiers with paths
1 parent 8ebb2d6 commit 536c02e

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

src/prefixPlugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ export default function denoPrefixPlugin(
2222
const resolved = await resolveDeno(id, root);
2323
if (resolved === null) return;
2424

25+
const match = resolved.id.match(/^(@?[^@/]+)(?:@?([^@/]+))?(\/.+)?$/);
26+
if (!match) return;
27+
28+
const [, pkg, _version, path = ''] = match;
29+
2530
// TODO: Resolving custom versions is not supported at the moment
26-
const actual = resolved.id.slice(0, resolved.id.indexOf("@"));
31+
const actual = pkg + path;
2732
const result = await this.resolve(actual);
2833
return result ?? actual;
2934
} else if (id.startsWith("http:") || id.startsWith("https:")) {

src/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function resolveDeno(
126126
};
127127
} else if (mod.kind === "npm") {
128128
return {
129-
id: mod.npmPackage,
129+
id: mod.specifier.replace(/^npm:\//, ""),
130130
kind: mod.kind,
131131
loader: null,
132132
dependencies: [],

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)