gulp-order node module with merged streams

2019-04-07 16:15发布

问题:

I'm using the gulp-order module along with the event-streams module and gulp-concat to concatenate javascript files into a single dest file. The gulp-order plugin has worked great for me in other projects where I wanted to concatenate files from the stream in a distinct order. For some reason in this project it is not working properly, and the files in the public/angular/config directory are dispersed amongst the files I specify to concatenate last in the public/js directory. I think this may have something to do with specifying multiply sources ie. the angular and js directories. I tried merging the streams with the event-streams module with no luck, whereas when I first began I specified the multiple sources by passing an array to the gulp.src function

gulp.src(['./public/angular/**/*.js', './public/js/*.js'])

Below is the code I'm using now. The piping and concatenation are working fine but the order is not following the specification:

var gulp         = require('gulp');
var concat       = require('gulp-concat');
var notify       = require('gulp-notify');
var handleErrors = require('../util/handleErrors');
var jshint       = require('gulp-jshint');
var ngmin        = require('gulp-ngmin');
var order        = require('gulp-order');
var es           = require('event-stream');

function getStream(streamPath) {
  return gulp.src(streamPath);
};

gulp.task('scripts', function() {
    return es.merge(getStream('./public/angular/**/*.js'),getStream('./public/js/*.js'))
        .pipe(order([
          './public/angular/config/*.js',
          './public/angular/services/**/*.js',
          './public/angular/modules/**/*.js',
          './public/angular/primitives/**/*.js',
          './public/js/**/*.js'
        ]))
        .pipe(concat('app.js'))
        .pipe(gulp.dest('./public/build/js'))
        .on('error', handleErrors);
});

回答1:

I had this same issue, and I used gulp-print to see the filenames in my stream, but they were correct. I found out that I needed to add the base option to gulp-order, then all worked well.

gulp.src(['myFiles/*', 'myOtherFiles/*'])
   .pipe(order([
      'myOtherFiles/lib1.js',
      'myFiles/lib2.js'
   ], {base: '.'});

Helpful article: http://martinwolf.org/2014/08/12/force-a-concatenation-order-with-gulp-js/

UPDATE: new link to blog article: https://martinwolf.org/blog/2014/08/force-a-concatenation-order-with-gulp-js