Normally, unified plugins receive upon attaching two arguments: processor
(the Processor
it’s attached to) and options
(an
Object
users can provide to configure the plugin).
If a plugin is attached by unified-engine, a third argument is given:
fileSet
.
The following example processes readme.md
and uses a plugin that adds a
“completer” and another file (history.md
).
import {engine} from 'unified-engine'
import {remark} from 'remark'
// Ensure the completer runs once per file-set.
completer.pluginId = 'some-plugin-id'
engine(
{
processor: remark(),
plugins: [plugin],
files: ['readme.md']
},
done
)
function done(error) {
if (error) throw error
}
function plugin(options, set) {
set.use(completer)
set.add('history.md')
}
function completer(set) {
console.log('done:', set.valueOf().map(path))
}
function path(file) {
return file.path
}
Yields:
done: [ 'readme.md', 'history.md' ]
readme.md: no issues found
Note that history.md
is not reported: only files given by the user are
reported (or written).
Internally, a fileSet
is created to process multiple files through unified
processors.
This set, containing all files, is exposed to plugins as an argument to the
attacher.
Access the files in a set.
Returns a list of VFile
s being processed.
Add a file to be processed. The given file is processed like other files with a few differences. The added files are:
- Ignored when their file path is already added
- Never written to the file system or
streamOut
- Not reported for
Returns self.
filePath
(string
) — path to virtual filefile
(VFile
) — virtual file
Attach a completer
to a middleware pipeline which runs when all
files are transformed (before compilation).
Returns self.
Function called when all files are processed.
If an error occurs (either because it’s thrown, returned, rejected, or passed to
next
), no further completers run and all files fail.
Error
Promise
— if a promise is returned, the function is asynchronous, and must be resolved (with nothing) or rejected (with anError
)
pluginId
(string
) — plugins specified through various mechanisms are attached to a newprocessor
for each file. If acompleter
isuse
d multiple times, it is called multiple times as well. To prevent completers from attaching multiple times, specify apluginId
. This will ensure only one completer perpluginId
is added.
If the signature of a completer includes next
(second argument), the function
may finish asynchronous, and must call next()
.
error
(Error
, optional) — fatal error