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