npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-plugin

v12.0.1

Published

Generic plugin loader functionality for Node.js frameworks.

Downloads

43,535

Readme

use-plugin

Npm Travis Coveralls Maintainability DeepScan grade

Generic plugin loader functionality for Node.js frameworks.

For use in framework modules to provide a plugin mechanism for extensions. While a simple require in calling code is a good start, this plugin provides some convenience abstractions over vanilla requires so that you can offer a more user-friendly interface.

Support

If you're using this module, feel free to contact us on twitter if you have any questions! :) @senecajs

See the seneca module for an example of practical usage.

Quick example

// myframework.js
module.exports = function() {
  var use = require('use-plugin')({prefix:'foo',module:module})

  return {
    use: function( plugin_name ) {
      var plugin_description == use(plugin_name)
      
      // call the init function to init the plugin
      plugin_description.init()
    }
  }
}

// callingcode.js
var fm = require('myframework')

// this will try to load:
// 'bar', 'foo-bar', './foo', './foo-bar'
// against the framework module, and then the callingcode module
// nice error messages are thrown if there are problems
fm.use('bar')

Install

npm install use-plugin

There's an npm module page for use-plugin.

Usage

The module provides a builder function that you call with your desired options. In particular, you should always set your module, as above.

The builder function returns a plugin loader function that you can use inside your framework. Calling the loader function returns an object with properties that describe the plugin.

In particular, the point of this module is to resolve (via require), the init function of the plugin, so that you can call it in your framework.

Plugins can be loaded in the following ways:

  • By name: fm.use('bar')
  • By name with options: fm.use('bar', {color:'red'})
  • As a function: fm.use(function(){...})
  • As a named function: fm.use(function bar(){...})
  • As a (named) function with options: fm.use(function bar(){...}, {color:'red'})
  • As an object: fm.use({name:'bar', init:function(){...}})
  • As a require: fm.use( require('./bar.js' ) )

When loaded as an Object, you must provide at least the name and init function. When loaded as a require note that the returned value can be any of string, function or object, to which the same rules apply. In particular, you need to explicitly provide a name property if you want an explicit name.

Note that plugins cannot have the same names as builtin Node.js modules. You can however change the list of builtin Node.js module names using the system_modules option.

Plugin Name Resolution

The name of the plugin is determined by the following procedure:

  • Plugin specified as string: the given string.
  • Plugin specified as function: the Function object name, otherwise generate a name.
  • Plugin specifed as object: the name property (which is required)

The plugin may also have a tag. This is a separate string that allows multiple plugins with the same name to be loaded, depending on your use-case. To provide a tag, use the name format: name$tag, or provide a tag property on the plugin object or function specification.

Options

When calling the builder function, you can pass:

  • module: The module variable of your framework.
  • prefix: An optional prefix for plugin names. Aallows your users to drop the prefix and use abbreviated plugin names.
  • builtin: Load builtin plugins first from this sub-folder of your framework.
  • errmsgprefix: Prefix string for error messages.

Plugin Description Object

If found, an object is returned describing your plugin:

  • name : The resolved name of the plugin.
  • tag : The resolved tag of the plugin.
  • init : The resolved initialization function of the plugin.
  • options : Any user-supplied plugin options provided as the second parameter to the created use function.
  • callback : Optional user-supplied callback provided as the third parameter to the created use function. You'll have to call this yourself.
  • history : The actual require search history. Array of {module:module-path, name:require-path}.
  • search : The require paths to search for.
  • modulepath : Module path where found.
  • requirepath : Require path where found.
  • err : Error object, if any.
  • found : Internal search entry details.