Awesome
broccoli-multifilter
This is a helper base class for Broccoli plugins similar to
broccoli-filter. The
broccoli-filter base class maps 1 input file into 1 output file at a time. As a
result, plugins for compilers that have include
directives to include other
dependent files cannot use broccoli-filter, since broccoli-filter's caching
logic cannot accomodate dependencies. By contrast, broccoli-multifilter allows
you to provide a list of dependencies for each input file, thereby mapping m
input files into n output files at a time.
Installation
npm install --save broccoli-multifilter
This package requires Node 6 or newer.
Usage example
let Multifilter = require("broccoli-multifilter");
class MyPlugin extends Multifilter {
build() {
let inputFiles = ["foo.js"];
return this.buildAndCache(
inputFiles,
(inputFile, outputDirectory) => {
let fullInputPath = path.join(this.inputPaths[0], inputFile);
let fullOutputPath = path.join(outputDirectory, inputFile);
// Compile into the outputDirectory
fs.copyFileSync(fullInputPath, fullOutputPath);
return {
dependencies: [
fullInputPath,
path.join(this.inputPaths[0], "included.js")
]
};
}
);
}
}
The file "foo.js" will be rebuilt using the callback whenever "foo.js" or "included.js" change.
Reference
-
class Multifilter
: A Plugin subclass that implements a singlethis.buildAndCache
helper method, which you should call frombuild
.-
Multifilter.buildAndCache(inputFilePaths, callback)
: For eachinputFilePat
, callcallback
in sequence. This returns a promise, so be sure toreturn
its return value frombuild
.-
inputFilePaths
: An array of strings identifying input files.While you will typically use input file paths relative to
this.inputPaths[0]
,Multifilter
makes no assumption about the meaning of these strings and simply treats them as opaque identifiers. -
callback(inputFilePath, outputDirectory)
: Your callback function to rebuild the file identified byinputFilePath
and place the output file(s) intooutputDirectory
. It is important that you write intooutputDirectory
and not intothis.outputPath
.Every input file will get its own
outputDirectory
, which will be empty on each rebuild. After calling your callbacks for eachinputFilePath
,buildAndCache
will merge theoutputDirectories
for allinputFilePaths
into the plugin's output (this.outputPath
), similar to broccoli-merge-trees with{ overwrite: false }
.The
callback
function must return an object (or a promise to an object) of the form{ dependencies: dependencyPaths }
where
dependencyPaths
is an array of paths to each file or directory that the compilation forinputFilePath
depends on.On rebuild,
buildAndCache
may re-use the output from the previous build instead of callingcallback
, provided that none of the files or directories (and their contents, recursively) identified bydependencyPaths
have changed.You must include the main input file itself in
dependencyPaths
. Therefore,dependencyPaths
must always be non-empty. For example, if eachinputFilePath
is the relative path to an input file (as is typical), you might return{ dependencies: [ [path.join(this.inputPaths[0], inputFilePath)].concat( dependenciesReturnedByTheCompiler ) ] }
-
-