load-plugin
Load a submodule, plugin, or file
Last updated 2 years ago by wooorm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install load-plugin 
SYNC missed versions from official npm registry.

load-plugin

Build Coverage Downloads

Load submodules, plugins, or files.

Contents

What is this?

This package is useful when you want to load plugins. It resolves things like Node.js does, but supports a prefix (e.g., when given a prefix remark and the user provided value gfm, it can find remark-gfm), can load from several places, and optionally global too.

When to use this?

This package is particularly useful when you want users to configure something with plugins. One example is remark-cli which can load remark plugins from configuration files.

Install

This package is ESM only. In Node.js (version 14.14+, 16.0+), install with npm:

npm install load-plugin

Use

Say we’re in this project (with dependencies installed):

import {loadPlugin, resolvePlugin} from 'load-plugin'

console.log(await resolvePlugin('lint', {prefix: 'remark'}))
// => '/Users/tilde/projects/oss/load-plugin/node_modules/remark-lint/index.js'

console.log(await resolvePlugin('validator-identifier', {prefix: '@babel/helper'}))
// => '/Users/tilde/Projects/oss/load-plugin/node_modules/@babel/helper-validator-identifier/lib/index.js'

console.log(await resolvePlugin('./index.js', {prefix: 'remark'}))
// => '/Users/tilde/projects/oss/load-plugin/index.js'

console.log(await loadPlugin('lint', {prefix: 'remark'}))
// => [Function: remarkLint]

API

This package exports the identifiers loadPlugin and resolvePlugin. There is no default export.

loadPlugin(name[, options])

Uses Node’s resolution algorithm (through import-meta-resolve) to load CJS and ESM packages and files to import name in each given cwd (and optionally the global node_modules directory).

If a prefix is given and name is not a path, $prefix-$name is also searched (preferring these over non-prefixed modules). If name starts with a scope (@scope/name), the prefix is applied after it: @scope/$prefix-name.

options

Configuration (optional).

options.prefix

Prefix to search for (string, optional).

options.cwd

Place or places to search from (string, Array<string>, default: process.cwd()).

options.global

Whether to look for name in global places (boolean, optional, defaults to whether global is detected). If this is nullish, load-plugin will detect if it’s currently running in global mode: either because it’s in Electron, or because a globally installed package is running it.

Note: Electron runs its own version of Node instead of your system Node. That means global packages cannot be found, unless you’ve set-up a prefix in your .npmrc or are using nvm to manage your system node.

options.key

Identifier to take from the exports (string or false, default: 'default'). For example when given 'whatever', the value of export const whatever = 1 will be returned, when given 'default', the value of export default … is used, and when false the whole module object is returned.

Returns

Promise yielding the results of importing the first path that exists (Promise<unknown>). The promise rejects if importing an existing path fails, or if no existing path exists.

resolvePlugin(name[, options])

Search for name. Accepts the same parameters as loadPlugin (except key) but returns a promise resolving to an absolute URL (string) for name instead of importing it. Throws if name cannot be found.

Types

This package is fully typed with TypeScript. It exports the additional types ResolveOptions and LoadOptions.

Compatibility

This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. It also works in Deno and modern browsers.

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer

Current Tags

  • 5.1.0                                ...           latest (2 years ago)

15 Versions

  • 5.1.0                                ...           2 years ago
  • 5.0.0                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           4 years ago
  • 3.0.0                                ...           5 years ago
  • 2.3.1                                ...           6 years ago
  • 2.3.0                                ...           6 years ago
  • 2.2.2                                ...           7 years ago
  • 2.2.1                                ...           7 years ago
  • 2.2.0 [deprecated]           ...           7 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.1.1                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (9)
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |