# @financial-times/dotcom-page-kit-pluggable

> A lightweight library for facilitating pluggability within apps and libraries.

Latest version **0.6.3** (published 2020-03-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @financial-times/dotcom-page-kit-pluggable
pnpm add @financial-times/dotcom-page-kit-pluggable
yarn add @financial-times/dotcom-page-kit-pluggable
bun add @financial-times/dotcom-page-kit-pluggable
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.3 |
| Published | 2020-03-18 |
| First published | 2019-07-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 12.0.0 |
| Dependencies | 1 |
| Unpacked size | 41 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | aendrew, axgy, bobhaslett, briggsc, chee, conor-mullen, ft-internal-products, i-like-robots, jakedchampion, joannaskao, mattandrews, notlee, quarterto, robinmarr-ft, rowanmanning, sdbernard, tatiana.stantonian, the-ft |

## Links

- npm: https://www.npmjs.com/package/@financial-times/dotcom-page-kit-pluggable
- Homepage: https://github.com/Financial-Times/dotcom-page-kit/tree/master/packages/dotcom-page-kit-pluggable
- npm.io page: https://npm.io/package/@financial-times/dotcom-page-kit-pluggable

## Dependencies (1)

- [lodash.mergewith](https://npm.io/package/lodash.mergewith.md) ^4.6.1

## Recent versions

- 0.6.3 (latest) — 2020-03-18
- 0.6.5 (maintenance) — 2020-04-16
- 0.5.0-beta.3 (pre-release) — 2019-11-28
- 0.6.2 — 2020-02-24
- 0.6.1 — 2020-02-11
- 0.6.0 — 2020-02-10
- 0.5.8 — 2020-01-27
- 0.5.7 — 2020-01-22
- 0.5.6 — 2020-01-22
- 0.5.5 — 2020-01-21
- 0.4.5 — 2020-01-10
- 0.5.4 — 2020-01-10
- 0.4.4 — 2019-12-20
- 0.5.3 — 2019-12-20
- 0.5.2 — 2019-12-13
- … 28 more at https://npm.io/package/@financial-times/dotcom-page-kit-pluggable/versions

## README

# @financial-times/dotcom-page-kit-pluggable

A lightweight library for facilitating pluggability within apps and libraries.

## Installation

This package is compatible with Node 12+ and is distributed via npm

```
npm install --save-dev @financial-times/dotcom-page-kit-pluggable
```

## General Usage

At the center of this package, is the `Pluggable` class. It is the class that is meant to either be extended (i.e., via class inheritance) or instantiated directly to provide the functionality required to facilitate pluggability within apps and libraries. An instance of this class can be used to register plugins / resource handlers, as well as to publish resources to the registered plugins / resource handlers for possible amendment, as seen in the following example:

```js
import { Pluggable } from '@financial-times/dotcom-page-kit-pluggable'

const plugin = ({ on }) => {
  on('person', ({ resource: person }) => {
    person.name = 'Jack'
  })
}

function greetPerson({ publish }: Pluggable) {
  const person = { name: 'John' }
  publish('person', person)
  return `Hello ${person.name}`
}

const pluggable = new Pluggable().with(plugin)
const result = greetPerson(pluggable)

expect(result).toBe('Hello Jack')
```

## Concepts

### Resource

A resource is a value that will be used by the app to determine how to proceed. It is expected that altering this value would in some way also alter how the app behaves.

### Resource Handler

A resource handler is a function that will be used to amend the resource.

### Resource Hook

A resource hook is a string value that uniquely represents the resource within the system. So for instance, if the resource is a webpack config, then the hook can be called `webpackConfig`

### Plugin

A plugin is a function that accepts a `Pluggable` instance, and then registers resource handlers on it.

```js
const plugin = (pluggable) => {
  pluggable.on('foo', handlerFn)
}
```

## The `Pluggable` Class

#### Methods

##### `.on(hook: string, handler: Function)`

Registers a resource handler with the `Pluggable` instance.

##### `.publish(hook: string, resource: any)`

Publishes a resource to the registered handlers for potential amendment.

##### `.registerPlugins(plugins: string, resource: any)`

#### Properties

##### `.alias`

Sets the alias of the `Pluggable` instance. The alias is used to allow for accessing the instance via a more appropriate name than `pluggable` when the instance is destructured. It is also used to alias the `Pluggable`
instance on the args that are supplied to resource handlers, as follows:

```ts
class CliContext extends Pluggable {
  alias = 'cli'
  args = {}
  options = {}
}

function getWebpackConfig({ publish, cli }: CliContext) {
  const babelConfig = getBabelConfig(cli)
  const webpackConfig = { babelConfig }
  publish('webpackConfig', webpackConfig)
  return webpackConfig
}

const plugin = ({on} => {
  on('webpackConfig', ({ cli }) => ({
    cssRule: getCssRule(cli)
  }))
})
```

---
_Source: https://npm.io/package/@financial-times/dotcom-page-kit-pluggable · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
