coffeescriptgulp

How can I pass arguments into a gulp task callback?


I am trying to make two tasks, a watch and build task. The watch task calls my 'coffee' task which compiles my .coffee files into JavaScript. The build task should basically do the same, except that I want to parse a Boolean into the function, so that I can compile the code including source maps.

gulp   = require 'gulp'
gutil  = require 'gulp-util'
clean  = require 'gulp-clean'
coffee = require 'gulp-coffee'

gulp.task 'clean', ->
  gulp.src('./lib/*', read: false)
      .pipe clean()

gulp.task 'coffee', (map) ->
  gutil.log('sourceMap', map)
  gulp.src('./src/*.coffee')
    .pipe coffee({sourceMap: map}).on('error', gutil.log)
    .pipe gulp.dest('./lib/')

# build app
gulp.task 'watch', ->
  gulp.watch './src/*.coffee', ['coffee']

# build app
gulp.task 'build', ->
  gulp.tasks.clean.fn()
  gulp.tasks.coffee.fn(true)

# The default task (called when you run `gulp` from cli)
gulp.task 'default', ['clean', 'coffee', 'watch']

Does somebody have a solution for my problem? Am I doing something in principle wrong? Thanks in advance.


Solution

  • The coffee task need not be a gulp task. Just make it a JavaScript function.

    gulp       = require 'gulp'
    gutil      = require 'gulp-util'
    clean      = require 'gulp-clean'
    coffee     = require 'gulp-coffee'
    
    gulp.task 'clean', ->
      gulp.src('./lib/*', read: false)
          .pipe clean()
    
    compile = (map) ->
      gutil.log('sourceMap', map)
      gulp.src('./src/*.coffee')
        .pipe coffee({sourceMap: map}).on('error', gutil.log)
        .pipe gulp.dest('./lib/')
    
    # build app
    gulp.task 'watch', ->
      gulp.watch './src/*.coffee', =>
        compile(false)
    
    # build app
    gulp.task 'build', ['clean'], ->
      compile(true)
    
    # The default task (called when you run `gulp` from cli)
    gulp.task 'default', ['clean', 'build', 'watch']