@esm.sh/import-map 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -54
- package/dist/index.mjs +563 -5
- package/package.json +7 -7
- package/types/index.d.ts +59 -27
- package/dist/add.mjs +0 -374
- package/dist/blank.mjs +0 -17
- package/dist/parse.mjs +0 -88
- package/dist/resolve.mjs +0 -46
- package/dist/support.mjs +0 -6
package/dist/add.mjs
DELETED
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
import { satisfies, valid } from "semver";
|
|
2
|
-
async function addImport(importMap, specifier, noSRI) {
|
|
3
|
-
const imp = parseImportSpecifier(specifier);
|
|
4
|
-
const config = importMap.config ?? {};
|
|
5
|
-
const target = normalizeTarget(config.target);
|
|
6
|
-
const cdnOrigin = getCdnOrigin(config.cdn);
|
|
7
|
-
const meta = await fetchImportMeta(cdnOrigin, imp, target);
|
|
8
|
-
const mark = /* @__PURE__ */ new Set();
|
|
9
|
-
await addImportMeta(importMap, mark, meta, false, void 0, cdnOrigin, target, noSRI ?? false);
|
|
10
|
-
}
|
|
11
|
-
const KNOWN_TARGETS = /* @__PURE__ */ new Set([
|
|
12
|
-
"es2015",
|
|
13
|
-
"es2016",
|
|
14
|
-
"es2017",
|
|
15
|
-
"es2018",
|
|
16
|
-
"es2019",
|
|
17
|
-
"es2020",
|
|
18
|
-
"es2021",
|
|
19
|
-
"es2022",
|
|
20
|
-
"es2023",
|
|
21
|
-
"es2024",
|
|
22
|
-
"esnext"
|
|
23
|
-
]);
|
|
24
|
-
const ESM_SEGMENTS = /* @__PURE__ */ new Set([
|
|
25
|
-
"es2015",
|
|
26
|
-
"es2016",
|
|
27
|
-
"es2017",
|
|
28
|
-
"es2018",
|
|
29
|
-
"es2019",
|
|
30
|
-
"es2020",
|
|
31
|
-
"es2021",
|
|
32
|
-
"es2022",
|
|
33
|
-
"es2023",
|
|
34
|
-
"es2024",
|
|
35
|
-
"esnext",
|
|
36
|
-
"denonext",
|
|
37
|
-
"deno",
|
|
38
|
-
"node"
|
|
39
|
-
]);
|
|
40
|
-
const SPECIFIER_MARK_SEPARATOR = "\0";
|
|
41
|
-
const META_CACHE = /* @__PURE__ */ new Map();
|
|
42
|
-
function parseImportSpecifier(specifier) {
|
|
43
|
-
let source = specifier.trim();
|
|
44
|
-
const imp = {
|
|
45
|
-
name: "",
|
|
46
|
-
version: "",
|
|
47
|
-
subPath: "",
|
|
48
|
-
github: false,
|
|
49
|
-
jsr: false,
|
|
50
|
-
external: false,
|
|
51
|
-
dev: false
|
|
52
|
-
};
|
|
53
|
-
if (source.startsWith("gh:")) {
|
|
54
|
-
imp.github = true;
|
|
55
|
-
source = source.slice(3);
|
|
56
|
-
} else if (source.startsWith("jsr:")) {
|
|
57
|
-
imp.jsr = true;
|
|
58
|
-
source = source.slice(4);
|
|
59
|
-
}
|
|
60
|
-
let scopeName = "";
|
|
61
|
-
if ((source.startsWith("@") || imp.github) && source.includes("/")) {
|
|
62
|
-
[scopeName, source] = splitByFirst(source, "/");
|
|
63
|
-
}
|
|
64
|
-
let packageAndVersion = "";
|
|
65
|
-
[packageAndVersion, imp.subPath] = splitByFirst(source, "/");
|
|
66
|
-
[imp.name, imp.version] = splitByFirst(packageAndVersion, "@");
|
|
67
|
-
if (scopeName) {
|
|
68
|
-
imp.name = `${scopeName}/${imp.name}`;
|
|
69
|
-
}
|
|
70
|
-
if (!imp.name) {
|
|
71
|
-
throw new Error(`invalid package name or version: ${specifier}`);
|
|
72
|
-
}
|
|
73
|
-
return imp;
|
|
74
|
-
}
|
|
75
|
-
function normalizeTarget(target) {
|
|
76
|
-
if (target && KNOWN_TARGETS.has(target)) {
|
|
77
|
-
return target;
|
|
78
|
-
}
|
|
79
|
-
return "es2022";
|
|
80
|
-
}
|
|
81
|
-
function getCdnOrigin(cdn) {
|
|
82
|
-
if (cdn && (cdn.startsWith("https://") || cdn.startsWith("http://"))) {
|
|
83
|
-
return cdn.replace(/\/+$/, "");
|
|
84
|
-
}
|
|
85
|
-
return "https://esm.sh";
|
|
86
|
-
}
|
|
87
|
-
function specifierOf(imp) {
|
|
88
|
-
const prefix = imp.github ? "gh:" : imp.jsr ? "jsr:" : "";
|
|
89
|
-
return `${prefix}${imp.name}${imp.subPath ? `/${imp.subPath}` : ""}`;
|
|
90
|
-
}
|
|
91
|
-
function esmSpecifierOf(imp) {
|
|
92
|
-
const prefix = imp.github ? "gh/" : imp.jsr ? "jsr/" : "";
|
|
93
|
-
const external = hasExternalImports(imp) ? "*" : "";
|
|
94
|
-
return `${prefix}${external}${imp.name}@${imp.version}`;
|
|
95
|
-
}
|
|
96
|
-
function registryPrefix(imp) {
|
|
97
|
-
if (imp.github) {
|
|
98
|
-
return "gh/";
|
|
99
|
-
}
|
|
100
|
-
if (imp.jsr) {
|
|
101
|
-
return "jsr/";
|
|
102
|
-
}
|
|
103
|
-
return "";
|
|
104
|
-
}
|
|
105
|
-
function hasExternalImports(meta) {
|
|
106
|
-
if (meta.peerImports.length > 0) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
for (const dep of meta.imports) {
|
|
110
|
-
if (!dep.startsWith("/node/") && !dep.startsWith(`/${meta.name}@`)) {
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
function moduleUrlOf(cdnOrigin, target, imp) {
|
|
117
|
-
let url = `${cdnOrigin}/${esmSpecifierOf(imp)}/${target}/`;
|
|
118
|
-
if (imp.subPath) {
|
|
119
|
-
if (imp.dev || imp.subPath === "jsx-dev-runtime") {
|
|
120
|
-
url += `${imp.subPath}.development.mjs`;
|
|
121
|
-
} else {
|
|
122
|
-
url += `${imp.subPath}.mjs`;
|
|
123
|
-
}
|
|
124
|
-
return url;
|
|
125
|
-
}
|
|
126
|
-
const fileName = imp.name.includes("/") ? imp.name.split("/").at(-1) : imp.name;
|
|
127
|
-
return `${url}${fileName}.mjs`;
|
|
128
|
-
}
|
|
129
|
-
async function fetchImportMeta(cdnOrigin, imp, target) {
|
|
130
|
-
const star = imp.external ? "*" : "";
|
|
131
|
-
const version = imp.version ? `@${imp.version}` : "";
|
|
132
|
-
const subPath = imp.subPath ? `/${imp.subPath}` : "";
|
|
133
|
-
const targetQuery = target !== "es2022" ? `&target=${encodeURIComponent(target)}` : "";
|
|
134
|
-
const url = `${cdnOrigin}/${star}${registryPrefix(imp)}${imp.name}${version}${subPath}?meta${targetQuery}`;
|
|
135
|
-
const cached = META_CACHE.get(url);
|
|
136
|
-
if (cached) {
|
|
137
|
-
return cached;
|
|
138
|
-
}
|
|
139
|
-
const pending = (async () => {
|
|
140
|
-
const res = await fetch(url);
|
|
141
|
-
if (res.status === 404) {
|
|
142
|
-
throw new Error(`package not found: ${imp.name}${version}${subPath}`);
|
|
143
|
-
}
|
|
144
|
-
if (!res.ok) {
|
|
145
|
-
throw new Error(`unexpected http status ${res.status}: ${await res.text()}`);
|
|
146
|
-
}
|
|
147
|
-
const bodyText = await res.text();
|
|
148
|
-
let data;
|
|
149
|
-
try {
|
|
150
|
-
data = JSON.parse(bodyText);
|
|
151
|
-
} catch {
|
|
152
|
-
throw new Error(`invalid meta response from ${url}: ${bodyText.slice(0, 200)}`);
|
|
153
|
-
}
|
|
154
|
-
return {
|
|
155
|
-
name: data.name ?? imp.name,
|
|
156
|
-
version: data.version ?? imp.version,
|
|
157
|
-
subPath: imp.subPath,
|
|
158
|
-
github: imp.github,
|
|
159
|
-
jsr: imp.jsr,
|
|
160
|
-
external: imp.external,
|
|
161
|
-
dev: imp.dev,
|
|
162
|
-
module: data.module ?? "",
|
|
163
|
-
integrity: data.integrity ?? "",
|
|
164
|
-
exports: data.exports ?? [],
|
|
165
|
-
imports: data.imports ?? [],
|
|
166
|
-
peerImports: data.peerImports ?? []
|
|
167
|
-
};
|
|
168
|
-
})();
|
|
169
|
-
META_CACHE.set(url, pending);
|
|
170
|
-
try {
|
|
171
|
-
return await pending;
|
|
172
|
-
} catch (error) {
|
|
173
|
-
META_CACHE.delete(url);
|
|
174
|
-
throw error;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
async function addImportMeta(importMap, mark, imp, indirect, targetImports, cdnOrigin, target, noSRI) {
|
|
178
|
-
const markedSpecifier = `${specifierOf(imp)}${SPECIFIER_MARK_SEPARATOR}${imp.version}`;
|
|
179
|
-
if (mark.has(markedSpecifier)) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
mark.add(markedSpecifier);
|
|
183
|
-
const cdnScopeKey = `${cdnOrigin}/`;
|
|
184
|
-
const cdnScopeImports = importMap.scopes?.[cdnScopeKey];
|
|
185
|
-
const imports = indirect ? targetImports ?? ensureScope(importMap, cdnScopeKey) : importMap.imports;
|
|
186
|
-
const moduleUrl = moduleUrlOf(cdnOrigin, target, imp);
|
|
187
|
-
const currentSpecifier = specifierOf(imp);
|
|
188
|
-
imports[currentSpecifier] = moduleUrl;
|
|
189
|
-
await updateIntegrity(importMap, imp, moduleUrl, cdnOrigin, target, noSRI);
|
|
190
|
-
if (!indirect) {
|
|
191
|
-
if (cdnScopeImports) {
|
|
192
|
-
delete cdnScopeImports[currentSpecifier];
|
|
193
|
-
}
|
|
194
|
-
pruneEmptyScopes(importMap);
|
|
195
|
-
}
|
|
196
|
-
const allDeps = [
|
|
197
|
-
...imp.peerImports.map((pathname) => ({ pathname, isPeer: true })),
|
|
198
|
-
...imp.imports.map((pathname) => ({ pathname, isPeer: false }))
|
|
199
|
-
];
|
|
200
|
-
await Promise.all(
|
|
201
|
-
allDeps.map(async ({ pathname, isPeer }) => {
|
|
202
|
-
if (pathname.startsWith("/node/")) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
const depImport = parseEsmPath(pathname);
|
|
206
|
-
if (depImport.name === imp.name) {
|
|
207
|
-
depImport.version = imp.version;
|
|
208
|
-
}
|
|
209
|
-
const depSpecifier = specifierOf(depImport);
|
|
210
|
-
const existingUrl = importMap.imports[depSpecifier] ?? importMap.scopes?.[cdnScopeKey]?.[depSpecifier];
|
|
211
|
-
let scopedTargetImports = targetImports;
|
|
212
|
-
if (existingUrl?.startsWith(`${cdnOrigin}/`)) {
|
|
213
|
-
const existingImport = parseEsmPath(existingUrl);
|
|
214
|
-
const existingVersion = valid(existingImport.version);
|
|
215
|
-
if (existingVersion && depImport.version === existingImport.version) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
if (existingVersion && depImport.version && !valid(depImport.version)) {
|
|
219
|
-
if (satisfies(existingVersion, depImport.version, { includePrerelease: true })) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
if (isPeer) {
|
|
223
|
-
console.warn(
|
|
224
|
-
`incorrect peer dependency(unmeet ${depImport.version}): ${depImport.name}@${existingVersion}`
|
|
225
|
-
);
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
const scope = `${cdnOrigin}/${esmSpecifierOf(imp)}/`;
|
|
229
|
-
scopedTargetImports = ensureScope(importMap, scope);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
const depMeta = await fetchImportMeta(cdnOrigin, depImport, target);
|
|
233
|
-
await addImportMeta(importMap, mark, depMeta, !isPeer, scopedTargetImports, cdnOrigin, target, noSRI);
|
|
234
|
-
})
|
|
235
|
-
);
|
|
236
|
-
pruneEmptyScopes(importMap);
|
|
237
|
-
}
|
|
238
|
-
async function updateIntegrity(importMap, imp, moduleUrl, cdnOrigin, target, noSRI) {
|
|
239
|
-
if (noSRI) {
|
|
240
|
-
if (importMap.integrity) {
|
|
241
|
-
delete importMap.integrity[moduleUrl];
|
|
242
|
-
if (Object.keys(importMap.integrity).length === 0) {
|
|
243
|
-
delete importMap.integrity;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
if (!hasExternalImports(imp)) {
|
|
249
|
-
if (imp.integrity) {
|
|
250
|
-
importMap.integrity ??= {};
|
|
251
|
-
importMap.integrity[moduleUrl] = imp.integrity;
|
|
252
|
-
}
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
const integrityMeta = await fetchImportMeta(
|
|
256
|
-
cdnOrigin,
|
|
257
|
-
{
|
|
258
|
-
name: imp.name,
|
|
259
|
-
version: imp.version,
|
|
260
|
-
subPath: imp.subPath,
|
|
261
|
-
github: imp.github,
|
|
262
|
-
jsr: imp.jsr,
|
|
263
|
-
external: true,
|
|
264
|
-
dev: imp.dev
|
|
265
|
-
},
|
|
266
|
-
target
|
|
267
|
-
);
|
|
268
|
-
if (integrityMeta.integrity) {
|
|
269
|
-
importMap.integrity ??= {};
|
|
270
|
-
importMap.integrity[moduleUrl] = integrityMeta.integrity;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
function ensureScope(importMap, scopeKey) {
|
|
274
|
-
importMap.scopes ??= {};
|
|
275
|
-
importMap.scopes[scopeKey] ??= {};
|
|
276
|
-
return importMap.scopes[scopeKey];
|
|
277
|
-
}
|
|
278
|
-
function pruneEmptyScopes(importMap) {
|
|
279
|
-
if (!importMap.scopes) {
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
for (const [scope, imports] of Object.entries(importMap.scopes)) {
|
|
283
|
-
if (Object.keys(imports).length === 0) {
|
|
284
|
-
delete importMap.scopes[scope];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
if (Object.keys(importMap.scopes).length === 0) {
|
|
288
|
-
delete importMap.scopes;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
function parseEsmPath(pathnameOrUrl) {
|
|
292
|
-
let pathname;
|
|
293
|
-
if (pathnameOrUrl.startsWith("https://") || pathnameOrUrl.startsWith("http://")) {
|
|
294
|
-
pathname = new URL(pathnameOrUrl).pathname;
|
|
295
|
-
} else if (pathnameOrUrl.startsWith("/")) {
|
|
296
|
-
pathname = splitByFirst(splitByFirst(pathnameOrUrl, "#")[0], "?")[0];
|
|
297
|
-
} else {
|
|
298
|
-
throw new Error(`invalid pathname or url: ${pathnameOrUrl}`);
|
|
299
|
-
}
|
|
300
|
-
const imp = {
|
|
301
|
-
name: "",
|
|
302
|
-
version: "",
|
|
303
|
-
subPath: "",
|
|
304
|
-
github: false,
|
|
305
|
-
jsr: false,
|
|
306
|
-
external: false,
|
|
307
|
-
dev: false
|
|
308
|
-
};
|
|
309
|
-
if (pathname.startsWith("/gh/")) {
|
|
310
|
-
imp.github = true;
|
|
311
|
-
pathname = pathname.slice(3);
|
|
312
|
-
} else if (pathname.startsWith("/jsr/")) {
|
|
313
|
-
imp.jsr = true;
|
|
314
|
-
pathname = pathname.slice(4);
|
|
315
|
-
}
|
|
316
|
-
const segs = pathname.split("/").filter(Boolean);
|
|
317
|
-
if (segs.length === 0) {
|
|
318
|
-
throw new Error(`invalid pathname: ${pathnameOrUrl}`);
|
|
319
|
-
}
|
|
320
|
-
if (segs[0].startsWith("@")) {
|
|
321
|
-
if (!segs[1]) {
|
|
322
|
-
throw new Error(`invalid pathname: ${pathnameOrUrl}`);
|
|
323
|
-
}
|
|
324
|
-
const [name, version] = splitByLast(segs[1], "@");
|
|
325
|
-
imp.name = `${segs[0]}/${name}`.replace(/^\*/, "");
|
|
326
|
-
imp.version = version;
|
|
327
|
-
segs.splice(0, 2);
|
|
328
|
-
} else {
|
|
329
|
-
const [name, version] = splitByLast(segs[0], "@");
|
|
330
|
-
imp.name = name.replace(/^\*/, "");
|
|
331
|
-
imp.version = version;
|
|
332
|
-
segs.splice(0, 1);
|
|
333
|
-
}
|
|
334
|
-
let hasTargetSegment = false;
|
|
335
|
-
if (segs[0] && ESM_SEGMENTS.has(segs[0])) {
|
|
336
|
-
hasTargetSegment = true;
|
|
337
|
-
segs.shift();
|
|
338
|
-
}
|
|
339
|
-
if (segs.length > 0) {
|
|
340
|
-
if (hasTargetSegment && pathname.endsWith(".mjs")) {
|
|
341
|
-
let subPath = segs.join("/");
|
|
342
|
-
if (subPath.endsWith(".mjs")) {
|
|
343
|
-
subPath = subPath.slice(0, -4);
|
|
344
|
-
}
|
|
345
|
-
if (subPath.endsWith(".development")) {
|
|
346
|
-
subPath = subPath.slice(0, -12);
|
|
347
|
-
imp.dev = true;
|
|
348
|
-
}
|
|
349
|
-
if (subPath.includes("/") || subPath !== imp.name && !imp.name.endsWith(`/${subPath}`)) {
|
|
350
|
-
imp.subPath = subPath;
|
|
351
|
-
}
|
|
352
|
-
} else {
|
|
353
|
-
imp.subPath = segs.join("/");
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
return imp;
|
|
357
|
-
}
|
|
358
|
-
function splitByFirst(value, separator) {
|
|
359
|
-
const idx = value.indexOf(separator);
|
|
360
|
-
if (idx < 0) {
|
|
361
|
-
return [value, ""];
|
|
362
|
-
}
|
|
363
|
-
return [value.slice(0, idx), value.slice(idx + separator.length)];
|
|
364
|
-
}
|
|
365
|
-
function splitByLast(value, separator) {
|
|
366
|
-
const idx = value.lastIndexOf(separator);
|
|
367
|
-
if (idx < 0) {
|
|
368
|
-
return [value, ""];
|
|
369
|
-
}
|
|
370
|
-
return [value.slice(0, idx), value.slice(idx + separator.length)];
|
|
371
|
-
}
|
|
372
|
-
export {
|
|
373
|
-
addImport
|
|
374
|
-
};
|
package/dist/blank.mjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
function createBlankImportMap(baseURL) {
|
|
2
|
-
return {
|
|
3
|
-
baseURL: new URL(baseURL ?? ".", "file:///"),
|
|
4
|
-
imports: {}
|
|
5
|
-
};
|
|
6
|
-
}
|
|
7
|
-
function isBlankImportMap(importMap) {
|
|
8
|
-
const { imports, scopes } = importMap;
|
|
9
|
-
if (Object.keys(imports).length > 0 || scopes && Object.keys(scopes).length > 0) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
export {
|
|
15
|
-
createBlankImportMap,
|
|
16
|
-
isBlankImportMap
|
|
17
|
-
};
|
package/dist/parse.mjs
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { createBlankImportMap } from "./blank.ts";
|
|
2
|
-
function importMapFrom(v, baseURL) {
|
|
3
|
-
const im = createBlankImportMap(baseURL);
|
|
4
|
-
if (isObject(v)) {
|
|
5
|
-
const { config, imports, scopes, integrity } = v;
|
|
6
|
-
if (isObject(config)) {
|
|
7
|
-
validateStringMap(config);
|
|
8
|
-
im.config = config;
|
|
9
|
-
}
|
|
10
|
-
if (isObject(imports)) {
|
|
11
|
-
validateImports(imports);
|
|
12
|
-
im.imports = imports;
|
|
13
|
-
}
|
|
14
|
-
if (isObject(scopes)) {
|
|
15
|
-
validateScopes(scopes);
|
|
16
|
-
im.scopes = scopes;
|
|
17
|
-
}
|
|
18
|
-
if (isObject(integrity)) {
|
|
19
|
-
validateStringMap(integrity);
|
|
20
|
-
im.integrity = integrity;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return im;
|
|
24
|
-
}
|
|
25
|
-
function parseImportMapFromJson(json, baseURL) {
|
|
26
|
-
const im = createBlankImportMap(baseURL);
|
|
27
|
-
const v = JSON.parse(json);
|
|
28
|
-
if (isObject(v)) {
|
|
29
|
-
const { config, imports, scopes, integrity } = v;
|
|
30
|
-
if (isObject(config)) {
|
|
31
|
-
validateStringMap(config);
|
|
32
|
-
im.config = config;
|
|
33
|
-
}
|
|
34
|
-
if (isObject(imports)) {
|
|
35
|
-
validateImports(imports);
|
|
36
|
-
im.imports = imports;
|
|
37
|
-
}
|
|
38
|
-
if (isObject(scopes)) {
|
|
39
|
-
validateScopes(scopes);
|
|
40
|
-
im.scopes = scopes;
|
|
41
|
-
}
|
|
42
|
-
if (isObject(integrity)) {
|
|
43
|
-
validateStringMap(integrity);
|
|
44
|
-
im.integrity = integrity;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return im;
|
|
48
|
-
}
|
|
49
|
-
function parseImportMapFromHtml(html, baseURL) {
|
|
50
|
-
const tplEl = document.createElement("template");
|
|
51
|
-
tplEl.innerHTML = html;
|
|
52
|
-
const scriptEl = tplEl.content.querySelector("script[type='importmap']");
|
|
53
|
-
if (scriptEl) {
|
|
54
|
-
return parseImportMapFromJson(scriptEl.textContent, baseURL);
|
|
55
|
-
}
|
|
56
|
-
return createBlankImportMap(baseURL);
|
|
57
|
-
}
|
|
58
|
-
function validateImports(imports) {
|
|
59
|
-
for (const [k, v] of Object.entries(imports)) {
|
|
60
|
-
if (!v || typeof v !== "string") {
|
|
61
|
-
delete imports[k];
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
function validateScopes(imports) {
|
|
66
|
-
for (const [k, v] of Object.entries(imports)) {
|
|
67
|
-
if (isObject(v)) {
|
|
68
|
-
validateImports(v);
|
|
69
|
-
} else {
|
|
70
|
-
delete imports[k];
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
function validateStringMap(map) {
|
|
75
|
-
for (const [k, v] of Object.entries(map)) {
|
|
76
|
-
if (typeof v !== "string") {
|
|
77
|
-
delete map[k];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function isObject(v) {
|
|
82
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
83
|
-
}
|
|
84
|
-
export {
|
|
85
|
-
importMapFrom,
|
|
86
|
-
parseImportMapFromHtml,
|
|
87
|
-
parseImportMapFromJson
|
|
88
|
-
};
|
package/dist/resolve.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
function resolve(importMap, specifier, containingFile) {
|
|
2
|
-
const { baseURL, imports, scopes } = importMap;
|
|
3
|
-
const { origin, pathname } = new URL(containingFile, baseURL);
|
|
4
|
-
const sameOriginScopes = [];
|
|
5
|
-
for (const scopeName in scopes ?? {}) {
|
|
6
|
-
const scopeUrl = new URL(scopeName, baseURL);
|
|
7
|
-
if (scopeUrl.origin === origin) {
|
|
8
|
-
sameOriginScopes.push([scopeUrl.pathname, (scopes ?? {})[scopeName]]);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
sameOriginScopes.sort(([a], [b]) => b.split("/").length - a.split("/").length);
|
|
12
|
-
if (sameOriginScopes.length > 0) {
|
|
13
|
-
for (const [scopePathname, scopeImports] of sameOriginScopes) {
|
|
14
|
-
if (pathname.startsWith(scopePathname)) {
|
|
15
|
-
const url = matchImport(specifier, scopeImports);
|
|
16
|
-
if (url) {
|
|
17
|
-
return [url, true];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
if (origin === baseURL.origin) {
|
|
23
|
-
const url = matchImport(specifier, imports);
|
|
24
|
-
if (url) {
|
|
25
|
-
return [url, true];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return [specifier, false];
|
|
29
|
-
}
|
|
30
|
-
function matchImport(specifier, imports) {
|
|
31
|
-
if (specifier in imports) {
|
|
32
|
-
return imports[specifier];
|
|
33
|
-
}
|
|
34
|
-
let bestMatch = null;
|
|
35
|
-
let bestKeyLength = -1;
|
|
36
|
-
for (const [k, v] of Object.entries(imports)) {
|
|
37
|
-
if (k.endsWith("/") && specifier.startsWith(k) && k.length > bestKeyLength) {
|
|
38
|
-
bestMatch = v + specifier.slice(k.length);
|
|
39
|
-
bestKeyLength = k.length;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return bestMatch;
|
|
43
|
-
}
|
|
44
|
-
export {
|
|
45
|
-
resolve
|
|
46
|
-
};
|