Concat scripts in order with Gulp

后端 未结 17 1513
后悔当初
后悔当初 2020-11-28 01:51

Say, for example, you are building a project on Backbone or whatever and you need to load scripts in a certain order, e.g. underscore.js needs to be loaded befo

17条回答
  •  佛祖请我去吃肉
    2020-11-28 02:46

    I tried several solutions from this page, but none worked. I had a series of numbered files which I simply wanted be ordered by alphabetical foldername so when piped to concat() they'd be in the same order. That is, preserve the order of the globbing input. Easy, right?

    Here's my specific proof-of-concept code (print is just to see the order printed to the cli):

    var order = require('gulp-order');
    var gulp = require('gulp');
    var print = require('gulp-print').default;
    
    var options = {};
    
    options.rootPath = {
      inputDir: process.env.INIT_CWD + '/Draft',
      inputGlob: '/**/*.md',
    };
    
    gulp.task('default', function(){
      gulp.src(options.rootPath.inputDir + options.rootPath.inputGlob, {base: '.'})
        .pipe(order([options.rootPath.inputDir + options.rootPath.inputGlob]))
        .pipe(print());
    });
    

    The reason for the madness of gulp.src? I determined that gulp.src was running async when I was able to use a sleep() function (using a .map with sleeptime incremented by index) to order the stream output properly.

    The upshot of the async of src mean dirs with more files in it came after dirs with fewer files, because they took longer to process.

提交回复
热议问题