Giter Site home page Giter Site logo

load-plugin's Introduction

load-plugin

Build Coverage Downloads

Load a submodule, plugin, or file.

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 (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 16+), 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'}))
// => 'file:///Users/tilde/Projects/oss/load-plugin/node_modules/remark-lint/index.js'

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

console.log(await resolvePlugin('./index.js', {prefix: 'remark'}))
// => 'file:///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.

It exports the TypeScript types LoadOptions and ResolveOptions.

loadPlugin(name[, options])

Import name from from (and optionally the global node_modules directory).

Uses the Node.js resolution algorithm (through import-meta-resolve) to resolve CJS and ESM packages and files.

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.

Parameters
  • name (string) — specifier
  • options (LoadOptions, optional) — configuration
Returns

Promise to a whole module or specific export (Promise<unknown>).

resolvePlugin(name[, options])

Resolve name from from.

Parameters
  • name (string) — specifier
  • options (ResolveOptions, optional) — configuration
Returns

Promise to a file URL (Promise<string>).

LoadOptions

Configuration for loadPlugin (TypeScript type).

This type extends ResolveOptions and adds:

Fields
  • key (boolean or string, default: 'default') — identifier to take from the exports; for example when given 'x', the value of export const x = 1 will be returned; when given 'default', the value of export default … is used, and when false the whole module object is returned

ResolveOptions

Configuration for resolvePlugin (TypeScript type).

Fields
  • from (Array<URL | string> | URL | string, optional) — place or places to search from; defaults to the current working directory
  • global (boolean, default: whether global is detected) — whether to look for name in global places; 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 that Electron runs its own version of Node instead of your system Node, meaning global packages cannot be found, unless you’ve set-up a prefix in your .npmrc or are using nvm to manage your system node
  • prefix (string, optional) — prefix to search for

Compatibility

This projects is compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, load-plugin@6, compatible with Node.js 16.

Security

This package reads the file system and imports things into Node.js.

Contribute

Yes please! See How to Contribute to Open Source.

License

MIT © Titus Wormer

load-plugin's People

Contributors

greenkeeperio-bot avatar remcohaszing avatar wooorm avatar zobzn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

load-plugin's Issues

Wrong detection of npm prefix on windows

Hi. You rely on npm-prefix package for detecting npm prefix.
But this package is very outdated I suppose.
At least it may give wrong results on windows.

By default nodejs with npm are installing into 'system' folder:

  • C:\Program Files\nodejs\node.exe
  • C:\Program Files\nodejs\npm.cmd
  • C:\Program Files\nodejs\node_modules\npm\...

In this situation the npm-prefix package correctly returns C:\Program Files\nodejs.

But... all consequent global installs go to user's folder (without requirements of privileged access to system folders):
C:\Users\{username}\AppData\Roaming\npm\node_modules\...
npm i -g npm becomes C:\Users\{username}\AppData\Roaming\npm\node_modules\npm\...
npm i -g remark-cli becomes C:\Users\{username}\AppData\Roaming\npm\node_modules\remark-cli\...
npm i -g remark-html becomes C:\Users\{username}\AppData\Roaming\npm\node_modules\remark-html\...

And here arises the problem with npm-prefix.
It still returns C:\Program Files\nodejs but not the actual C:\Users\{username}\AppData\Roaming\npm.
Eventually your load-plugin searches for modules in a wrong directory.

So I suggest you to find another solution.
Some other package or maybe just npm config get prefix?

Thank you.

Yarn PnP compatibility?

I believe the issue described here (remarkjs/remark-language-server#6 (comment)) is a result of an assumption that packages are stored in a node_modules directory.

I tried running resolvePlugin and loadPlugin against my project and got the following error

% yarn node test.mjs

Error: Cannot find package 'lint' imported from /<mycwd>/
    at new NodeError (file:///<mycwd>/.yarn/cache/import-meta-resolve-npm-2.1.0-fcf1208127-4554ea5e2d.zip/node_modules/import-meta-resolve/lib/errors.js:276:5)
    at packageResolve (file:///<mycwd>/.yarn/cache/import-meta-resolve-npm-2.1.0-fcf1208127-4554ea5e2d.zip/node_modules/import-meta-resolve/lib/resolve.js:1060:9)
    at moduleResolve (file:///<mycwd>/.yarn/cache/import-meta-resolve-npm-2.1.0-fcf1208127-4554ea5e2d.zip/node_modules/import-meta-resolve/lib/resolve.js:1117:20)
    at defaultResolve (file:///<mycwd>/.yarn/cache/import-meta-resolve-npm-2.1.0-fcf1208127-4554ea5e2d.zip/node_modules/import-meta-resolve/lib/resolve.js:1266:15)
    at resolve (file:///<mycwd>/.yarn/cache/import-meta-resolve-npm-2.1.0-fcf1208127-4554ea5e2d.zip/node_modules/import-meta-resolve/index.js:29:12)
    at attempt (file:///<mycwd>/.yarn/cache/load-plugin-npm-5.1.0-5695bc6a89-d450c9a083.zip/node_modules/load-plugin/index.js:167:15)
    at resolvePlugin (file:///<mycwd>/.yarn/cache/load-plugin-npm-5.1.0-5695bc6a89-d450c9a083.zip/node_modules/load-plugin/index.js:148:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async file:///<mycwd>/test.mjs:5:17 {
  code: 'ERR_MODULE_NOT_FOUND'
}

where test.mjs is

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

(async () => {
  try {
    console.log(await resolvePlugin('lint', { prefix: 'remark' }));
  } catch (error) {
    console.error(error);
  }
})();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.