Last Updated: February 25, 2016
· davidpelaez

Simple Broccoli filter plugin in CoffeeScript

Javascript can be a bloated and hard to read language to an extent that it can keep you from using it. Sometimes however, an equivalent piece of code in CoffeeScript can be enlightening to highlight the simplicity of something as nice as Broccoli.

I read @joliss CS filter in JS and then wrote this simple example of a filter in CS. I understand you can be a JS master in making sense of verbose code but for me ergonomics are king and it would be awesome if you can create something useful with this simple broccoli filter example.

Filter = require("broccoli-filter")

class MyFilter extends Filter

  extensions: ['filterable', 'myext]
  targetExtension: 'out'

  constructor: (@inputTree, @options={}) ->
    super inputTree, options
    @extensions = @options.extensions or @extensions
    @targetExtension = @options.targetExtension or @targetExtension

  processString: (string) ->
    return "result"

module.exports = (inputTree, options)->
  new MyFilter inputTree, options

Some things to note:

  • The constructor saves the inputTree and options as an instance variables so they remain accessible even when the constructor has finished.
  • Note that extensions and targetExtensions can be changed through the filter's options.
  • Finally the thing that you export is a simple factory, just a function that creates an instance of your filter with the passed params.