0.6.3 • Published 4 years ago

@financial-times/dotcom-page-kit-pluggable v0.6.3

Weekly downloads
2
License
MIT
Repository
-
Last release
4 years ago

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

A lightweight library for facilitating pluggability within apps and libraries.

Installation

This package is compatible with Node 8+ 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:

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.

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:

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)
  }))
})
0.6.5

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.4.5

4 years ago

0.5.4

4 years ago

0.4.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.5.0-beta.3

4 years ago

0.5.0-beta.1

4 years ago

0.5.0-beta.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.1-test.2

5 years ago

0.0.1-test.1

5 years ago