Concat scripts in order with Gulp

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

    The sort-stream may also be used to ensure specific order of files with gulp.src. Sample code that puts the backbone.js always as the last file to process:

    var gulp = require('gulp');
    var sort = require('sort-stream');
    gulp.task('scripts', function() {
    gulp.src(['./source/js/*.js', './source/js/**/*.js'])
      .pipe(sort(function(a, b){
        aScore = a.path.match(/backbone.js$/) ? 1 : 0;
        bScore = b.path.match(/backbone.js$/) ? 1 : 0;
        return aScore - bScore;
      }))
      .pipe(concat('script.js'))
      .pipe(stripDebug())
      .pipe(uglify())
      .pipe(gulp.dest('./build/js/'));
    });
    

提交回复
热议问题