Gulp + Browserify + CoffeeScriptで複数エントリーポイントを登録する


標題の環境で、エントリーポイントを複数登録するのに結構ハマりました。とりあえず無理矢理の対応です。
transform辺りについて、もっと熟知すれば効率よく書けるのかも。

gulpfile.coffee

gulp = require 'gulp'
browserify = require 'browserify'
glob = require 'glob'
notify = require 'gulp-notify'
source = require 'vinyl-source-stream'

path =
  source: 'develop/source'
  build: 'develop/build'

gulp.task 'browserify', ->
  # plumberの代替
  handleErrors = ->
    args = Array.prototype.slice.call arguments

    notify.onError
      title: 'Compile Error'
      message: '<%= error %>'
    .apply @, args

    @emit 'end'

  files = glob.sync "./#{path.source}/coffee/*.coffee"

  # エントリーポイントを複数登録するためのループ
  for i in files
    browserify
      entries: i
      extensions: '.coffee'
    .transform 'coffeeify'
    .bundle()
    .on 'error', handleErrors
    .pipe source i.replace(/.+\/(.+)\.coffee/g, '$1') + '.js'
    .pipe gulp.dest "#{path.build}/js/"