What is broccoli-persistent-filter?
broccoli-persistent-filter is a Broccoli plugin that provides a base class for creating filters that can cache their results to improve build performance. It is particularly useful for tasks that involve transforming files, such as transpiling, minifying, or linting.
What are broccoli-persistent-filter's main functionalities?
Basic Filtering
This feature allows you to create a custom filter by extending the `Filter` class and implementing the `processString` method. The `processString` method is where you define the transformation logic for the file contents.
const Filter = require('broccoli-persistent-filter');
class MyFilter extends Filter {
constructor(inputNode, options) {
super(inputNode, options);
}
processString(contents, relativePath) {
// Perform some transformation on the file contents
return contents.toUpperCase();
}
}
module.exports = MyFilter;
Caching
This feature demonstrates how to implement caching in your custom filter. By overriding the `cacheKeyProcessString` method, you can generate a cache key based on the file contents, which helps in reusing the cached results for unchanged files.
const Filter = require('broccoli-persistent-filter');
class MyFilter extends Filter {
constructor(inputNode, options) {
super(inputNode, options);
this.extensions = ['js'];
this.targetExtension = 'js';
}
processString(contents, relativePath) {
// Perform some transformation on the file contents
return contents.toUpperCase();
}
cacheKeyProcessString(contents, relativePath) {
// Generate a cache key based on the file contents
return contents;
}
}
module.exports = MyFilter;
Handling Multiple File Types
This feature shows how to handle multiple file types by setting the `extensions` property. The `targetExtension` property specifies the extension of the output files.
const Filter = require('broccoli-persistent-filter');
class MyFilter extends Filter {
constructor(inputNode, options) {
super(inputNode, options);
this.extensions = ['js', 'css'];
this.targetExtension = 'txt';
}
processString(contents, relativePath) {
// Perform some transformation on the file contents
return contents.toUpperCase();
}
}
module.exports = MyFilter;
Other packages similar to broccoli-persistent-filter
broccoli-filter
broccoli-filter is a base class for Broccoli plugins that process files. It is similar to broccoli-persistent-filter but does not include built-in caching mechanisms. Developers need to implement their own caching if required.
broccoli-plugin
broccoli-plugin is a lower-level base class for creating Broccoli plugins. It provides more flexibility but requires more boilerplate code compared to broccoli-persistent-filter. It does not include built-in filtering or caching functionalities.
broccoli-caching-writer
broccoli-caching-writer is another base class for Broccoli plugins that provides caching capabilities. It is similar to broccoli-persistent-filter but focuses more on writing files to the output directory efficiently.