@esm.sh/import-map 0.2.1 → 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 +52 -49
- 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 -64
- package/dist/support.mjs +0 -6
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/)
|
|
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
|
-
### `
|
|
18
|
+
### `new ImportMap(baseURL?: string, raw?: ImportMapRaw)`
|
|
19
19
|
|
|
20
|
-
Create an
|
|
20
|
+
Create an import map instance:
|
|
21
21
|
|
|
22
22
|
```ts
|
|
23
|
-
import {
|
|
23
|
+
import { ImportMap } from "@esm.sh/import-map";
|
|
24
24
|
|
|
25
|
-
const im =
|
|
25
|
+
const im = new ImportMap();
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
### `
|
|
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 {
|
|
51
|
+
import { parseFromJson } from "@esm.sh/import-map";
|
|
50
52
|
|
|
51
|
-
const im =
|
|
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
|
-
### `
|
|
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 {
|
|
65
|
+
import { parseFromHtml } from "@esm.sh/import-map";
|
|
64
66
|
|
|
65
|
-
const im =
|
|
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
|
-
### `
|
|
77
|
-
|
|
78
|
-
Resolve a specifier using import-map matching rules:
|
|
78
|
+
### `im.addImport(specifier: string, noSRI?: boolean)`
|
|
79
79
|
|
|
80
|
-
|
|
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
|
+
Fetch package metadata from [esm.sh](https://esm.sh) CDN and add 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
|
|
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 {
|
|
96
|
+
import { ImportMap } from "@esm.sh/import-map";
|
|
108
97
|
|
|
109
|
-
const im =
|
|
110
|
-
await addImport(
|
|
98
|
+
const im = new ImportMap();
|
|
99
|
+
await im.addImport("react-dom@19/client");
|
|
111
100
|
```
|
|
112
101
|
|
|
113
|
-
### `
|
|
102
|
+
### `im.resolve(specifier: string, containingFile: string)`
|
|
114
103
|
|
|
115
|
-
|
|
104
|
+
Resolve a specifier using import-map matching rules:
|
|
116
105
|
|
|
117
106
|
```ts
|
|
118
|
-
|
|
107
|
+
const [url, ok] = im.resolve("react", "file:///app/main.ts");
|
|
108
|
+
```
|
|
119
109
|
|
|
120
|
-
|
|
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
|
+
// }
|
|
121
124
|
```
|
|
122
125
|
|
|
123
|
-
### `
|
|
126
|
+
### `isSupportImportMap()`
|
|
124
127
|
|
|
125
|
-
Returns
|
|
128
|
+
Returns whether the current browser supports import maps.
|
|
126
129
|
|
|
127
130
|
```ts
|
|
128
|
-
import {
|
|
131
|
+
import { isSupportImportMap } from "@esm.sh/import-map";
|
|
129
132
|
|
|
130
|
-
const
|
|
133
|
+
const supported = isSupportImportMap();
|
|
131
134
|
```
|
|
132
135
|
|
|
133
136
|
## Development
|
|
134
137
|
|
|
135
138
|
```bash
|
|
136
|
-
|
|
137
|
-
|
|
139
|
+
bun test
|
|
140
|
+
bun run build
|
|
138
141
|
```
|
|
139
142
|
|
|
140
143
|
## License
|