Gulp:将参数从watch声明传递给任务

Bea*_*ans 23 gulp

问题:我想维护文件的"集合".这将有助于构建时间和灵活性.例如,每当我编辑我的app.js文件时,我都不想重新编译我的所有twitter引导程序文件.

我当然可以通过2个任务和2个监视声明实现这一点 - 问题是除了文件数组之外,任务是相同的.理想情况下,我想在watch声明中传递这些作为参数有没有办法做类似下面的伪代码?:

var files = {
    scripts: [
        'www/assets/scripts/plugins/**/*.js', 
        'www/assets/scripts/main.js', 
    ],
    vendor: [
        'vendor/jquery/dist/jquery.js',
        'vendor/jqueryui/ui/jquery.ui.widget.js',                       
        'vendor/holderjs/holder.js'
    ],              
};
...


gulp.task('js', ['lint'], function (files, output) {
    return gulp.src(files)
        .pipe(debug())
        .pipe(concat(output))
        .pipe(uglify({outSourceMap: true}))
        .pipe(gulp.dest(targetJSDir))       
        .pipe(notify('JS minified'))
        .on('error', gutil.log) 
});

...

gulp.watch('scripts/**/*.js', ['lint', 'js'], files.scripts, 'app.min.js');
gulp.watch('vendor/**/*.js', ['lint', 'js'], files.vendor, 'vendor.min.js');
Run Code Online (Sandbox Code Playgroud)

翻转另一种方式:是命名空间调用任务的监视声明吗?这样我可以检查哪个手表触发了任务,并以任务本身为条件.

lau*_*iad 6

问题是除了文件数组之外,任务是相同的.

我相信lazypipe(见其gh页面)非常适合您的需求.这是一个有趣的问题.我将尝试回答我认为你所问的问题(lazypipe满意)以及我认为你可能正在考虑的内容,或者如果你超过管道的参数化将最终考虑问题.

我们想要的一个方面是我们不想在没有改变的文件上重新运行jshint.此外,我们希望保持DRY,除了更改的文件之外,我们还想要获取新文件.

这是经过测试并适用于我:

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var es = require('event-stream');
var lazypipe = require('lazypipe');
var gutil = require('gulp-util');
var path = require('path');

var files = {
  scripts: ['src/**/*.js'],
  vendor: ['vendor/**/*.js']
};

// sets up a lazy pipe that does jshint related stuff
function getJsMultiPipe(name) {
  return lazypipe()
    .pipe($.jshint)
    .pipe($.jshint.reporter, 'jshint-stylish')

    // if you don't want to fail on style errors remove/comment this out:
    .pipe($.jshint.reporter, 'fail');
}

// sets up a lazy pipe that does concat and post-concat stuff
function getJsCombinedPipe(groupName, outfile) {
  return lazypipe()
      .pipe($.concat, outfile)
      .pipe($.uglify, {outSourceMap: true})
      .pipe(gulp.dest, 'build')
      .pipe($.notify, {message: groupName + ' JS minified', onLast: true});
}

// sets up a pipe for the initial build task, combining the above two pipes
function getBuildPipe(groupName, outfile) {
  return gulp.src(files[groupName])
        .pipe(getJsMultiPipe(groupName)())
        .pipe(getJsCombinedPipe(groupName, outfile)());
}

// sets up a watch pipe, such that only the changed file is jshinted,
// but all files are included in the concat steps
function setWatchPipe(groupName, outfile) {
  return $.watch({
    glob: files[groupName],
    name: groupName,
    emitOnGlob: false,
    emit: 'one'
  }, function(file, done) {
    return file
        .pipe($.debug({title: 'watch -- changed file'}))
        .pipe(getJsMultiPipe(groupName)())
        // switch context
        .pipe(gulp.src(files[groupName]))
        .pipe($.debug({title: 'watch -- entire group'}))
        .pipe(getJsCombinedPipe(groupName, outfile)())
        .pipe($.debug({title: 'watch -- concatted/source-mapped'}))
        .pipe($.notify({message: 'JS minified', onLast: true}));
  });
}

// task to do an initial full build
gulp.task('build', function() {
  return es.merge(
      getBuildPipe('scripts', 'app.min.js'),
      getBuildPipe('vendor', 'vendor.min.js')
  )
  .pipe($.notify({message: 'JS minified', onLast: true}));
});


// task to do an initial full build and then set up watches for
// incremental change
gulp.task('watch', ['build'], function(done) {
  setWatchPipe('scripts', 'app.min.js');
  setWatchPipe('vendor', 'vendor.min.js');
  done();
});
Run Code Online (Sandbox Code Playgroud)

我的依赖关系看起来像:

  "devDependencies": {
    "jshint-stylish": "^0.1.5",
    "gulp-concat": "^2.2.0",
    "gulp-uglify": "^0.2.1",
    "gulp-debug": "^0.3.0",
    "gulp-notify": "^1.2.5",
    "gulp-jshint": "^1.5.3",
    "gulp": "^3.6.0",
    "gulp-load-plugins": "^0.5.0",
    "lazypipe": "^0.2.1",
    "event-stream": "^3.1.1",
    "gulp-util": "^2.2.14",
    "gulp-watch": "^0.5.3"
  }
Run Code Online (Sandbox Code Playgroud)

编辑:我再次瞥了一眼,我注意到这些线条:

    // switch context
    .pipe(gulp.src(files[groupName]))
Run Code Online (Sandbox Code Playgroud)

请注意,我相信自从我写这篇文章以来gulp.src API已经发生了变化,并且当你将内容输入gulp.src时它当前没有切换上下文,因此这个地方可能需要更改.对于较新版本的gulp,我认为会发生的是你将添加到上下文中,相反可能会失去一点点效率.