5

我正在尝试做两个任务,一个监视和构建任务。监视任务调用我的“咖啡”任务,它将我的.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']

有人可以解决我的问题吗?我在原则上做错了什么吗?提前致谢。

4

1 回答 1

6

coffee任务不必是 gulp 任务。只需将其设为 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']
于 2014-02-24T03:54:05.693 回答