|
| 1 | +const extensionRe = /\.[^/]*$/; |
| 2 | +const mains = ["unpkg", "jsdelivr", "browser", "main"]; |
| 3 | + |
| 4 | +(async () => { |
| 5 | + console.log(`import {dependency} from "./dependency.js";`); |
| 6 | + { |
| 7 | + const info = await resolve("d3"); |
| 8 | + console.log(`export const d3 = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 9 | + } |
| 10 | + { |
| 11 | + const info = await resolve("@observablehq/inputs"); |
| 12 | + console.log(`export const inputs = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 13 | + } |
| 14 | + { |
| 15 | + const info = await resolve("@observablehq/plot"); |
| 16 | + console.log(`export const plot = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 17 | + } |
| 18 | + { |
| 19 | + const info = await resolve("@observablehq/graphviz"); |
| 20 | + console.log(`export const graphviz = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 21 | + } |
| 22 | + { |
| 23 | + const info = await resolve("@observablehq/highlight.js"); |
| 24 | + console.log(`export const highlight = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 25 | + } |
| 26 | + { |
| 27 | + const info = await resolve("@observablehq/katex"); |
| 28 | + console.log(`export const katex = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 29 | + } |
| 30 | + { |
| 31 | + const info = await resolve("lodash"); |
| 32 | + console.log(`export const lodash = dependency("${info.name}", "${info.version}", "${info.export.replace(/\.js$/, ".min.js")}");`); |
| 33 | + } |
| 34 | + { |
| 35 | + const info = await resolve("htl"); |
| 36 | + console.log(`export const htl = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 37 | + } |
| 38 | + { |
| 39 | + const info = await resolve("jszip"); |
| 40 | + console.log(`export const jszip = dependency("${info.name}", "${info.version}", "dist/jszip.min.js");`); |
| 41 | + } |
| 42 | + { |
| 43 | + const info = await resolve("[email protected]"); |
| 44 | + console.log(`export const marked = dependency("${info.name}", "${info.version}", "marked.min.js");`); |
| 45 | + } |
| 46 | + { |
| 47 | + const info = await resolve("sql.js"); |
| 48 | + console.log(`export const sql = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 49 | + } |
| 50 | + { |
| 51 | + const info = await resolve("vega"); |
| 52 | + console.log(`export const vega = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 53 | + } |
| 54 | + { |
| 55 | + const info = await resolve("vega-lite"); |
| 56 | + console.log(`export const vegalite = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 57 | + } |
| 58 | + { |
| 59 | + const info = await resolve("vega-lite-api"); |
| 60 | + console.log(`export const vegaliteApi = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 61 | + } |
| 62 | + { |
| 63 | + const info = await resolve("apache-arrow@4"); |
| 64 | + console.log(`export const arrow4 = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 65 | + } |
| 66 | + { |
| 67 | + const info = await resolve("apache-arrow@9"); |
| 68 | + console.log(`export const arrow9 = dependency("${info.name}", "${info.version}", "+esm");`); |
| 69 | + } |
| 70 | + { |
| 71 | + const info = await resolve("arquero"); |
| 72 | + console.log(`export const arquero = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 73 | + } |
| 74 | + { |
| 75 | + const info = await resolve("topojson-client"); |
| 76 | + console.log(`export const topojson = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 77 | + } |
| 78 | + { |
| 79 | + const info = await resolve("exceljs"); |
| 80 | + console.log(`export const exceljs = dependency("${info.name}", "${info.version}", "${info.export}");`); |
| 81 | + } |
| 82 | + { |
| 83 | + const info = await resolve("mermaid"); |
| 84 | + console.log(`export const mermaid = dependency("${info.name}", "${info.version}", "${info.export.replace(/\.core\.js$/, ".min.js")}");`); |
| 85 | + } |
| 86 | + { |
| 87 | + const info = await resolve("leaflet"); |
| 88 | + console.log(`export const leaflet = dependency("${info.name}", "${info.version}", "${info.export.replace(/-src\.js$/, ".js")}");`); |
| 89 | + } |
| 90 | + { |
| 91 | + const info = await resolve("@duckdb/duckdb-wasm"); |
| 92 | + console.log(`export const duckdb = dependency("${info.name}", "${info.version}", "+esm");`); |
| 93 | + } |
| 94 | +})(); |
| 95 | + |
| 96 | +async function resolve(specifier) { |
| 97 | + const response = await fetch(`https://cdn.jsdelivr.net/npm/${specifier}/package.json`); |
| 98 | + const info = await response.json(); |
| 99 | + return { |
| 100 | + name: info.name, |
| 101 | + version: info.version, |
| 102 | + export: main(info) |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +// https://github.com/d3/d3-require/blob/4056a786912e9335a86b41c2b1cdfa392bd14289/src/index.js#L20-L27 |
| 107 | +function main(meta) { |
| 108 | + for (const key of mains) { |
| 109 | + const value = meta[key]; |
| 110 | + if (typeof value === "string") { |
| 111 | + return (extensionRe.test(value) ? value : `${value}.js`).replace(/^\.\//, ""); |
| 112 | + } |
| 113 | + } |
| 114 | + return "index.js"; |
| 115 | +} |
0 commit comments