Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

broccoli-plugin

Package Overview
Dependencies
Maintainers
4
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

broccoli-plugin

Base class for all Broccoli plugins

  • 4.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
increased by7.17%
Maintainers
4
Weekly downloads
 
Created

What is broccoli-plugin?

The broccoli-plugin npm package is a base class for Broccoli plugins. Broccoli is a JavaScript build tool, and this package allows developers to create custom plugins for their build pipelines. It provides a structured way to define input and output nodes, manage file transformations, and handle build errors.

What are broccoli-plugin's main functionalities?

Creating a Basic Plugin

This code demonstrates how to create a basic Broccoli plugin that reads a file, transforms its content to uppercase, and writes the result to the output directory.

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

class MyPlugin extends Plugin {
  constructor(inputNodes, options) {
    super(inputNodes, options);
  }

  build() {
    const inputPath = this.inputPaths[0];
    const outputPath = this.outputPath;
    const inputFile = `${inputPath}/input.txt`;
    const outputFile = `${outputPath}/output.txt`;

    const content = fs.readFileSync(inputFile, 'utf8');
    const transformedContent = content.toUpperCase();
    fs.writeFileSync(outputFile, transformedContent);
  }
}

module.exports = MyPlugin;

Handling Multiple Input Nodes

This example shows how to handle multiple input nodes in a Broccoli plugin. It reads files from two different input directories, combines their contents, and writes the result to the output directory.

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

class MultiInputPlugin extends Plugin {
  constructor(inputNodes, options) {
    super(inputNodes, options);
  }

  build() {
    const inputPath1 = this.inputPaths[0];
    const inputPath2 = this.inputPaths[1];
    const outputPath = this.outputPath;

    const content1 = fs.readFileSync(`${inputPath1}/file1.txt`, 'utf8');
    const content2 = fs.readFileSync(`${inputPath2}/file2.txt`, 'utf8');
    const combinedContent = content1 + '\n' + content2;

    fs.writeFileSync(`${outputPath}/combined.txt`, combinedContent);
  }
}

module.exports = MultiInputPlugin;

Error Handling in Plugins

This code demonstrates how to handle errors in a Broccoli plugin. It checks if the input file exists and throws an error if it does not. Any errors encountered during the build process are caught and logged.

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

class ErrorHandlingPlugin extends Plugin {
  constructor(inputNodes, options) {
    super(inputNodes, options);
  }

  build() {
    try {
      const inputPath = this.inputPaths[0];
      const outputPath = this.outputPath;
      const inputFile = `${inputPath}/input.txt`;
      const outputFile = `${outputPath}/output.txt`;

      if (!fs.existsSync(inputFile)) {
        throw new Error('Input file does not exist');
      }

      const content = fs.readFileSync(inputFile, 'utf8');
      const transformedContent = content.toUpperCase();
      fs.writeFileSync(outputFile, transformedContent);
    } catch (error) {
      console.error('Build error:', error);
    }
  }
}

module.exports = ErrorHandlingPlugin;

Other packages similar to broccoli-plugin

Keywords

FAQs

Package last updated on 03 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc