What is broccoli-caching-writer?
The broccoli-caching-writer package is a Broccoli plugin that provides a base class for Broccoli plugins that need to cache their output. It helps in optimizing build times by caching the results of expensive operations and reusing them when the input hasn't changed.
What are broccoli-caching-writer's main functionalities?
Caching Output
This feature allows you to create a custom Broccoli plugin that caches its output. The `updateCache` method is where you perform your expensive operations, and the results are cached for future builds.
const CachingWriter = require('broccoli-caching-writer');
const fs = require('fs');
class MyCachingWriter extends CachingWriter {
updateCache(srcDir, destDir) {
// Perform expensive operations here
fs.writeFileSync(`${destDir}/output.txt`, 'Cached content');
}
}
module.exports = MyCachingWriter;
Handling Multiple Input Nodes
This feature allows you to handle multiple input nodes. The `updateCache` method receives an array of source directories, enabling you to process multiple inputs and cache their results.
const CachingWriter = require('broccoli-caching-writer');
const fs = require('fs');
class MultiInputCachingWriter extends CachingWriter {
constructor(inputNodes, options) {
super(inputNodes, options);
}
updateCache(srcDirs, destDir) {
// srcDirs is an array of input directories
srcDirs.forEach((srcDir, index) => {
fs.writeFileSync(`${destDir}/output${index}.txt`, `Cached content from ${srcDir}`);
});
}
}
module.exports = MultiInputCachingWriter;
Custom Cache Key
This feature allows you to define a custom cache key for your plugin. The `cacheKey` method generates a unique key that can be used to determine if the cache should be invalidated.
const CachingWriter = require('broccoli-caching-writer');
const fs = require('fs');
const crypto = require('crypto');
class CustomCacheKeyWriter extends CachingWriter {
constructor(inputNodes, options) {
super(inputNodes, options);
}
cacheKey() {
// Generate a custom cache key
return crypto.createHash('md5').update('custom-key').digest('hex');
}
updateCache(srcDir, destDir) {
fs.writeFileSync(`${destDir}/output.txt`, 'Cached content with custom key');
}
}
module.exports = CustomCacheKeyWriter;
Other packages similar to broccoli-caching-writer
broccoli-plugin
The broccoli-plugin package provides a base class for Broccoli plugins, similar to broccoli-caching-writer but without built-in caching functionality. It is more general-purpose and can be extended to create various types of Broccoli plugins.
broccoli-persistent-filter
The broccoli-persistent-filter package is another Broccoli plugin base class that provides persistent caching. It is designed for plugins that need to filter files and cache the results, similar to broccoli-caching-writer but with a focus on filtering operations.
broccoli-funnel
The broccoli-funnel package is used to funnel files from one Broccoli tree to another. While it doesn't provide caching, it is often used in conjunction with caching plugins to optimize build processes by selecting specific files or directories.
Broccoli Caching Writer
Adds a thin caching layer based on the computed hash of the input tree. If the input tree has changed,
the updateCache
method will be called, otherwise (input is the same) the results of the last updateCache
call will be used instead.
If you would prefer to perform your plugins work in a non-synchronous way, simply return a promise from updateCache
.
Documentation
new CachingWriter(inputTrees, options)
inputTrees
{Array of Trees | Single Tree}
Can either be a single tree, or an array of trees. If an array was specified, an array of source paths will be provided when
calling updateCache
.
Options
filterFromCache.include
{Array of RegExps}
An array of regular expressions that files and directories in the input tree must pass (match at least one pattern) in order to be included in the cache hash for rebuilds. In other words, a whitelist of patterns that identify which files and/or directories can trigger a rebuild.
Default: []
filterFromCache.exclude
{Array of RegExps}
An array of regular expressions that files and directories in the input tree cannot pass in order to be included in the cache hash for rebuilds. In other words, a blacklist of patterns that identify which files and/or directories will never trigger a rebuild.
Note, in the case when a file or directory matches both an include and exlude pattern, the exclude pattern wins
Default: []
Switching from broccoli-writer
If your broccoli plugin currently extends broccoli-writer
,
and you wish to extend broccoli-caching-writer
instead:
- Switch the constructor
- Require this module:
var CachingWriter = require('broccoli-caching-writer');
- Change the prototype to use
CachingWriter
: MyBroccoliWriter.prototype = Object.create(CachingWriter.prototype);
- In the constructor, ensure that you are calling
CachingWriter.apply(this, arguments);
.
- Switch
write
function for an updateCache
function.
- Switch the function signatures:
- From:
MyBroccoliWriter.prototype.write = function(readTree, destDir) {
- To:
MyBroccoliWriter.prototype.updateCache = function(srcDir, destDir) {
- Get rid of
readTree
, as srcPaths
(array of paths from input trees) is already provided:
- Code that looks like:
return readTree(this.inputTree).then(function (srcPaths) { /* Do the main processing */ });
- Simply extract the code,
/* Do the main processing */
, and get rid of the function wrapping it.
Inheritance
broccoli-caching-writer inherits from core-object to allow super simple
inheritance. You can still absolutely use the standard prototypal inheritance, but as you can see below there may be no
need.
Make an index.js
for your package:
var CachingWriter = require('broccoli-caching-writer');
module.exports = CachingWriter.extend({
init: function(inputTrees, options) {
},
updateCache: function(srcPaths, destDir) {
}
});
Then in a consuming Brocfile:
var MyFoo = require('my-foo');
var tree = new MyFoo([someInput], { some: 'options' });
ZOMG!!! TESTS?!?!!?
I know, right?
Running the tests:
npm install
npm test
License
This project is distributed under the MIT license.