【问题标题】:Gulp - concat result of watchify with few JS filesGulp - watchify 的 concat 结果与几个 JS 文件
【发布时间】:2015-11-25 19:17:27
【问题描述】:

我有一个使用 watchify 的 JS 应用程序。

所以,我想将 watchify 命令的结果与其他一些 javascripts 文件连接起来,这些文件往往是全局的(jQuery 等等)。这是我的 Javascript watchify 命令。

var source = require('vinyl-source-stream');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var reactify = require('reactify');
var watchify = require('watchify');
var notify = require("gulp-notify");

var scriptsDir = './scripts';
var buildDir = './build';


function handleErrors() {
  var args = Array.prototype.slice.call(arguments);
  notify.onError({
    title: "Compile Error",
    message: "<%= error.message %>"
  }).apply(this, args);
  this.emit('end'); // Keep gulp from hanging on this task
}


function buildScript(file, watch) {
  var props = {entries: [scriptsDir + '/' + file]};
  var bundler = watch ? watchify(props) : browserify(props);
  bundler.transform(reactify);
  function rebundle() {
    var stream = bundler.bundle({debug: true});
    return stream.on('error', handleErrors)
    .pipe(source(file))
    .pipe(gulp.dest(buildDir + '/'));
  }
  bundler.on('update', function() {
    rebundle();
    gutil.log('Rebundle...');
  });
  return rebundle();
}


gulp.task('build', function() {
  return buildScript('main.js', false);
});


gulp.task('default', ['build'], function() {
  return buildScript('main.js', true);
});

这是我用于附加 javascript 文件的任务。

return gulp.src([
  './bower_components/jquery/dist/jquery.min.js',
  './bower_components/redactor-wysiwyg/redactor/redactor.js',
  './build/main.js' // output of `gulp build`.
]).pipe(concat('application.js'))
  .pipe(gulp.dest('./public/'));

如何使用一个函数buildScript 连接这些 javascript 文件?

【问题讨论】:

    标签: javascript jquery gulp browserify watchify


    【解决方案1】:

    主要是nodejs流有end事件。所以,

    function rebundle() {
      var stream = bundler.bundle({debug: true});
    
      stream.on('end', function() { gulp.start('everything_you_want') }); 
    
      return stream.on('error', handleErrors)
        .pipe(source(file))
        .pipe(gulp.dest(buildDir + '/'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多