Concat scripts in order with Gulp

后端 未结 17 1394
后悔当初
后悔当初 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:44

    Apparently you can pass in the "nosort" option to gulp.src gulp.src.

    0 讨论(0)
  • 2020-11-28 02:45

    For me I had natualSort() and angularFileSort() in pipe which was reordering the files. I removed it and now it works fine for me

    $.inject( // app/**/*.js files
        gulp.src(paths.jsFiles)
          .pipe($.plumber()), // use plumber so watch can start despite js errors
          //.pipe($.naturalSort())
          //.pipe($.angularFilesort()),
        {relative: true}))
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 02:47

    I had a similar problem recently with Grunt when building my AngularJS app. Here's a question I posted.

    What I ended up doing is to explicitly list the files in order in the grunt config. The config file will then look like this:

    [
      '/path/to/app.js',
      '/path/to/mymodule/mymodule.js',
      '/path/to/mymodule/mymodule/*.js'
    ]
    

    Grunt is able to figure out which files are duplicates and not include them. The same technique will work with Gulp as well.

    0 讨论(0)
  • 2020-11-28 02:49

    I just use gulp-angular-filesort

    function concatOrder() {
    
        return gulp.src('./build/src/app/**/*.js')
            .pipe(sort())
            .pipe(plug.concat('concat.js'))
            .pipe(gulp.dest('./output/'));
    }
    
    0 讨论(0)
提交回复
热议问题