如何将参数传递给gulp任务回调?

时间:2014-02-24 00:35:22

标签: coffeescript gulp

我正在尝试制作两项任务,一项观察和构建任务。 watch任务调用我的'coffee'任务,将我的.coffee文件编译成javascript。 构建任务应该基本上做同样的事情,除了我想在函数中解析一个布尔值,这样我就可以编译包括源映射的代码。

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']

有人能解决我的问题吗?我原则上做错了吗? 提前谢谢。

1 个答案:

答案 0 :(得分:6)

coffee任务不一定是一项任务。只需将其设为JavaScript函数即可。

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']