load-plugin 5.1.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +4 -40
- package/index.js +3 -172
- package/lib/index.d.ts +78 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +212 -0
- package/package.json +28 -31
- package/readme.md +119 -82
package/index.d.ts
CHANGED
|
@@ -1,40 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* @param {LoadOptions} [options]
|
|
6
|
-
* @returns {Promise<unknown>}
|
|
7
|
-
*/
|
|
8
|
-
export function loadPlugin(
|
|
9
|
-
name: string,
|
|
10
|
-
options?: LoadOptions | undefined
|
|
11
|
-
): Promise<unknown>
|
|
12
|
-
/**
|
|
13
|
-
* Find a plugin.
|
|
14
|
-
*
|
|
15
|
-
* See also:
|
|
16
|
-
* * https://docs.npmjs.com/files/folders#node-modules
|
|
17
|
-
* * https://github.com/sindresorhus/resolve-from
|
|
18
|
-
*
|
|
19
|
-
* Uses the standard node module loading strategy to find `$name` in each given
|
|
20
|
-
* `cwd` (and optionally the global `node_modules` directory).
|
|
21
|
-
*
|
|
22
|
-
* If a prefix is given and `$name` is not a path, `$prefix-$name` is also
|
|
23
|
-
* searched (preferring these over non-prefixed modules).
|
|
24
|
-
*
|
|
25
|
-
* @param {string} name
|
|
26
|
-
* @param {ResolveOptions} [options]
|
|
27
|
-
* @returns {Promise<string>}
|
|
28
|
-
*/
|
|
29
|
-
export function resolvePlugin(
|
|
30
|
-
name: string,
|
|
31
|
-
options?: ResolveOptions | undefined
|
|
32
|
-
): Promise<string>
|
|
33
|
-
export type ResolveOptions = {
|
|
34
|
-
prefix?: string | undefined
|
|
35
|
-
cwd?: string | string[] | undefined
|
|
36
|
-
global?: boolean | undefined
|
|
37
|
-
}
|
|
38
|
-
export type LoadOptions = ResolveOptions & {
|
|
39
|
-
key?: string | false
|
|
40
|
-
}
|
|
1
|
+
export type LoadOptions = import('./lib/index.js').LoadOptions;
|
|
2
|
+
export type ResolveOptions = import('./lib/index.js').ResolveOptions;
|
|
3
|
+
export { loadPlugin, resolvePlugin } from "./lib/index.js";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -1,175 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @typedef
|
|
3
|
-
* @
|
|
4
|
-
* @property {string|Array<string>} [cwd]
|
|
5
|
-
* @property {boolean} [global]
|
|
6
|
-
*
|
|
7
|
-
* @typedef {ResolveOptions & {key?: string|false}} LoadOptions
|
|
2
|
+
* @typedef {import('./lib/index.js').LoadOptions} LoadOptions
|
|
3
|
+
* @typedef {import('./lib/index.js').ResolveOptions} ResolveOptions
|
|
8
4
|
*/
|
|
9
5
|
|
|
10
|
-
|
|
11
|
-
import process from 'process'
|
|
12
|
-
import {pathToFileURL, fileURLToPath} from 'url'
|
|
13
|
-
import path from 'path'
|
|
14
|
-
// @ts-expect-error: untyped
|
|
15
|
-
import NpmConfig from '@npmcli/config'
|
|
16
|
-
import {resolve as esmResolve} from 'import-meta-resolve'
|
|
17
|
-
|
|
18
|
-
const electron = process.versions.electron !== undefined
|
|
19
|
-
const windows = process.platform === 'win32'
|
|
20
|
-
|
|
21
|
-
const argv = process.argv[1] || /* c8 ignore next */ ''
|
|
22
|
-
const nvm = process.env.NVM_BIN
|
|
23
|
-
|
|
24
|
-
/* c8 ignore next */
|
|
25
|
-
const globalsLibrary = windows ? '' : 'lib'
|
|
26
|
-
|
|
27
|
-
const config = new NpmConfig({definitions: {}})
|
|
28
|
-
|
|
29
|
-
config.loadGlobalPrefix()
|
|
30
|
-
|
|
31
|
-
/** @type {string} */
|
|
32
|
-
let npmPrefix = config.globalPrefix
|
|
33
|
-
|
|
34
|
-
// If there is no prefix defined, use the defaults
|
|
35
|
-
// See: <https://github.com/eush77/npm-prefix/blob/master/index.js>
|
|
36
|
-
/* c8 ignore next 5 */
|
|
37
|
-
if (!npmPrefix) {
|
|
38
|
-
npmPrefix = windows
|
|
39
|
-
? path.dirname(process.execPath)
|
|
40
|
-
: path.resolve(process.execPath, '../..')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const globalsDefault = electron || argv.indexOf(npmPrefix) === 0
|
|
44
|
-
let globalDir = path.resolve(npmPrefix, globalsLibrary, 'node_modules')
|
|
45
|
-
|
|
46
|
-
// If we’re in Electron, we’re running in a modified Node that cannot really
|
|
47
|
-
// install global node modules.
|
|
48
|
-
// To find the actual modules, the user has to set `prefix` somewhere in an
|
|
49
|
-
// `.npmrc` (which is picked up by `libnpmconfig`).
|
|
50
|
-
// Most people don’t do that, and some use NVM instead to manage different
|
|
51
|
-
// versions of Node.
|
|
52
|
-
// Luckily NVM leaks some environment variables that we can pick up on to try
|
|
53
|
-
// and detect the actual modules.
|
|
54
|
-
/* c8 ignore next 3 */
|
|
55
|
-
if (electron && nvm && !fs.existsSync(globalDir)) {
|
|
56
|
-
globalDir = path.resolve(nvm, '..', globalsLibrary, 'node_modules')
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Load the plugin found using `resolvePlugin`.
|
|
61
|
-
*
|
|
62
|
-
* @param {string} name The name to import.
|
|
63
|
-
* @param {LoadOptions} [options]
|
|
64
|
-
* @returns {Promise<unknown>}
|
|
65
|
-
*/
|
|
66
|
-
export async function loadPlugin(name, options = {}) {
|
|
67
|
-
const {key = 'default', ...rest} = options
|
|
68
|
-
const fp = await resolvePlugin(name, rest)
|
|
69
|
-
/** @type {Record<string, unknown>} */
|
|
70
|
-
// Bug with coverage on Node@12.
|
|
71
|
-
/* c8 ignore next 3 */
|
|
72
|
-
const mod = await import(pathToFileURL(fp).href)
|
|
73
|
-
return key === false ? mod : mod[key]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Find a plugin.
|
|
78
|
-
*
|
|
79
|
-
* See also:
|
|
80
|
-
* * https://docs.npmjs.com/files/folders#node-modules
|
|
81
|
-
* * https://github.com/sindresorhus/resolve-from
|
|
82
|
-
*
|
|
83
|
-
* Uses the standard node module loading strategy to find `$name` in each given
|
|
84
|
-
* `cwd` (and optionally the global `node_modules` directory).
|
|
85
|
-
*
|
|
86
|
-
* If a prefix is given and `$name` is not a path, `$prefix-$name` is also
|
|
87
|
-
* searched (preferring these over non-prefixed modules).
|
|
88
|
-
*
|
|
89
|
-
* @param {string} name
|
|
90
|
-
* @param {ResolveOptions} [options]
|
|
91
|
-
* @returns {Promise<string>}
|
|
92
|
-
*/
|
|
93
|
-
export async function resolvePlugin(name, options = {}) {
|
|
94
|
-
const prefix = options.prefix
|
|
95
|
-
? options.prefix +
|
|
96
|
-
(options.prefix.charAt(options.prefix.length - 1) === '-' ? '' : '-')
|
|
97
|
-
: undefined
|
|
98
|
-
const cwd = options.cwd
|
|
99
|
-
const globals =
|
|
100
|
-
options.global === undefined || options.global === null
|
|
101
|
-
? globalsDefault
|
|
102
|
-
: options.global
|
|
103
|
-
const sources = Array.isArray(cwd) ? cwd.concat() : [cwd || process.cwd()]
|
|
104
|
-
/** @type {string|undefined} */
|
|
105
|
-
let plugin
|
|
106
|
-
/** @type {Error|undefined} */
|
|
107
|
-
let lastError
|
|
108
|
-
|
|
109
|
-
// Non-path.
|
|
110
|
-
if (name.charAt(0) !== '.') {
|
|
111
|
-
if (globals) {
|
|
112
|
-
sources.push(globalDir)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
let scope = ''
|
|
116
|
-
|
|
117
|
-
// Unprefix module.
|
|
118
|
-
if (prefix) {
|
|
119
|
-
// Scope?
|
|
120
|
-
if (name.charAt(0) === '@') {
|
|
121
|
-
const slash = name.indexOf('/')
|
|
122
|
-
|
|
123
|
-
// Let’s keep the algorithm simple.
|
|
124
|
-
// No need to care if this is a “valid” scope (I think?).
|
|
125
|
-
// But we do check for the slash.
|
|
126
|
-
if (slash !== -1) {
|
|
127
|
-
scope = name.slice(0, slash + 1)
|
|
128
|
-
name = name.slice(slash + 1)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (name.slice(0, prefix.length) !== prefix) {
|
|
133
|
-
plugin = scope + prefix + name
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
name = scope + name
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
let index = -1
|
|
141
|
-
/** @type {string|undefined} */
|
|
142
|
-
let fp
|
|
143
|
-
|
|
144
|
-
while (++index < sources.length) {
|
|
145
|
-
fp = plugin ? await attempt(sources[index], plugin) : undefined
|
|
146
|
-
if (fp) return fp
|
|
147
|
-
|
|
148
|
-
fp = await attempt(sources[index], name)
|
|
149
|
-
if (fp) return fp
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// There’s always an error.
|
|
153
|
-
// Bug with coverage on Node@12.
|
|
154
|
-
/* c8 ignore next 8 */
|
|
155
|
-
throw lastError
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* @param {string} base
|
|
159
|
-
* @param {string} name
|
|
160
|
-
* @returns {Promise<string|undefined>}
|
|
161
|
-
*/
|
|
162
|
-
async function attempt(base, name) {
|
|
163
|
-
try {
|
|
164
|
-
// `import-meta-resolve` resolves from files, whereas `load-plugin` works
|
|
165
|
-
// on folders, which is why we add a `/` at the end.
|
|
166
|
-
return fileURLToPath(
|
|
167
|
-
await esmResolve(name, pathToFileURL(base).href + '/')
|
|
168
|
-
)
|
|
169
|
-
// Bug with coverage on Node@12.
|
|
170
|
-
/* c8 ignore next 1 */
|
|
171
|
-
} catch (error) {
|
|
172
|
-
lastError = /** @type {Error} */ (error)
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
6
|
+
export {loadPlugin, resolvePlugin} from './lib/index.js'
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import `name` from `from` (and optionally the global `node_modules` directory).
|
|
3
|
+
*
|
|
4
|
+
* Uses the Node.js resolution algorithm (through `import-meta-resolve`) to
|
|
5
|
+
* resolve CJS and ESM packages and files.
|
|
6
|
+
*
|
|
7
|
+
* If a `prefix` is given and `name` is not a path,
|
|
8
|
+
* `$prefix-$name` is also searched (preferring these over non-prefixed
|
|
9
|
+
* modules).
|
|
10
|
+
* If `name` starts with a scope (`@scope/name`),
|
|
11
|
+
* the prefix is applied after it: `@scope/$prefix-name`.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} name
|
|
14
|
+
* Specifier.
|
|
15
|
+
* @param {Readonly<LoadOptions> | null | undefined} [options]
|
|
16
|
+
* Configuration (optional).
|
|
17
|
+
* @returns {Promise<unknown>}
|
|
18
|
+
* Promise to a whole module or specific export.
|
|
19
|
+
*/
|
|
20
|
+
export function loadPlugin(name: string, options?: Readonly<LoadOptions> | null | undefined): Promise<unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Resolve `name` from `from`.
|
|
23
|
+
*
|
|
24
|
+
* @param {string} name
|
|
25
|
+
* Specifier.
|
|
26
|
+
* @param {Readonly<ResolveOptions> | null | undefined} [options]
|
|
27
|
+
* Configuration (optional).
|
|
28
|
+
* @returns {Promise<string>}
|
|
29
|
+
* Promise to a file URL.
|
|
30
|
+
*/
|
|
31
|
+
export function resolvePlugin(name: string, options?: Readonly<ResolveOptions> | null | undefined): Promise<string>;
|
|
32
|
+
/**
|
|
33
|
+
* Configuration for `loadPlugin`.
|
|
34
|
+
*/
|
|
35
|
+
export type LoadOptions = LoadOptionsExtraFields & ResolveOptions;
|
|
36
|
+
/**
|
|
37
|
+
* Extra configuration for `loadPlugin`.
|
|
38
|
+
*/
|
|
39
|
+
export type LoadOptionsExtraFields = {
|
|
40
|
+
/**
|
|
41
|
+
* Identifier to take from the exports (default: `'default'`);
|
|
42
|
+
* for example when given `'x'`,
|
|
43
|
+
* the value of `export const x = 1` will be returned;
|
|
44
|
+
* when given `'default'`,
|
|
45
|
+
* the value of `export default …` is used,
|
|
46
|
+
* and when `false` the whole module object is returned.
|
|
47
|
+
*/
|
|
48
|
+
key?: boolean | string | null | undefined;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Configuration for `resolvePlugin`.
|
|
52
|
+
*/
|
|
53
|
+
export type ResolveOptions = {
|
|
54
|
+
/**
|
|
55
|
+
* Place or places to search from (optional);
|
|
56
|
+
* defaults to the current working directory.
|
|
57
|
+
*/
|
|
58
|
+
from?: ReadonlyArray<Readonly<URL> | string> | Readonly<URL> | string | null | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Whether to look for `name` in global places (default: whether global is
|
|
61
|
+
* detected);
|
|
62
|
+
* if this is nullish,
|
|
63
|
+
* `load-plugin` will detect if it’s currently running in global mode: either
|
|
64
|
+
* because it’s in Electron or because a globally installed package is
|
|
65
|
+
* running it;
|
|
66
|
+
* note that Electron runs its own version of Node instead of your system
|
|
67
|
+
* Node,
|
|
68
|
+
* meaning global packages cannot be found,
|
|
69
|
+
* unless you’ve set-up a `prefix` in your `.npmrc` or are using nvm to
|
|
70
|
+
* manage your system node.
|
|
71
|
+
*/
|
|
72
|
+
global?: boolean | null | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Prefix to search for (optional).
|
|
75
|
+
*/
|
|
76
|
+
prefix?: string | null | undefined;
|
|
77
|
+
};
|
|
78
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAqGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,iCAPW,MAAM,YAEN,SAAS,WAAW,CAAC,GAAG,IAAI,GAAG,SAAS,GAEtC,QAAQ,OAAO,CAAC,CAY5B;AAED;;;;;;;;;GASG;AACH,oCAPW,MAAM,YAEN,SAAS,cAAc,CAAC,GAAG,IAAI,GAAG,SAAS,GAEzC,QAAQ,MAAM,CAAC,CAyE3B;;;;0BAlNY,sBAAsB,GAAG,cAAc;;;;;;;;;;;;;UAKtC,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;WAUnC,cAAc,SAAS,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,SAAS,GAAG,CAAC,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;;;;;aAGjF,OAAO,GAAG,IAAI,GAAG,SAAS;;;;aAY1B,MAAM,GAAG,IAAI,GAAG,SAAS"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {LoadOptionsExtraFields & ResolveOptions} LoadOptions
|
|
3
|
+
* Configuration for `loadPlugin`.
|
|
4
|
+
*
|
|
5
|
+
* @typedef LoadOptionsExtraFields
|
|
6
|
+
* Extra configuration for `loadPlugin`.
|
|
7
|
+
* @property {boolean | string | null | undefined} [key]
|
|
8
|
+
* Identifier to take from the exports (default: `'default'`);
|
|
9
|
+
* for example when given `'x'`,
|
|
10
|
+
* the value of `export const x = 1` will be returned;
|
|
11
|
+
* when given `'default'`,
|
|
12
|
+
* the value of `export default …` is used,
|
|
13
|
+
* and when `false` the whole module object is returned.
|
|
14
|
+
*
|
|
15
|
+
* @typedef ResolveOptions
|
|
16
|
+
* Configuration for `resolvePlugin`.
|
|
17
|
+
* @property {ReadonlyArray<Readonly<URL> | string> | Readonly<URL> | string | null | undefined} [from]
|
|
18
|
+
* Place or places to search from (optional);
|
|
19
|
+
* defaults to the current working directory.
|
|
20
|
+
* @property {boolean | null | undefined} [global=boolean]
|
|
21
|
+
* Whether to look for `name` in global places (default: whether global is
|
|
22
|
+
* detected);
|
|
23
|
+
* if this is nullish,
|
|
24
|
+
* `load-plugin` will detect if it’s currently running in global mode: either
|
|
25
|
+
* because it’s in Electron or because a globally installed package is
|
|
26
|
+
* running it;
|
|
27
|
+
* note that Electron runs its own version of Node instead of your system
|
|
28
|
+
* Node,
|
|
29
|
+
* meaning global packages cannot be found,
|
|
30
|
+
* unless you’ve set-up a `prefix` in your `.npmrc` or are using nvm to
|
|
31
|
+
* manage your system node.
|
|
32
|
+
* @property {string | null | undefined} [prefix]
|
|
33
|
+
* Prefix to search for (optional).
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
import fs from 'node:fs/promises'
|
|
37
|
+
import path from 'node:path'
|
|
38
|
+
import process from 'node:process'
|
|
39
|
+
import {fileURLToPath, pathToFileURL} from 'node:url'
|
|
40
|
+
// @ts-expect-error: untyped
|
|
41
|
+
import NpmConfig from '@npmcli/config'
|
|
42
|
+
import {resolve} from 'import-meta-resolve'
|
|
43
|
+
|
|
44
|
+
const electron = process.versions.electron !== undefined
|
|
45
|
+
const windows = process.platform === 'win32'
|
|
46
|
+
|
|
47
|
+
const argv = process.argv[1] || /* c8 ignore next -- windows */ ''
|
|
48
|
+
const nvm = process.env.NVM_BIN
|
|
49
|
+
|
|
50
|
+
const config = new NpmConfig({
|
|
51
|
+
definitions: {},
|
|
52
|
+
npmPath: fileURLToPath(new URL('.', import.meta.url))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
config.loadGlobalPrefix()
|
|
56
|
+
|
|
57
|
+
// Note: this is a file path.
|
|
58
|
+
// If there is no prefix defined,
|
|
59
|
+
// use the defaults.
|
|
60
|
+
// See: <https://github.com/eush77/npm-prefix/blob/master/index.js>
|
|
61
|
+
/* c8 ignore next 6 -- typically defined */
|
|
62
|
+
/** @type {string} */
|
|
63
|
+
const npmPrefix =
|
|
64
|
+
config.globalPrefix ||
|
|
65
|
+
(windows
|
|
66
|
+
? path.dirname(process.execPath)
|
|
67
|
+
: path.resolve(process.execPath, '../..'))
|
|
68
|
+
|
|
69
|
+
const defaultGlobal = electron || argv.startsWith(npmPrefix)
|
|
70
|
+
/** @type {Readonly<LoadOptions>} */
|
|
71
|
+
const defaultLoadOptions = {}
|
|
72
|
+
/** @type {Readonly<ResolveOptions>} */
|
|
73
|
+
const defaultResolveOptions = {}
|
|
74
|
+
|
|
75
|
+
/* c8 ignore next -- windows */
|
|
76
|
+
const nodeModules = windows ? 'node_modules' : 'lib/node_modules'
|
|
77
|
+
|
|
78
|
+
let globalFolder = new URL(nodeModules, pathToFileURL(npmPrefix) + '/')
|
|
79
|
+
|
|
80
|
+
// If we’re in Electron,
|
|
81
|
+
// we’re running in a modified Node that cannot really install global node
|
|
82
|
+
// modules.
|
|
83
|
+
// To find the actual modules,
|
|
84
|
+
// the user has to set `prefix` somewhere in an `.npmrc` (which is picked up
|
|
85
|
+
// by `@npmcli/config`).
|
|
86
|
+
// Most people don’t do that,
|
|
87
|
+
// and some use NVM instead to manage different versions of Node.
|
|
88
|
+
// Luckily NVM leaks some environment variables that we can pick up on to try
|
|
89
|
+
// and detect the actual modules.
|
|
90
|
+
/* c8 ignore next 10 -- Electron. */
|
|
91
|
+
if (electron && nvm) {
|
|
92
|
+
try {
|
|
93
|
+
await fs.stat(globalFolder)
|
|
94
|
+
} catch (error) {
|
|
95
|
+
const cause = /** @type {NodeJS.ErrnoException} */ (error)
|
|
96
|
+
if (cause && cause.code === 'ENOENT') {
|
|
97
|
+
globalFolder = new URL(nodeModules, pathToFileURL(nvm))
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Import `name` from `from` (and optionally the global `node_modules` directory).
|
|
104
|
+
*
|
|
105
|
+
* Uses the Node.js resolution algorithm (through `import-meta-resolve`) to
|
|
106
|
+
* resolve CJS and ESM packages and files.
|
|
107
|
+
*
|
|
108
|
+
* If a `prefix` is given and `name` is not a path,
|
|
109
|
+
* `$prefix-$name` is also searched (preferring these over non-prefixed
|
|
110
|
+
* modules).
|
|
111
|
+
* If `name` starts with a scope (`@scope/name`),
|
|
112
|
+
* the prefix is applied after it: `@scope/$prefix-name`.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} name
|
|
115
|
+
* Specifier.
|
|
116
|
+
* @param {Readonly<LoadOptions> | null | undefined} [options]
|
|
117
|
+
* Configuration (optional).
|
|
118
|
+
* @returns {Promise<unknown>}
|
|
119
|
+
* Promise to a whole module or specific export.
|
|
120
|
+
*/
|
|
121
|
+
export async function loadPlugin(name, options) {
|
|
122
|
+
const settings = options || defaultLoadOptions
|
|
123
|
+
const href = await resolvePlugin(name, settings)
|
|
124
|
+
const module = /** @type {Record<string, unknown>} */ (await import(href))
|
|
125
|
+
return typeof settings.key === 'string'
|
|
126
|
+
? module[settings.key]
|
|
127
|
+
: settings.key === false
|
|
128
|
+
? module
|
|
129
|
+
: module.default
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Resolve `name` from `from`.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} name
|
|
136
|
+
* Specifier.
|
|
137
|
+
* @param {Readonly<ResolveOptions> | null | undefined} [options]
|
|
138
|
+
* Configuration (optional).
|
|
139
|
+
* @returns {Promise<string>}
|
|
140
|
+
* Promise to a file URL.
|
|
141
|
+
*/
|
|
142
|
+
export async function resolvePlugin(name, options) {
|
|
143
|
+
const settings = options || defaultResolveOptions
|
|
144
|
+
const prefix = settings.prefix
|
|
145
|
+
? settings.prefix + (settings.prefix.at(-1) === '-' ? '' : '-')
|
|
146
|
+
: undefined
|
|
147
|
+
const fromNonEmpty = settings.from || pathToFileURL(process.cwd() + '/')
|
|
148
|
+
const from = /** @type {Array<Readonly<URL> | string>} */ (
|
|
149
|
+
// type-coverage:ignore-next-line -- TS fails on readonly arrays.
|
|
150
|
+
Array.isArray(fromNonEmpty) ? fromNonEmpty : [fromNonEmpty]
|
|
151
|
+
)
|
|
152
|
+
const globals =
|
|
153
|
+
typeof settings.global === 'boolean' ? settings.global : defaultGlobal
|
|
154
|
+
/** @type {string | undefined} */
|
|
155
|
+
let plugin
|
|
156
|
+
/** @type {Error | undefined} */
|
|
157
|
+
let lastError
|
|
158
|
+
|
|
159
|
+
// Bare specifier.
|
|
160
|
+
if (name.charAt(0) !== '.') {
|
|
161
|
+
if (globals) {
|
|
162
|
+
from.push(globalFolder)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let scope = ''
|
|
166
|
+
|
|
167
|
+
// Unprefix module.
|
|
168
|
+
if (prefix) {
|
|
169
|
+
// Scope?
|
|
170
|
+
if (name.charAt(0) === '@') {
|
|
171
|
+
const slash = name.indexOf('/')
|
|
172
|
+
|
|
173
|
+
// Let’s keep the algorithm simple.
|
|
174
|
+
// No need to care if this is a “valid” scope (I think?).
|
|
175
|
+
// But we do check for the slash.
|
|
176
|
+
if (slash !== -1) {
|
|
177
|
+
scope = name.slice(0, slash + 1)
|
|
178
|
+
name = name.slice(slash + 1)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (name.slice(0, prefix.length) !== prefix) {
|
|
183
|
+
plugin = scope + prefix + name
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
name = scope + name
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let index = -1
|
|
191
|
+
|
|
192
|
+
while (++index < from.length) {
|
|
193
|
+
const source = from[index]
|
|
194
|
+
const href = typeof source === 'string' ? source : source.href
|
|
195
|
+
|
|
196
|
+
if (plugin) {
|
|
197
|
+
try {
|
|
198
|
+
return resolve(plugin, href)
|
|
199
|
+
} catch (error) {
|
|
200
|
+
lastError = /** @type {Error} */ (error)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
return resolve(name, href)
|
|
206
|
+
} catch (error) {
|
|
207
|
+
lastError = /** @type {Error} */ (error)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
throw lastError
|
|
212
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "load-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Load a submodule, plugin, or file",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"load",
|
|
8
|
-
"submodule",
|
|
9
8
|
"package",
|
|
10
|
-
"plugin"
|
|
9
|
+
"plugin",
|
|
10
|
+
"submodule"
|
|
11
11
|
],
|
|
12
12
|
"repository": "wooorm/load-plugin",
|
|
13
13
|
"bugs": "https://github.com/wooorm/load-plugin/issues",
|
|
@@ -21,59 +21,56 @@
|
|
|
21
21
|
],
|
|
22
22
|
"sideEffects": false,
|
|
23
23
|
"type": "module",
|
|
24
|
-
"
|
|
25
|
-
"types": "index.d.ts",
|
|
24
|
+
"exports": "./index.js",
|
|
26
25
|
"files": [
|
|
26
|
+
"lib/",
|
|
27
27
|
"index.d.ts",
|
|
28
|
-
"index.js"
|
|
28
|
+
"index.js",
|
|
29
|
+
"index.map"
|
|
29
30
|
],
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@npmcli/config": "^
|
|
32
|
-
"import-meta-resolve": "^
|
|
32
|
+
"@npmcli/config": "^8.0.0",
|
|
33
|
+
"import-meta-resolve": "^3.0.0"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@types/node": "^
|
|
36
|
-
"c8": "^
|
|
37
|
-
"prettier": "^
|
|
36
|
+
"@types/node": "^20.0.0",
|
|
37
|
+
"c8": "^8.0.0",
|
|
38
|
+
"prettier": "^3.0.0",
|
|
38
39
|
"remark-cli": "^11.0.0",
|
|
39
40
|
"remark-lint": "^9.0.0",
|
|
40
41
|
"remark-preset-wooorm": "^9.0.0",
|
|
41
42
|
"type-coverage": "^2.0.0",
|
|
42
|
-
"typescript": "^
|
|
43
|
-
"xo": "^0.
|
|
43
|
+
"typescript": "^5.0.0",
|
|
44
|
+
"xo": "^0.56.0"
|
|
44
45
|
},
|
|
45
46
|
"scripts": {
|
|
46
|
-
"prepack": "npm run build && npm run format",
|
|
47
47
|
"build": "tsc --build --clean && tsc --build && type-coverage",
|
|
48
|
-
"format": "remark .
|
|
48
|
+
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
|
|
49
|
+
"prepack": "npm run build && npm run format",
|
|
50
|
+
"test": "npm run build && npm run format && npm run test-coverage",
|
|
49
51
|
"test-api": "node --conditions development test/index.js",
|
|
50
|
-
"test-coverage": "c8 --
|
|
51
|
-
"test": "npm run build && npm run format && npm run test-coverage"
|
|
52
|
+
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
|
|
52
53
|
},
|
|
53
54
|
"prettier": {
|
|
54
|
-
"tabWidth": 2,
|
|
55
|
-
"useTabs": false,
|
|
56
|
-
"singleQuote": true,
|
|
57
55
|
"bracketSpacing": false,
|
|
56
|
+
"singleQuote": true,
|
|
58
57
|
"semi": false,
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
"prettier": true,
|
|
63
|
-
"rules": {
|
|
64
|
-
"unicorn/prefer-node-protocol": "off",
|
|
65
|
-
"no-await-in-loop": "off"
|
|
66
|
-
}
|
|
58
|
+
"tabWidth": 2,
|
|
59
|
+
"trailingComma": "none",
|
|
60
|
+
"useTabs": false
|
|
67
61
|
},
|
|
68
62
|
"remarkConfig": {
|
|
69
63
|
"plugins": [
|
|
70
|
-
"preset-wooorm"
|
|
64
|
+
"remark-preset-wooorm"
|
|
71
65
|
]
|
|
72
66
|
},
|
|
73
67
|
"typeCoverage": {
|
|
74
68
|
"atLeast": 100,
|
|
75
69
|
"detail": true,
|
|
76
|
-
"
|
|
77
|
-
"
|
|
70
|
+
"ignoreCatch": true,
|
|
71
|
+
"strict": true
|
|
72
|
+
},
|
|
73
|
+
"xo": {
|
|
74
|
+
"prettier": true
|
|
78
75
|
}
|
|
79
76
|
}
|
package/readme.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# load-plugin
|
|
2
2
|
|
|
3
|
-
[![Build][build-
|
|
4
|
-
[![Coverage][coverage-
|
|
5
|
-
[![Downloads][downloads-
|
|
3
|
+
[![Build][badge-build-image]][badge-build-url]
|
|
4
|
+
[![Coverage][badge-coverage-image]][badge-coverage-url]
|
|
5
|
+
[![Downloads][badge-downloads-image]][badge-downloads-url]
|
|
6
6
|
|
|
7
|
-
Load
|
|
7
|
+
Load a submodule, plugin, or file.
|
|
8
8
|
|
|
9
9
|
## Contents
|
|
10
10
|
|
|
@@ -15,17 +15,21 @@ Load submodules, plugins, or files.
|
|
|
15
15
|
* [API](#api)
|
|
16
16
|
* [`loadPlugin(name[, options])`](#loadpluginname-options)
|
|
17
17
|
* [`resolvePlugin(name[, options])`](#resolvepluginname-options)
|
|
18
|
-
* [
|
|
18
|
+
* [`LoadOptions`](#loadoptions)
|
|
19
|
+
* [`ResolveOptions`](#resolveoptions)
|
|
19
20
|
* [Compatibility](#compatibility)
|
|
21
|
+
* [Security](#security)
|
|
20
22
|
* [Contribute](#contribute)
|
|
21
23
|
* [License](#license)
|
|
22
24
|
|
|
23
25
|
## What is this?
|
|
24
26
|
|
|
25
27
|
This package is useful when you want to load plugins.
|
|
26
|
-
It resolves things like Node.js does,
|
|
27
|
-
prefix `remark` and the user provided value
|
|
28
|
-
|
|
28
|
+
It resolves things like Node.js does,
|
|
29
|
+
but supports a prefix (when given a prefix `remark` and the user provided value
|
|
30
|
+
`gfm` it can find `remark-gfm`),
|
|
31
|
+
can load from several places,
|
|
32
|
+
and optionally global too.
|
|
29
33
|
|
|
30
34
|
## When to use this?
|
|
31
35
|
|
|
@@ -36,8 +40,9 @@ files.
|
|
|
36
40
|
|
|
37
41
|
## Install
|
|
38
42
|
|
|
39
|
-
This package is [ESM only][esm].
|
|
40
|
-
In Node.js (version
|
|
43
|
+
This package is [ESM only][github-gist-esm].
|
|
44
|
+
In Node.js (version 16+),
|
|
45
|
+
install with [npm][npm-install]:
|
|
41
46
|
|
|
42
47
|
```sh
|
|
43
48
|
npm install load-plugin
|
|
@@ -51,13 +56,15 @@ Say we’re in this project (with dependencies installed):
|
|
|
51
56
|
import {loadPlugin, resolvePlugin} from 'load-plugin'
|
|
52
57
|
|
|
53
58
|
console.log(await resolvePlugin('lint', {prefix: 'remark'}))
|
|
54
|
-
// => '
|
|
59
|
+
// => 'file:///Users/tilde/Projects/oss/load-plugin/node_modules/remark-lint/index.js'
|
|
55
60
|
|
|
56
|
-
console.log(
|
|
57
|
-
|
|
61
|
+
console.log(
|
|
62
|
+
await resolvePlugin('validator-identifier', {prefix: '@babel/helper'})
|
|
63
|
+
)
|
|
64
|
+
// => 'file:///Users/tilde/Projects/oss/load-plugin/node_modules/@babel/helper-validator-identifier/lib/index.js'
|
|
58
65
|
|
|
59
66
|
console.log(await resolvePlugin('./index.js', {prefix: 'remark'}))
|
|
60
|
-
// => '
|
|
67
|
+
// => 'file:///Users/tilde/Projects/oss/load-plugin/index.js'
|
|
61
68
|
|
|
62
69
|
console.log(await loadPlugin('lint', {prefix: 'remark'}))
|
|
63
70
|
// => [Function: remarkLint]
|
|
@@ -65,124 +72,154 @@ console.log(await loadPlugin('lint', {prefix: 'remark'}))
|
|
|
65
72
|
|
|
66
73
|
## API
|
|
67
74
|
|
|
68
|
-
This package exports the identifiers
|
|
75
|
+
This package exports the identifiers
|
|
76
|
+
[`loadPlugin`][api-load-plugin] and [`resolvePlugin`][api-resolve-plugin].
|
|
69
77
|
There is no default export.
|
|
70
78
|
|
|
79
|
+
It exports the [TypeScript][] types
|
|
80
|
+
[`LoadOptions`][api-load-options] and [`ResolveOptions`][api-resolve-options].
|
|
81
|
+
|
|
71
82
|
### `loadPlugin(name[, options])`
|
|
72
83
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
`
|
|
84
|
+
Import `name` from `from` (and optionally the global `node_modules` directory).
|
|
85
|
+
|
|
86
|
+
Uses the Node.js [resolution algorithm][nodejs-resolution-algo] (through
|
|
87
|
+
[`import-meta-resolve`][github-import-meta-resolve]) to resolve CJS and ESM
|
|
88
|
+
packages and files.
|
|
77
89
|
|
|
78
|
-
If a `prefix` is given and `name` is not a path,
|
|
79
|
-
searched (preferring these over non-prefixed
|
|
80
|
-
|
|
81
|
-
`@scope
|
|
90
|
+
If a `prefix` is given and `name` is not a path,
|
|
91
|
+
`$prefix-$name` is also searched (preferring these over non-prefixed
|
|
92
|
+
modules).
|
|
93
|
+
If `name` starts with a scope (`@scope/name`),
|
|
94
|
+
the prefix is applied after it: `@scope/$prefix-name`.
|
|
82
95
|
|
|
83
|
-
|
|
96
|
+
###### Parameters
|
|
84
97
|
|
|
85
|
-
|
|
98
|
+
* `name` (`string`)
|
|
99
|
+
— specifier
|
|
100
|
+
* `options` ([`LoadOptions`][api-load-options], optional)
|
|
101
|
+
— configuration
|
|
102
|
+
|
|
103
|
+
###### Returns
|
|
86
104
|
|
|
87
|
-
|
|
105
|
+
Promise to a whole module or specific export (`Promise<unknown>`).
|
|
88
106
|
|
|
89
|
-
|
|
107
|
+
### `resolvePlugin(name[, options])`
|
|
90
108
|
|
|
91
|
-
|
|
109
|
+
Resolve `name` from `from`.
|
|
92
110
|
|
|
93
|
-
|
|
94
|
-
`process.cwd()`).
|
|
111
|
+
###### Parameters
|
|
95
112
|
|
|
96
|
-
|
|
113
|
+
* `name` (`string`)
|
|
114
|
+
— specifier
|
|
115
|
+
* `options` ([`ResolveOptions`][api-resolve-options], optional)
|
|
116
|
+
— configuration
|
|
97
117
|
|
|
98
|
-
|
|
99
|
-
defaults to whether global is detected).
|
|
100
|
-
If this is nullish, `load-plugin` will detect if it’s currently running in
|
|
101
|
-
global mode: either because it’s in Electron, or because a globally installed
|
|
102
|
-
package is running it.
|
|
118
|
+
###### Returns
|
|
103
119
|
|
|
104
|
-
|
|
105
|
-
That means global packages cannot be found, unless you’ve [set-up][] a [`prefix`
|
|
106
|
-
in your `.npmrc`][prefix] or are using [nvm][] to manage your system node.
|
|
120
|
+
Promise to a file URL (`Promise<string>`).
|
|
107
121
|
|
|
108
|
-
|
|
122
|
+
### `LoadOptions`
|
|
109
123
|
|
|
110
|
-
|
|
111
|
-
For example when given `'whatever'`, the value of `export const whatever = 1`
|
|
112
|
-
will be returned, when given `'default'`, the value of `export default …` is
|
|
113
|
-
used, and when `false` the whole module object is returned.
|
|
124
|
+
Configuration for `loadPlugin` (TypeScript type).
|
|
114
125
|
|
|
115
|
-
|
|
126
|
+
This type extends `ResolveOptions` and adds:
|
|
116
127
|
|
|
117
|
-
|
|
118
|
-
(`Promise<unknown>`).
|
|
119
|
-
The promise rejects if importing an existing path fails, or if no existing
|
|
120
|
-
path exists.
|
|
128
|
+
###### Fields
|
|
121
129
|
|
|
122
|
-
|
|
130
|
+
* `key` (`boolean` or `string`, default: `'default'`)
|
|
131
|
+
— identifier to take from the exports;
|
|
132
|
+
for example when given `'x'`,
|
|
133
|
+
the value of `export const x = 1` will be returned;
|
|
134
|
+
when given `'default'`,
|
|
135
|
+
the value of `export default …` is used,
|
|
136
|
+
and when `false` the whole module object is returned
|
|
123
137
|
|
|
124
|
-
|
|
125
|
-
Accepts the same parameters as [`loadPlugin`][load-plugin] (except `key`) but
|
|
126
|
-
returns a promise resolving to an absolute URL (`string`) for `name` instead of
|
|
127
|
-
importing it.
|
|
128
|
-
Throws if `name` cannot be found.
|
|
138
|
+
### `ResolveOptions`
|
|
129
139
|
|
|
130
|
-
|
|
140
|
+
Configuration for `resolvePlugin` (TypeScript type).
|
|
131
141
|
|
|
132
|
-
|
|
133
|
-
|
|
142
|
+
###### Fields
|
|
143
|
+
|
|
144
|
+
* `from` (`Array<URL | string> | URL | string`, optional)
|
|
145
|
+
— place or places to search from;
|
|
146
|
+
defaults to the current working directory
|
|
147
|
+
* `global` (`boolean`, default: whether global is detected)
|
|
148
|
+
— whether to look for `name` in [global places][npm-node-modules];
|
|
149
|
+
if this is nullish,
|
|
150
|
+
`load-plugin` will detect if it’s currently running in global mode: either
|
|
151
|
+
because it’s in Electron or because a globally installed package is running
|
|
152
|
+
it;
|
|
153
|
+
note that Electron runs its own version of Node instead of your system Node,
|
|
154
|
+
meaning global packages cannot be found,
|
|
155
|
+
unless you’ve set-up a [`prefix`][npm-prefix] in your `.npmrc` or are using
|
|
156
|
+
[nvm][github-nvm] to manage your system node
|
|
157
|
+
* `prefix` (`string`, optional)
|
|
158
|
+
— prefix to search for
|
|
134
159
|
|
|
135
160
|
## Compatibility
|
|
136
161
|
|
|
137
|
-
This
|
|
138
|
-
|
|
139
|
-
|
|
162
|
+
This projects is compatible with maintained versions of Node.js.
|
|
163
|
+
|
|
164
|
+
When we cut a new major release,
|
|
165
|
+
we drop support for unmaintained versions of Node.
|
|
166
|
+
This means we try to keep the current release line,
|
|
167
|
+
`load-plugin@6`,
|
|
168
|
+
compatible with Node.js 16.
|
|
169
|
+
|
|
170
|
+
## Security
|
|
171
|
+
|
|
172
|
+
This package reads the file system and imports things into Node.js.
|
|
140
173
|
|
|
141
174
|
## Contribute
|
|
142
175
|
|
|
143
176
|
Yes please!
|
|
144
|
-
See [How to Contribute to Open Source][contribute].
|
|
177
|
+
See [How to Contribute to Open Source][open-source-guide-contribute].
|
|
145
178
|
|
|
146
179
|
## License
|
|
147
180
|
|
|
148
|
-
[MIT][license] © [Titus Wormer][
|
|
181
|
+
[MIT][file-license] © [Titus Wormer][wooorm]
|
|
149
182
|
|
|
150
183
|
<!-- Definitions -->
|
|
151
184
|
|
|
152
|
-
[
|
|
185
|
+
[api-load-plugin]: #loadpluginname-options
|
|
153
186
|
|
|
154
|
-
[
|
|
187
|
+
[api-load-options]: #loadoptions
|
|
155
188
|
|
|
156
|
-
[
|
|
189
|
+
[api-resolve-plugin]: #resolvepluginname-options
|
|
157
190
|
|
|
158
|
-
[
|
|
191
|
+
[api-resolve-options]: #resolveoptions
|
|
159
192
|
|
|
160
|
-
[
|
|
193
|
+
[badge-build-image]: https://github.com/wooorm/load-plugin/workflows/main/badge.svg
|
|
161
194
|
|
|
162
|
-
[
|
|
195
|
+
[badge-build-url]: https://github.com/wooorm/load-plugin/actions
|
|
163
196
|
|
|
164
|
-
[
|
|
197
|
+
[badge-coverage-image]: https://img.shields.io/codecov/c/github/wooorm/load-plugin.svg
|
|
165
198
|
|
|
166
|
-
[
|
|
199
|
+
[badge-coverage-url]: https://codecov.io/github/wooorm/load-plugin
|
|
167
200
|
|
|
168
|
-
[
|
|
201
|
+
[badge-downloads-image]: https://img.shields.io/npm/dm/load-plugin.svg
|
|
169
202
|
|
|
170
|
-
[
|
|
203
|
+
[badge-downloads-url]: https://www.npmjs.com/package/load-plugin
|
|
171
204
|
|
|
172
|
-
[
|
|
205
|
+
[file-license]: license
|
|
206
|
+
|
|
207
|
+
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
|
173
208
|
|
|
174
|
-
[
|
|
209
|
+
[github-import-meta-resolve]: https://github.com/wooorm/import-meta-resolve
|
|
175
210
|
|
|
176
|
-
[
|
|
211
|
+
[github-nvm]: https://github.com/nvm-sh/nvm
|
|
177
212
|
|
|
178
|
-
[
|
|
213
|
+
[nodejs-resolution-algo]: https://nodejs.org/api/esm.html#esm_resolution_algorithm
|
|
179
214
|
|
|
180
|
-
[
|
|
215
|
+
[npm-install]: https://docs.npmjs.com/cli/install
|
|
181
216
|
|
|
182
|
-
[
|
|
217
|
+
[npm-node-modules]: https://docs.npmjs.com/cli/v10/configuring-npm/folders#node-modules
|
|
183
218
|
|
|
184
|
-
[
|
|
219
|
+
[npm-prefix]: https://docs.npmjs.com/cli/v10/using-npm/config
|
|
185
220
|
|
|
186
|
-
[
|
|
221
|
+
[open-source-guide-contribute]: https://opensource.guide/how-to-contribute/
|
|
222
|
+
|
|
223
|
+
[typescript]: https://www.typescriptlang.org
|
|
187
224
|
|
|
188
|
-
[
|
|
225
|
+
[wooorm]: https://wooorm.com
|