Skip to content
This repository has been archived by the owner on Mar 27, 2020. It is now read-only.

Latest commit

 

History

History

plugin

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

⚙️ plugin

npm linux windows coverage deps

Plugin creator.

Install

$ yarn add @start/plugin
# or
$ npm install --save-dev @start/plugin

Usage

Types

See src/index.ts for all the types. It's (usually) better than any words.

Example

import plugin from '@start/plugin'

export default plugin('noop', () => () => {})
import plugin from '@start/plugin'

export default plugin('foo', ({ logPath }) => async ({ files }) => {
  const { default: fooTransform } = await import('foo-lib')

  return {
    files: await Promise.all(
      files.map(async (file) =>
        const { transformedData, sourceMap } = fooTransform(file.path)

        logPath(file.path)

        return {
          path: file.path,
          data: transformedData,
          map: sourceMap
        }
      )
    )
  }
})
import plugin from '@start/plugin'

export default (barOptions) =>
  plugin('bar', ({ logMessage }) => async () => {
    const { default: barCheck } = await import('bar-lib')

    const barResult = barCheck(files, barOptions)

    if (barResult.issues.length === 0) {
      logMessage('¯\\_(ツ)_/¯')
    }
  })

Notes

  • Dynamic imports – it's a good idea to "lazyload" dependencies inside of a plugin function instead of importing it on top of a file
  • Plugin can return whatever "props" object or just nothing, and that output will extend an input props as an overall plugin result
  • files – many plugins works with files structure: it's an array of { path, data, map } objects, where:
    • path – absolute file path
    • data – file data as utf8 string, if any
    • mapSource Map object, if any
  • logMessage – any random message from plugin
  • logPath – current file path to indicate some kind of progress
  • reporter – advanced prop which should be passed through if plugin operates other plugins, like sequence or watch