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

assemble-handle

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assemble-handle

Assemble pipeline plugin for handling custom middleware stages.

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29K
decreased by-3.42%
Maintainers
2
Weekly downloads
 
Created
Source

assemble-handle NPM version NPM monthly downloads NPM total downloads Linux Build Status

Assemble pipeline plugin for handling custom middleware stages.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save assemble-handle

Usage

const handle = require('assemble-handle');

handle

Handle middleware for the given middleware "stage".

app.task('default', function() {
  return app.src('*.js')
    .pipe(handle(app, 'handlerName')) //<= handle middleware
    .pipe(app.dest('foo'))
});

Example

const assemble = require('assemble');
const handle = require('assemble-handle');
const app = assemble();

/**
 * create some middleware "stages"
 */

app.handler('onStream');
app.handler('preWrite');
app.handler('postWrite');

/**
 * Create middleware
 */

app.onStream(/\.(js|css)$/, function(file, next) {
  // lint javascript
  next();
});

app.preWrite(/\.(jpg|png)$/, function(file, next) {
  // minify images
  next();
});

app.postWrite(/./, function(file, next) {
  // create files tree or something
  next();
});

/**
 * Run (handle) the middleware 
 */

app.task('site', function() {
  return app.src('assets/**/*.*')
    .pipe(handle(app, 'onStream'))  // handle onStream
    .pipe(handle(app, 'preWrite'))  // handle preWrite
    .pipe(app.dest('site/'));
    .pipe(handle(app, 'postWrite')) // handle postWrite
});

handle.once

A .once method is exposed, which has the same exact behavior as the main function, but will ensure that middleware is only handled once for a given "stage".

Example

For example the given middleware will only run once.

const assemble = require('assemble-core');
const handle = require('assemble-handle');
const app = assemble();

app.handler('onFoo');

app.onFoo(/./, function(file, next) {
  file.count = file.count || 0;
  file.count++;
  next();
});

app.task('handle-once', function(cb) {
  let files = [];
  app.src('test/**/*.*')
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .pipe(handle.once(app, 'onFoo'))
    .on('data', function(file) {
      files.push(file);
    })
    .pipe(app.dest('test/actual'))
    .on('end', function() {
      console.log(files[0].count);
      //=> 1
      cb();
    });
});

app.task('handle', function(cb) {
  let files = [];
  app.src('test/**/*.*')
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .pipe(handle(app, 'onFoo'))
    .on('data', function(file) {
      files.push(file);
    })
    .pipe(app.dest('test/actual'))
    .on('end', function() {
      console.log(files[0].count);
      //=> 5
      cb();
    });
});

Custom handlers

Create custom middleware handlers.

app.handler('onFoo');

This adds an .onFoo method to the instance:

app.onFoo(/\.hbs$/, function(file, next) {
  // do stuff to file
  next();
});

All .onFoo middleware will now run when the onFoo handler is called:

app.task('default', function() {
  return app.src('*.hbs')

    // call the `onFoo` handler
    .pipe(handle(app, 'onFoo')) 
});

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

  • assemble-core: The core assemble application with no presets or defaults. All configuration is left to the… more | homepage
  • assemble-fs: Light wrapper for vinyl-fs to add streams support in a way that plays nice with… more | homepage
  • assemble-streams: Assemble pipeline plugin for pushing views into a vinyl stream. | homepage
  • assemble: Get the rocks out of your socks! Assemble makes you fast at creating web projects… more | homepage

Contributors

CommitsContributor
17jonschlinkert
3doowb

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on December 11, 2018.

Keywords

FAQs

Package last updated on 12 Dec 2018

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