bare-module-traverse 1.3.3 → 1.4.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/index.d.ts ADDED
@@ -0,0 +1,111 @@
1
+ import URL from 'bare-url'
2
+ import Buffer from 'bare-buffer'
3
+ import {
4
+ type ConditionalSpecifier,
5
+ type ImportsMap,
6
+ type ResolutionsMap
7
+ } from 'bare-module-resolve'
8
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
9
+ import { type Import } from 'bare-module-lexer'
10
+
11
+ interface Dependency {
12
+ url: URL
13
+ source: string | Buffer
14
+ imports: ImportsMap
15
+ }
16
+
17
+ interface TraverseOptions extends ResolveOptions {
18
+ resolve?: (entry: Import, parentURL: URL, opts?: ResolveOptions) => Resolver
19
+ }
20
+
21
+ declare function traverse(
22
+ entry: URL,
23
+ readModule: (url: URL) => Buffer | string | null,
24
+ listPrefix?: (url: URL) => Iterable<URL>
25
+ ): Iterable<Dependency>
26
+
27
+ declare function traverse(
28
+ entry: URL,
29
+ readModule: (url: URL) => Promise<Buffer | string | null>,
30
+ listPrefix?: (url: URL) => AsyncIterable<URL>
31
+ ): AsyncIterable<Dependency>
32
+
33
+ declare function traverse(
34
+ entry: URL,
35
+ opts: TraverseOptions,
36
+ readModule: (url: URL) => Buffer | string | null,
37
+ listPrefix?: (url: URL) => Iterable<URL>
38
+ ): Iterable<Dependency>
39
+
40
+ declare function traverse(
41
+ entry: URL,
42
+ opts: TraverseOptions,
43
+ readModule: (url: URL) => Promise<Buffer | string | null>,
44
+ listPrefix?: (url: URL) => AsyncIterable<URL>
45
+ ): AsyncIterable<Dependency>
46
+
47
+ declare namespace traverse {
48
+ export { type TraverseOptions }
49
+
50
+ export type Traversal = Generator<
51
+ { module: URL } | { prefix: URL } | { children: URL } | { dependency: URL },
52
+ boolean,
53
+ void | URL[] | Buffer | string | null
54
+ >
55
+
56
+ export interface Artifacts {
57
+ addons: URL[]
58
+ assets: URL[]
59
+ }
60
+
61
+ export function module(
62
+ url: URL,
63
+ source: string | Buffer,
64
+ artifacts: Artifacts,
65
+ visited: Set<string>,
66
+ opts?: TraverseOptions
67
+ ): Traversal
68
+
69
+ export function package(
70
+ url: URL,
71
+ source: string | Buffer,
72
+ artifacts: Artifacts,
73
+ visited: Set<string>,
74
+ opts?: TraverseOptions
75
+ ): Traversal
76
+
77
+ export function preresolved(
78
+ url: URL,
79
+ source: string | Buffer,
80
+ resolution: ResolutionsMap,
81
+ artifacts: Artifacts,
82
+ visited: Set<string>,
83
+ opts?: TraverseOptions
84
+ ): Traversal
85
+
86
+ export function imports(
87
+ parentURL: URL,
88
+ source: string | Buffer,
89
+ imports: ImportsMap,
90
+ artifacts: Artifacts,
91
+ visited: Set<string>,
92
+ opts?: TraverseOptions
93
+ ): Traversal
94
+
95
+ export function prebuilds(
96
+ packageURL: URL,
97
+ artifacts: Artifacts,
98
+ visited: Set<string>,
99
+ opts?: TraverseOptions
100
+ ): Traversal
101
+
102
+ export function assets(
103
+ patterns: ConditionalSpecifier,
104
+ packageURL: URL,
105
+ artifacts: Artifacts,
106
+ visited: Set<string>,
107
+ opts?: TraverseOptions
108
+ ): Traversal
109
+ }
110
+
111
+ export = traverse
package/index.js CHANGED
@@ -16,10 +16,6 @@ module.exports = exports = function traverse(
16
16
  opts = {}
17
17
  }
18
18
 
19
- if (typeof listPrefix !== 'function') {
20
- listPrefix = defaultListPrefix
21
- }
22
-
23
19
  return {
24
20
  *[Symbol.iterator]() {
25
21
  const artifacts = { addons: [], assets: [] }
@@ -39,8 +35,14 @@ module.exports = exports = function traverse(
39
35
  } else if (value.prefix) {
40
36
  const result = []
41
37
 
42
- for (const url of listPrefix(value.prefix)) {
43
- result.push(url)
38
+ if (typeof listPrefix === 'function') {
39
+ for (const url of listPrefix(value.prefix)) {
40
+ result.push(url)
41
+ }
42
+ } else {
43
+ if (readModule(value.prefix) !== null) {
44
+ result.push(value.prefix)
45
+ }
44
46
  }
45
47
 
46
48
  next = generator.next(result)
@@ -74,8 +76,14 @@ module.exports = exports = function traverse(
74
76
  } else if (value.prefix) {
75
77
  const result = []
76
78
 
77
- for await (const url of listPrefix(value.prefix)) {
78
- result.push(url)
79
+ if (typeof listPrefix === 'function') {
80
+ for await (const url of listPrefix(value.prefix)) {
81
+ result.push(url)
82
+ }
83
+ } else {
84
+ if ((await readModule(value.prefix)) !== null) {
85
+ result.push(value.prefix)
86
+ }
79
87
  }
80
88
 
81
89
  next = generator.next(result)
@@ -93,8 +101,6 @@ module.exports = exports = function traverse(
93
101
  }
94
102
  }
95
103
 
96
- function* defaultListPrefix() {}
97
-
98
104
  exports.resolve = resolve
99
105
 
100
106
  exports.module = function* (url, source, artifacts, visited, opts = {}) {
@@ -229,6 +235,7 @@ exports.imports = function* (
229
235
  resolve = exports.resolve.default,
230
236
  builtinProtocol = 'builtin:',
231
237
  linkedProtocol = 'linked:',
238
+ deferredProtocol = 'deferred:',
232
239
  matchedConditions = []
233
240
  } = opts
234
241
 
@@ -264,11 +271,28 @@ exports.imports = function* (
264
271
 
265
272
  if (
266
273
  url.protocol === builtinProtocol ||
267
- url.protocol === linkedProtocol
274
+ url.protocol === linkedProtocol ||
275
+ url.protocol === deferredProtocol
268
276
  ) {
269
277
  addResolution(imports, specifier, matchedConditions, url)
270
278
 
271
279
  resolved = yielded = true
280
+ } else if (condition === 'asset') {
281
+ const prefix = yield { prefix: url }
282
+
283
+ if (prefix.length !== 0) {
284
+ addResolution(imports, specifier, matchedConditions, url)
285
+
286
+ for (const url of prefix) {
287
+ yield {
288
+ children: exports.module(url, null, artifacts, visited, opts)
289
+ }
290
+
291
+ addURL(artifacts.assets, url)
292
+ }
293
+
294
+ resolved = yielded = true
295
+ }
272
296
  } else {
273
297
  const source = yield { module: url }
274
298
 
@@ -285,7 +309,6 @@ exports.imports = function* (
285
309
 
286
310
  if (resolved) {
287
311
  if (condition === 'addon') addURL(artifacts.addons, url)
288
- else if (condition === 'asset') addURL(artifacts.assets, url)
289
312
 
290
313
  resolutions++
291
314
  }
@@ -0,0 +1,21 @@
1
+ import { type Import } from 'bare-module-lexer'
2
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
+
4
+ interface BareResolveOptions extends ResolveOptions {
5
+ platform?: string
6
+ arch?: string
7
+ simulator?: boolean
8
+ target?: string[]
9
+ }
10
+
11
+ declare function resolve(
12
+ entry: Import,
13
+ parentURL: URL,
14
+ opts?: BareResolveOptions
15
+ ): Resolver
16
+
17
+ declare namespace resolve {
18
+ export { type BareResolveOptions }
19
+ }
20
+
21
+ export = resolve
@@ -1,14 +1,13 @@
1
1
  const lex = require('bare-module-lexer')
2
2
  const resolve = require('../resolve')
3
+ const runtime = require('../runtime')
3
4
 
4
5
  module.exports = function (entry, parentURL, opts = {}) {
5
- const runtime = require('#runtime')
6
-
7
6
  const {
8
7
  linked = false,
9
8
  platform = runtime.platform,
10
9
  arch = runtime.arch,
11
- simulator = false,
10
+ simulator = runtime.simulator,
12
11
  host = `${platform}-${arch}${simulator ? '-simulator' : ''}`,
13
12
  target = [host]
14
13
  } = opts
@@ -0,0 +1,10 @@
1
+ import { type Import } from 'bare-module-lexer'
2
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
+
4
+ declare function resolve(
5
+ entry: Import,
6
+ parentURL: URL,
7
+ opts?: ResolveOptions
8
+ ): Resolver
9
+
10
+ export = resolve
@@ -0,0 +1,21 @@
1
+ import { type Import } from 'bare-module-lexer'
2
+ import { type ResolveOptions, type Resolver } from 'bare-addon-resolve'
3
+
4
+ interface NodeResolveOptions extends ResolveOptions {
5
+ platform?: string
6
+ arch?: string
7
+ simulator?: boolean
8
+ target?: string[]
9
+ }
10
+
11
+ declare function resolve(
12
+ entry: Import,
13
+ parentURL: URL,
14
+ opts?: NodeResolveOptions
15
+ ): Resolver
16
+
17
+ declare namespace resolve {
18
+ export { type NodeResolveOptions }
19
+ }
20
+
21
+ export = resolve
@@ -1,13 +1,12 @@
1
1
  const lex = require('bare-module-lexer')
2
2
  const resolve = require('../resolve')
3
+ const runtime = require('../runtime')
3
4
 
4
5
  module.exports = function (entry, parentURL, opts = {}) {
5
- const runtime = require('#runtime')
6
-
7
6
  const {
8
7
  platform = runtime.platform,
9
8
  arch = runtime.arch,
10
- simulator = false,
9
+ simulator = runtime.simulator,
11
10
  host = `${platform}-${arch}${simulator ? '-simulator' : ''}`,
12
11
  target = [host]
13
12
  } = opts
@@ -0,0 +1,8 @@
1
+ import { module } from 'bare-module-resolve'
2
+ import { addon } from 'bare-addon-resolve'
3
+
4
+ import defaultResolve from './resolve/default'
5
+ import bare from './resolve/bare'
6
+ import node from './resolve/node'
7
+
8
+ export { module, addon, defaultResolve as default, bare, node }
@@ -1,3 +1,3 @@
1
- /* global Bare */
2
1
  exports.platform = Bare.platform
3
2
  exports.arch = Bare.arch
3
+ exports.simulator = Bare.simulator
@@ -1,2 +1,3 @@
1
1
  exports.platform = process.platform
2
2
  exports.arch = process.arch
3
+ exports.simulator = false
package/lib/runtime.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('#runtime')
package/package.json CHANGED
@@ -1,17 +1,33 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "1.3.3",
3
+ "version": "1.4.1",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
- ".": "./index.js",
7
- "./package": "./package.json",
8
- "./resolve": "./lib/resolve.js",
9
- "./resolve/default": "./lib/resolve/default.js",
10
- "./resolve/bare": "./lib/resolve/bare.js",
11
- "./resolve/node": "./lib/resolve/node.js"
6
+ ".": {
7
+ "types": "./index.d.ts",
8
+ "default": "./index.js"
9
+ },
10
+ "./resolve": {
11
+ "types": "./lib/resolve.d.ts",
12
+ "default": "./lib/resolve.js"
13
+ },
14
+ "./resolve/default": {
15
+ "types": "./lib/resolve/default.d.ts",
16
+ "default": "./lib/resolve/default.js"
17
+ },
18
+ "./resolve/bare": {
19
+ "types": "./lib/resolve/bare.d.ts",
20
+ "default": "./lib/resolve/bare.js"
21
+ },
22
+ "./resolve/node": {
23
+ "types": "./lib/resolve/node.d.ts",
24
+ "default": "./lib/resolve/node.js"
25
+ },
26
+ "./package": "./package.json"
12
27
  },
13
28
  "files": [
14
29
  "index.js",
30
+ "index.d.ts",
15
31
  "lib"
16
32
  ],
17
33
  "imports": {
@@ -42,8 +58,22 @@
42
58
  "bare-module-resolve": "^1.7.0"
43
59
  },
44
60
  "devDependencies": {
61
+ "bare-buffer": "^3.0.2",
62
+ "bare-url": "^2.1.3",
45
63
  "brittle": "^3.2.1",
46
64
  "prettier": "^3.4.2",
47
65
  "prettier-config-standard": "^7.0.0"
66
+ },
67
+ "peerDependencies": {
68
+ "bare-buffer": "*",
69
+ "bare-url": "*"
70
+ },
71
+ "peerDependenciesMeta": {
72
+ "bare-buffer": {
73
+ "optional": true
74
+ },
75
+ "bare-url": {
76
+ "optional": true
77
+ }
48
78
  }
49
79
  }