node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



***A structural design pattern for working with Gulp.***

Get rid of all the pipe's, gulp.src's, gulp.dest's etc.. make it more readable and coherent.

___ **A basic example in Coffeescript**

Given this extremely basic directory structure to keep it short:


we can write the following code to make it happen:

gulp = require 'gulp'
plug = do require 'gulp-load-plugins'
[ buildsource ] = require 'build-source'
source.root = 'source/'
build.root = 'build/'
    lib: [ 'js/lib/*.js''js/lib' ]
    index: [ 'index.html''',
    jade: [ 'jade/*.jade''html',
    css: [ 'css/*.css''css',
        [ plug.cssminkeepSpecialComments: 0 ]
    coffee: [ 'coffee/*.coffee''js',,
    minifyCoffee: [ 'coffee/*.coffee''js',,
        [ plug.concat'scripts.min.js' ],
    webserver: [ '^build/''!'[
            livereload : true
            open : true
            directoryListing : true
    watch: -> source.watchAll()
    default: -> build.tasks.startAll()

Several plugins were used in the example, below the list that you can paste straight into your package.json file if you want to play with it:

    "build-source": "latest",
    "coffee-script": "^1.8.0",
    "event-stream": "^3.1.7",
    "gulp": "^3.8.10",
    "gulp-autoprefixer": "^2.0.0",
    "gulp-coffee": "^2.2.0",
    "gulp-concat": "^2.4.2",
    "gulp-cssmin": "^0.1.6",
    "gulp-jade": "^0.10.0",
    "gulp-load-plugins": "^0.7.1",
    "gulp-minify-html": "^0.1.7",
    "gulp-ruby-sass": "^0.7.1",
    "gulp-uglify": "^1.0.1",
    "gulp-webserver": "^0.8.7"


object structure:

source : <function>
    root : <string>
    noRoot : <string>
    watchable : <boolean>
    watcher : <object>
    watch : <object>
build : <function>
    root : <string>
    task : <object>
        src : <string>/<array>
        dest : <string>
        plugs : <array>
    tasks : <function>
        all : <array>
        ignore : <array>
        ignored : <boolean>
      startAll : <function>
    source : <object>



<gulp> source( <string>/<array> paths )

Returns a gulp.src with path(s) mapped relative to source.root.


<string> source.root

Holds the source root path. Defaults to '', which means having no effect, but can be set to any base path you prefer.


<string> source.noRoot

noRoot can be prepended to a src string to momentary prevent prepending source.root. Only necessary if source.root is set (default).

noRoot defaults to '^'. I chose the caret because it is also used in regexp to denote that the following characters need to be the start of the string, or the base path in our case. You could set noRoot to another (single!) character, although I cannot find one good reason to do that.


<boolean> source.watchable( <string> id )

Internally this relates to the watchAll method. All id's in build.tasks.ignore are not watchable, as well as the 'default' and 'watch' tasks of course.


<object> source.watcher

Holds all watchers that were set with

<> <object> tasks )

Uses to set multiple listeners in one call. source.watcher[ path ] holds the listener for that specific path.


<function> source.watchAll( )

Runs for all watchable build.task id's.



<function> build( <string> path )

Returns a build function that writes to the given path ralative to build.root.

toLib= build 'build/js/lib'
    default: -> toLib 'lib/*.js'
# copies all .js files from ./js/lib to ./build/js/lib 


<string> build.root

Holds the build root path. Defaults to '', which means having no effect, but can be set to any base path you prefer.

build.task[ id ]

<function> build.task

build.task[ id ] holds the actual method that runs the gulp.task with id. Besides you can manually run the task if needed, there are also three properties bound to the function:

  • src
  • dest
  • plugs

See the description below for their contents.

build.task[ id ].src

<string>/<array> build.task[ id ].src

Holds the src for id, which can be a single string or an array of strings.

build.task[ id ].dest

<string> build.task[ id ].dest

Holds the dest or the write path for id.

build.task[ id ].plugs

<array> build.task[ id ].plugs

Holds all the plugs for id in an array, including their possible arguments.


<undefined> build.tasks( <object> tasks )

build.tasks is the main function to call for creating your tasks. It takes an object where:

  • you can add tasks as build-source style arrays, having the following format:
gulp.src gulp.dest plugins...

where the object key represents the gulp.task:

    jade: [ 'jade/*.jade''html',
        [ concat'all.html' ]
  • or the default function way, except for that the key remains the id of the gulp.task.
    jade: ->
        gulp.src 'jade/*.jade'
        .pipe jade()
        .pipe minifyHtml()
        .pipe concat 'all.html'
        .pipe gulp.dest 'html'

When using the array format:

  • you can use the caret ^ for inhibiting the build.root or source.root
  • you can use the exclamation mark ! in the gulp.dest field to prevent outputting to gulp.dest, it simply means: no output. As a result, you cannot write to a directory called !
  • if a plug needs arguments, you'll have to wrap the plug and argument(s) in an array, where possible following arguments can be comma seperated as in a normal function call.
    # ^ means: ignore source.root and use build/ as source path for the webserver
    # ! means: no output to file
    webserver: [ '^build/', '!', [
            livereload : true
            open : true
    ] ]


<array> build.tasks.all

Contains all id's of the tasks added with build.tasks.


<array> build.tasks.ignore

An empty array where you can add id's to, to prevent them from being fired by build.tasks.startAll. You don't have to add 'default' as build-source will never call the 'default' task.


<boolean> build.tasks.ignored( <string> id )

Used internally. You can call to find out if an id is in the ignore list or it is 'default'.


<undefined> build.tasks.startAll()

Starts all gulp tasks created with build.tasks that are not ignored.


<function> build.source( <string>/<array> paths )

An alias to make available source, in case you're not using Coffeescript.

change log


Added the option for putting a '!' in the "src.dest" field. Now you can use processing without output to a file, as needed for certain plugs like gulp-webserver for example.


Fixed: Custom build function now doesn't crash anymore when no plugins are given.

I removed the presets for source.root and build.root. Now you can run a build without first creating a ./source dir or setting source.root to some other path. They can of course still be set manually if you liked the idea.


Fixed: missing Strings error

Added: build.tasks.all


Initial commit.


I am always open for feature requests or any feedback. You can reach me at Github.