Skip to content

Commit

Permalink
Add Bare and Node.js resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Oct 27, 2024
1 parent a14674c commit 27d8c8a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 15 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ for await (const dependency of traverse(new URL('file:///directory/file.js'), re

Traverse the module graph rooted at `url`, which must be a WHATWG `URL` instance. `readModule` is called with a `URL` instance for every module to be read and must either return the module source, if it exists, or `null`. `listPrefix` is called with a `URL` instance of every prefix to be listed and must return a list of `URL` instances that have the specified `URL` as a prefix. If not provided, prefixes won't be traversed. If one or both of `readModule` or `listPrefix` returns a promise, synchronous iteration is not supported.

Options include:

```js
{
resolve: resolve.default
}
```

Options supported by <https://github.com/holepunchto/bare-module-resolve> and <https://github.com/holepunchto/bare-addon-resolve> may also be specified.

#### `for (const dependency of dependencies)`

Synchronously iterate the module graph. Each yielded dependency has the following shape:
Expand All @@ -68,6 +78,28 @@ Synchronously iterate the module graph. Each yielded dependency has the followin

Asynchronously iterate the module graph. If one or both of `readModule` or `listPrefix` returns promises, these will be awaited. The same comments as `for (const dependency of dependencies)` apply.

### Resolution

#### `resolve.module`

Convenience export from <https://github.com/holepunchto/bare-module-resolve>.

#### `resolve.addon`

Convenience export from <https://github.com/holepunchto/bare-addon-resolve>.

#### `resolve.default`

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.

#### `resolve.bare`

The Bare resolver, which matches the options used by the Bare module system.

#### `resolve.node`

The Node.js resolver, which matches the options used by the Node.js module system.

### Algorithm

The following generator functions implement the traversal algorithm. To drive the generator functions, a loop like the following can be used:
Expand Down
18 changes: 3 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi

return {
* [Symbol.iterator] () {
const artifacts = createArtifacts()
const artifacts = { addons: [], assets: [] }

const queue = [exports.module(entry, null, artifacts, new Set(), opts)]

Expand Down Expand Up @@ -45,7 +45,7 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
},

async * [Symbol.asyncIterator] () {
const artifacts = createArtifacts()
const artifacts = { addons: [], assets: [] }

const queue = [exports.module(entry, null, artifacts, new Set(), opts)]

Expand Down Expand Up @@ -79,18 +79,6 @@ function defaultListPrefix () {
return []
}

function defaultResolve (entry, parentURL, opts) {
if (entry.type & lex.constants.ADDON) {
return resolve.addon(entry.specifier, parentURL, opts)
} else {
return resolve.module(entry.specifier, parentURL, opts)
}
}

function createArtifacts () {
return { addons: [], assets: [] }
}

function addURL (array, url) {
let lo = 0
let hi = array.length - 1
Expand Down Expand Up @@ -204,7 +192,7 @@ exports.preresolved = function * (url, source, resolutions, artifacts, visited,
}

exports.imports = function * (parentURL, source, imports, artifacts, visited, opts = {}) {
const { resolve = defaultResolve } = opts
const { resolve = exports.resolve.default } = opts

let yielded = false

Expand Down
66 changes: 66 additions & 0 deletions lib/resolve.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
const lex = require('bare-module-lexer')

exports.module = require('bare-module-resolve').module
exports.addon = require('bare-addon-resolve').addon

exports.default = function resolve (entry, parentURL, opts) {
if (entry.type & lex.constants.ADDON) {
return exports.addon(entry.specifier, parentURL, opts)
}

return exports.module(entry.specifier, parentURL, opts)
}

exports.bare = function resolve (entry, parentURL, opts = {}) {
const runtime = require('#runtime')

const {
platform = runtime.platform,
arch = runtime.arch,
simulator = false
} = opts

let extensions
let conditions = ['bare', 'node', platform, arch]

if (simulator) conditions = [...conditions, 'simulator']

if (entry.type & lex.constants.ADDON) {
extensions = ['.bare', '.node']
conditions = ['addon', ...conditions]

return exports.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
}

if (entry.type & lex.constants.ASSET) {
conditions = ['asset', ...conditions]
} else {
extensions = ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']

if (entry.type & lex.constants.REQUIRE) {
conditions = ['require', ...conditions]
} else if (entry.type & lex.constants.IMPORT) {
conditions = ['import', ...conditions]
}
}

return exports.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
}

exports.node = function resolve (entry, parentURL, opts) {
let extensions
let conditions

if (entry.type & lex.constants.ADDON) {
extensions = ['.node']

return exports.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
}

if (entry.type & lex.constants.REQUIRE) {
extensions = ['.js', '.json', '.node']
conditions = ['node', 'require']
} else if (entry.type & lex.constants.IMPORT) {
conditions = ['node', 'import']
}

return exports.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
}
3 changes: 3 additions & 0 deletions lib/runtime/bare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* global Bare */
exports.platform = Bare.platform
exports.arch = Bare.arch
2 changes: 2 additions & 0 deletions lib/runtime/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exports.platform = process.platform
exports.arch = process.arch
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"index.js",
"lib"
],
"imports": {
"#runtime": {
"bare": "./lib/runtime/bare.js",
"node": "./lib/runtime/node.js"
}
},
"scripts": {
"test": "standard && bare test.js"
},
Expand Down

0 comments on commit 27d8c8a

Please sign in to comment.