2.0.0 • Published 5 years ago
@start/plugin v2.0.0
⚙️ plugin
Plugin creator.
Install
$ yarn add @start/pluginUsage
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 withfilesstructure: it's an array of{ path, data, map }objects, where:path– absolute file pathdata– file data as utf8 string, if anymap– Source Map object, if any
logMessage– any random message from pluginlogPath– current file path to indicate some kind of progressreporter– advanced prop which should be passed through if plugin operates other plugins, like sequence or watch