当我第一次浏览我的coffeescript代码时,它运行良好。但是,当我对源代码进行更改并且watchify尝试重新运行捆绑包时,coffify似乎要针对我的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一起工作?

最佳答案

将转换移到bundle函数之外:

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();

}

10-07 13:32