使用gulp + browserify + watchify + coffeeify尝试重新传输javascript而不是coffeescript

时间:2015-04-10 10:11:57

标签: coffeescript gulp browserify watchify

当我第一次浏览我的coffeescript代码时,它运行正常。但是,当我对源代码进行更改并观察尝试重新运行捆绑时,coffeeify似乎想要针对我的javascript运行coffeescript - 基于我从gulp得到的这个错误:

events.js:72
    throw er; // Unhandled 'error' event
          ^
SyntaxError: reserved word 'var' while parsing file: /Volumes/dev/app/frontend/editor/main.coffee

这是我的Gulpfile:

var gulp = require('gulp');
var gutil = require('gulp-util');
var watchify = require('watchify');
var browserify = require('browserify');
var coffeeify = require('coffeeify');
var source = require('vinyl-source-stream');
var stringify = require('stringify');
var _ = require('underscore');

var browserifyOpts = {
  basedir: "./app/frontend/editor",
  debug: true,
  extensions: ['.coffee'],
  entries: ['./main.coffee']
};
var opts = _.extend({}, watchify.args, browserifyOpts);
var bundler = browserify(opts);
var watch = watchify(bundler);

watch.on('update', bundle);
watch.on('log', gutil.log);

function bundle() {

  var b = function() {
    return bundler
      .transform(stringify(['.html']))
      .transform('coffeeify')
      .bundle()
      .pipe(source('main.js'))
      .pipe(gulp.dest('app/assets/javascripts/bundles'));
  };

  return b();

}

gulp.task('default', ['browserify-main']);
gulp.task('browserify-main', bundle);

如何使捆绑过程与watchify一起使用?

1 个答案:

答案 0 :(得分:3)

将变换移到捆绑功能之外:

var bundler = browserify(opts);
bundler.transform(stringify(['.html']))
bundler.transform('coffeeify')
var watch = watchify(bundler);

watch.on('update', bundle);
watch.on('log', gutil.log);

function bundle() {

  var b = function() {
    return bundler
      .bundle()
      .pipe(source('main.js'))
      .pipe(gulp.dest('app/assets/javascripts/bundles'));
  };

  return b();

}