-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
rollup-importmap-plugin.ts
69 lines (63 loc) · 1.9 KB
/
rollup-importmap-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Plugin } from "rollup";
import fs from "node:fs/promises";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { fetch } from "@jspm/generator";
import { Flags } from "../types";
import { getGenerator, JspmError } from "../utils";
const isValidUrl = (url: string) => {
try {
return Boolean(new URL(url));
} catch (e) {
return false;
}
};
export const RollupImportmapPlugin = async (flags: Flags): Promise<Plugin> => {
/*
We need to load the importmap from local into the generator.
And then run a re-install. So, the generator uses the importmap
to resolve any dependencies.
*/
const generator = await getGenerator({ ...flags });
await generator.reinstall();
return {
name: "rollup-importmap-plugin",
resolveId: async (id: string, importer: string) => {
if (isValidUrl(id)) {
const url = new URL(id);
if (url.protocol === "deno:" || url.protocol === "node:") {
return { id, external: true };
}
}
try {
const resolved = generator.importMap.resolve(id, importer);
return { id: resolved };
} catch (err) {
console.warn(
`Failed to resolve ${id} from ${importer}, makring as external`
);
return { id, external: true };
}
},
load: async (id: string) => {
try {
const url = new URL(id);
if (url.protocol === "file:") {
const filePath =
path.extname(url.pathname) === ""
? `${url.pathname}.js`
: url.pathname;
return await fs.readFile(pathToFileURL(filePath), "utf-8");
}
if (url.protocol === "https:") {
const response = await fetch(id);
return await response.text();
}
} catch (err) {
throw new JspmError(
`\n Unsupported protocol ${id} \n ${err.message} \n`
);
}
},
};
};