@esm.sh/import-map 0.2.1 → 0.3.1

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
@@ -1,6 +1,6 @@
1
1
  # @esm.sh/import-map
2
2
 
3
- An [Import Maps](https://wicg.github.io/import-maps/) manager, with features:
3
+ An [Import Maps](https://wicg.github.io/import-maps/) parser and resolver, with features:
4
4
 
5
5
  - Parse import maps from JSON/HTML
6
6
  - Resolve specifiers to URLs using import map matching rules
@@ -15,54 +15,56 @@ npm i @esm.sh/import-map
15
15
 
16
16
  ## API
17
17
 
18
- ### `createBlankImportMap(baseURL?: string)`
18
+ ### `new ImportMap(baseURL?: string, raw?: ImportMapRaw)`
19
19
 
20
- Create an empty import map:
20
+ Create an import map instance:
21
21
 
22
22
  ```ts
23
- import { createBlankImportMap } from "@esm.sh/import-map";
23
+ import { ImportMap } from "@esm.sh/import-map";
24
24
 
25
- const im = createBlankImportMap("file:///");
25
+ const im = new ImportMap();
26
26
  ```
27
27
 
28
- ### `importMapFrom(value: any, baseURL?: string)`
29
-
30
- Build an import map from a JS object.
31
- Supports `config`, `imports`, `scopes`, and `integrity`.
32
- Non-string values inside these maps are removed during validation.
28
+ You can also initialize from a raw object:
33
29
 
34
30
  ```ts
35
- import { importMapFrom } from "@esm.sh/import-map";
36
-
37
- const im = importMapFrom({
31
+ const im = new ImportMap("https://example.com/app/", {
32
+ config: { cdn: "https://esm.sh", target: "es2022" },
38
33
  imports: { react: "https://esm.sh/react@19.2.4/es2022/react.mjs" },
39
- integrity: { "https://esm.sh/react@19.2.4/es2022/react.mjs": "sha384-..." },
34
+ scopes: {
35
+ "https://esm.sh/": {
36
+ scheduler: "https://esm.sh/scheduler@0.27.0/es2022/scheduler.mjs",
37
+ },
38
+ },
39
+ integrity: {
40
+ "https://esm.sh/react@19.2.4/es2022/react.mjs": "sha384-...",
41
+ },
40
42
  });
41
43
  ```
42
44
 
43
- ### `parseImportMapFromJson(json: string, baseURL?: string)`
45
+ ### `parseFromJson(json: string, baseURL?: string)`
44
46
 
45
47
  Parse an import map from JSON text.
46
48
  Preserves and validates `config`, `imports`, `scopes`, and `integrity`.
47
49
 
48
50
  ```ts
49
- import { parseImportMapFromJson } from "@esm.sh/import-map";
51
+ import { parseFromJson } from "@esm.sh/import-map";
50
52
 
51
- const im = parseImportMapFromJson(`{
53
+ const im = parseFromJson(`{
52
54
  "imports": {
53
55
  "react": "https://esm.sh/react@19.2.4/es2022/react.mjs"
54
56
  }
55
57
  }`);
56
58
  ```
57
59
 
58
- ### `parseImportMapFromHtml(html: string, baseURL?: string)`
60
+ ### `parseFromHtml(html: string, baseURL?: string)`
59
61
 
60
62
  Parse the first `<script type="importmap">` from HTML (browser environment). Returns an empty import map if no `importmap` script tag is found.
61
63
 
62
64
  ```ts
63
- import { parseImportMapFromHtml } from "@esm.sh/import-map";
65
+ import { parseFromHtml } from "@esm.sh/import-map";
64
66
 
65
- const im = parseImportMapFromHtml(`<script type="importmap">
67
+ const im = parseFromHtml(`<script type="importmap">
66
68
  {
67
69
  "imports": {
68
70
  "react": "https://esm.sh/react@19.2.4/es2022/react.mjs"
@@ -73,22 +75,9 @@ const im = parseImportMapFromHtml(`<script type="importmap">
73
75
 
74
76
  > Note: This function requires a browser environment.
75
77
 
76
- ### `resolve(importMap: ImportMap, specifier: string, containingFile: string)`
77
-
78
- Resolve a specifier using import-map matching rules:
78
+ ### `ImportMap.addImport(specifier: string, noSRI?: boolean)`
79
79
 
80
- ```ts
81
- import { resolve } from "@esm.sh/import-map";
82
-
83
- const [url, ok] = resolve(im, "react", "file:///app/main.ts");
84
- ```
85
-
86
- Returns `[resolvedUrl, true]` when matched, otherwise `[originalSpecifier, false]`.
87
-
88
- ### `addImport(importMap: ImportMap, specifier: string, noSRI?: boolean)`
89
-
90
- Fetch package metadata from [esm.sh](https://esm.sh) CDN and add an import entry (plus relevant deps)
91
- into 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.
92
81
 
93
82
  Supported specifiers include:
94
83
 
@@ -98,43 +87,57 @@ Supported specifiers include:
98
87
 
99
88
  Behavior highlights:
100
89
 
101
- - adds top-level specifier into `imports`
90
+ - adds top-level entries into `imports`
102
91
  - adds nested deps into `scopes` when needed
103
92
  - cleans up empty scopes
104
93
  - updates `integrity` unless `noSRI` is `true`
105
94
 
106
95
  ```ts
107
- import { addImport, createBlankImportMap } from "@esm.sh/import-map";
96
+ import { ImportMap } from "@esm.sh/import-map";
108
97
 
109
- const im = createBlankImportMap();
110
- await addImport(im, "react-dom@19/client");
98
+ const im = new ImportMap();
99
+ await im.addImport("react-dom@19/client");
111
100
  ```
112
101
 
113
- ### `isSupportImportMap()`
102
+ ### `ImportMap.resolve(specifier: string, containingFile: string)`
114
103
 
115
- Returns whether the current browser supports import maps.
104
+ The `resolve` method resolves a specifier using import-map matching rules:
116
105
 
117
106
  ```ts
118
- import { isSupportImportMap } from "@esm.sh/import-map";
107
+ const [url, ok] = im.resolve("react", "file:///app/main.ts");
108
+ ```
119
109
 
120
- const supported = isSupportImportMap();
110
+ Returns `[resolvedUrl, true]` when matched, otherwise `[originalSpecifier, false]`.
111
+
112
+ ### `ImportMap.raw`
113
+
114
+ The `raw` getter returns a clean, key-ordered import-map object (`ImportMapRaw`):
115
+
116
+ ```ts
117
+ const raw = im.raw;
118
+ // {
119
+ // config?: { ... },
120
+ // imports?: { ... },
121
+ // scopes?: { ... },
122
+ // integrity?: { ... },
123
+ // }
121
124
  ```
122
125
 
123
- ### `isBlankImportMap(importMap: ImportMap)`
126
+ ### `isSupportImportMap()`
124
127
 
125
- Returns `true` when `imports` and `scopes` are empty.
128
+ function `isSupportImportMap()` returns whether the current browser supports import maps.
126
129
 
127
130
  ```ts
128
- import { isBlankImportMap } from "@esm.sh/import-map";
131
+ import { isSupportImportMap } from "@esm.sh/import-map";
129
132
 
130
- const blank = isBlankImportMap(im);
133
+ const supported = isSupportImportMap();
131
134
  ```
132
135
 
133
136
  ## Development
134
137
 
135
138
  ```bash
136
- npm test
137
- npm run build
139
+ bun test
140
+ bun run build
138
141
  ```
139
142
 
140
143
  ## License