@esm.sh/import-map 0.3.0 → 0.3.2

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 CHANGED
@@ -75,9 +75,9 @@ const im = parseFromHtml(`<script type="importmap">
75
75
 
76
76
  > Note: This function requires a browser environment.
77
77
 
78
- ### `im.addImport(specifier: string, noSRI?: boolean)`
78
+ ### `ImportMap.addImport(specifier: string, noSRI?: boolean)`
79
79
 
80
- Fetch package metadata from [esm.sh](https://esm.sh) CDN and add an entry (plus relevant deps) to the map.
80
+ The `addImport` method fetches package metadata from [esm.sh](https://esm.sh) CDN and adds an entry (plus relevant deps) to the map.
81
81
 
82
82
  Supported specifiers include:
83
83
 
@@ -99,9 +99,9 @@ const im = new ImportMap();
99
99
  await im.addImport("react-dom@19/client");
100
100
  ```
101
101
 
102
- ### `im.resolve(specifier: string, containingFile: string)`
102
+ ### `ImportMap.resolve(specifier: string, containingFile: string)`
103
103
 
104
- Resolve a specifier using import-map matching rules:
104
+ The `resolve` method resolves a specifier using import-map matching rules:
105
105
 
106
106
  ```ts
107
107
  const [url, ok] = im.resolve("react", "file:///app/main.ts");
@@ -109,9 +109,9 @@ const [url, ok] = im.resolve("react", "file:///app/main.ts");
109
109
 
110
110
  Returns `[resolvedUrl, true]` when matched, otherwise `[originalSpecifier, false]`.
111
111
 
112
- ### `im.raw`
112
+ ### `ImportMap.raw`
113
113
 
114
- `raw` getter returns a clean, key-ordered import-map object (`ImportMapRaw`):
114
+ The `raw` getter returns a clean, key-ordered import-map object (`ImportMapRaw`):
115
115
 
116
116
  ```ts
117
117
  const raw = im.raw;
@@ -125,7 +125,7 @@ const raw = im.raw;
125
125
 
126
126
  ### `isSupportImportMap()`
127
127
 
128
- Returns whether the current browser supports import maps.
128
+ function `isSupportImportMap()` returns whether the current browser supports import maps.
129
129
 
130
130
  ```ts
131
131
  import { isSupportImportMap } from "@esm.sh/import-map";
package/dist/index.mjs CHANGED
@@ -39,6 +39,8 @@ async function addImport(importMap, specifier, noSRI) {
39
39
  const meta = await fetchImportMeta(cdnOrigin, imp, target);
40
40
  const mark = /* @__PURE__ */ new Set();
41
41
  await addImportImpl(importMap, mark, meta, false, void 0, cdnOrigin, target, noSRI ?? false);
42
+ pruneScopeSpecifiersShadowedByImports(importMap);
43
+ pruneEmptyScopes(importMap);
42
44
  }
43
45
  async function addImportImpl(importMap, mark, imp, indirect, targetImports, cdnOrigin, target, noSRI) {
44
46
  const markedSpecifier = `${specifierOf(imp)}${SPECIFIER_MARK_SEPARATOR}${imp.version}`;
@@ -283,6 +285,20 @@ function pruneEmptyScopes(importMap) {
283
285
  }
284
286
  }
285
287
  }
288
+ function pruneScopeSpecifiersShadowedByImports(importMap) {
289
+ for (const [scopeKey, scopedImports] of Object.entries(importMap.scopes)) {
290
+ if (scopeKey.startsWith("https://") || scopeKey.startsWith("http://")) {
291
+ const url = new URL(scopeKey);
292
+ if (url.pathname === "/") {
293
+ for (const specifier of Object.keys(scopedImports)) {
294
+ if (specifier in importMap.imports) {
295
+ delete scopedImports[specifier];
296
+ }
297
+ }
298
+ }
299
+ }
300
+ }
301
+ }
286
302
  function parseEsmPath(pathnameOrUrl) {
287
303
  let pathname;
288
304
  if (pathnameOrUrl.startsWith("https://") || pathnameOrUrl.startsWith("http://")) {
@@ -476,9 +492,6 @@ var ImportMap = class {
476
492
  get baseURL() {
477
493
  return this.#baseURL;
478
494
  }
479
- get isBlank() {
480
- return Object.keys(this.imports).length === 0 && Object.keys(this.scopes).length === 0;
481
- }
482
495
  get raw() {
483
496
  const json = {};
484
497
  const config = sortStringMap(this.config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esm.sh/import-map",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A import map parser and resolver.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
package/types/index.d.ts CHANGED
@@ -42,9 +42,6 @@ export class ImportMap {
42
42
  /** The base URL of the import map. */
43
43
  get baseURL(): URL;
44
44
 
45
- /** Check if the import map is blank. */
46
- get isBlank(): boolean;
47
-
48
45
  /** Return a clean, key-ordered raw import map object. */
49
46
  get raw(): ImportMapRaw;
50
47