Giter Club home page Giter Club logo

broccoli-plugin's Introduction

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 = fs.readFileSync(`${this.inputPaths[2]}/foo.txt`);
    const output = someCompiler(input);

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

Reference

new Plugin(inputNodes, options)

Call this base class constructor from your subclass constructor.

  • inputNodes: An array of node objects that this plugin will read from. Nodes are usually other plugin instances; they were formerly known as "trees".

  • options

    • name: The name of this plugin class. Defaults to this.constructor.name.
    • annotation: A descriptive annotation. Useful for debugging, to tell multiple instances of the same plugin apart.
    • persistentOutput: If true, the output directory is not automatically emptied between builds.
    • needsCache : If true, a cache directory is created automatically and the path is set at cachePath. If false, a cache directory is not created and this.cachePath is undefined. Defaults to true.
    • volatile : If true, memoization will not be applied and the build method will always be called regardless if the inputNodes have changed. Defaults to false.

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:

  • this.inputPaths: An array of paths on disk corresponding to each node in inputNodes. Your plugin will read files from these paths.

  • this.outputPath: The path on disk corresponding to this plugin instance (this node). Your plugin will write files to this path. This directory is emptied by Broccoli before each build, unless the persistentOutput options is true.

  • this.cachePath: The path on disk to an auxiliary cache directory. Use this to store files that you want preserved between builds. This directory will only be deleted when Broccoli exits. If a cache directory is not needed, set needsCache to false when calling broccoli-plugin constructor.

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.

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:

  • file: Path of the file in which the error occurred, relative to one of the inputPaths directories
  • treeDir: The path that file is relative to. Must be an element of this.inputPaths. (The name treeDir is for historical reasons.)
  • line: Line in which the error occurred (one-indexed)
  • column: Column in which the error occurred (zero-indexed)

broccoli-plugin's People

Contributors

joliss avatar krisselden avatar rwjblue avatar stefanpenner avatar thoov avatar turbo87 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.