bare-module-traverse 2.4.4 → 2.5.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 +9 -118
- package/index.d.ts +33 -0
- package/index.js +17 -2
- package/lib/resolve/bare.d.ts +6 -0
- package/lib/resolve/default.d.ts +6 -0
- package/lib/resolve/node.d.ts +6 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,118 +50,9 @@ for await (const dependency of traverse(
|
|
|
50
50
|
|
|
51
51
|
## API
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
See the [`bare-module-traverse` reference](https://docs.pears.com/reference/bare/modules/bare-module-traverse).
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
Options include:
|
|
58
|
-
|
|
59
|
-
```js
|
|
60
|
-
options = {
|
|
61
|
-
defaultType: constants.SCRIPT,
|
|
62
|
-
aliases: {
|
|
63
|
-
// Map an extension to a supported extension, e.g. `'.ts': '.js'`. The
|
|
64
|
-
// aliased extension is used for module type detection, so `readModule()`
|
|
65
|
-
// must return source compatible with that type. Aliased modules are also
|
|
66
|
-
// emitted with the aliased extension, and resolutions to them are
|
|
67
|
-
// rewritten to match, so `'.ts': '.js'` yields a `file:///foo.js`
|
|
68
|
-
// dependency rather than `file:///foo.ts`.
|
|
69
|
-
},
|
|
70
|
-
resolve: resolve.default,
|
|
71
|
-
visited: new Set()
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
When a module URL uses the `data:` protocol, its source is decoded directly from the specifier rather than read via `readModule()`, so no source module effect is emitted for it. base64 encoded data is decoded to a `Buffer`, other data is percent-decoded to a string, and the module type is derived from the media type (`text/javascript` and `application/javascript` map to a script or module, `application/json` to JSON, and anything else to `defaultType`). A JavaScript `data:` URL inherits the type of the module that imports it, so it is a script when imported from a script and a module when imported from a module, falling back to `defaultType` when it has no referrer, such as a `data:` entry. Only the UTF-8 charset is supported; a `data:` URL specifying any other charset throws.
|
|
76
|
-
|
|
77
|
-
`visited` is a `Set` of already visited module hrefs. If provided, modules whose href is already in the set are skipped and the set is updated in place as traversal proceeds. This allows a single set to be shared across several traversals so that a later traversal, such as one rooted at a dynamically imported module, does not revisit modules already seen by an earlier one.
|
|
78
|
-
|
|
79
|
-
Options supported by <https://github.com/holepunchto/bare-module-resolve> and <https://github.com/holepunchto/bare-addon-resolve> may also be specified.
|
|
80
|
-
|
|
81
|
-
#### `for (const dependency of dependencies)`
|
|
82
|
-
|
|
83
|
-
Synchronously iterate the module graph. Each yielded dependency has the following shape:
|
|
84
|
-
|
|
85
|
-
```js
|
|
86
|
-
dependency = {
|
|
87
|
-
url: URL,
|
|
88
|
-
source: 'string' | Buffer, // Source as returned by `readModule()`
|
|
89
|
-
type: constants.SCRIPT, // The detected module type, or `0` if unknown
|
|
90
|
-
imports: {
|
|
91
|
-
// See https://github.com/holepunchto/bare-module#imports
|
|
92
|
-
},
|
|
93
|
-
lexer: {
|
|
94
|
-
imports: [
|
|
95
|
-
// See https://github.com/holepunchto/bare-module-lexer#api
|
|
96
|
-
],
|
|
97
|
-
exports: [
|
|
98
|
-
// See https://github.com/holepunchto/bare-module-lexer#api
|
|
99
|
-
]
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
#### `for await (const dependency of dependencies)`
|
|
105
|
-
|
|
106
|
-
Asynchronously iterate the module graph. If `readModule` returns a promise or `listPrefix` returns a promise generator, these will be awaited. The same comments as `for (const dependency of dependencies)` apply.
|
|
107
|
-
|
|
108
|
-
#### `constants`
|
|
109
|
-
|
|
110
|
-
The module type constants used by the `type` field of each dependency and accepted as the `defaultType` option.
|
|
111
|
-
|
|
112
|
-
| Constant | Description |
|
|
113
|
-
| :------- | :----------------- |
|
|
114
|
-
| `SCRIPT` | A CommonJS module. |
|
|
115
|
-
| `MODULE` | An ES module. |
|
|
116
|
-
| `JSON` | A JSON module. |
|
|
117
|
-
| `BUNDLE` | A bundle module. |
|
|
118
|
-
| `ADDON` | A native addon. |
|
|
119
|
-
| `BINARY` | A binary module. |
|
|
120
|
-
| `TEXT` | A text module. |
|
|
121
|
-
|
|
122
|
-
### Resolution
|
|
123
|
-
|
|
124
|
-
Module and addon resolution is configurable by providing a resolver function. A resolver function is a generator function that yields values matching the shapes defined by <https://github.com/holepunchto/bare-module-resolve#algorithm>. Several resolvers are provided out of the box to support the most common use cases.
|
|
125
|
-
|
|
126
|
-
#### `resolve.module`
|
|
127
|
-
|
|
128
|
-
Convenience export from <https://github.com/holepunchto/bare-module-resolve>.
|
|
129
|
-
|
|
130
|
-
#### `resolve.addon`
|
|
131
|
-
|
|
132
|
-
Convenience export from <https://github.com/holepunchto/bare-addon-resolve>.
|
|
133
|
-
|
|
134
|
-
#### `resolve.default`
|
|
135
|
-
|
|
136
|
-
The default resolver, which simply forwards to <https://github.com/holepunchto/bare-module-resolve> and <https://github.com/holepunchto/bare-addon-resolve> with the literal options passed by the caller.
|
|
137
|
-
|
|
138
|
-
#### `resolve.bare`
|
|
139
|
-
|
|
140
|
-
The Bare resolver, which matches the options used by the Bare module system. The resolver accepts the following additional options:
|
|
141
|
-
|
|
142
|
-
```js
|
|
143
|
-
options = {
|
|
144
|
-
host,
|
|
145
|
-
hosts: [host]
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
For single target traversal it is sufficient to pass `host`. For multi target traversal pass a list of `hosts` identifiers instead.
|
|
150
|
-
|
|
151
|
-
#### `resolve.node`
|
|
152
|
-
|
|
153
|
-
The Node.js resolver, which matches the options used by the Node.js module system. The resolver accepts the following additional options:
|
|
154
|
-
|
|
155
|
-
```js
|
|
156
|
-
options = {
|
|
157
|
-
host,
|
|
158
|
-
hosts: [host]
|
|
159
|
-
}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
For single target traversal it is sufficient to pass `host`. For multi target traversal pass a list of `hosts` identifiers instead.
|
|
163
|
-
|
|
164
|
-
### Algorithm
|
|
55
|
+
## Algorithm
|
|
165
56
|
|
|
166
57
|
The following generator functions implement the traversal algorithm. The yielded values have the following shape:
|
|
167
58
|
|
|
@@ -321,19 +212,19 @@ Options are the same as `traverse()` for all functions.
|
|
|
321
212
|
> [!WARNING]
|
|
322
213
|
> These functions are currently subject to change between minor releases. If using them directly, make sure to specify a tilde range (`~1.2.3`) when declaring the module dependency.
|
|
323
214
|
|
|
324
|
-
|
|
215
|
+
### `const generator = traverse.module(url, source, attributes, artifacts, visited[, options])`
|
|
325
216
|
|
|
326
|
-
|
|
217
|
+
### `const generator = traverse.package(url, source, artifacts, visited[, options])`
|
|
327
218
|
|
|
328
|
-
|
|
219
|
+
### `const generator = traverse.preresolved(url, source, resolutions, artifacts, visited[, options])`
|
|
329
220
|
|
|
330
|
-
|
|
221
|
+
### `const generator = traverse.imports(parentURL, source, imports, artifacts, lexer, visited[, options])`
|
|
331
222
|
|
|
332
|
-
|
|
223
|
+
### `const generator = traverse.link(entry, specifier, condition, parentURL, imports, artifacts, visited[, options])`
|
|
333
224
|
|
|
334
|
-
|
|
225
|
+
### `const generator = traverse.addons(parentURL, artifacts, visited[, options])`
|
|
335
226
|
|
|
336
|
-
|
|
227
|
+
### `const generator = traverse.assets(patterns, parentURL, artifacts, visited[, options])`
|
|
337
228
|
|
|
338
229
|
## License
|
|
339
230
|
|
package/index.d.ts
CHANGED
|
@@ -36,9 +36,42 @@ type AliasableExtension =
|
|
|
36
36
|
interface TraverseOptions extends ResolveOptions {
|
|
37
37
|
defaultType?: number
|
|
38
38
|
aliases?: Record<string, AliasableExtension>
|
|
39
|
+
/**
|
|
40
|
+
* @param entry - The import to resolve, as produced by `bare-module-lexer`.
|
|
41
|
+
* @param parentURL - The WHATWG `URL` to resolve `entry` relative to.
|
|
42
|
+
* @param opts - Resolve options forwarded to the underlying resolution algorithm.
|
|
43
|
+
* @returns A `Resolver` that yields the candidate resolutions for `entry`.
|
|
44
|
+
*/
|
|
39
45
|
resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
|
|
46
|
+
/**
|
|
47
|
+
* Record a specifier that resolves nowhere under `deferredProtocol` rather
|
|
48
|
+
* than throwing, leaving it for whoever names it to resolve when they get
|
|
49
|
+
* there. Only for callers that reach a resolver again, such as a module
|
|
50
|
+
* system; a tool that only links wants the error.
|
|
51
|
+
*/
|
|
52
|
+
deferUnresolved?: boolean
|
|
40
53
|
}
|
|
41
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Traverse the module graph rooted at `entry`, which must be a WHATWG `URL` instance. `readModule`
|
|
57
|
+
* is called with a `URL` instance for every module to be read and must either return the module
|
|
58
|
+
* source, if it exists, or `null`. `listPrefix` is called with a `URL` instance of every prefix to
|
|
59
|
+
* be listed and must yield `URL` instances that have the specified `URL` as a prefix. If not
|
|
60
|
+
* provided, prefixes won't be traversed. If `readModule` returns a promise or `listPrefix` returns
|
|
61
|
+
* a promise generator, synchronous iteration is not supported.
|
|
62
|
+
* @param entry - The WHATWG `URL` of the entry module to root the graph at.
|
|
63
|
+
* @param readModule - Called with the `URL` of each module to read; returns its source as a
|
|
64
|
+
* `Buffer` or `string`, or `null` if it does not exist. Returning a promise disables synchronous
|
|
65
|
+
* iteration.
|
|
66
|
+
* @param listPrefix - Called with the `URL` of each prefix to list; must yield the `URL`s that have
|
|
67
|
+
* it as a prefix. If omitted, prefixes are not traversed.
|
|
68
|
+
* @param probeModule - Called with the `URL` of each module to probe for existence; returns a
|
|
69
|
+
* boolean, or `undefined` to fall back to `readModule`.
|
|
70
|
+
* @param resolveModule - Called with each resolution `URL` to transform; returns the `URL` to use
|
|
71
|
+
* in its place. Defaults to the identity function.
|
|
72
|
+
* @returns An iterable of resolved `Dependency` records for the module graph; asynchronous when any
|
|
73
|
+
* callback returns a promise.
|
|
74
|
+
*/
|
|
42
75
|
declare function traverse(
|
|
43
76
|
entry: URL,
|
|
44
77
|
readModule: (url: URL) => Buffer | string | null,
|
package/index.js
CHANGED
|
@@ -464,7 +464,8 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
|
|
|
464
464
|
resolve = exports.resolve.default,
|
|
465
465
|
builtinProtocol = 'builtin:',
|
|
466
466
|
linkedProtocol = 'linked:',
|
|
467
|
-
deferredProtocol = 'deferred:'
|
|
467
|
+
deferredProtocol = 'deferred:',
|
|
468
|
+
deferUnresolved = false
|
|
468
469
|
} = opts
|
|
469
470
|
|
|
470
471
|
const matchedConditions = []
|
|
@@ -503,9 +504,13 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
|
|
|
503
504
|
} else if (condition === 'asset') {
|
|
504
505
|
const prefix = url
|
|
505
506
|
|
|
507
|
+
let prefixResolution = null
|
|
508
|
+
|
|
506
509
|
for (const url of yield { prefix }) {
|
|
507
510
|
const resolution = yield* postresolve(url)
|
|
508
511
|
|
|
512
|
+
if (url.href === prefix.href) prefixResolution = resolution
|
|
513
|
+
|
|
509
514
|
yield {
|
|
510
515
|
children: exports.module(resolution, null, {}, artifacts, visited, {
|
|
511
516
|
...opts,
|
|
@@ -519,7 +524,11 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
|
|
|
519
524
|
resolved = true
|
|
520
525
|
}
|
|
521
526
|
|
|
522
|
-
if (resolved)
|
|
527
|
+
if (resolved) {
|
|
528
|
+
resolution = prefixResolution || (yield* postresolve(prefix))
|
|
529
|
+
|
|
530
|
+
addResolution(imports, specifier, matchedConditions, resolution)
|
|
531
|
+
}
|
|
523
532
|
} else if (
|
|
524
533
|
condition === 'addon' ||
|
|
525
534
|
moduleType(url, entry.attributes, null, opts) === constants.ADDON
|
|
@@ -586,6 +595,12 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
|
|
|
586
595
|
}
|
|
587
596
|
}
|
|
588
597
|
|
|
598
|
+
if (resolutions === 0 && deferUnresolved) {
|
|
599
|
+
addResolution(imports, specifier, matchedConditions, new URL(deferredProtocol + specifier))
|
|
600
|
+
|
|
601
|
+
resolutions++
|
|
602
|
+
}
|
|
603
|
+
|
|
589
604
|
matchedConditions.pop()
|
|
590
605
|
|
|
591
606
|
if (resolutions === 0) {
|
package/lib/resolve/bare.d.ts
CHANGED
|
@@ -7,6 +7,12 @@ interface BareResolveOptions extends ResolveOptions {
|
|
|
7
7
|
hosts?: string[]
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* @param entry - The import to resolve, as produced by `bare-module-lexer`.
|
|
12
|
+
* @param parentURL - The WHATWG `URL` to resolve `entry` relative to.
|
|
13
|
+
* @param opts - Resolve options forwarded to the underlying resolution algorithm.
|
|
14
|
+
* @returns A `Resolver` that yields the candidate resolutions for `entry`.
|
|
15
|
+
*/
|
|
10
16
|
declare function resolve(entry: Import, parentURL: URL, opts?: BareResolveOptions): Resolver
|
|
11
17
|
|
|
12
18
|
declare namespace resolve {
|
package/lib/resolve/default.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { type Import } from 'bare-module-lexer'
|
|
2
2
|
import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @param entry - The import to resolve, as produced by `bare-module-lexer`.
|
|
6
|
+
* @param parentURL - The WHATWG `URL` to resolve `entry` relative to.
|
|
7
|
+
* @param opts - Resolve options forwarded to the underlying resolution algorithm.
|
|
8
|
+
* @returns A `Resolver` that yields the candidate resolutions for `entry`.
|
|
9
|
+
*/
|
|
4
10
|
declare function resolve(entry: Import, parentURL: URL, opts?: ResolveOptions): Resolver
|
|
5
11
|
|
|
6
12
|
export = resolve
|
package/lib/resolve/node.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ interface NodeResolveOptions extends ResolveOptions {
|
|
|
6
6
|
hosts?: string[]
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @param entry - The import to resolve, as produced by `bare-module-lexer`.
|
|
11
|
+
* @param parentURL - The WHATWG `URL` to resolve `entry` relative to.
|
|
12
|
+
* @param opts - Resolve options forwarded to the underlying resolution algorithm.
|
|
13
|
+
* @returns A `Resolver` that yields the candidate resolutions for `entry`.
|
|
14
|
+
*/
|
|
9
15
|
declare function resolve(entry: Import, parentURL: URL, opts?: NodeResolveOptions): Resolver
|
|
10
16
|
|
|
11
17
|
declare namespace resolve {
|