Note: Gulp 使用 .coffee 配置文件

注意: 文章已经过时, Gulp 已经支持 .coffee 配置文件, 通过 liftoff 实现

可以参考下边这篇文章看具体实现的方式: http://bocoup.com/weblog/building-command-line-tools-in-node-with-liftoff/


尝试了一下 Gulp, 拷贝代码运行成功后马上找怎么样 .coffee 后缀.
官方仓库有讨论: https://github.com/gulpjs/gulp/pull/123
大意是说, 虽然作者自己写 CoffeeScript, 但是不能纵容 .coffee
而是使用 --require 参数来允许后缀的使用:

bashalias gulp="gulp --require coffee-script/register"

这样运行命令后. 就可以从 gulpfile.js 去加载 .coffee 的配置.
我这边的例子是这样的, 测试时忘了加 package.json:

➤➤ tree -I node_modules
.
├── coffee
│   ├── demo.coffee
│   └── main.coffee
├── gulpfile.coffee
├── gulpfile.js
└── js
    ├── demo.js
    └── main.js

2 directories, 7 files

安装在本地的模块是这样

➤➤ l node_modules/
coffee-script/ gulp/          gulp-coffee/   gulp-watch/

gulpfile.coffee 的配置:

coffeegulp = require 'gulp'
coffee = require 'gulp-coffee'
watch = require 'gulp-watch'

gulp.task 'coffee', -> gulp
  .src './coffee/*.coffee'
  .pipe (coffee bare: true)
  .on 'error', console.log
  .pipe (gulp.dest './js/')

gulp.task 'watch', -> gulp
  .src 'coffee/*.coffee'
  .pipe watch (files) ->
    files.pipe (coffee bare: true)
    .pipe (gulp.dest './js/')

最后是 gulpfile.js, 注意不是.coffee 编译的, 只是个启动器:

jsrequire('coffee-script')
require('./gulpfile.coffee')

然后运行 alias 之后的 gulp 就可以运行了.

你可能感兴趣的:(coffeescript,gulp)