Skip to content

Latest commit

 

History

History
152 lines (121 loc) · 2.89 KB

.verb.md

File metadata and controls

152 lines (121 loc) · 2.89 KB

Usage

const handle = require('{%= name %}');

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('{%= name %}');
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('{%= name %}');
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')) 
});