Home

Awesome

The Broccoli Plugin Base Class

Build Status Build status

Example Usage

const Plugin = require('broccoli-plugin');

class MyPlugin extends Plugin {
  constructor(inputNodes, options = {}) {
    super(inputNodes, {
      annotation: options.annotation,
      // see `options` in the below README to see a full list of constructor options
    });
  }

  build() {
    // Read files from this.inputPaths, and write files to this.outputPath.
    // Silly example:

    // Read 'foo.txt' from the third input node
    const input = this.input.readFileSync(`foo.txt`);
    const output = someCompiler(input);

    // Write to 'bar.txt' in this node's output
    this.output.writeFileSync(`bar.txt`, output);
  }
}

Reference

new Plugin(inputNodes, options)

Call this base class constructor from your subclass constructor.

Plugin.prototype.build()

Override this method in your subclass. It will be called on each (re-)build.

This function will typically access the following read-only properties:

All paths stay the same between builds.

To perform asynchronous work, return a promise. The promise's eventual value is ignored (typically null).

To report a compile error, throw it or return a rejected promise. Also see section "Error Objects" below.

If the trackInputChanges option was set to true, an object will be passed to the build method with the shape of:

{
  changedNodes: [true, true, ...]
}

This array contain a booleans corresponding to each input node as to whether or not that node changed since the last rebuild. For the initial build all values in the array will be true.

Plugin.prototype.getCallbackObject()

Advanced usage only.

Return the object on which Broccoli will call obj.build(). Called once after instantiation. By default, returns this. Plugins do not usually need to override this, but it can be useful for base classes that other plugins in turn derive from, such as broccoli-caching-writer.

For example, to intercept .build() calls, you might return { build: this.buildWrapper.bind(this) }. Or, to hand off the plugin implementation to a completely separate object: return new MyPluginWorker(this.inputPaths, this.outputPath, this.cachePath), where MyPluginWorker provides a .build method.

Error Objects

To help with displaying clear error messages for build errors, error objects may have the following optional properties in addition to the standard message property:

Plugin.prototype.input

An api which enables a plugin to easily read from one or more input directories ergonomically and safely.

Note: We recommend users stop using this.inputPaths and instead rely on this.input. Our plan at present is to strongly consider deprecation of this.inputPaths once this.input has had time to bake.

this.input's features:

Example:

// old
fs.readFileSync(this.inputPaths[0] + '/file.txt');

// new (merged): Most Common
this.input.readFileSync('file.txt');

// new (indexed): For when you need to disambiguate between inputs.
this.input.at(0).readFileSync('file.txt);

// ReadOnly
this.input.writeFileSync // throws error

List of Methods

Read more about input here

Note: input will be available only after the build starts.

Plugin.prototype.output

An api which enables a plugin to easily write to the output directory ergonomically and safely.

Note: We recommend users stop using this.outputPath and instead rely on this.output. Our plan at present is to strongly consider deprecation of this.outputPath once this.output has had time to bake.

this.output's features:

Ex:

// old
fs.writeFileSync(this.outputPath + '/file.txt', 'text');

// new
this.output.writeFileSync('file.txt', 'text');

List of Methods

Read more about APIs present in output here.

Note: output will be available only after the build starts.