bare-module-resolve
Advanced tools
+82
-32
@@ -61,2 +61,4 @@ const { satisfies } = require('bare-semver') | ||
| const RESOLVED = YIELDED | 0x2 | ||
| // A resolution cycle was detected | ||
| const CYCLIC = 0x4 | ||
@@ -66,3 +68,4 @@ exports.constants = { | ||
| YIELDED, | ||
| RESOLVED | ||
| RESOLVED, | ||
| CYCLIC | ||
| } | ||
@@ -117,3 +120,9 @@ | ||
| return yield* exports.directory(specifier, parentURL, opts) | ||
| const yielded = (status & YIELDED) !== 0 | ||
| status = yield* exports.directory(specifier, parentURL, opts) | ||
| if (yielded) status |= YIELDED | ||
| return status | ||
| } | ||
@@ -143,3 +152,3 @@ | ||
| if (status) return status | ||
| if (status && (status & CYCLIC) === 0) return status | ||
| } | ||
@@ -276,3 +285,9 @@ | ||
| return yield* exports.directory(packageSubpath, packageURL, opts) | ||
| const yielded = (status & YIELDED) !== 0 | ||
| status = yield* exports.directory(packageSubpath, packageURL, opts) | ||
| if (yielded) status |= YIELDED | ||
| return status | ||
| } | ||
@@ -303,7 +318,13 @@ } | ||
| const status = yield* exports.file(packageSubpath, packageURL, false, opts) | ||
| let status = yield* exports.file(packageSubpath, packageURL, false, opts) | ||
| if (status === RESOLVED) return status | ||
| return yield* exports.directory(packageSubpath, packageURL, opts) | ||
| const yielded = (status & YIELDED) !== 0 | ||
| status = yield* exports.directory(packageSubpath, packageURL, opts) | ||
| if (yielded) status |= YIELDED | ||
| return status | ||
| } | ||
@@ -475,4 +496,6 @@ } | ||
| exports.packageTarget = function* (packageURL, target, patternMatch, isImports, opts = {}) { | ||
| const { conditions = [], matchedConditions = [] } = opts | ||
| const { conditions = [], matchedConditions = [], matchedTargets = [] } = opts | ||
| opts = { ...opts, matchedConditions, matchedTargets } | ||
| if (typeof target === 'string') { | ||
@@ -489,6 +512,14 @@ if (!target.startsWith('./') && !isImports) { | ||
| const status = yield* exports.url(target, packageURL, opts) | ||
| if (matchedTargets.includes(target)) return CYCLIC | ||
| if (status) return status | ||
| matchedTargets.push(target) | ||
| let status = yield* exports.url(target, packageURL, opts) | ||
| if (status) { | ||
| matchedTargets.pop() | ||
| return status | ||
| } | ||
| if ( | ||
@@ -503,21 +534,27 @@ target === '.' || | ||
| matchedTargets.pop() | ||
| return resolved ? RESOLVED : YIELDED | ||
| } | ||
| return yield* exports.package(target, packageURL, opts) | ||
| status = yield* exports.package(target, packageURL, opts) | ||
| matchedTargets.pop() | ||
| return status | ||
| } | ||
| if (Array.isArray(target)) { | ||
| let status = UNRESOLVED | ||
| for (const targetValue of target) { | ||
| const status = yield* exports.packageTarget( | ||
| packageURL, | ||
| targetValue, | ||
| patternMatch, | ||
| isImports, | ||
| opts | ||
| ) | ||
| status |= yield* exports.packageTarget(packageURL, targetValue, patternMatch, isImports, opts) | ||
| if (status) return status | ||
| if (status === RESOLVED) return status | ||
| } | ||
| } else if (typeof target === 'object' && target !== null) { | ||
| return status | ||
| } | ||
| if (typeof target === 'object' && target !== null) { | ||
| let status = UNRESOLVED | ||
@@ -540,3 +577,3 @@ | ||
| if (status) return status | ||
| return status | ||
| } | ||
@@ -550,2 +587,4 @@ | ||
| opts = { ...opts, matchedConditions } | ||
| if (typeof target === 'string') { | ||
@@ -590,14 +629,19 @@ const targetParts = target.split('@') | ||
| } | ||
| } else if (Array.isArray(target)) { | ||
| return UNRESOLVED | ||
| } | ||
| if (Array.isArray(target)) { | ||
| let status = UNRESOLVED | ||
| for (const targetValue of target) { | ||
| const status = yield* exports.builtinTarget( | ||
| packageSpecifier, | ||
| packageVersion, | ||
| targetValue, | ||
| opts | ||
| ) | ||
| status |= yield* exports.builtinTarget(packageSpecifier, packageVersion, targetValue, opts) | ||
| if (status) return status | ||
| if (status === RESOLVED) return status | ||
| } | ||
| } else if (typeof target === 'object' && target !== null) { | ||
| return status | ||
| } | ||
| if (typeof target === 'object' && target !== null) { | ||
| let status = UNRESOLVED | ||
@@ -620,3 +664,3 @@ | ||
| if (status) return status | ||
| return status | ||
| } | ||
@@ -763,7 +807,13 @@ | ||
| if (typeof info.main === 'string' && info.main !== '') { | ||
| const status = yield* exports.file(info.main, directoryURL, false, opts) | ||
| let status = yield* exports.file(info.main, directoryURL, false, opts) | ||
| if (status === RESOLVED) return status | ||
| return yield* exports.directory(info.main, directoryURL, opts) | ||
| const yielded = (status & YIELDED) !== 0 | ||
| status = yield* exports.directory(info.main, directoryURL, opts) | ||
| if (yielded) status |= YIELDED | ||
| return status | ||
| } | ||
@@ -770,0 +820,0 @@ } |
+5
-2
| { | ||
| "name": "bare-module-resolve", | ||
| "version": "1.12.1", | ||
| "version": "1.12.2", | ||
| "description": "Low-level module resolution algorithm for Bare", | ||
@@ -22,3 +22,5 @@ "exports": { | ||
| "scripts": { | ||
| "test": "prettier . --check && bare test.js" | ||
| "format": "prettier --write . && lunte --fix", | ||
| "lint": "prettier --check . && lunte", | ||
| "test": "brittle-bare --coverage test.js" | ||
| }, | ||
@@ -41,2 +43,3 @@ "repository": { | ||
| "brittle": "^3.2.1", | ||
| "lunte": "^1.7.0", | ||
| "prettier": "^3.3.3", | ||
@@ -43,0 +46,0 @@ "prettier-config-holepunch": "^2.0.0" |
+9
-5
@@ -67,2 +67,5 @@ # bare-module-resolve | ||
| matchedConditions: [], | ||
| // An array reference which will contain the matched targets when yielding | ||
| // remapped specifiers., such as those from resolution or import maps. | ||
| matchedTargets: [], | ||
| // The supported engine versions. | ||
@@ -286,9 +289,10 @@ engines: {}, | ||
| 1. Replace every instance of `*` in `target` with `patternMatch`. | ||
| 3. If `url(target, packageURL, options)` yields, return. | ||
| 4. If `target` equals `.` or `..`, or if `target` starts with `/`, `./`, or `../`: | ||
| 3. If `target` forms a cycle with a previous `target` in the same resolution chain, return. | ||
| 4. If `url(target, packageURL, options)` yields, return. | ||
| 5. If `target` equals `.` or `..`, or if `target` starts with `/`, `./`, or `../`: | ||
| 1. Yield the resolution of `target` relative to `packageURL` and return. | ||
| 5. Return `package(target, packageURL, options)`. | ||
| 6. Return `package(target, packageURL, options)`. | ||
| 2. If `target` is an array: | ||
| 1. For each value `targetValue` of `target`: | ||
| 1. If `packageTarget(packageURL, targetValue, patternMatch, isImports, options)` yields, return. | ||
| 1. If `packageTarget(packageURL, targetValue, patternMatch, isImports, options)` resolves, return. | ||
| 3. If `target` is a non-`null` object: | ||
@@ -319,3 +323,3 @@ 1. For each key `condition` of `target`: | ||
| 1. For each value `targetValue` of `target`: | ||
| 1. If `builtinTarget(packageSpecifier, packageVersion, targetValue, options)` yields, return. | ||
| 1. If `builtinTarget(packageSpecifier, packageVersion, targetValue, options)` resolves, return. | ||
| 3. If `target` is a non-`null` object: | ||
@@ -322,0 +326,0 @@ 1. For each key `condition` of `target`: |
56700
2.24%798
2.84%357
1.13%5
25%