fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+356
@@ -0,0 +1,356 @@
|
||||
# bare-module-resolve
|
||||
|
||||
Low-level module resolution algorithm for Bare. The algorithm is implemented as a generator function that yields either package manifests to be read or resolution candidates to be tested by the caller. As a convenience, the main export is a synchronous and asynchronous iterable that relies on package manifests being read by a callback. For asynchronous iteration, the callback may return promises which will be awaited before being passed to the generator.
|
||||
|
||||
```
|
||||
npm i bare-module-resolve
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
For synchronous resolution:
|
||||
|
||||
```js
|
||||
const resolve = require('bare-module-resolve')
|
||||
|
||||
function readPackage(url) {
|
||||
// Read and parse `url` if it exists, otherwise `null`
|
||||
}
|
||||
|
||||
for (const resolution of resolve('./file.js', new URL('file:///directory/'), readPackage)) {
|
||||
console.log(resolution)
|
||||
}
|
||||
```
|
||||
|
||||
For asynchronous resolution:
|
||||
|
||||
```js
|
||||
const resolve = require('bare-module-resolve')
|
||||
|
||||
async function readPackage(url) {
|
||||
// Read and parse `url` if it exists, otherwise `null`
|
||||
}
|
||||
|
||||
for await (const resolution of resolve('./file.js', new URL('file:///directory/'), readPackage)) {
|
||||
console.log(resolution)
|
||||
}
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
#### `const resolver = resolve(specifier, parentURL[, options][, readPackage])`
|
||||
|
||||
Resolve `specifier` relative to `parentURL`, which must be a WHATWG `URL` instance. `readPackage` is called with a `URL` instance for every package manifest to be read and must either return the parsed JSON package manifest, if it exists, or `null`. If `readPackage` returns a promise, synchronous iteration is not supported.
|
||||
|
||||
Options include:
|
||||
|
||||
```js
|
||||
options = {
|
||||
// A default "imports" map to apply to all specifiers. Follows the same
|
||||
// syntax and rules as the "imports" property defined in `package.json`.
|
||||
imports,
|
||||
// A list of builtin module specifiers. If matched, the protocol of the
|
||||
// resolved URL will be `builtinProtocol`.
|
||||
builtins: [],
|
||||
// The protocol to use for resolved builtin module specifiers.
|
||||
builtinProtocol: 'builtin:',
|
||||
// A list of module specifiers whose resolution should be deferred. If matched,
|
||||
// the protocol of the resolved URL will be `deferredProtocol`.
|
||||
defer: [],
|
||||
// The protocol to use for resolved deferred module specifiers.
|
||||
deferredProtocol: 'deferred:',
|
||||
// The supported import conditions. "default" is always recognized.
|
||||
conditions: [],
|
||||
// An array reference which will contain the matched conditions when yielding
|
||||
// resolutions.
|
||||
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.
|
||||
engines: {},
|
||||
// The file extensions to look for. Must be provided to support extensionless
|
||||
// specifier resolution and directory support, such as resolving './foo' to
|
||||
// './foo.js' or './foo/index.js'.
|
||||
extensions: [],
|
||||
// A map of preresolved imports with keys being serialized parent URLs and
|
||||
// values being "imports" maps.
|
||||
resolutions
|
||||
}
|
||||
```
|
||||
|
||||
#### `for (const resolution of resolver)`
|
||||
|
||||
Synchronously iterate the module resolution candidates. The resolved module is the first candidate that exists, either as a file on a file system, a resource at a URL, or something else entirely.
|
||||
|
||||
#### `for await (const resolution of resolver)`
|
||||
|
||||
Asynchronously iterate the module resolution candidates. If `readPackage` returns promises, these will be awaited. The same comments as `for (const resolution of resolver)` apply.
|
||||
|
||||
### Algorithm
|
||||
|
||||
The following generator functions implement the resolution algorithm, which has been adapted from the Node.js resolution algorithms for CommonJS and ES modules. Unlike Node.js, Bare uses the same resolution algorithm for both module formats. The yielded values have the following shape:
|
||||
|
||||
**Package manifest**
|
||||
|
||||
```js
|
||||
next.value = {
|
||||
package: URL
|
||||
}
|
||||
```
|
||||
|
||||
If the package manifest identified by `next.value.package` exists, `generator.next()` must be passed the parsed JSON value of the manifest. If it does not exist, pass `null` instead.
|
||||
|
||||
**Resolution candidate**
|
||||
|
||||
```js
|
||||
next.value = {
|
||||
resolution: URL
|
||||
}
|
||||
```
|
||||
|
||||
If the module identified by `next.value.resolution` exists, `generator.next()` may be passed `true` to signal that the resolution for the current set of conditions has been identified. If it does not exist, pass `false` instead.
|
||||
|
||||
To drive the generator functions, a loop like the following can be used:
|
||||
|
||||
```js
|
||||
const generator = resolve.module(specifier, parentURL)
|
||||
|
||||
let next = generator.next()
|
||||
|
||||
while (next.done !== true) {
|
||||
const value = next.value
|
||||
|
||||
if (value.package) {
|
||||
// Read and parse `value.package` if it exists, otherwise `null`
|
||||
let info
|
||||
|
||||
next = generator.next(info)
|
||||
} else {
|
||||
const resolution = value.resolution
|
||||
|
||||
// `true` if `resolution` was the correct candidate, otherwise `false`
|
||||
let resolved
|
||||
|
||||
next = generator.next(resolved)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Options are the same as `resolve()` for all functions.
|
||||
|
||||
> [!WARNING]
|
||||
> 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.
|
||||
|
||||
#### `const generator = resolve.module(specifier, parentURL[, options])`
|
||||
|
||||
1. If `specifier` [starts with a Windows drive letter](https://url.spec.whatwg.org/#start-with-a-windows-drive-letter):
|
||||
1. Prepend `/` to `specifier`.
|
||||
2. If `options.resolutions` is set:
|
||||
1. If `preresolved(specifier, options.resolutions, parentURL, options)` yields, return.
|
||||
3. If `url(specifier, parentURL, options)` yields, return.
|
||||
4. If `packageImports(specifier, parentURL, options)` yields, return.
|
||||
5. If `specifier` equals `.` or `..`, or if `specifier` starts with `/`, `\`, `./`, `.\`, `../`, or `..\`:
|
||||
1. If `options.imports` is set:
|
||||
1. If `packageImportsExports(specifier, options.imports, parentURL, true, options)` yields, return.
|
||||
2. If `deferred(specifier, options)` yields, return.
|
||||
3. If `file(specifier, parentURL, false, options)` resolves, return.
|
||||
4. Return `directory(specifier, parentURL, options)`.
|
||||
6. Return `package(specifier, parentURL, options)`.
|
||||
|
||||
#### `const generator = resolve.url(url, parentURL[, options])`
|
||||
|
||||
1. If `url` is not a valid URL, return.
|
||||
2. If `options.imports` is set:
|
||||
1. If `packageImportsExports(url.href, options.imports, parentURL, true, options)` yields, return.
|
||||
3. If `url.protocol` equals `options.deferredProtocol`:
|
||||
1. Let `specifier` be `url.pathname`.
|
||||
2. If `options.resolutions` is set:
|
||||
1. Let `imports` be `options.resolutions[parentURL]`.
|
||||
2. If `imports` is a non-`null` object:
|
||||
1. Set `imports[specifier]` to `null`.
|
||||
3. Return `module(specifier, parentURL, options)`.
|
||||
4. If `url.protocol` equals `node:`:
|
||||
1. Let `specifier` be `url.pathname`.
|
||||
2. If `specifier` equals `.` or `..`, or if `specifier` starts with `/`, `\`, `./`, `.\`, `../`, or `..\`, throw.
|
||||
3. Return `package(specifier, parentURL, options)`.
|
||||
5. Yield `url`.
|
||||
|
||||
#### `const generator = resolve.preresolved(specifier, resolutions, parentURL[, options])`
|
||||
|
||||
1. Let `imports` be `resolutions[parentURL]`.
|
||||
2. If `imports` is a non-`null` object:
|
||||
1. Return `packageImportsExports(specifier, imports, parentURL, true, options)`.
|
||||
|
||||
#### `const generator = resolve.deferred(specifier[, options])`
|
||||
|
||||
1. If `options.defer` includes `specifier`:
|
||||
1. Yield `options.deferredProtocol` concatenated with `specifier` and return.
|
||||
|
||||
#### `const generator = resolve.package(packageSpecifier, parentURL[, options])`
|
||||
|
||||
1. If `packageSpecifier` is the empty string, throw.
|
||||
2. If `packageSpecifier` does not start with `@`:
|
||||
1. Set `packageName` to the substring of `packageSpecifier` until the first `/` or the end of the string.
|
||||
3. Let `packageName` be `undefined`.
|
||||
4. Otherwise:
|
||||
1. If `packageSpecifier` does not include `/`, throw.
|
||||
2. Set `packageName` to the substring of `packageSpecifier` until the second `/` or the end of the string.
|
||||
5. If `packageName` starts with `.` or includes `\` or `%`, throw.
|
||||
6. If `builtinTarget(packageSpecifier, null, options.builtins, options)` yields, return.
|
||||
7. If `deferred(packageSpecifier, options)` yields, return.
|
||||
8. Let `packageSubpath` be `.` concatenated with the substring of `packageSpecifier` from the position at the length of `packageName`.
|
||||
9. If `packageSelf(packageName, packageSubpath, parentURL, options)` yields, return.
|
||||
10. For each value `packageURL` of `lookupPackageRoot(packageName, parentURL, options)`:
|
||||
1. Let `info` be the result of yielding `packageURL`.
|
||||
2. If `info` is not `null`:
|
||||
1. If `info.engines` is set:
|
||||
1. Call `validateEngines(packageURL, info.engines, options)`.
|
||||
2. If `info.exports` is set:
|
||||
1. Return `packageExports(packageURL, packageSubpath, info.exports, options)`.
|
||||
3. If `packageSubpath` is `.`:
|
||||
1. If `info.main` is a non-empty string:
|
||||
1. Set `packageSubpath` to `info.main`.
|
||||
2. Otherwise:
|
||||
1. Return `file('index', packageURL, true, options)`.
|
||||
4. If `file(packageSubpath, packageURL, false, options)` resolves, return.
|
||||
5. Return `directory(packageSubpath, packageURL, options)`.
|
||||
|
||||
#### `const generator = resolve.packageSelf(packageName, packageSubpath, parentURL[, options])`
|
||||
|
||||
1. For each value `packageURL` of `lookupPackageScope(parentURL, options)`:
|
||||
1. Let `info` be the result of yielding `packageURL`.
|
||||
2. If `info` is not `null`:
|
||||
1. If `info.name` does not equal `packageName`, return.
|
||||
2. If `info.exports` is set:
|
||||
1. Return `packageExports(packageURL, packageSubpath, info.exports, options)`.
|
||||
3. If `packageSubpath` is `.`:
|
||||
1. If `info.main` is a non-empty string:
|
||||
1. Set `packageSubpath` to `info.main`.
|
||||
2. Otherwise:
|
||||
1. Return `file('index', packageURL, true, options)`.
|
||||
4. If `file(packageSubpath, packageURL, false, options)` resolves, return.
|
||||
5. Return `directory(packageSubpath, packageURL, options)`.
|
||||
|
||||
#### `const generator = resolve.packageExports(packageURL, subpath, exports[, options])`
|
||||
|
||||
1. If `subpath` is `.`:
|
||||
1. Let `mainExport` be `undefined`.
|
||||
2. If `exports` is a string or an array:
|
||||
1. Set `mainExport` to `exports`.
|
||||
3. If `exports` is a non-`null` object:
|
||||
1. If some keys of `exports` start with `.`:
|
||||
1. If `.` is a key of `exports`:
|
||||
1. Set `mainExport` to `exports['.']`.
|
||||
2. Otherwise:
|
||||
1. Set `mainExport` to `exports`.
|
||||
4. If `mainExport` is not `undefined`:
|
||||
1. If `packageTarget(packageURL, mainExport, null, false, options)` yields, return.
|
||||
2. Otherwise, if `exports` is a non-`null` object:
|
||||
1. If every key of `exports` starts with `.`:
|
||||
1. If `packageImportsExports(subpath, exports, packageURL, false, options)` yields, return.
|
||||
3. Throw.
|
||||
|
||||
#### `const generator = resolve.packageImports(specifier, parentURL[, options])`
|
||||
|
||||
1. If `specifier` is `#` or starts with `#/`, throw.
|
||||
2. For each value `packageURL` of `lookupPackageScope(parentURL, options)`:
|
||||
1. Let `info` be the result of yielding `packageURL`.
|
||||
2. If `info` is not `null`:
|
||||
1. If `info.imports` is set:
|
||||
1. If `packageImportsExports(specifier, info.imports, packageURL, true, options)` yields, return.
|
||||
2. If specifier starts with `#`, throw.
|
||||
3. Return.
|
||||
3. If `options.imports` is set:
|
||||
1. If `packageImportsExports(url.href, options.imports, parentURL, true, options)` yields, return.
|
||||
|
||||
#### `const generator = resolve.packageImportsExports(matchKey, matchObject, packageURL, isImports[, options])`
|
||||
|
||||
1. If `matchKey` is a key of `matchObject` and `matchKey` does not include `*`:
|
||||
1. Let `target` be `matchObject[matchKey]`.
|
||||
2. Return `packageTarget(packageURL, target, null, isImports, options)`.
|
||||
2. Let `expansionKeys` be the keys of `matchObject` that include `*` sorted by `patternKeyCompare`.
|
||||
3. For each value `expansionKey` of `expansionKeys`:
|
||||
1. Let `patternBase` be the substring of `expansionKey` until the first `*`.
|
||||
2. If `matchKey` starts with but isn't equal to `patternBase`:
|
||||
1. Let `patternTrailer` be the substring of `expansionKey` from the position at the index after the first `*`.
|
||||
2. If `patternTrailer` is the empty string, or if `matchKey` ends with `patternTrailer` and the length of `matchKey` is greater than or equal to the length of `expansionKey`:
|
||||
1. Let `target` be `matchObject[expansionKey]`.
|
||||
2. Let `patternMatch` be the substring of `matchKey` from the position at the length of `patternBase` until the length of `matchKey` minus the length of `patternTrailer`.
|
||||
3. Return `packageTarget(packageURL, target, patternMatch, isImports, options)`.
|
||||
|
||||
#### `const generator = resolve.packageTarget(packageURL, target, patternMatch, isImports[, options])`
|
||||
|
||||
1. If `target` is a string:
|
||||
1. If `target` does not start with `./` and `isImports` is `false`, throw.
|
||||
2. If `patternMatch` is not `null`:
|
||||
1. Replace every instance of `*` in `target` with `patternMatch`.
|
||||
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.
|
||||
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)` resolves, return.
|
||||
3. If `target` is a non-`null` object:
|
||||
1. For each key `condition` of `target`:
|
||||
1. If `condition` equals `default` or if `options.conditions` includes `condition`:
|
||||
1. Let `targetValue` be `target[condition]`.
|
||||
2. Return `packageTarget(packageURL, targetValue, patternMatch, isImports, options)`.
|
||||
|
||||
#### `const generator = resolve.builtinTarget(packageSpecifier, packageVersion, target[, options])`
|
||||
|
||||
1. If `target` is a string:
|
||||
1. If `target` does not start with `@`:
|
||||
1. Let `targetName` be the substring of `target` until the first `@` or the end of the string.
|
||||
2. Let `targetVersion` be the substring of `target` from the character following the first `@` and to the end of string, or `null` if no such substring exists.
|
||||
2. Otherwise:
|
||||
1. Let `targetName` be the substring of `target` until the second `@` or the end of the string.
|
||||
2. Let `targetVersion` be the substring of `target` from the character following the second `@` and to the end of string, or `null` if no such substring exists.
|
||||
3. If `packageSpecifier` equals `targetName`:
|
||||
1. If `packageVersion` is `null` and `targetVersion` is `null`:
|
||||
1. Yield `options.builtinProtocol` concatenated with `packageSpecifier` and return.
|
||||
2. Let `version` be `null`.
|
||||
3. If `packageVersion` is `null`, let `version` be `targetVersion`.
|
||||
4. Otherwise, if `targetVersion` is either `null` or equals `packageVersion`, let `version` be `packageVersion`
|
||||
5. If `version` is not `null`:
|
||||
1. Yield `options.builtinProtocol` concatenated with `packageSpecifier`, `@`, and `version` and return.
|
||||
2. If `target` is an array:
|
||||
1. For each value `targetValue` of `target`:
|
||||
1. If `builtinTarget(packageSpecifier, packageVersion, targetValue, options)` resolves, return.
|
||||
3. If `target` is a non-`null` object:
|
||||
1. For each key `condition` of `target`:
|
||||
1. If `condition` equals `default` or if `options.conditions` includes `condition`:
|
||||
1. Let `targetValue` be `target[condition]`.
|
||||
2. Return `builtinTarget(packageSpecifier, packageVersion, targetValue, options)`.
|
||||
|
||||
#### `const generator = resolve.file(filename, parentURL, isIndex[, options])`
|
||||
|
||||
1. If `filename` equals `.` or `..`, or if `filename` ends with `/` or `\`, return.
|
||||
2. If `parentURL` is a `file:` URL and `filename` includes encoded `/` or `\`, throw.
|
||||
3. If `isIndex` is `false`:
|
||||
1. Yield the resolution of `filename` relative to `parentURL`.
|
||||
4. For each value `ext` of `options.extensions`:
|
||||
1. If `filename` ends with `ext`, continue.
|
||||
2. Yield the resolution of `filename` concatenated with `ext` relative to `parentURL`.
|
||||
|
||||
#### `const generator = resolve.directory(dirname, parentURL[, options])`
|
||||
|
||||
1. Let `directoryURL` be `undefined`.
|
||||
2. If `dirname` ends with `/` or `\`:
|
||||
1. Set `directoryURL` to the resolution of `dirname` relative to `parentURL`.
|
||||
3. Otherwise:
|
||||
1. Set `directoryURL` to the resolution of `dirname` concatenated with `/` relative to `parentURL`.
|
||||
4. Let `info` be the result of yielding the resolution of `package.json` relative to `directoryURL`.
|
||||
5. If `info` is not `null`:
|
||||
1. If `info.exports` is set:
|
||||
1. Return `packageExports(directoryURL, '.', info.exports, options)`.
|
||||
2. If `info.main` is a non-empty string:
|
||||
1. If `file(info.main, directoryURL, false, options)` resolves, return.
|
||||
2. Return `directory(info.main, directoryURL, options)`.
|
||||
6. Return `file('index', directoryURL, true, options)`.
|
||||
|
||||
## License
|
||||
|
||||
Apache-2.0
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
import URL from 'bare-url'
|
||||
|
||||
type ConditionalSpecifier =
|
||||
| string
|
||||
| ConditionalSpecifier[]
|
||||
| { [condition: string]: ConditionalSpecifier }
|
||||
|
||||
type ImportsMap = { [specifier: string]: ConditionalSpecifier }
|
||||
|
||||
type ExportsMap = ImportsMap
|
||||
|
||||
type ResolutionsMap = { [href: string]: ImportsMap }
|
||||
|
||||
type Builtins = ConditionalSpecifier[]
|
||||
|
||||
type Conditions = string[] | Conditions[]
|
||||
|
||||
type Engines = { [name: string]: string }
|
||||
|
||||
type JSON = string | number | boolean | JSON[] | { [key: string]: JSON }
|
||||
|
||||
interface ResolveOptions {
|
||||
builtinProtocol?: string
|
||||
builtins?: Builtins
|
||||
conditions?: Conditions
|
||||
defer?: string[]
|
||||
deferredProtocol?: string
|
||||
engines?: Engines
|
||||
extensions?: string[]
|
||||
imports?: ImportsMap
|
||||
matchedConditions?: string[]
|
||||
resolutions?: ResolutionsMap
|
||||
}
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
readPackage?: (url: URL) => JSON | null
|
||||
): Iterable<URL>
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
readPackage: (url: URL) => Promise<JSON | null>
|
||||
): AsyncIterable<URL>
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
opts: ResolveOptions,
|
||||
readPackage?: (url: URL) => JSON | null
|
||||
): Iterable<URL>
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
opts: ResolveOptions,
|
||||
readPackage: (url: URL) => Promise<JSON | null>
|
||||
): AsyncIterable<URL>
|
||||
|
||||
declare namespace resolve {
|
||||
export {
|
||||
type ConditionalSpecifier,
|
||||
type ImportsMap,
|
||||
type ExportsMap,
|
||||
type ResolutionsMap,
|
||||
type Builtins,
|
||||
type Conditions,
|
||||
type Engines,
|
||||
type ResolveOptions
|
||||
}
|
||||
|
||||
export const constants: {
|
||||
UNRESOLVED: number
|
||||
YIELDED: number
|
||||
RESOLVED: number
|
||||
}
|
||||
|
||||
export type Resolver = Generator<
|
||||
{ package: URL } | { resolution: URL },
|
||||
number,
|
||||
void | boolean | JSON | null
|
||||
>
|
||||
|
||||
export function module(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function url(url: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function preresolved(
|
||||
specifier: string,
|
||||
resolutions: ResolutionsMap,
|
||||
parentURL: URL,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function package(packageSpecifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function packageSelf(
|
||||
packageName: string,
|
||||
packageSubpath: string,
|
||||
parentURL: URL,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function packageExports(
|
||||
packageURL: URL,
|
||||
subpath: string,
|
||||
packageExports: ExportsMap,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function packageImports(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function packageImportsExports(
|
||||
matchKey: string,
|
||||
matchObject: ImportsMap | ExportsMap,
|
||||
packageURL: URL,
|
||||
isImports: boolean,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function packageTarget(
|
||||
packageURL: URL,
|
||||
target: ConditionalSpecifier,
|
||||
patternMatch: string,
|
||||
isImports: boolean,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function builtinTarget(
|
||||
packageSpecifier: string,
|
||||
packageVersion: string,
|
||||
target: ConditionalSpecifier,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function file(
|
||||
filename: string,
|
||||
parentURL: URL,
|
||||
isIndex: boolean,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function directory(dirname: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
}
|
||||
|
||||
export = resolve
|
||||
+846
@@ -0,0 +1,846 @@
|
||||
const { satisfies } = require('bare-semver')
|
||||
const errors = require('./lib/errors')
|
||||
|
||||
module.exports = exports = function resolve(specifier, parentURL, opts, readPackage) {
|
||||
if (typeof opts === 'function') {
|
||||
readPackage = opts
|
||||
opts = {}
|
||||
} else if (typeof readPackage !== 'function') {
|
||||
readPackage = defaultReadPackage
|
||||
}
|
||||
|
||||
return {
|
||||
*[Symbol.iterator]() {
|
||||
const generator = exports.module(specifier, parentURL, opts)
|
||||
|
||||
let next = generator.next()
|
||||
|
||||
while (next.done !== true) {
|
||||
const value = next.value
|
||||
|
||||
if (value.package) {
|
||||
next = generator.next(readPackage(value.package))
|
||||
} else {
|
||||
next = generator.next(yield value.resolution)
|
||||
}
|
||||
}
|
||||
|
||||
return next.value
|
||||
},
|
||||
|
||||
async *[Symbol.asyncIterator]() {
|
||||
const generator = exports.module(specifier, parentURL, opts)
|
||||
|
||||
let next = generator.next()
|
||||
|
||||
while (next.done !== true) {
|
||||
const value = next.value
|
||||
|
||||
if (value.package) {
|
||||
next = generator.next(await readPackage(value.package))
|
||||
} else {
|
||||
next = generator.next(yield value.resolution)
|
||||
}
|
||||
}
|
||||
|
||||
return next.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function defaultReadPackage() {
|
||||
return null
|
||||
}
|
||||
|
||||
// No resolution candidate was yielded
|
||||
const UNRESOLVED = 0x0
|
||||
// At least 1 resolution candidate was yielded
|
||||
const YIELDED = 0x1
|
||||
// At least 1 resolution candidate was yielded and resolved
|
||||
const RESOLVED = YIELDED | 0x2
|
||||
// A resolution cycle was detected
|
||||
const CYCLIC = 0x4
|
||||
|
||||
exports.constants = {
|
||||
UNRESOLVED,
|
||||
YIELDED,
|
||||
RESOLVED,
|
||||
CYCLIC
|
||||
}
|
||||
|
||||
exports.module = function* (specifier, parentURL, opts = {}) {
|
||||
const { resolutions = null, imports = null } = opts
|
||||
|
||||
if (exports.startsWithWindowsDriveLetter(specifier)) {
|
||||
specifier = '/' + specifier
|
||||
}
|
||||
|
||||
let status
|
||||
|
||||
if (resolutions) {
|
||||
status = yield* exports.preresolved(specifier, resolutions, parentURL, opts)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
|
||||
status = yield* exports.url(specifier, parentURL, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
status = yield* exports.packageImports(specifier, parentURL, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
if (
|
||||
specifier === '.' ||
|
||||
specifier === '..' ||
|
||||
specifier[0] === '/' ||
|
||||
specifier[0] === '\\' ||
|
||||
specifier.startsWith('./') ||
|
||||
specifier.startsWith('.\\') ||
|
||||
specifier.startsWith('../') ||
|
||||
specifier.startsWith('..\\')
|
||||
) {
|
||||
if (imports) {
|
||||
status = yield* exports.packageImportsExports(specifier, imports, parentURL, true, opts)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
|
||||
status = yield* exports.deferred(specifier, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
status = yield* exports.file(specifier, parentURL, false, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
|
||||
const yielded = (status & YIELDED) !== 0
|
||||
|
||||
status = yield* exports.directory(specifier, parentURL, opts)
|
||||
|
||||
if (yielded) status |= YIELDED
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
return yield* exports.package(specifier, parentURL, opts)
|
||||
}
|
||||
|
||||
exports.url = function* (url, parentURL, opts = {}) {
|
||||
const { imports = null, deferredProtocol = 'deferred:', resolutions = null } = opts
|
||||
|
||||
let resolution
|
||||
try {
|
||||
resolution = new URL(url)
|
||||
} catch {
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
if (imports) {
|
||||
const status = yield* exports.packageImportsExports(
|
||||
resolution.href,
|
||||
imports,
|
||||
parentURL,
|
||||
true,
|
||||
opts
|
||||
)
|
||||
|
||||
if (status && (status & CYCLIC) === 0) return status
|
||||
}
|
||||
|
||||
if (resolution.protocol === deferredProtocol) {
|
||||
const specifier = resolution.pathname
|
||||
|
||||
if (resolutions) {
|
||||
const imports = resolutions[parentURL.href]
|
||||
|
||||
if (typeof imports === 'object' && imports !== null) {
|
||||
opts = {
|
||||
...opts,
|
||||
resolutions: { ...resolutions, [parentURL.href]: { ...imports, [specifier]: null } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return yield* exports.module(specifier, parentURL, opts)
|
||||
}
|
||||
|
||||
if (resolution.protocol === 'node:') {
|
||||
const specifier = resolution.pathname
|
||||
|
||||
if (
|
||||
specifier === '.' ||
|
||||
specifier === '..' ||
|
||||
specifier[0] === '/' ||
|
||||
specifier.startsWith('./') ||
|
||||
specifier.startsWith('../')
|
||||
) {
|
||||
throw errors.INVALID_MODULE_SPECIFIER(`Module specifier '${url}' is not a valid package name`)
|
||||
}
|
||||
|
||||
return yield* exports.package(specifier, parentURL, opts)
|
||||
}
|
||||
|
||||
const resolved = yield { resolution }
|
||||
|
||||
return resolved ? RESOLVED : YIELDED
|
||||
}
|
||||
|
||||
exports.preresolved = function* (specifier, resolutions, parentURL, opts = {}) {
|
||||
const imports = resolutions[parentURL.href]
|
||||
|
||||
if (typeof imports === 'object' && imports !== null) {
|
||||
return yield* exports.packageImportsExports(specifier, imports, parentURL, true, opts)
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.deferred = function* (specifier, opts = {}) {
|
||||
const { deferredProtocol = 'deferred:', defer = [] } = opts
|
||||
|
||||
if (defer.includes(specifier)) {
|
||||
const resolved = yield { resolution: new URL(deferredProtocol + specifier) }
|
||||
|
||||
return resolved ? RESOLVED : YIELDED
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.package = function* (packageSpecifier, parentURL, opts = {}) {
|
||||
const { builtins = [] } = opts
|
||||
|
||||
if (packageSpecifier === '') {
|
||||
throw errors.INVALID_MODULE_SPECIFIER(
|
||||
`Module specifier '${packageSpecifier}' is not a valid package name`
|
||||
)
|
||||
}
|
||||
|
||||
let packageName
|
||||
|
||||
if (packageSpecifier[0] !== '@') {
|
||||
packageName = packageSpecifier.split('/', 1).join()
|
||||
} else {
|
||||
if (!packageSpecifier.includes('/')) {
|
||||
throw errors.INVALID_MODULE_SPECIFIER(
|
||||
`Module specifier '${packageSpecifier}' is not a valid package name`
|
||||
)
|
||||
}
|
||||
|
||||
packageName = packageSpecifier.split('/', 2).join('/')
|
||||
}
|
||||
|
||||
if (packageName[0] === '.' || packageName.includes('\\') || packageName.includes('%')) {
|
||||
throw errors.INVALID_MODULE_SPECIFIER(
|
||||
`Module specifier '${packageSpecifier}' is not a valid package name`
|
||||
)
|
||||
}
|
||||
|
||||
let status
|
||||
|
||||
status = yield* exports.builtinTarget(packageSpecifier, null, builtins, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
status = yield* exports.deferred(packageSpecifier, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
let packageSubpath = '.' + packageSpecifier.substring(packageName.length)
|
||||
|
||||
status = yield* exports.packageSelf(packageName, packageSubpath, parentURL, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
parentURL = new URL(parentURL.href)
|
||||
|
||||
for (const packageURL of exports.lookupPackageRoot(packageName, parentURL)) {
|
||||
const info = yield { package: packageURL }
|
||||
|
||||
if (info) {
|
||||
if (info.engines) exports.validateEngines(packageURL, info.engines, opts)
|
||||
|
||||
if (info.exports) {
|
||||
return yield* exports.packageExports(packageURL, packageSubpath, info.exports, opts)
|
||||
}
|
||||
|
||||
if (packageSubpath === '.') {
|
||||
if (typeof info.main === 'string' && info.main !== '') {
|
||||
packageSubpath = info.main
|
||||
} else {
|
||||
return yield* exports.file('index', packageURL, true, opts)
|
||||
}
|
||||
}
|
||||
|
||||
status = yield* exports.file(packageSubpath, packageURL, false, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
|
||||
const yielded = (status & YIELDED) !== 0
|
||||
|
||||
status = yield* exports.directory(packageSubpath, packageURL, opts)
|
||||
|
||||
if (yielded) status |= YIELDED
|
||||
|
||||
return status
|
||||
}
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.packageSelf = function* (packageName, packageSubpath, parentURL, opts = {}) {
|
||||
for (const packageURL of exports.lookupPackageScope(parentURL, opts)) {
|
||||
const info = yield { package: packageURL }
|
||||
|
||||
if (info) {
|
||||
if (info.name !== packageName) return false
|
||||
|
||||
if (info.exports) {
|
||||
return yield* exports.packageExports(packageURL, packageSubpath, info.exports, opts)
|
||||
}
|
||||
|
||||
if (packageSubpath === '.') {
|
||||
if (typeof info.main === 'string' && info.main !== '') {
|
||||
packageSubpath = info.main
|
||||
} else {
|
||||
return yield* exports.file('index', packageURL, true, opts)
|
||||
}
|
||||
}
|
||||
|
||||
let status = yield* exports.file(packageSubpath, packageURL, false, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
|
||||
const yielded = (status & YIELDED) !== 0
|
||||
|
||||
status = yield* exports.directory(packageSubpath, packageURL, opts)
|
||||
|
||||
if (yielded) status |= YIELDED
|
||||
|
||||
return status
|
||||
}
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.packageExports = function* (packageURL, subpath, packageExports, opts = {}) {
|
||||
if (subpath === '.') {
|
||||
let mainExport
|
||||
|
||||
if (typeof packageExports === 'string' || Array.isArray(packageExports)) {
|
||||
mainExport = packageExports
|
||||
} else if (typeof packageExports === 'object' && packageExports !== null) {
|
||||
const keys = Object.keys(packageExports)
|
||||
|
||||
if (keys.some((key) => key.startsWith('.'))) {
|
||||
if ('.' in packageExports) mainExport = packageExports['.']
|
||||
} else {
|
||||
mainExport = packageExports
|
||||
}
|
||||
}
|
||||
|
||||
if (mainExport) {
|
||||
const status = yield* exports.packageTarget(packageURL, mainExport, null, false, opts)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
} else if (typeof packageExports === 'object' && packageExports !== null) {
|
||||
const keys = Object.keys(packageExports)
|
||||
|
||||
if (keys.every((key) => key.startsWith('.'))) {
|
||||
const status = yield* exports.packageImportsExports(
|
||||
subpath,
|
||||
packageExports,
|
||||
packageURL,
|
||||
false,
|
||||
opts
|
||||
)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
}
|
||||
|
||||
throw errors.PACKAGE_PATH_NOT_EXPORTED(
|
||||
`Package subpath '${subpath}' is not defined by "exports" in '${packageURL}'`
|
||||
)
|
||||
}
|
||||
|
||||
exports.packageImports = function* (specifier, parentURL, opts = {}) {
|
||||
const { imports = null } = opts
|
||||
|
||||
if (specifier === '#' || specifier.startsWith('#/')) {
|
||||
throw errors.INVALID_MODULE_SPECIFIER(
|
||||
`Module specifier '${specifier}' is not a valid internal imports specifier`
|
||||
)
|
||||
}
|
||||
|
||||
for (const packageURL of exports.lookupPackageScope(parentURL, opts)) {
|
||||
const info = yield { package: packageURL }
|
||||
|
||||
if (info) {
|
||||
if (info.imports) {
|
||||
const status = yield* exports.packageImportsExports(
|
||||
specifier,
|
||||
info.imports,
|
||||
packageURL,
|
||||
true,
|
||||
opts
|
||||
)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
|
||||
if (specifier.startsWith('#')) {
|
||||
throw errors.PACKAGE_IMPORT_NOT_DEFINED(
|
||||
`Package import specifier '${specifier}' is not defined by "imports" in '${packageURL}'`
|
||||
)
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (imports) {
|
||||
const status = yield* exports.packageImportsExports(specifier, imports, parentURL, true, opts)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.packageImportsExports = function* (
|
||||
matchKey,
|
||||
matchObject,
|
||||
packageURL,
|
||||
isImports,
|
||||
opts = {}
|
||||
) {
|
||||
if (matchKey in matchObject && !matchKey.includes('*')) {
|
||||
const target = matchObject[matchKey]
|
||||
|
||||
return yield* exports.packageTarget(packageURL, target, null, isImports, opts)
|
||||
}
|
||||
|
||||
const expansionKeys = Object.keys(matchObject)
|
||||
.filter((key) => key.includes('*'))
|
||||
.sort(exports.patternKeyCompare)
|
||||
|
||||
for (const expansionKey of expansionKeys) {
|
||||
const patternIndex = expansionKey.indexOf('*')
|
||||
const patternBase = expansionKey.substring(0, patternIndex)
|
||||
|
||||
if (matchKey.startsWith(patternBase) && matchKey !== patternBase) {
|
||||
const patternTrailer = expansionKey.substring(patternIndex + 1)
|
||||
|
||||
if (
|
||||
patternTrailer === '' ||
|
||||
(matchKey.endsWith(patternTrailer) && matchKey.length >= expansionKey.length)
|
||||
) {
|
||||
const target = matchObject[expansionKey]
|
||||
|
||||
const patternMatch = matchKey.substring(
|
||||
patternBase.length,
|
||||
matchKey.length - patternTrailer.length
|
||||
)
|
||||
|
||||
return yield* exports.packageTarget(packageURL, target, patternMatch, isImports, opts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.validateEngines = function validateEngines(packageURL, packageEngines, opts = {}) {
|
||||
const { engines = {} } = opts
|
||||
|
||||
for (const [engine, range] of Object.entries(packageEngines)) {
|
||||
if (engine in engines) {
|
||||
const version = engines[engine]
|
||||
|
||||
if (!satisfies(version, range)) {
|
||||
throw errors.UNSUPPORTED_ENGINE(
|
||||
`Package not compatible with engine '${engine}' ${version}, requires range '${range}' defined by "engines" in '${packageURL}'`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.patternKeyCompare = function patternKeyCompare(keyA, keyB) {
|
||||
const patternIndexA = keyA.indexOf('*')
|
||||
const patternIndexB = keyB.indexOf('*')
|
||||
const baseLengthA = patternIndexA === -1 ? keyA.length : patternIndexA + 1
|
||||
const baseLengthB = patternIndexB === -1 ? keyB.length : patternIndexB + 1
|
||||
if (baseLengthA > baseLengthB) return -1
|
||||
if (baseLengthB > baseLengthA) return 1
|
||||
if (patternIndexA === -1) return 1
|
||||
if (patternIndexB === -1) return -1
|
||||
if (keyA.length > keyB.length) return -1
|
||||
if (keyB.length > keyA.length) return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
exports.packageTarget = function* (packageURL, target, patternMatch, isImports, opts = {}) {
|
||||
const { conditions = [], matchedConditions = [], matchedTargets = [] } = opts
|
||||
|
||||
opts = { ...opts, matchedConditions, matchedTargets }
|
||||
|
||||
if (typeof target === 'string') {
|
||||
if (!target.startsWith('./') && !isImports) {
|
||||
throw errors.INVALID_PACKAGE_TARGET(
|
||||
`Invalid target '${target}' defined by "exports" in '${packageURL}'`
|
||||
)
|
||||
}
|
||||
|
||||
if (patternMatch !== null) {
|
||||
target = target.replaceAll('*', patternMatch)
|
||||
}
|
||||
|
||||
if (matchedTargets.includes(target)) return CYCLIC
|
||||
|
||||
matchedTargets.push(target)
|
||||
|
||||
let status = yield* exports.url(target, packageURL, opts)
|
||||
|
||||
if (status) {
|
||||
matchedTargets.pop()
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
if (
|
||||
target === '.' ||
|
||||
target === '..' ||
|
||||
target[0] === '/' ||
|
||||
target.startsWith('./') ||
|
||||
target.startsWith('../')
|
||||
) {
|
||||
const resolved = yield { resolution: new URL(target, packageURL) }
|
||||
|
||||
matchedTargets.pop()
|
||||
|
||||
return resolved ? RESOLVED : YIELDED
|
||||
}
|
||||
|
||||
status = yield* exports.package(target, packageURL, opts)
|
||||
|
||||
matchedTargets.pop()
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
if (Array.isArray(target)) {
|
||||
let status = UNRESOLVED
|
||||
|
||||
for (const targetValue of target) {
|
||||
status |= yield* exports.packageTarget(packageURL, targetValue, patternMatch, isImports, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
if (typeof target === 'object' && target !== null) {
|
||||
let status = UNRESOLVED
|
||||
|
||||
for (const [condition, targetValue, subset] of exports.conditionMatches(
|
||||
target,
|
||||
conditions,
|
||||
opts
|
||||
)) {
|
||||
matchedConditions.push(condition)
|
||||
|
||||
status |= yield* exports.packageTarget(packageURL, targetValue, patternMatch, isImports, {
|
||||
...opts,
|
||||
conditions: subset
|
||||
})
|
||||
|
||||
matchedConditions.pop()
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.builtinTarget = function* (packageSpecifier, packageVersion, target, opts = {}) {
|
||||
const { builtinProtocol = 'builtin:', conditions = [], matchedConditions = [] } = opts
|
||||
|
||||
opts = { ...opts, matchedConditions }
|
||||
|
||||
if (typeof target === 'string') {
|
||||
const targetParts = target.split('@')
|
||||
|
||||
let targetName
|
||||
let targetVersion
|
||||
|
||||
if (target[0] !== '@') {
|
||||
targetName = targetParts[0]
|
||||
targetVersion = targetParts[1] || null
|
||||
} else {
|
||||
targetName = targetParts.slice(0, 2).join('@')
|
||||
targetVersion = targetParts[2] || null
|
||||
}
|
||||
|
||||
if (packageSpecifier === targetName) {
|
||||
if (packageVersion === null && targetVersion === null) {
|
||||
const resolved = yield {
|
||||
resolution: new URL(builtinProtocol + packageSpecifier)
|
||||
}
|
||||
|
||||
return resolved ? RESOLVED : YIELDED
|
||||
}
|
||||
|
||||
let version = null
|
||||
|
||||
if (packageVersion === null) {
|
||||
version = targetVersion
|
||||
} else if (targetVersion === null || packageVersion === targetVersion) {
|
||||
version = packageVersion
|
||||
}
|
||||
|
||||
if (version !== null) {
|
||||
const resolved = yield {
|
||||
resolution: new URL(builtinProtocol + packageSpecifier + '@' + version)
|
||||
}
|
||||
|
||||
return resolved ? RESOLVED : YIELDED
|
||||
}
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
if (Array.isArray(target)) {
|
||||
let status = UNRESOLVED
|
||||
|
||||
for (const targetValue of target) {
|
||||
status |= yield* exports.builtinTarget(packageSpecifier, packageVersion, targetValue, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
if (typeof target === 'object' && target !== null) {
|
||||
let status = UNRESOLVED
|
||||
|
||||
for (const [condition, targetValue, subset] of exports.conditionMatches(
|
||||
target,
|
||||
conditions,
|
||||
opts
|
||||
)) {
|
||||
matchedConditions.push(condition)
|
||||
|
||||
status |= yield* exports.builtinTarget(packageSpecifier, packageVersion, targetValue, {
|
||||
...opts,
|
||||
conditions: subset
|
||||
})
|
||||
|
||||
matchedConditions.pop()
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.conditionMatches = function* conditionMatches(target, conditions, opts = {}) {
|
||||
if (conditions.every((condition) => typeof condition === 'string')) {
|
||||
const keys = Object.keys(target)
|
||||
|
||||
for (const condition of keys) {
|
||||
if (condition === 'default' || conditions.includes(condition)) {
|
||||
yield [condition, target[condition], conditions]
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
let yielded = false
|
||||
|
||||
for (const subset of conditions) {
|
||||
if (yield* conditionMatches(target, subset, opts)) {
|
||||
yielded = true
|
||||
}
|
||||
}
|
||||
|
||||
return yielded
|
||||
}
|
||||
|
||||
exports.lookupPackageRoot = function* (packageName, parentURL) {
|
||||
parentURL = new URL(parentURL.href)
|
||||
|
||||
do {
|
||||
const packageURL = new URL('node_modules/' + packageName + '/', parentURL)
|
||||
|
||||
const info = yield new URL('package.json', packageURL)
|
||||
|
||||
if (info) return info
|
||||
|
||||
parentURL.pathname = parentURL.pathname.substring(0, parentURL.pathname.lastIndexOf('/'))
|
||||
|
||||
if (
|
||||
parentURL.pathname.length === 3 &&
|
||||
exports.isWindowsDriveLetter(parentURL.pathname.substring(1))
|
||||
) {
|
||||
break
|
||||
}
|
||||
} while (parentURL.pathname !== '' && parentURL.pathname !== '/')
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
exports.lookupPackageScope = function* lookupPackageScope(scopeURL, opts = {}) {
|
||||
const { resolutions = null } = opts
|
||||
|
||||
if (resolutions) {
|
||||
for (const { resolution } of exports.preresolved('#package', resolutions, scopeURL, opts)) {
|
||||
if (resolution) return yield resolution
|
||||
}
|
||||
}
|
||||
|
||||
scopeURL = new URL(scopeURL.href)
|
||||
|
||||
do {
|
||||
if (scopeURL.pathname.endsWith('/node_modules')) break
|
||||
|
||||
const info = yield new URL('package.json', scopeURL)
|
||||
|
||||
if (info) return info
|
||||
|
||||
scopeURL.pathname = scopeURL.pathname.substring(0, scopeURL.pathname.lastIndexOf('/'))
|
||||
|
||||
if (
|
||||
scopeURL.pathname.length === 3 &&
|
||||
exports.isWindowsDriveLetter(scopeURL.pathname.substring(1))
|
||||
) {
|
||||
break
|
||||
}
|
||||
} while (scopeURL.pathname !== '' && scopeURL.pathname !== '/')
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
exports.file = function* (filename, parentURL, isIndex, opts = {}) {
|
||||
if (
|
||||
filename === '.' ||
|
||||
filename === '..' ||
|
||||
filename[filename.length - 1] === '/' ||
|
||||
filename[filename.length - 1] === '\\'
|
||||
) {
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
if (parentURL.protocol === 'file:' && /%2f|%5c/i.test(filename)) {
|
||||
throw errors.INVALID_MODULE_SPECIFIER(`Module specifier '${filename}' is invalid`)
|
||||
}
|
||||
|
||||
const { extensions = [] } = opts
|
||||
|
||||
let status = UNRESOLVED
|
||||
|
||||
if (!isIndex) {
|
||||
if (yield { resolution: new URL(filename, parentURL) }) {
|
||||
return RESOLVED
|
||||
}
|
||||
|
||||
status = YIELDED
|
||||
}
|
||||
|
||||
for (const ext of extensions) {
|
||||
if (filename.endsWith(ext)) continue
|
||||
|
||||
if (yield { resolution: new URL(filename + ext, parentURL) }) {
|
||||
return RESOLVED
|
||||
}
|
||||
|
||||
status = YIELDED
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
exports.directory = function* (dirname, parentURL, opts = {}) {
|
||||
let directoryURL
|
||||
|
||||
if (dirname[dirname.length - 1] === '/' || dirname[dirname.length - 1] === '\\') {
|
||||
directoryURL = new URL(dirname, parentURL)
|
||||
} else {
|
||||
directoryURL = new URL(dirname + '/', parentURL)
|
||||
}
|
||||
|
||||
const info = yield { package: new URL('package.json', directoryURL) }
|
||||
|
||||
if (info) {
|
||||
if (info.exports) {
|
||||
return yield* exports.packageExports(directoryURL, '.', info.exports, opts)
|
||||
}
|
||||
|
||||
if (typeof info.main === 'string' && info.main !== '') {
|
||||
let status = yield* exports.file(info.main, directoryURL, false, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
|
||||
const yielded = (status & YIELDED) !== 0
|
||||
|
||||
status = yield* exports.directory(info.main, directoryURL, opts)
|
||||
|
||||
if (yielded) status |= YIELDED
|
||||
|
||||
return status
|
||||
}
|
||||
}
|
||||
|
||||
return yield* exports.file('index', directoryURL, true, opts)
|
||||
}
|
||||
|
||||
// https://infra.spec.whatwg.org/#ascii-upper-alpha
|
||||
function isASCIIUpperAlpha(c) {
|
||||
return c >= 0x41 && c <= 0x5a
|
||||
}
|
||||
|
||||
// https://infra.spec.whatwg.org/#ascii-lower-alpha
|
||||
function isASCIILowerAlpha(c) {
|
||||
return c >= 0x61 && c <= 0x7a
|
||||
}
|
||||
|
||||
// https://infra.spec.whatwg.org/#ascii-alpha
|
||||
function isASCIIAlpha(c) {
|
||||
return isASCIIUpperAlpha(c) || isASCIILowerAlpha(c)
|
||||
}
|
||||
|
||||
// https://url.spec.whatwg.org/#windows-drive-letter
|
||||
exports.isWindowsDriveLetter = function isWindowsDriveLetter(input) {
|
||||
return (
|
||||
input.length >= 2 &&
|
||||
isASCIIAlpha(input.charCodeAt(0)) &&
|
||||
(input.charCodeAt(1) === 0x3a || input.charCodeAt(1) === 0x7c)
|
||||
)
|
||||
}
|
||||
|
||||
// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
|
||||
exports.startsWithWindowsDriveLetter = function startsWithWindowsDriveLetter(input) {
|
||||
return (
|
||||
input.length >= 2 &&
|
||||
exports.isWindowsDriveLetter(input) &&
|
||||
(input.length === 2 ||
|
||||
input.charCodeAt(2) === 0x2f ||
|
||||
input.charCodeAt(2) === 0x5c ||
|
||||
input.charCodeAt(2) === 0x3f ||
|
||||
input.charCodeAt(2) === 0x23)
|
||||
)
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
declare class ModuleResolveError extends Error {
|
||||
readonly code: string
|
||||
|
||||
static INVALID_MODULE_SPECIFIER(msg: string): ModuleResolveError
|
||||
static INVALID_PACKAGE_TARGET(msg: string): ModuleResolveError
|
||||
static PACKAGE_PATH_NOT_EXPORTED(msg: string): ModuleResolveError
|
||||
static PACKAGE_IMPORT_NOT_DEFINED(msg: string): ModuleResolveError
|
||||
static UNSUPPORTED_ENGINE(msg: string): ModuleResolveError
|
||||
}
|
||||
|
||||
export = ModuleResolveError
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
module.exports = class ModuleResolveError extends Error {
|
||||
constructor(msg, code, fn = ModuleResolveError) {
|
||||
super(`${code}: ${msg}`)
|
||||
this.code = code
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, fn)
|
||||
}
|
||||
}
|
||||
|
||||
get name() {
|
||||
return 'ModuleResolveError'
|
||||
}
|
||||
|
||||
static INVALID_MODULE_SPECIFIER(msg) {
|
||||
return new ModuleResolveError(
|
||||
msg,
|
||||
'INVALID_MODULE_SPECIFIER',
|
||||
ModuleResolveError.INVALID_MODULE_SPECIFIER
|
||||
)
|
||||
}
|
||||
|
||||
static INVALID_PACKAGE_TARGET(msg) {
|
||||
return new ModuleResolveError(
|
||||
msg,
|
||||
'INVALID_PACKAGE_TARGET',
|
||||
ModuleResolveError.INVALID_PACKAGE_TARGET
|
||||
)
|
||||
}
|
||||
|
||||
static PACKAGE_PATH_NOT_EXPORTED(msg) {
|
||||
return new ModuleResolveError(
|
||||
msg,
|
||||
'PACKAGE_PATH_NOT_EXPORTED',
|
||||
ModuleResolveError.PACKAGE_PATH_NOT_EXPORTED
|
||||
)
|
||||
}
|
||||
|
||||
static PACKAGE_IMPORT_NOT_DEFINED(msg) {
|
||||
return new ModuleResolveError(
|
||||
msg,
|
||||
'PACKAGE_IMPORT_NOT_DEFINED',
|
||||
ModuleResolveError.PACKAGE_IMPORT_NOT_DEFINED
|
||||
)
|
||||
}
|
||||
|
||||
static UNSUPPORTED_ENGINE(msg) {
|
||||
return new ModuleResolveError(msg, 'UNSUPPORTED_ENGINE', ModuleResolveError.UNSUPPORTED_ENGINE)
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "bare-module-resolve",
|
||||
"version": "1.12.2",
|
||||
"description": "Low-level module resolution algorithm for Bare",
|
||||
"exports": {
|
||||
"./package": "./package.json",
|
||||
".": {
|
||||
"types": "./index.d.ts",
|
||||
"default": "./index.js"
|
||||
},
|
||||
"./errors": {
|
||||
"types": "./lib/errors.d.ts",
|
||||
"default": "./lib/errors.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"lib"
|
||||
],
|
||||
"scripts": {
|
||||
"format": "prettier --write . && lunte --fix",
|
||||
"lint": "prettier --check . && lunte",
|
||||
"test": "brittle-bare --coverage test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/holepunchto/bare-module-resolve.git"
|
||||
},
|
||||
"author": "Holepunch",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/holepunchto/bare-module-resolve/issues"
|
||||
},
|
||||
"homepage": "https://github.com/holepunchto/bare-module-resolve#readme",
|
||||
"dependencies": {
|
||||
"bare-semver": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bare-url": "^2.1.3",
|
||||
"brittle": "^3.2.1",
|
||||
"lunte": "^1.7.0",
|
||||
"prettier": "^3.3.3",
|
||||
"prettier-config-holepunch": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bare-url": "*"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bare-url": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user