Skip to content

Commit

Permalink
rollup plugin support jsr import
Browse files Browse the repository at this point in the history
  • Loading branch information
zuisong committed May 21, 2024
1 parent 001faf5 commit 48a01e8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
6 changes: 3 additions & 3 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json",
"imports": {
"@std/assert": "jsr:@std/assert@^0.225.1",
"@std/async": "jsr:@std/async@^0.224.0",
"@std/testing": "jsr:@std/testing@^0.224.0",
"@std/assert": "jsr:@std/assert",
"@std/async": "jsr:@std/async",
"@std/testing": "jsr:@std/testing",
"esm.sh/": "https://esm.sh/v135/",
"happy-dom": "npm:happy-dom-without-node",
"rollup": "npm:@rollup/[email protected]",
Expand Down
29 changes: 12 additions & 17 deletions deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
"version": "3",
"packages": {
"specifiers": {
"jsr:@std/assert": "jsr:@std/[email protected]",
"jsr:@std/assert@^0.224.0": "jsr:@std/[email protected]",
"jsr:@std/assert@^0.225.1": "jsr:@std/[email protected]",
"jsr:@std/async@^0.224.0": "jsr:@std/[email protected]",
"jsr:@std/fmt@^0.225.0": "jsr:@std/[email protected]",
"jsr:@std/internal@^0.225.0": "jsr:@std/[email protected]",
"jsr:@std/testing@^0.224.0": "jsr:@std/[email protected]",
"jsr:@std/async": "jsr:@std/[email protected]",
"jsr:@std/internal@^0.225.1": "jsr:@std/[email protected]",
"jsr:@std/testing": "jsr:@std/[email protected]",
"npm:@rollup/[email protected]": "npm:@rollup/[email protected]",
"npm:@swc/[email protected]": "npm:@swc/[email protected]",
"npm:@types/node": "npm:@types/[email protected]",
Expand All @@ -17,11 +16,10 @@
"@std/[email protected]": {
"integrity": "8643233ec7aec38a940a8264a6e3eed9bfa44e7a71cc6b3c8874213ff401967f"
},
"@std/[email protected].1": {
"integrity": "7342fa32a6d82c2c7c98ffea566baa9a892d04b22815b9fcf62cedeee5764f9c",
"@std/[email protected].2": {
"integrity": "6fd566c3ea01654d29c2b633298b7fc7599716336233852eb87e9843658fa192",
"dependencies": [
"jsr:@std/fmt@^0.225.0",
"jsr:@std/internal@^0.225.0"
"jsr:@std/internal@^0.225.1"
]
},
"@std/[email protected]": {
Expand All @@ -30,11 +28,8 @@
"jsr:@std/assert@^0.224.0"
]
},
"@std/[email protected]": {
"integrity": "982e7d1a8b8869338cc5b58e2669f7c6889ea9822f1278494dc43edba7f4c4b4"
},
"@std/[email protected]": {
"integrity": "6f501a91aa5ee992f8f9ffb45fa103bc04031df65f6a3550e1c5d7e6304cd34a"
"@std/[email protected]": {
"integrity": "7d8a812be88c2792f15ab9cf46911e521ccca50435948a8f24df5cefda96bde9"
},
"@std/[email protected]": {
"integrity": "371b8a929aa7132240d5dd766a439be8f780ef5c176ab194e0bcab72370c761e"
Expand Down Expand Up @@ -116,9 +111,9 @@
},
"workspace": {
"dependencies": [
"jsr:@std/assert@^0.225.1",
"jsr:@std/async@^0.224.0",
"jsr:@std/testing@^0.224.0",
"jsr:@std/assert",
"jsr:@std/async",
"jsr:@std/testing",
"npm:@rollup/[email protected]",
"npm:@swc/[email protected]",
"npm:happy-dom-without-node"
Expand Down
47 changes: 44 additions & 3 deletions rollup-deno-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ function shouldResolveUri(str: string) {
}
interface ModuleInfo {
modules: Module[];
redirects: Record<string, string>;
packages: Record<string, string>;
}

interface Module {
emit: string;
local: string;
specifier: string;
}
Expand All @@ -16,10 +19,20 @@ export default function denoResolve(baseUrl: string): rollup.Plugin {
const resolver = new DenoResolve();
return {
name: "deno-resolve",
resolveId(source: string, importer: string | undefined) {
async resolveId(source: string, importer: string | undefined) {
if (source.startsWith("node:")) {
return undefined;
}
if (source.startsWith("bun:")) {
return undefined;
}

if (shouldResolveUri(source)) {
return resolveUri(source, importer);
}
if (source.startsWith("jsr:")) {
return await resolver.resolveJsr(source);
}
return import.meta.resolve(source);
},

Expand All @@ -35,14 +48,15 @@ export default function denoResolve(baseUrl: string): rollup.Plugin {

class DenoResolve {
private moduleCache = new Map<string, Module>();
private moduleRedirects = new Map<string, string>();
async resolve(url: URL): Promise<string> {
const module = await this.module(url);
if (!module) {
throw new Error(`invariant: ${url} not found`);
}
return await Deno.readTextFile(module.local);
return await Deno.readTextFile(module.emit ?? module.local);
}
private async module(nameStr: URL): Promise<Module | undefined> {
async module(nameStr: URL): Promise<Module | undefined> {
const foundModule = this.moduleCache.get(nameStr.toString());
if (foundModule) {
return foundModule;
Expand All @@ -51,6 +65,24 @@ class DenoResolve {
for (const module of moduleInfo.modules) {
this.moduleCache.set(module.specifier, module);
}
for (
const [key, value] of [
Object.entries(moduleInfo.packages),
Object.entries(moduleInfo.redirects),
].flat()
) {
if (!this.moduleRedirects.has(key)) {
this.moduleRedirects.set(key, value);
}
}

// console.log("11", nameStr, this);

const redirected = this.moduleRedirects.get(nameStr.toString());
if (redirected) {
return this.module(new URL(redirected));
}

return this.moduleCache.get(nameStr.toString());
}
private async info(nameStr: URL, cwd?: string) {
Expand All @@ -64,4 +96,13 @@ class DenoResolve {
}
return JSON.parse(new TextDecoder().decode(output.stdout)) as ModuleInfo;
}

async resolveJsr(source: string): Promise<string | undefined> {
await this.module(new URL(source));
const res = this.moduleRedirects.get(source);
if (!res) {
return undefined;
}
return res?.startsWith("jsr:") ? this.resolveJsr(res) : res;
}
}

0 comments on commit 48a01e8

Please sign in to comment.