@flex-development/mlly 1.0.0-beta.4 → 1.0.0-beta.5
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/CHANGELOG.md +15 -0
- package/README.md +162 -79
- package/dist/index.d.mts +1885 -5
- package/dist/index.mjs +1 -5
- package/dist/internal/chain-or-call.d.mts +51 -0
- package/dist/internal/chain-or-call.mjs +1 -34
- package/dist/internal/chars.d.mts +16 -0
- package/dist/internal/chars.mjs +1 -17
- package/dist/internal/check-invalid-segments.d.mts +18 -0
- package/dist/internal/check-invalid-segments.mjs +1 -49
- package/dist/internal/constant.d.mts +21 -0
- package/dist/internal/constant.mjs +1 -23
- package/dist/internal/fs.browser.d.mts +12 -0
- package/dist/internal/fs.browser.mjs +1 -42
- package/dist/internal/fs.node.d.mts +12 -0
- package/dist/internal/fs.node.mjs +1 -16
- package/dist/internal/identity.d.mts +21 -0
- package/dist/internal/identity.mjs +1 -23
- package/dist/internal/invalid-package-target.d.mts +31 -0
- package/dist/internal/invalid-package-target.mjs +1 -36
- package/dist/internal/invalid-subpath.d.mts +32 -0
- package/dist/internal/invalid-subpath.mjs +1 -37
- package/dist/internal/is-promise.d.mts +21 -0
- package/dist/internal/is-promise.mjs +1 -26
- package/dist/internal/process.browser.d.mts +9 -0
- package/dist/internal/process.browser.mjs +1 -8
- package/dist/lib/can-parse-url.mjs +1 -31
- package/dist/lib/cwd.mjs +1 -17
- package/dist/lib/default-conditions.mjs +1 -14
- package/dist/lib/default-extensions.mjs +1 -29
- package/dist/lib/default-main-fields.mjs +1 -13
- package/dist/lib/extension-format-map.mjs +1 -31
- package/dist/lib/formats.mjs +1 -22
- package/dist/lib/get-source.mjs +1 -153
- package/dist/lib/index.mjs +1 -41
- package/dist/lib/is-absolute-specifier.mjs +1 -34
- package/dist/lib/is-array-index.mjs +1 -28
- package/dist/lib/is-bare-specifier.mjs +1 -33
- package/dist/lib/is-directory.mjs +1 -54
- package/dist/lib/is-file.mjs +1 -54
- package/dist/lib/is-imports-subpath.mjs +1 -28
- package/dist/lib/is-module-id.mjs +1 -24
- package/dist/lib/is-relative-specifier.mjs +1 -36
- package/dist/lib/lookup-package-scope.mjs +1 -102
- package/dist/lib/pattern-key-compare.mjs +1 -71
- package/dist/lib/pattern-match.mjs +1 -85
- package/dist/lib/read-package-json.mjs +1 -114
- package/dist/lib/resolve-alias.mjs +1 -102
- package/dist/lib/resolve-module.mjs +1 -186
- package/dist/lib/resolver.mjs +1 -1015
- package/dist/lib/root.mjs +1 -12
- package/dist/lib/to-relative-specifier.mjs +1 -63
- package/dist/lib/to-url.mjs +1 -34
- package/package.json +28 -10
- package/dist/interfaces/aliases.d.mts +0 -21
- package/dist/interfaces/buffer-encoding-map.d.mts +0 -29
- package/dist/interfaces/condition-map.d.mts +0 -24
- package/dist/interfaces/file-system.d.mts +0 -29
- package/dist/interfaces/get-source-context.d.mts +0 -44
- package/dist/interfaces/get-source-options.d.mts +0 -56
- package/dist/interfaces/index.d.mts +0 -22
- package/dist/interfaces/is-directory.d.mts +0 -17
- package/dist/interfaces/is-file.d.mts +0 -17
- package/dist/interfaces/main-field-map.d.mts +0 -22
- package/dist/interfaces/module-format-map.d.mts +0 -26
- package/dist/interfaces/pattern-key-comparison-map.d.mts +0 -31
- package/dist/interfaces/protocol-map.d.mts +0 -44
- package/dist/interfaces/read-file.d.mts +0 -42
- package/dist/interfaces/realpath.d.mts +0 -29
- package/dist/interfaces/resolve-alias-options.d.mts +0 -43
- package/dist/interfaces/resolve-module-options.d.mts +0 -79
- package/dist/interfaces/stat.d.mts +0 -27
- package/dist/interfaces/stats.d.mts +0 -23
- package/dist/lib/can-parse-url.d.mts +0 -23
- package/dist/lib/cwd.d.mts +0 -14
- package/dist/lib/default-conditions.d.mts +0 -15
- package/dist/lib/default-extensions.d.mts +0 -14
- package/dist/lib/default-main-fields.d.mts +0 -14
- package/dist/lib/extension-format-map.d.mts +0 -16
- package/dist/lib/formats.d.mts +0 -21
- package/dist/lib/get-source.d.mts +0 -46
- package/dist/lib/index.d.mts +0 -41
- package/dist/lib/is-absolute-specifier.d.mts +0 -23
- package/dist/lib/is-array-index.d.mts +0 -19
- package/dist/lib/is-bare-specifier.d.mts +0 -23
- package/dist/lib/is-directory.d.mts +0 -27
- package/dist/lib/is-file.d.mts +0 -27
- package/dist/lib/is-imports-subpath.d.mts +0 -26
- package/dist/lib/is-module-id.d.mts +0 -23
- package/dist/lib/is-relative-specifier.d.mts +0 -23
- package/dist/lib/lookup-package-scope.d.mts +0 -49
- package/dist/lib/pattern-key-compare.d.mts +0 -28
- package/dist/lib/pattern-match.d.mts +0 -22
- package/dist/lib/read-package-json.d.mts +0 -63
- package/dist/lib/resolve-alias.d.mts +0 -21
- package/dist/lib/resolve-module.d.mts +0 -43
- package/dist/lib/resolver.d.mts +0 -346
- package/dist/lib/root.d.mts +0 -11
- package/dist/lib/to-relative-specifier.d.mts +0 -27
- package/dist/lib/to-url.d.mts +0 -26
- package/dist/types/awaitable.d.mts +0 -12
- package/dist/types/buffer-encoding.d.mts +0 -13
- package/dist/types/change-ext-fn.d.mts +0 -29
- package/dist/types/condition.d.mts +0 -13
- package/dist/types/dot.d.mts +0 -9
- package/dist/types/empty-array.d.mts +0 -9
- package/dist/types/empty-object.d.mts +0 -19
- package/dist/types/empty-string.d.mts +0 -9
- package/dist/types/ext.d.mts +0 -12
- package/dist/types/file-content.d.mts +0 -11
- package/dist/types/get-source-handler.d.mts +0 -23
- package/dist/types/get-source-handlers.d.mts +0 -15
- package/dist/types/index.d.mts +0 -24
- package/dist/types/list.d.mts +0 -12
- package/dist/types/main-field.d.mts +0 -13
- package/dist/types/module-format.d.mts +0 -13
- package/dist/types/module-id.d.mts +0 -12
- package/dist/types/numeric.d.mts +0 -9
- package/dist/types/pattern-key-comparison.d.mts +0 -14
- package/dist/types/pattern-match.d.mts +0 -10
- package/dist/types/protocol.d.mts +0 -13
|
@@ -1,102 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @file lookupPackageScope
|
|
3
|
-
* @module mlly/lib/lookupPackageScope
|
|
4
|
-
*/
|
|
5
|
-
import isPromise from '#internal/is-promise';
|
|
6
|
-
import canParseUrl from '#lib/can-parse-url';
|
|
7
|
-
import isFile from '#lib/is-file';
|
|
8
|
-
import isModuleId from '#lib/is-module-id';
|
|
9
|
-
import root from '#lib/root';
|
|
10
|
-
import toUrl from '#lib/to-url';
|
|
11
|
-
import pathe from '@flex-development/pathe';
|
|
12
|
-
export default lookupPackageScope;
|
|
13
|
-
/**
|
|
14
|
-
* Get the package scope URL for a module `url`.
|
|
15
|
-
*
|
|
16
|
-
* Implements the `LOOKUP_PACKAGE_SCOPE` algorithm.
|
|
17
|
-
*
|
|
18
|
-
* > 👉 **Note**: Returns a promise if `fs.stat` is async.
|
|
19
|
-
*
|
|
20
|
-
* @see {@linkcode Awaitable}
|
|
21
|
-
* @see {@linkcode FileSystem}
|
|
22
|
-
* @see {@linkcode ModuleId}
|
|
23
|
-
*
|
|
24
|
-
* @this {void}
|
|
25
|
-
*
|
|
26
|
-
* @param {ModuleId | null | undefined} url
|
|
27
|
-
* The URL of the module to scope
|
|
28
|
-
* @param {ModuleId | null | undefined} [end]
|
|
29
|
-
* The URL of the directory to end search at, defaults to {@linkcode root}
|
|
30
|
-
* @param {FileSystem | null | undefined} [fs]
|
|
31
|
-
* The file system API
|
|
32
|
-
* @return {Awaitable<URL | null>}
|
|
33
|
-
* The URL of nearest directory containing a `package.json` file
|
|
34
|
-
*/
|
|
35
|
-
function lookupPackageScope(url, end, fs) {
|
|
36
|
-
if (isModuleId(url) && canParseUrl(url)) {
|
|
37
|
-
/**
|
|
38
|
-
* The promise context.
|
|
39
|
-
*
|
|
40
|
-
* > 👉 **Note**: Only used if {@linkcode isFile} returns a promise.
|
|
41
|
-
*
|
|
42
|
-
* @const {{ scope: URL | null }} context
|
|
43
|
-
*/
|
|
44
|
-
const context = { scope: null };
|
|
45
|
-
/**
|
|
46
|
-
* The promises to resolve.
|
|
47
|
-
*
|
|
48
|
-
* > 👉 **Note**: Only used if {@linkcode isFile} returns a promise.
|
|
49
|
-
*
|
|
50
|
-
* @const {Awaitable<undefined>[]} promises
|
|
51
|
-
*/
|
|
52
|
-
const promises = [];
|
|
53
|
-
/**
|
|
54
|
-
* The current package scope URL.
|
|
55
|
-
*
|
|
56
|
-
* @var {URL} directory
|
|
57
|
-
*/
|
|
58
|
-
let dir = new URL(url);
|
|
59
|
-
/**
|
|
60
|
-
* Whether the current file exists.
|
|
61
|
-
*
|
|
62
|
-
* @var {Awaitable<boolean>} exists
|
|
63
|
-
*/
|
|
64
|
-
let exists;
|
|
65
|
-
while (String(dir) !== String(end ?? root)) {
|
|
66
|
-
// reset directory to parent directory url.
|
|
67
|
-
dir = toUrl(pathe.dot.repeat(/[/\\]$/.test(dir.pathname) ? 2 : 1), dir);
|
|
68
|
-
// end search early at node_modules.
|
|
69
|
-
if (/node_modules[/\\]$/.test(dir.pathname))
|
|
70
|
-
break;
|
|
71
|
-
/**
|
|
72
|
-
* The URL of the package manifest.
|
|
73
|
-
*
|
|
74
|
-
* @const {URL} packageUrl
|
|
75
|
-
*/
|
|
76
|
-
const packageUrl = new URL('package.json', dir);
|
|
77
|
-
/**
|
|
78
|
-
* The package scope URL.
|
|
79
|
-
*
|
|
80
|
-
* @const {URL} scopeUrl
|
|
81
|
-
*/
|
|
82
|
-
const scopeUrl = new URL(pathe.dot, packageUrl);
|
|
83
|
-
// check if package manifest exists.
|
|
84
|
-
exists = isFile(packageUrl, fs);
|
|
85
|
-
// collect promises, or return scope url if manifest exists.
|
|
86
|
-
if (isPromise(exists)) {
|
|
87
|
-
promises.push(exists.then(isFile => {
|
|
88
|
-
if (!isFile || context.scope)
|
|
89
|
-
return;
|
|
90
|
-
return context.scope = scopeUrl, void 0;
|
|
91
|
-
}));
|
|
92
|
-
}
|
|
93
|
-
else if (exists) {
|
|
94
|
-
return scopeUrl;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// resolve scope url.
|
|
98
|
-
if (promises.length)
|
|
99
|
-
return Promise.all(promises).then(() => context.scope);
|
|
100
|
-
}
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
1
|
+
var f=Object.defineProperty;var m=(o,r)=>f(o,"name",{value:r,configurable:!0});import u from"#internal/is-promise";import d from"#lib/can-parse-url";import h from"#lib/is-file";import U from"#lib/is-module-id";import g from"#lib/root";import k from"#lib/to-url";import l from"@flex-development/pathe";var v=w;function w(o,r,a){if(U(o)&&d(o)){const i={scope:null},n=[];let e=new URL(o),t;for(;String(e)!==String(r??g)&&(e=k(l.dot.repeat(/[/\\]$/.test(e.pathname)?2:1),e),!/node_modules[/\\]$/.test(e.pathname));){const s=new URL("package.json",e),p=new URL(l.dot,s);if(t=h(s,a),u(t))n.push(t.then(c=>{if(!(!c||i.scope))return i.scope=p,void 0}));else if(t)return p}if(n.length)return Promise.all(n).then(()=>i.scope)}return null}m(w,"lookupPackageScope");export{v as default};
|
|
@@ -1,71 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @file patternKeyCompare
|
|
3
|
-
* @module mlly/lib/patternKeyCompare
|
|
4
|
-
*/
|
|
5
|
-
import chars from '#internal/chars';
|
|
6
|
-
import { ok } from 'devlop';
|
|
7
|
-
/**
|
|
8
|
-
* Compare two pattern keys and return a value indicating their order.
|
|
9
|
-
*
|
|
10
|
-
* Implements the `PATTERN_KEY_COMPARE` algorithm.
|
|
11
|
-
*
|
|
12
|
-
* @see {@linkcode PatternKeyComparison}
|
|
13
|
-
*
|
|
14
|
-
* @this {void}
|
|
15
|
-
*
|
|
16
|
-
* @param {string} a
|
|
17
|
-
* The first key
|
|
18
|
-
* @param {string} b
|
|
19
|
-
* The key to compare against `a`
|
|
20
|
-
* @return {PatternKeyComparison}
|
|
21
|
-
* The pattern key comparsion result
|
|
22
|
-
*/
|
|
23
|
-
function patternKeyCompare(a, b) {
|
|
24
|
-
/**
|
|
25
|
-
* Index of pattern character in {@linkcode a}.
|
|
26
|
-
*
|
|
27
|
-
* @var {number} aPatternIndex
|
|
28
|
-
*/
|
|
29
|
-
let aPatternIndex = a.indexOf(chars.asterisk);
|
|
30
|
-
/**
|
|
31
|
-
* Index of pattern character in {@linkcode b}.
|
|
32
|
-
*
|
|
33
|
-
* @var {number} bPatternIndex
|
|
34
|
-
*/
|
|
35
|
-
let bPatternIndex = b.indexOf(chars.asterisk);
|
|
36
|
-
ok(aPatternIndex === a.lastIndexOf(chars.asterisk), 'expected a single "*"');
|
|
37
|
-
ok(bPatternIndex === b.lastIndexOf(chars.asterisk), 'expected a single "*"');
|
|
38
|
-
/**
|
|
39
|
-
* Base length of {@linkcode a}.
|
|
40
|
-
*
|
|
41
|
-
* @const {number} baseLenA
|
|
42
|
-
*/
|
|
43
|
-
const baseLenA = aPatternIndex === patternKeyCompare.LESS_THAN
|
|
44
|
-
? a.length
|
|
45
|
-
: aPatternIndex + 1;
|
|
46
|
-
/**
|
|
47
|
-
* Base length of {@linkcode b}.
|
|
48
|
-
*
|
|
49
|
-
* @const {number} baseLenB
|
|
50
|
-
*/
|
|
51
|
-
const baseLenB = bPatternIndex === patternKeyCompare.LESS_THAN
|
|
52
|
-
? b.length
|
|
53
|
-
: bPatternIndex + 1;
|
|
54
|
-
return baseLenA > baseLenB
|
|
55
|
-
? patternKeyCompare.LESS_THAN
|
|
56
|
-
: baseLenB > baseLenA
|
|
57
|
-
? patternKeyCompare.GREATER_THAN
|
|
58
|
-
: aPatternIndex === -1
|
|
59
|
-
? patternKeyCompare.GREATER_THAN
|
|
60
|
-
: bPatternIndex === -1
|
|
61
|
-
? patternKeyCompare.LESS_THAN
|
|
62
|
-
: a.length > b.length
|
|
63
|
-
? patternKeyCompare.LESS_THAN
|
|
64
|
-
: b.length > a.length
|
|
65
|
-
? patternKeyCompare.GREATER_THAN
|
|
66
|
-
: patternKeyCompare.EQUAL;
|
|
67
|
-
}
|
|
68
|
-
patternKeyCompare.EQUAL = 0;
|
|
69
|
-
patternKeyCompare.GREATER_THAN = 1;
|
|
70
|
-
patternKeyCompare.LESS_THAN = -1;
|
|
71
|
-
export default patternKeyCompare;
|
|
1
|
+
var S=Object.defineProperty;var a=(t,n)=>S(t,"name",{value:n,configurable:!0});import r from"#internal/chars";import{ok as l}from"devlop";function e(t,n){let s=t.indexOf(r.asterisk),A=n.indexOf(r.asterisk);l(s===t.lastIndexOf(r.asterisk),'expected a single "*"'),l(A===n.lastIndexOf(r.asterisk),'expected a single "*"');const E=s===e.LESS_THAN?t.length:s+1,T=A===e.LESS_THAN?n.length:A+1;return E>T?e.LESS_THAN:T>E||s===-1?e.GREATER_THAN:A===-1||t.length>n.length?e.LESS_THAN:n.length>t.length?e.GREATER_THAN:e.EQUAL}a(e,"patternKeyCompare"),e.EQUAL=0,e.GREATER_THAN=1,e.LESS_THAN=-1;var L=e;export{L as default};
|
|
@@ -1,85 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @file patternMatch
|
|
3
|
-
* @module mlly/lib/patternMatch
|
|
4
|
-
*/
|
|
5
|
-
import chars from '#internal/chars';
|
|
6
|
-
import patternKeyCompare from '#lib/pattern-key-compare';
|
|
7
|
-
import { ok } from 'devlop';
|
|
8
|
-
/**
|
|
9
|
-
* Get a subpath pattern match for `matchKey`.
|
|
10
|
-
*
|
|
11
|
-
* @see {@linkcode PatternMatch}
|
|
12
|
-
*
|
|
13
|
-
* @this {void}
|
|
14
|
-
*
|
|
15
|
-
* @param {string} matchKey
|
|
16
|
-
* The key to expand
|
|
17
|
-
* @param {unknown} matchObject
|
|
18
|
-
* The match keys object
|
|
19
|
-
* @return {PatternMatch | null}
|
|
20
|
-
* List, where the first item is the key of a package exports
|
|
21
|
-
* or imports target object, and the last is a subpath pattern match
|
|
22
|
-
*/
|
|
23
|
-
function patternMatch(matchKey, matchObject) {
|
|
24
|
-
if (typeof matchObject === 'object' && matchObject) {
|
|
25
|
-
if (Object.prototype.hasOwnProperty.call(matchObject, matchKey) &&
|
|
26
|
-
!matchKey.includes(chars.asterisk)) {
|
|
27
|
-
return [matchKey, null];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Keys of {@linkcode matchObject} containing only a single "*".
|
|
31
|
-
*
|
|
32
|
-
* Keys are sorted by {@linkcode patternKeyCompare}, which orders in
|
|
33
|
-
* descending order of specificity.
|
|
34
|
-
*
|
|
35
|
-
* @const {string[]} expansionKeys
|
|
36
|
-
*/
|
|
37
|
-
const expansionKeys = Object
|
|
38
|
-
.getOwnPropertyNames(matchObject)
|
|
39
|
-
.filter(expansionKey => {
|
|
40
|
-
/**
|
|
41
|
-
* Index of {@linkcode chars.asterisk} in {@linkcode expansionKey}.
|
|
42
|
-
*
|
|
43
|
-
* @const {number} patternIndex
|
|
44
|
-
*/
|
|
45
|
-
const patternIndex = expansionKey.indexOf(chars.asterisk);
|
|
46
|
-
return (patternIndex >= 0 &&
|
|
47
|
-
patternIndex === expansionKey.lastIndexOf(chars.asterisk));
|
|
48
|
-
})
|
|
49
|
-
.sort(patternKeyCompare);
|
|
50
|
-
for (const expansionKey of expansionKeys) {
|
|
51
|
-
/**
|
|
52
|
-
* Index of {@linkcode chars.asterisk} in {@linkcode expansionKey}.
|
|
53
|
-
*
|
|
54
|
-
* @const {number} patternIndex
|
|
55
|
-
*/
|
|
56
|
-
const patternIndex = expansionKey.indexOf(chars.asterisk);
|
|
57
|
-
/**
|
|
58
|
-
* Segment before {@linkcode chars.asterisk} in {@linkcode expansionKey}.
|
|
59
|
-
*
|
|
60
|
-
* @const {string} patternBase
|
|
61
|
-
*/
|
|
62
|
-
const patternBase = expansionKey.slice(0, patternIndex);
|
|
63
|
-
ok(patternIndex >= 0, 'expected `patternIndex >= 0`');
|
|
64
|
-
if (matchKey !== patternBase && matchKey.startsWith(patternBase)) {
|
|
65
|
-
/**
|
|
66
|
-
* Segment after {@linkcode chars.asterisk} in {@linkcode expansionKey}.
|
|
67
|
-
*
|
|
68
|
-
* @const {string} patternTrailer
|
|
69
|
-
*/
|
|
70
|
-
const patternTrailer = expansionKey.slice(patternIndex + 1);
|
|
71
|
-
if (!patternTrailer.length ||
|
|
72
|
-
(matchKey.endsWith(patternTrailer) &&
|
|
73
|
-
(matchKey.length >= expansionKey.length ||
|
|
74
|
-
matchKey === patternBase + patternTrailer))) {
|
|
75
|
-
return [
|
|
76
|
-
expansionKey,
|
|
77
|
-
matchKey.slice(patternBase.length, matchKey.length - patternTrailer.length)
|
|
78
|
-
];
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return null;
|
|
84
|
-
}
|
|
85
|
-
export default patternMatch;
|
|
1
|
+
var f=Object.defineProperty;var l=(t,n)=>f(t,"name",{value:n,configurable:!0});import i from"#internal/chars";import a from"#lib/pattern-key-compare";import{ok as d}from"devlop";function u(t,n){if(typeof n=="object"&&n){if(Object.prototype.hasOwnProperty.call(n,t)&&!t.includes(i.asterisk))return[t,null];const p=Object.getOwnPropertyNames(n).filter(e=>{const r=e.indexOf(i.asterisk);return r>=0&&r===e.lastIndexOf(i.asterisk)}).sort(a);for(const e of p){const r=e.indexOf(i.asterisk),s=e.slice(0,r);if(d(r>=0,"expected `patternIndex >= 0`"),t!==s&&t.startsWith(s)){const o=e.slice(r+1);if(!o.length||t.endsWith(o)&&(t.length>=e.length||t===s+o))return[e,t.slice(s.length,t.length-o.length)]}}}return null}l(u,"patternMatch");var O=u;export{O as default};
|
|
@@ -1,114 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @file readPackageJson
|
|
3
|
-
* @module mlly/lib/readPackageJson
|
|
4
|
-
*/
|
|
5
|
-
import chainOrCall from '#internal/chain-or-call';
|
|
6
|
-
import dfs from '#internal/fs';
|
|
7
|
-
import canParseUrl from '#lib/can-parse-url';
|
|
8
|
-
import isFile from '#lib/is-file';
|
|
9
|
-
import isModuleId from '#lib/is-module-id';
|
|
10
|
-
import { ERR_INVALID_PACKAGE_CONFIG } from '@flex-development/errnode';
|
|
11
|
-
import pathe from '@flex-development/pathe';
|
|
12
|
-
export default readPackageJson;
|
|
13
|
-
/**
|
|
14
|
-
* Read a `package.json` file.
|
|
15
|
-
*
|
|
16
|
-
* Implements the `READ_PACKAGE_JSON` algorithm.
|
|
17
|
-
*
|
|
18
|
-
* > 👉 **Note**: Returns a promise if `fs.readFile` or `fs.stat` is async.
|
|
19
|
-
*
|
|
20
|
-
* @see {@linkcode Awaitable}
|
|
21
|
-
* @see {@linkcode ErrInvalidPackageConfig}
|
|
22
|
-
* @see {@linkcode FileSystem}
|
|
23
|
-
* @see {@linkcode ModuleId}
|
|
24
|
-
* @see {@linkcode PackageJson}
|
|
25
|
-
*
|
|
26
|
-
* @this {void}
|
|
27
|
-
*
|
|
28
|
-
* @param {ModuleId | null | undefined} id
|
|
29
|
-
* The URL of the package directory, the `package.json` file,
|
|
30
|
-
* or a module in the same directory as a `package.json`
|
|
31
|
-
* @param {string | null | undefined} [specifier]
|
|
32
|
-
* The module specifier that initiated the reading of the `package.json` file
|
|
33
|
-
* > 👉 **Note**: Should be a `file:` URL if `parent` is not a URL
|
|
34
|
-
* @param {ModuleId | null | undefined} [parent]
|
|
35
|
-
* The URL of the parent module
|
|
36
|
-
* @param {FileSystem | null | undefined} [fs]
|
|
37
|
-
* The file system API
|
|
38
|
-
* @return {Awaitable<PackageJson | null>}
|
|
39
|
-
* The parsed file contents
|
|
40
|
-
* @throws {ErrInvalidPackageConfig}
|
|
41
|
-
* If `package.json` file does not parse as valid JSON
|
|
42
|
-
* or package manifest object is not a JSON object
|
|
43
|
-
*/
|
|
44
|
-
function readPackageJson(id, specifier, parent, fs) {
|
|
45
|
-
if (isModuleId(id) && canParseUrl(id)) {
|
|
46
|
-
/**
|
|
47
|
-
* The URL of the `package.json` file.
|
|
48
|
-
*
|
|
49
|
-
* @const {URL} url
|
|
50
|
-
*/
|
|
51
|
-
const url = new URL('package.json', id);
|
|
52
|
-
/**
|
|
53
|
-
* Whether the file exists.
|
|
54
|
-
*
|
|
55
|
-
* @const {Awaitable<boolean>} exists
|
|
56
|
-
*/
|
|
57
|
-
const exists = isFile(url, fs ??= dfs);
|
|
58
|
-
// read package manifest.
|
|
59
|
-
return chainOrCall(exists, isFile => {
|
|
60
|
-
if (!(isFile ?? exists))
|
|
61
|
-
return null;
|
|
62
|
-
/**
|
|
63
|
-
* The stringified contents of the package manifest file.
|
|
64
|
-
*
|
|
65
|
-
* @const {Awaitable<string>} contents
|
|
66
|
-
*/
|
|
67
|
-
const contents = fs.readFile(url, 'utf8');
|
|
68
|
-
// parse file content.
|
|
69
|
-
return chainOrCall(contents, (data = contents) => {
|
|
70
|
-
try {
|
|
71
|
-
/**
|
|
72
|
-
* The parsed file contents.
|
|
73
|
-
*
|
|
74
|
-
* @const {unknown} parsed
|
|
75
|
-
*/
|
|
76
|
-
const parsed = JSON.parse(data);
|
|
77
|
-
if (isPackageJson(parsed))
|
|
78
|
-
return parsed;
|
|
79
|
-
throw new Error('Invalid package manifest object', { cause: parsed });
|
|
80
|
-
}
|
|
81
|
-
catch (e) {
|
|
82
|
-
/**
|
|
83
|
-
* The cause of the error.
|
|
84
|
-
*
|
|
85
|
-
* @const {Error} cause
|
|
86
|
-
*/
|
|
87
|
-
const cause = e;
|
|
88
|
-
/**
|
|
89
|
-
* The invalid package config error.
|
|
90
|
-
*
|
|
91
|
-
* @const {ErrInvalidPackageConfig} error
|
|
92
|
-
*/
|
|
93
|
-
const error = new ERR_INVALID_PACKAGE_CONFIG(url.pathname, parent && specifier
|
|
94
|
-
? `"${specifier}" from ${pathe.fileURLToPath(parent)}`
|
|
95
|
-
: specifier && pathe.fileURLToPath(specifier), cause.message);
|
|
96
|
-
error.cause = cause;
|
|
97
|
-
throw error;
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* @this {void}
|
|
106
|
-
*
|
|
107
|
-
* @param {unknown} value
|
|
108
|
-
* The thing to check
|
|
109
|
-
* @return {value is PackageJson}
|
|
110
|
-
* `true` if `value` looks like package manifest, `false` otherwise
|
|
111
|
-
*/
|
|
112
|
-
function isPackageJson(value) {
|
|
113
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
114
|
-
}
|
|
1
|
+
var d=Object.defineProperty;var n=(r,o)=>d(r,"name",{value:o,configurable:!0});import u from"#internal/chain-or-call";import P from"#internal/fs";import g from"#lib/can-parse-url";import w from"#lib/is-file";import A from"#lib/is-module-id";import{ERR_INVALID_PACKAGE_CONFIG as I}from"@flex-development/errnode";import l from"@flex-development/pathe";var N=R;function R(r,o,a,s){if(A(r)&&g(r)){const e=new URL("package.json",r),c=w(e,s??=P);return u(c,p=>{if(!(p??c))return null;const m=s.readFile(e,"utf8");return u(m,(h=m)=>{try{const t=JSON.parse(h);if(k(t))return t;throw new Error("Invalid package manifest object",{cause:t})}catch(t){const f=t,i=new I(e.pathname,a&&o?`"${o}" from ${l.fileURLToPath(a)}`:o&&l.fileURLToPath(o),f.message);throw i.cause=f,i}})})}return null}n(R,"readPackageJson");function k(r){return typeof r=="object"&&r!==null&&!Array.isArray(r)}n(k,"isPackageJson");export{N as default};
|
|
@@ -1,102 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @file resolveAlias
|
|
3
|
-
* @module mlly/lib/resolveAlias
|
|
4
|
-
*/
|
|
5
|
-
import chars from '#internal/chars';
|
|
6
|
-
import cwd from '#lib/cwd';
|
|
7
|
-
import isRelativeSpecifier from '#lib/is-relative-specifier';
|
|
8
|
-
import patternMatch from '#lib/pattern-match';
|
|
9
|
-
import toRelativeSpecifier from '#lib/to-relative-specifier';
|
|
10
|
-
import pathe from '@flex-development/pathe';
|
|
11
|
-
import { ok } from 'devlop';
|
|
12
|
-
export default resolveAlias;
|
|
13
|
-
/**
|
|
14
|
-
* Resolve an aliased `specifier`.
|
|
15
|
-
*
|
|
16
|
-
* @see {@linkcode ResolveAliasOptions}
|
|
17
|
-
*
|
|
18
|
-
* @this {void}
|
|
19
|
-
*
|
|
20
|
-
* @param {string} specifier
|
|
21
|
-
* The specifier using an alias
|
|
22
|
-
* @param {ResolveAliasOptions | null | undefined} [options]
|
|
23
|
-
* Alias resolution options
|
|
24
|
-
* @return {string | null}
|
|
25
|
-
* The specifier of the aliased module
|
|
26
|
-
*/
|
|
27
|
-
function resolveAlias(specifier, options) {
|
|
28
|
-
/**
|
|
29
|
-
* The expansion key and pattern match.
|
|
30
|
-
*
|
|
31
|
-
* @const {PatternMatch | null} match
|
|
32
|
-
*/
|
|
33
|
-
const match = patternMatch(specifier, options?.aliases);
|
|
34
|
-
/**
|
|
35
|
-
* The resolved alias.
|
|
36
|
-
*
|
|
37
|
-
* @var {string | null} aliased
|
|
38
|
-
*/
|
|
39
|
-
let aliased = null;
|
|
40
|
-
if (match) {
|
|
41
|
-
ok(options, 'expected `options`');
|
|
42
|
-
ok(options.aliases, 'expected `options.aliases`');
|
|
43
|
-
aliased = resolveAliasTarget(options.aliases[match[0]], match[1]);
|
|
44
|
-
if (typeof aliased === 'string') {
|
|
45
|
-
/**
|
|
46
|
-
* The URL of the aliased module.
|
|
47
|
-
*
|
|
48
|
-
* @const {URL} url
|
|
49
|
-
*/
|
|
50
|
-
const url = new URL(aliased, options.cwd ?? cwd());
|
|
51
|
-
if (options.absolute) {
|
|
52
|
-
aliased = url.href;
|
|
53
|
-
}
|
|
54
|
-
else if (options.parent) {
|
|
55
|
-
aliased = toRelativeSpecifier(url, options.parent);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return aliased;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Resolve a path alias target.
|
|
63
|
-
*
|
|
64
|
-
* @internal
|
|
65
|
-
*
|
|
66
|
-
* @this {void}
|
|
67
|
-
*
|
|
68
|
-
* @param {unknown} target
|
|
69
|
-
* The path alias target to resolve
|
|
70
|
-
* @param {string | null | undefined} [patternMatch]
|
|
71
|
-
* The pattern match, replaces `*` in `target`
|
|
72
|
-
* @return {string | null}
|
|
73
|
-
* The matched alias
|
|
74
|
-
*/
|
|
75
|
-
function resolveAliasTarget(target, patternMatch) {
|
|
76
|
-
if (typeof target === 'string') {
|
|
77
|
-
/**
|
|
78
|
-
* The matched alias.
|
|
79
|
-
*
|
|
80
|
-
* @var {string} match
|
|
81
|
-
*/
|
|
82
|
-
let match = typeof patternMatch === 'string'
|
|
83
|
-
? target.replace(chars.asterisk, patternMatch)
|
|
84
|
-
: target;
|
|
85
|
-
if (!isRelativeSpecifier(match))
|
|
86
|
-
match = pathe.dot + pathe.sep + match;
|
|
87
|
-
return match;
|
|
88
|
-
}
|
|
89
|
-
if (Array.isArray(target)) {
|
|
90
|
-
for (const targetValue of target) {
|
|
91
|
-
/**
|
|
92
|
-
* The resolved path alias.
|
|
93
|
-
*
|
|
94
|
-
* @const {string | null} resolved
|
|
95
|
-
*/
|
|
96
|
-
const resolved = resolveAliasTarget(targetValue, patternMatch);
|
|
97
|
-
if (typeof resolved === 'string')
|
|
98
|
-
return resolved;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
1
|
+
var m=Object.defineProperty;var f=(i,e)=>m(i,"name",{value:e,configurable:!0});import c from"#internal/chars";import n from"#lib/cwd";import p from"#lib/is-relative-specifier";import u from"#lib/pattern-match";import d from"#lib/to-relative-specifier";import l from"@flex-development/pathe";import{ok as o}from"devlop";var S=y;function y(i,e){const r=u(i,e?.aliases);let t=null;if(r&&(o(e,"expected `options`"),o(e.aliases,"expected `options.aliases`"),t=a(e.aliases[r[0]],r[1]),typeof t=="string")){const s=new URL(t,e.cwd??n());e.absolute?t=s.href:e.parent&&(t=d(s,e.parent))}return t}f(y,"resolveAlias");function a(i,e){if(typeof i=="string"){let r=typeof e=="string"?i.replace(c.asterisk,e):i;return p(r)||(r=l.dot+l.sep+r),r}if(Array.isArray(i))for(const r of i){const t=a(r,e);if(typeof t=="string")return t}return null}f(a,"resolveAliasTarget");export{S as default};
|
|
@@ -1,186 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* @file resolveModule
|
|
3
|
-
* @module mlly/lib/resolveModule
|
|
4
|
-
*/
|
|
5
|
-
import constant from '#internal/constant';
|
|
6
|
-
import identity from '#internal/identity';
|
|
7
|
-
import isPromise from '#internal/is-promise';
|
|
8
|
-
import defaultExtensions from '#lib/default-extensions';
|
|
9
|
-
import resolveAlias from '#lib/resolve-alias';
|
|
10
|
-
import { moduleResolve } from '#lib/resolver';
|
|
11
|
-
import { codes, isNodeError } from '@flex-development/errnode';
|
|
12
|
-
import pathe from '@flex-development/pathe';
|
|
13
|
-
export default resolveModule;
|
|
14
|
-
/**
|
|
15
|
-
* Resolve a module `specifier`.
|
|
16
|
-
*
|
|
17
|
-
* Implements the `ESM_RESOLVE` algorithm, mostly 😉.
|
|
18
|
-
*
|
|
19
|
-
* Adds support for:
|
|
20
|
-
*
|
|
21
|
-
* - Changing file extensions
|
|
22
|
-
* - Directory index resolution
|
|
23
|
-
* - Extensionless file resolution
|
|
24
|
-
* - Path alias resolution
|
|
25
|
-
* - Scopeless `@types/*` resolution (i.e. `unist` -> `@types/unist`)
|
|
26
|
-
*
|
|
27
|
-
* > 👉 **Note**: Returns a promise if {@linkcode moduleResolve}
|
|
28
|
-
* > returns a promise.
|
|
29
|
-
*
|
|
30
|
-
* @see {@linkcode Awaitable}
|
|
31
|
-
* @see {@linkcode ModuleId}
|
|
32
|
-
* @see {@linkcode NodeError}
|
|
33
|
-
* @see {@linkcode ResolveModuleOptions}
|
|
34
|
-
*
|
|
35
|
-
* @this {void}
|
|
36
|
-
*
|
|
37
|
-
* @param {string} specifier
|
|
38
|
-
* The module specifier to resolve
|
|
39
|
-
* @param {ModuleId} parent
|
|
40
|
-
* The URL of the parent module
|
|
41
|
-
* @param {ResolveModuleOptions | null | undefined} [options]
|
|
42
|
-
* Resolution options
|
|
43
|
-
* @return {Awaitable<URL>}
|
|
44
|
-
* The resolved URL
|
|
45
|
-
* @throws {NodeError}
|
|
46
|
-
*/
|
|
47
|
-
function resolveModule(specifier, parent, options) {
|
|
48
|
-
/**
|
|
49
|
-
* The resolved URL.
|
|
50
|
-
*
|
|
51
|
-
* @var {Awaitable<URL>} resolved
|
|
52
|
-
*/
|
|
53
|
-
let resolved;
|
|
54
|
-
try {
|
|
55
|
-
resolved = moduleResolve(resolveAlias(specifier, { ...options, parent }) ?? specifier, parent, options?.conditions, options?.mainFields, options?.preserveSymlinks, options?.fs);
|
|
56
|
-
}
|
|
57
|
-
catch (e) {
|
|
58
|
-
return retry(e, specifier, parent, options);
|
|
59
|
-
}
|
|
60
|
-
if (isPromise(resolved)) {
|
|
61
|
-
return resolved.then((url) => changeExt(url, specifier, options?.ext), (e) => retry(e, specifier, parent, options));
|
|
62
|
-
}
|
|
63
|
-
return changeExt(resolved, specifier, options?.ext);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @this {void}
|
|
67
|
-
*
|
|
68
|
-
* @param {URL} url
|
|
69
|
-
* The resolved URL
|
|
70
|
-
* @param {string} specifier
|
|
71
|
-
* The module specifier being resolved
|
|
72
|
-
* @param {ChangeExtFn | string | null | undefined} [ext]
|
|
73
|
-
* The replacement file extension or a function that returns a file extension
|
|
74
|
-
* @return {URL}
|
|
75
|
-
* The modified `url`
|
|
76
|
-
*/
|
|
77
|
-
function changeExt(url, specifier, ext) {
|
|
78
|
-
if (url.protocol === 'file:' && ext !== undefined) {
|
|
79
|
-
url.href = typeof ext === 'function'
|
|
80
|
-
? pathe.changeExt(url.href, ext(url, specifier))
|
|
81
|
-
: pathe.changeExt(url.href, ext);
|
|
82
|
-
url = new URL(pathe.toPosix(url.href).replace(/\/index$/, pathe.sep));
|
|
83
|
-
}
|
|
84
|
-
return url;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* @this {void}
|
|
88
|
-
*
|
|
89
|
-
* @param {unknown} e
|
|
90
|
-
* The error to handle
|
|
91
|
-
* @param {string} specifier
|
|
92
|
-
* The module specifier to resolve
|
|
93
|
-
* @param {ModuleId} parent
|
|
94
|
-
* The URL of the parent module
|
|
95
|
-
* @param {ResolveModuleOptions | null | undefined} [options]
|
|
96
|
-
* Resolution options
|
|
97
|
-
* @return {Awaitable<URL>}
|
|
98
|
-
* The resolved URL
|
|
99
|
-
* @throws {NodeError}
|
|
100
|
-
*/
|
|
101
|
-
function retry(e, specifier, parent, options) {
|
|
102
|
-
/**
|
|
103
|
-
* The list of error codes to ignore.
|
|
104
|
-
*
|
|
105
|
-
* @const {Set<Code>} ignore
|
|
106
|
-
*/
|
|
107
|
-
const ignore = new Set([
|
|
108
|
-
codes.ERR_MODULE_NOT_FOUND,
|
|
109
|
-
codes.ERR_UNSUPPORTED_DIR_IMPORT
|
|
110
|
-
]);
|
|
111
|
-
if (isNodeError(e) && ignore.has(e.code)) {
|
|
112
|
-
/**
|
|
113
|
-
* The module extensions to probe for.
|
|
114
|
-
*
|
|
115
|
-
* @const {string[]} extensions
|
|
116
|
-
*/
|
|
117
|
-
const extensions = [...(options?.extensions ?? defaultExtensions)];
|
|
118
|
-
/**
|
|
119
|
-
* The promises to resolve.
|
|
120
|
-
*
|
|
121
|
-
* > 👉 **Note**: Only used if {@linkcode moduleResolve} returns a promise.
|
|
122
|
-
*
|
|
123
|
-
* @const {Awaitable<URL | undefined>} promises
|
|
124
|
-
*/
|
|
125
|
-
const promises = [];
|
|
126
|
-
/**
|
|
127
|
-
* The resolved URL.
|
|
128
|
-
*
|
|
129
|
-
* @var {Awaitable<URL>} resolved
|
|
130
|
-
*/
|
|
131
|
-
let resolved;
|
|
132
|
-
/**
|
|
133
|
-
* The module specifiers to try resolving.
|
|
134
|
-
*
|
|
135
|
-
* @var {string[]} tries
|
|
136
|
-
*/
|
|
137
|
-
let tries = [];
|
|
138
|
-
// add @types resolution attempts if package resolution failed.
|
|
139
|
-
if (e.code === codes.ERR_MODULE_NOT_FOUND &&
|
|
140
|
-
!e.url) {
|
|
141
|
-
tries = [
|
|
142
|
-
specifier.startsWith('@types/') ? specifier : '@types/' + specifier
|
|
143
|
-
].flatMap(specifier => [
|
|
144
|
-
specifier,
|
|
145
|
-
specifier + pathe.sep + 'index.d.ts',
|
|
146
|
-
specifier + pathe.sep + 'index.d.mts',
|
|
147
|
-
specifier + pathe.sep + 'index.d.cts'
|
|
148
|
-
]);
|
|
149
|
-
}
|
|
150
|
-
// add extensionless file resolution attempts if file resolution failed.
|
|
151
|
-
if (e.code === codes.ERR_MODULE_NOT_FOUND &&
|
|
152
|
-
e.url) {
|
|
153
|
-
tries = extensions.map(ext => specifier + pathe.formatExt(ext));
|
|
154
|
-
}
|
|
155
|
-
// add directory index resolution attempts if directory resolution failed.
|
|
156
|
-
if (e.code === codes.ERR_UNSUPPORTED_DIR_IMPORT) {
|
|
157
|
-
tries = extensions.map(ext => {
|
|
158
|
-
return specifier + pathe.sep + 'index' + pathe.formatExt(ext);
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
// try module resolution attempts.
|
|
162
|
-
for (let attempt of tries) {
|
|
163
|
-
try {
|
|
164
|
-
resolved = moduleResolve(attempt, parent, options?.conditions, options?.mainFields, options?.preserveSymlinks, options?.fs);
|
|
165
|
-
if (!isPromise(resolved)) {
|
|
166
|
-
return changeExt(resolved, specifier, options?.ext);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
catch {
|
|
170
|
-
continue; // swallow error to continue resolution attempts.
|
|
171
|
-
}
|
|
172
|
-
// collect promises to resolve.
|
|
173
|
-
promises.push(resolved.then(identity, constant(undefined)));
|
|
174
|
-
}
|
|
175
|
-
if (promises.length) {
|
|
176
|
-
return Promise.all(promises).then(resolved => {
|
|
177
|
-
for (const url of resolved) {
|
|
178
|
-
if (url)
|
|
179
|
-
return changeExt(url, specifier, options?.ext);
|
|
180
|
-
}
|
|
181
|
-
throw e; // could not resolve specifier.
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
throw e;
|
|
186
|
-
}
|
|
1
|
+
var i=Object.defineProperty;var a=(t,e)=>i(t,"name",{value:e,configurable:!0});import O from"#internal/constant";import U from"#internal/identity";import x from"#internal/is-promise";import D from"#lib/default-extensions";import y from"#lib/resolve-alias";import{moduleResolve as _}from"#lib/resolver";import{codes as f,isNodeError as N}from"@flex-development/errnode";import m from"@flex-development/pathe";var k=P;function P(t,e,r){let n;try{n=_(y(t,{...r,parent:e})??t,e,r?.conditions,r?.mainFields,r?.preserveSymlinks,r?.fs)}catch(d){return c(d,t,e,r)}return x(n)?n.then(d=>h(d,t,r?.ext),d=>c(d,t,e,r)):h(n,t,r?.ext)}a(P,"resolveModule");function h(t,e,r){return t.protocol==="file:"&&r!==void 0&&(t.href=typeof r=="function"?m.changeExt(t.href,r(t,e)):m.changeExt(t.href,r),t=new URL(m.toPosix(t.href).replace(/\/index$/,m.sep))),t}a(h,"changeExt");function c(t,e,r,n){const d=new Set([f.ERR_MODULE_NOT_FOUND,f.ERR_UNSUPPORTED_DIR_IMPORT]);if(N(t)&&d.has(t.code)){const E=[...n?.extensions??D],R=[];let s,l=[];t.code===f.ERR_MODULE_NOT_FOUND&&!t.url&&(l=[e.startsWith("@types/")?e:"@types/"+e].flatMap(o=>[o,o+m.sep+"index.d.ts",o+m.sep+"index.d.mts",o+m.sep+"index.d.cts"])),t.code===f.ERR_MODULE_NOT_FOUND&&t.url&&(l=E.map(o=>e+m.formatExt(o))),t.code===f.ERR_UNSUPPORTED_DIR_IMPORT&&(l=E.map(o=>e+m.sep+"index"+m.formatExt(o)));for(let o of l){try{if(s=_(o,r,n?.conditions,n?.mainFields,n?.preserveSymlinks,n?.fs),!x(s))return h(s,e,n?.ext)}catch{continue}R.push(s.then(U,O(void 0)))}if(R.length)return Promise.all(R).then(o=>{for(const u of o)if(u)return h(u,e,n?.ext);throw t})}throw t}a(c,"retry");export{k as default};
|