load-plugin 6.0.2 → 6.0.4

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 CHANGED
@@ -1,4 +1,4 @@
1
- export type LoadOptions = import('./lib/index.js').LoadOptions;
2
- export type ResolveOptions = import('./lib/index.js').ResolveOptions;
1
+ export type LoadOptions = import("./lib/index.js").LoadOptions;
2
+ export type ResolveOptions = import("./lib/index.js").ResolveOptions;
3
3
  export { loadPlugin, resolvePlugin } from "./lib/index.js";
4
4
  //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"0BACa,OAAO,gBAAgB,EAAE,WAAW;6BACpC,OAAO,gBAAgB,EAAE,cAAc"}
package/lib/index.d.ts CHANGED
@@ -1,14 +1,7 @@
1
1
  /**
2
- * Import `name` from `from` (and optionally the global `node_modules` directory).
2
+ * Import `name` from `from` (and optionally the global `node_modules` folder).
3
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`.
4
+ * See Algorithm in the readme for how `name` is resolved.
12
5
  *
13
6
  * @param {string} name
14
7
  * Specifier.
@@ -19,7 +12,10 @@
19
12
  */
20
13
  export function loadPlugin(name: string, options?: Readonly<LoadOptions> | null | undefined): Promise<unknown>;
21
14
  /**
22
- * Resolve `name` from `from`.
15
+ * Resolve `name` from `from` (and optionally the global `node_modules`
16
+ * folder).
17
+ *
18
+ * See Algorithm in the readme for how `name` is resolved.
23
19
  *
24
20
  * @param {string} name
25
21
  * Specifier.
@@ -31,6 +27,8 @@ export function loadPlugin(name: string, options?: Readonly<LoadOptions> | null
31
27
  export function resolvePlugin(name: string, options?: Readonly<ResolveOptions> | null | undefined): Promise<string>;
32
28
  /**
33
29
  * Configuration for `loadPlugin`.
30
+ *
31
+ * Includes the fields of `ResolveOptions`.
34
32
  */
35
33
  export type LoadOptions = LoadOptionsExtraFields & ResolveOptions;
36
34
  /**
@@ -40,10 +38,11 @@ export type LoadOptionsExtraFields = {
40
38
  /**
41
39
  * Identifier to take from the exports (default: `'default'`);
42
40
  * for example when given `'x'`,
43
- * the value of `export const x = 1` will be returned;
41
+ * the value of `export const x = 1` is returned;
44
42
  * when given `'default'`,
45
- * the value of `export default …` is used,
46
- * and when `false` the whole module object is returned.
43
+ * the value of `export default …` is returned;
44
+ * when `false`,
45
+ * the whole module object is returned.
47
46
  */
48
47
  key?: boolean | string | null | undefined;
49
48
  };
@@ -57,17 +56,15 @@ export type ResolveOptions = {
57
56
  */
58
57
  from?: ReadonlyArray<Readonly<URL> | string> | Readonly<URL> | string | null | undefined;
59
58
  /**
60
- * Whether to look for `name` in global places (default: whether global is
59
+ * Whether to also search global `node_modules` (default: whether global is
61
60
  * 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.
61
+ * when nullish,
62
+ * global mode is detected:
63
+ * it is on when running in Electron or from a globally installed package;
64
+ * note that Electron runs its own Node instead of your system Node,
65
+ * so global packages cannot be found,
66
+ * unless you set a `prefix` in your `.npmrc` or use nvm to manage your
67
+ * system Node.
71
68
  */
72
69
  global?: boolean | null | undefined;
73
70
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AA8FA;;;;;;;;;;;;;;;;;;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;;;;0BA3MY,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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AAiEA;;;;;;;;;;;GAWG;AACH,iCAPW,MAAM,YAEN,QAAQ,CAAC,WAAW,CAAC,GAAG,IAAI,GAAG,SAAS,GAEtC,OAAO,CAAC,OAAO,CAAC,CAY5B;AAED;;;;;;;;;;;;GAYG;AACH,oCAPW,MAAM,YAEN,QAAQ,CAAC,cAAc,CAAC,GAAG,IAAI,GAAG,SAAS,GAEzC,OAAO,CAAC,MAAM,CAAC,CA8F3B;;;;;;0BA/LY,sBAAsB,GAAG,cAAc;;;;;;;;;;;;;;UAStC,OAAO,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;WAanC,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS;;;;;;;;;;;;aAGjF,OAAO,GAAG,IAAI,GAAG,SAAS;;;;aAU1B,MAAM,GAAG,IAAI,GAAG,SAAS"}
package/lib/index.js CHANGED
@@ -2,107 +2,71 @@
2
2
  * @typedef {LoadOptionsExtraFields & ResolveOptions} LoadOptions
3
3
  * Configuration for `loadPlugin`.
4
4
  *
5
+ * Includes the fields of `ResolveOptions`.
6
+ */
7
+
8
+ /**
5
9
  * @typedef LoadOptionsExtraFields
6
10
  * Extra configuration for `loadPlugin`.
7
- * @property {boolean | string | null | undefined} [key]
11
+ * @property {boolean | string | null | undefined} [key='default']
8
12
  * Identifier to take from the exports (default: `'default'`);
9
13
  * for example when given `'x'`,
10
- * the value of `export const x = 1` will be returned;
14
+ * the value of `export const x = 1` is returned;
11
15
  * when given `'default'`,
12
- * the value of `export default …` is used,
13
- * and when `false` the whole module object is returned.
14
- *
16
+ * the value of `export default …` is returned;
17
+ * when `false`,
18
+ * the whole module object is returned.
19
+ */
20
+
21
+ /**
15
22
  * @typedef ResolveOptions
16
23
  * Configuration for `resolvePlugin`.
17
24
  * @property {ReadonlyArray<Readonly<URL> | string> | Readonly<URL> | string | null | undefined} [from]
18
25
  * Place or places to search from (optional);
19
26
  * 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
27
+ * @property {boolean | null | undefined} [global]
28
+ * Whether to also search global `node_modules` (default: whether global is
22
29
  * 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.
30
+ * when nullish,
31
+ * global mode is detected:
32
+ * it is on when running in Electron or from a globally installed package;
33
+ * note that Electron runs its own Node instead of your system Node,
34
+ * so global packages cannot be found,
35
+ * unless you set a `prefix` in your `.npmrc` or use nvm to manage your
36
+ * system Node.
32
37
  * @property {string | null | undefined} [prefix]
33
38
  * Prefix to search for (optional).
34
39
  */
35
40
 
36
- import fs from 'node:fs'
37
41
  import path from 'node:path'
38
42
  import process from 'node:process'
39
- import {fileURLToPath, pathToFileURL} from 'node:url'
43
+ import {pathToFileURL} from 'node:url'
40
44
  // @ts-expect-error: untyped
41
45
  import NpmConfig from '@npmcli/config'
46
+ // @ts-expect-error: untyped
47
+ import definitions from '@npmcli/config/lib/definitions/definitions.js'
42
48
  import {resolve} from 'import-meta-resolve'
43
49
 
44
50
  const electron = process.versions.electron !== undefined
45
- const windows = process.platform === 'win32'
46
-
47
- const argv = process.argv[1] || /* c8 ignore next -- windows */ ''
48
51
  const nvm = process.env.NVM_BIN
52
+ const windows = process.platform === 'win32'
53
+ /* c8 ignore next -- windows */
54
+ const argv = process.argv[1] || ''
55
+ /* c8 ignore next -- windows */
56
+ const nodeModules = windows ? 'node_modules' : 'lib/node_modules'
49
57
 
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
58
  /** @type {Readonly<LoadOptions>} */
71
59
  const defaultLoadOptions = {}
72
60
  /** @type {Readonly<ResolveOptions>} */
73
61
  const defaultResolveOptions = {}
74
62
 
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 && !fs.existsSync(globalFolder)) {
92
- globalFolder = new URL(nodeModules, pathToFileURL(nvm))
93
- }
63
+ /** @type {Promise<string> | string | undefined} */
64
+ let npmPrefix
94
65
 
95
66
  /**
96
- * Import `name` from `from` (and optionally the global `node_modules` directory).
97
- *
98
- * Uses the Node.js resolution algorithm (through `import-meta-resolve`) to
99
- * resolve CJS and ESM packages and files.
67
+ * Import `name` from `from` (and optionally the global `node_modules` folder).
100
68
  *
101
- * If a `prefix` is given and `name` is not a path,
102
- * `$prefix-$name` is also searched (preferring these over non-prefixed
103
- * modules).
104
- * If `name` starts with a scope (`@scope/name`),
105
- * the prefix is applied after it: `@scope/$prefix-name`.
69
+ * See Algorithm in the readme for how `name` is resolved.
106
70
  *
107
71
  * @param {string} name
108
72
  * Specifier.
@@ -123,7 +87,10 @@ export async function loadPlugin(name, options) {
123
87
  }
124
88
 
125
89
  /**
126
- * Resolve `name` from `from`.
90
+ * Resolve `name` from `from` (and optionally the global `node_modules`
91
+ * folder).
92
+ *
93
+ * See Algorithm in the readme for how `name` is resolved.
127
94
  *
128
95
  * @param {string} name
129
96
  * Specifier.
@@ -139,11 +106,17 @@ export async function resolvePlugin(name, options) {
139
106
  : undefined
140
107
  const fromNonEmpty = settings.from || pathToFileURL(process.cwd() + '/')
141
108
  const from = /** @type {Array<Readonly<URL> | string>} */ (
142
- // type-coverage:ignore-next-line -- TS fails on readonly arrays.
143
109
  Array.isArray(fromNonEmpty) ? fromNonEmpty : [fromNonEmpty]
144
110
  )
111
+
112
+ if (!npmPrefix) npmPrefix = inferNpmPrefix()
113
+ if (typeof npmPrefix !== 'string') npmPrefix = await npmPrefix
114
+
145
115
  const globals =
146
- typeof settings.global === 'boolean' ? settings.global : defaultGlobal
116
+ typeof settings.global === 'boolean'
117
+ ? settings.global
118
+ : electron || argv.startsWith(npmPrefix)
119
+
147
120
  /** @type {string | undefined} */
148
121
  let plugin
149
122
  /** @type {Error | undefined} */
@@ -152,13 +125,28 @@ export async function resolvePlugin(name, options) {
152
125
  // Bare specifier.
153
126
  if (name.charAt(0) !== '.') {
154
127
  if (globals) {
155
- from.push(globalFolder)
128
+ from.push(new URL(nodeModules, pathToFileURL(npmPrefix) + '/'))
129
+
130
+ // If we’re in Electron,
131
+ // we’re running in a modified Node that cannot really install global node
132
+ // modules.
133
+ // To find the actual modules,
134
+ // the user has to set `prefix` somewhere in an `.npmrc` (which is picked up
135
+ // by `@npmcli/config`).
136
+ // Most people don’t do that,
137
+ // and some use NVM instead to manage different versions of Node.
138
+ // Luckily NVM leaks some environment variables that we can pick up on to try
139
+ // and detect the actual modules.
140
+ /* c8 ignore next 3 -- Electron. */
141
+ if (electron && nvm) {
142
+ from.push(new URL(nodeModules, pathToFileURL(nvm)))
143
+ }
156
144
  }
157
145
 
158
- let scope = ''
159
-
160
146
  // Unprefix module.
161
147
  if (prefix) {
148
+ let scope = ''
149
+
162
150
  // Scope?
163
151
  if (name.charAt(0) === '@') {
164
152
  const slash = name.indexOf('/')
@@ -203,3 +191,29 @@ export async function resolvePlugin(name, options) {
203
191
 
204
192
  throw lastError
205
193
  }
194
+
195
+ /**
196
+ * Find npm prefix.
197
+ *
198
+ * @returns {Promise<string>}
199
+ * Folder where npm installs global packages.
200
+ */
201
+ async function inferNpmPrefix() {
202
+ const config = new NpmConfig({
203
+ argv: [],
204
+ definitions,
205
+ // `env` is mutated.
206
+ env: {...process.env},
207
+ npmPath: ''
208
+ })
209
+
210
+ await config.load()
211
+
212
+ /* c8 ignore next 6 -- typically defined */
213
+ return (
214
+ config.globalPrefix ||
215
+ (windows
216
+ ? path.dirname(process.execPath)
217
+ : path.resolve(process.execPath, '../..'))
218
+ )
219
+ }
package/license CHANGED
@@ -1,6 +1,6 @@
1
1
  (The MIT License)
2
2
 
3
- Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
3
+ Copyright (c) Titus Wormer <tituswormer@gmail.com>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
package/package.json CHANGED
@@ -1,60 +1,49 @@
1
1
  {
2
- "name": "load-plugin",
3
- "version": "6.0.2",
4
- "description": "Load a submodule, plugin, or file",
5
- "license": "MIT",
6
- "keywords": [
7
- "load",
8
- "package",
9
- "plugin",
10
- "submodule"
11
- ],
12
- "repository": "wooorm/load-plugin",
13
- "bugs": "https://github.com/wooorm/load-plugin/issues",
14
- "funding": {
15
- "type": "github",
16
- "url": "https://github.com/sponsors/wooorm"
17
- },
18
2
  "author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
3
+ "bugs": "https://github.com/wooorm/load-plugin/issues",
19
4
  "contributors": [
20
5
  "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
21
6
  ],
22
- "sideEffects": false,
23
- "type": "module",
24
- "exports": "./index.js",
25
- "files": [
26
- "lib/",
27
- "index.d.ts",
28
- "index.js",
29
- "index.map"
30
- ],
31
7
  "dependencies": {
32
8
  "@npmcli/config": "^8.0.0",
33
9
  "import-meta-resolve": "^4.0.0"
34
10
  },
11
+ "description": "Load a submodule, plugin, or file",
35
12
  "devDependencies": {
36
- "@types/node": "^20.0.0",
37
- "c8": "^9.0.0",
13
+ "@types/node": "^26.0.0",
14
+ "c8": "^12.0.0",
15
+ "github-slugger": "^2.0.0",
38
16
  "prettier": "^3.0.0",
39
- "remark-cli": "^11.0.0",
40
- "remark-lint": "^9.0.0",
41
- "remark-preset-wooorm": "^9.0.0",
17
+ "remark-cli": "^12.0.0",
18
+ "remark-lint": "^10.0.0",
19
+ "remark-preset-wooorm": "^11.0.0",
42
20
  "type-coverage": "^2.0.0",
43
- "typescript": "^5.0.0",
44
- "xo": "^0.56.0"
21
+ "typescript": "^6.0.0",
22
+ "xo": "^5.0.0"
45
23
  },
46
- "scripts": {
47
- "build": "tsc --build --clean && tsc --build && type-coverage",
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",
51
- "test-api": "node --conditions development test/index.js",
52
- "test-coverage": "c8 --100 --reporter lcov npm run test-api"
24
+ "exports": "./index.js",
25
+ "files": [
26
+ "lib/",
27
+ "index.d.ts.map",
28
+ "index.d.ts",
29
+ "index.js"
30
+ ],
31
+ "funding": {
32
+ "type": "github",
33
+ "url": "https://github.com/sponsors/wooorm"
53
34
  },
35
+ "keywords": [
36
+ "load",
37
+ "package",
38
+ "plugin",
39
+ "submodule"
40
+ ],
41
+ "license": "MIT",
42
+ "name": "load-plugin",
54
43
  "prettier": {
55
44
  "bracketSpacing": false,
56
- "singleQuote": true,
57
45
  "semi": false,
46
+ "singleQuote": true,
58
47
  "tabWidth": 2,
59
48
  "trailingComma": "none",
60
49
  "useTabs": false
@@ -64,13 +53,20 @@
64
53
  "remark-preset-wooorm"
65
54
  ]
66
55
  },
56
+ "repository": "wooorm/load-plugin",
57
+ "scripts": {
58
+ "build": "tsc --build --clean && tsc --build && type-coverage",
59
+ "format": "remark --frail --output --quiet -- . && prettier --log-level warn --write -- . && xo --fix",
60
+ "prepack": "npm run build && npm run format",
61
+ "test-api": "node --conditions development test/index.js",
62
+ "test-coverage": "c8 --100 --reporter lcov -- npm run test-api",
63
+ "test": "npm run build && npm run format && npm run test-coverage"
64
+ },
65
+ "sideEffects": false,
67
66
  "typeCoverage": {
68
67
  "atLeast": 100,
69
- "detail": true,
70
- "ignoreCatch": true,
71
68
  "strict": true
72
69
  },
73
- "xo": {
74
- "prettier": true
75
- }
70
+ "type": "module",
71
+ "version": "6.0.4"
76
72
  }
package/readme.md CHANGED
@@ -8,41 +8,44 @@ Load a submodule, plugin, or file.
8
8
 
9
9
  ## Contents
10
10
 
11
- * [What is this?](#what-is-this)
12
- * [When to use this?](#when-to-use-this)
13
- * [Install](#install)
14
- * [Use](#use)
15
- * [API](#api)
16
- * [`loadPlugin(name[, options])`](#loadpluginname-options)
17
- * [`resolvePlugin(name[, options])`](#resolvepluginname-options)
18
- * [`LoadOptions`](#loadoptions)
19
- * [`ResolveOptions`](#resolveoptions)
20
- * [Compatibility](#compatibility)
21
- * [Security](#security)
22
- * [Contribute](#contribute)
23
- * [License](#license)
11
+ * [What is this?](#what-is-this)
12
+ * [When should I use this?](#when-should-i-use-this)
13
+ * [Install](#install)
14
+ * [Use](#use)
15
+ * [API](#api)
16
+ * [`LoadOptions`](#loadoptions)
17
+ * [`ResolveOptions`](#resolveoptions)
18
+ * [`loadPlugin(name[, options])`](#loadpluginname-options)
19
+ * [`resolvePlugin(name[, options])`](#resolvepluginname-options)
20
+ * [Algorithm](#algorithm)
21
+ * [Compatibility](#compatibility)
22
+ * [Security](#security)
23
+ * [Related](#related)
24
+ * [Contribute](#contribute)
25
+ * [License](#license)
24
26
 
25
27
  ## What is this?
26
28
 
27
29
  This package is useful when you want to load plugins.
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.
30
+ It resolves and imports modules like Node.js does with some extras.
31
+ For example,
32
+ it can add a prefix to names
33
+ (when given a prefix `remark` and the user-provided value `gfm`,
34
+ it finds `remark-gfm`),
35
+ search from several places,
36
+ and search global `node_modules` folders.
33
37
 
34
- ## When to use this?
38
+ ## When should I use this?
35
39
 
36
- This package is particularly useful when you want users to configure something
37
- with plugins.
38
- One example is `remark-cli` which can load remark plugins from configuration
39
- files.
40
+ You can use this when users configure your tool with plugins.
41
+ For example,
42
+ `remark-cli` uses it to load remark plugins named in configuration files.
40
43
 
41
44
  ## Install
42
45
 
43
46
  This package is [ESM only][github-gist-esm].
44
47
  In Node.js (version 16+),
45
- install with [npm][npm-install]:
48
+ install with [npm][npmjs-install]:
46
49
 
47
50
  ```sh
48
51
  npm install load-plugin
@@ -73,93 +76,107 @@ console.log(await loadPlugin('lint', {prefix: 'remark'}))
73
76
  ## API
74
77
 
75
78
  This package exports the identifiers
76
- [`loadPlugin`][api-load-plugin] and [`resolvePlugin`][api-resolve-plugin].
79
+ [`loadPlugin`][api-load-plugin] and
80
+ [`resolvePlugin`][api-resolve-plugin].
77
81
  There is no default export.
82
+ It exports the additional [TypeScript][] types
83
+ [`LoadOptions`][api-load-options] and
84
+ [`ResolveOptions`][api-resolve-options].
78
85
 
79
- It exports the [TypeScript][] types
80
- [`LoadOptions`][api-load-options] and [`ResolveOptions`][api-resolve-options].
86
+ ### `LoadOptions`
81
87
 
82
- ### `loadPlugin(name[, options])`
88
+ Configuration for [`loadPlugin`][api-load-plugin] (TypeScript type).
83
89
 
84
- Import `name` from `from` (and optionally the global `node_modules` directory).
90
+ Includes the fields of [`ResolveOptions`][api-resolve-options].
85
91
 
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.
92
+ ###### Fields
89
93
 
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`.
94
+ * `key` (`boolean` or `string`, default: `'default'`)
95
+ — identifier to take from the exports;
96
+ for example when given `'x'`,
97
+ the value of `export const x = 1` is returned;
98
+ when given `'default'`,
99
+ the value of `export default …` is returned;
100
+ when `false`,
101
+ the whole module object is returned
95
102
 
96
- ###### Parameters
103
+ ### `ResolveOptions`
97
104
 
98
- * `name` (`string`)
99
- — specifier
100
- * `options` ([`LoadOptions`][api-load-options], optional)
101
- — configuration
105
+ Configuration for [`resolvePlugin`][api-resolve-plugin] (TypeScript type).
102
106
 
103
- ###### Returns
107
+ ###### Fields
104
108
 
105
- Promise to a whole module or specific export (`Promise<unknown>`).
109
+ * `from` (`Array<URL | string> | URL | string`, optional)
110
+ — place or places to search from;
111
+ defaults to the current working directory
112
+ * `global` (`boolean`, default: whether global is detected)
113
+ — whether to also search [global `node_modules`][npmjs-node-modules];
114
+ when nullish,
115
+ global mode is detected:
116
+ it is on when running in Electron or from a globally installed package;
117
+ note that Electron runs its own Node instead of your system Node,
118
+ so global packages cannot be found,
119
+ unless you set a [`prefix`][npmjs-prefix] in your `.npmrc` or use
120
+ [nvm][github-nvm] to manage your system Node
121
+ * `prefix` (`string`, optional)
122
+ — prefix to search for
106
123
 
107
- ### `resolvePlugin(name[, options])`
124
+ ### `loadPlugin(name[, options])`
108
125
 
109
- Resolve `name` from `from`.
126
+ Import `name` from `from` (and optionally the global `node_modules`
127
+ folder).
128
+
129
+ See [Algorithm][] for how `name` is resolved.
110
130
 
111
131
  ###### Parameters
112
132
 
113
- * `name` (`string`)
114
- — specifier
115
- * `options` ([`ResolveOptions`][api-resolve-options], optional)
116
- — configuration
133
+ * `name` (`string`, required)
134
+ — specifier
135
+ * `options` ([`LoadOptions`][api-load-options], optional)
136
+ — configuration
117
137
 
118
138
  ###### Returns
119
139
 
120
- Promise to a file URL (`Promise<string>`).
140
+ Promise to a whole module or specific export (`Promise<unknown>`).
121
141
 
122
- ### `LoadOptions`
142
+ ### `resolvePlugin(name[, options])`
123
143
 
124
- Configuration for `loadPlugin` (TypeScript type).
144
+ Resolve `name` from `from` (and optionally the global `node_modules`
145
+ folder).
125
146
 
126
- This type extends `ResolveOptions` and adds:
147
+ See [Algorithm][] for how `name` is resolved.
127
148
 
128
- ###### Fields
149
+ ###### Parameters
129
150
 
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
151
+ * `name` (`string`, required)
152
+ — specifier
153
+ * `options` ([`ResolveOptions`][api-resolve-options], optional)
154
+ — configuration
137
155
 
138
- ### `ResolveOptions`
156
+ ###### Returns
139
157
 
140
- Configuration for `resolvePlugin` (TypeScript type).
158
+ Promise to a file URL (`Promise<string>`).
141
159
 
142
- ###### Fields
160
+ ## Algorithm
161
+
162
+ Names are resolved with the Node.js
163
+ [resolution algorithm][nodejs-esm-resolution]
164
+ (through [`import-meta-resolve`][github-import-meta-resolve])
165
+ to CJS and ESM packages and files.
166
+ Each place in `from` is tried in order.
167
+ When `global` is on and `name` is not a path,
168
+ the global `node_modules` folder is tried last.
143
169
 
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
170
+ When a `prefix` is given and `name` is not a path,
171
+ `$prefix-$name` is also searched,
172
+ and preferred over `name` in each place.
173
+ When `name` starts with a scope (`@scope/name`),
174
+ the prefix is added after it: `@scope/$prefix-name`.
175
+ A `name` that already starts with the prefix is not prefixed again.
159
176
 
160
177
  ## Compatibility
161
178
 
162
- This projects is compatible with maintained versions of Node.js.
179
+ This package is compatible with maintained versions of Node.js.
163
180
 
164
181
  When we cut a new major release,
165
182
  we drop support for unmaintained versions of Node.
@@ -169,12 +186,18 @@ compatible with Node.js 16.
169
186
 
170
187
  ## Security
171
188
 
172
- This package reads the file system and imports things into Node.js.
189
+ This package reads the file system and imports modules,
190
+ which runs that code.
191
+
192
+ ## Related
193
+
194
+ * [`wooorm/import-meta-resolve`][github-import-meta-resolve]
195
+ — resolve things like Node.js does
173
196
 
174
197
  ## Contribute
175
198
 
176
199
  Yes please!
177
- See [How to Contribute to Open Source][open-source-guide-contribute].
200
+ See [How to Contribute to Open Source][opensource-guide-contribute].
178
201
 
179
202
  ## License
180
203
 
@@ -182,14 +205,16 @@ See [How to Contribute to Open Source][open-source-guide-contribute].
182
205
 
183
206
  <!-- Definitions -->
184
207
 
185
- [api-load-plugin]: #loadpluginname-options
208
+ [algorithm]: #algorithm
186
209
 
187
210
  [api-load-options]: #loadoptions
188
211
 
189
- [api-resolve-plugin]: #resolvepluginname-options
212
+ [api-load-plugin]: #loadpluginname-options
190
213
 
191
214
  [api-resolve-options]: #resolveoptions
192
215
 
216
+ [api-resolve-plugin]: #resolvepluginname-options
217
+
193
218
  [badge-build-image]: https://github.com/wooorm/load-plugin/workflows/main/badge.svg
194
219
 
195
220
  [badge-build-url]: https://github.com/wooorm/load-plugin/actions
@@ -210,15 +235,15 @@ See [How to Contribute to Open Source][open-source-guide-contribute].
210
235
 
211
236
  [github-nvm]: https://github.com/nvm-sh/nvm
212
237
 
213
- [nodejs-resolution-algo]: https://nodejs.org/api/esm.html#esm_resolution_algorithm
238
+ [nodejs-esm-resolution]: https://nodejs.org/api/esm.html#esm_resolution_algorithm
214
239
 
215
- [npm-install]: https://docs.npmjs.com/cli/install
240
+ [npmjs-install]: https://docs.npmjs.com/cli/install
216
241
 
217
- [npm-node-modules]: https://docs.npmjs.com/cli/v10/configuring-npm/folders#node-modules
242
+ [npmjs-node-modules]: https://docs.npmjs.com/cli/configuring-npm/folders#node-modules
218
243
 
219
- [npm-prefix]: https://docs.npmjs.com/cli/v10/using-npm/config
244
+ [npmjs-prefix]: https://docs.npmjs.com/cli/using-npm/config
220
245
 
221
- [open-source-guide-contribute]: https://opensource.guide/how-to-contribute/
246
+ [opensource-guide-contribute]: https://opensource.guide/how-to-contribute/
222
247
 
223
248
  [typescript]: https://www.typescriptlang.org
224
249