Concat scripts in order with Gulp

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

    if you would like to order third party libraries dependencies, try wiredep. This package basically checks each package dependency in bower.json then wire them up for you.

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

    I just add numbers to the beginning of file name:

    0_normalize.scss
    1_tikitaka.scss
    main.scss
    

    It works in gulp without any problems.

    0 讨论(0)
  • 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/'));
    });
    
    0 讨论(0)
  • 2020-11-28 02:29

    Try stream-series. It works like merge-stream/event-stream.merge() except that instead of interleaving, it appends to the end. It doesn't require you to specify the object mode like streamqueue, so your code comes out cleaner.

    var series = require('stream-series');
    
    gulp.task('minifyInOrder', function() {
        return series(gulp.src('vendor/*'),gulp.src('extra'),gulp.src('house/*'))
            .pipe(concat('a.js'))
            .pipe(uglify())
            .pipe(gulp.dest('dest'))
    });
    
    0 讨论(0)
  • 2020-11-28 02:31

    I'm in a module environnement where all are core-dependents using gulp. So, the core module needs to be appended before the others.

    What I did:

    1. Move all the scripts to an src folder
    2. Just gulp-rename your core directory to _core
    3. gulp is keeping the order of your gulp.src, my concat src looks like this:

      concat: ['./client/src/js/*.js', './client/src/js/**/*.js', './client/src/js/**/**/*.js']
      

    It'll obviously take the _ as the first directory from the list (natural sort?).

    Note (angularjs): I then use gulp-angular-extender to dynamically add the modules to the core module. Compiled it looks like this:

    angular.module('Core', ["ui.router","mm.foundation",(...),"Admin","Products"])
    

    Where Admin and Products are two modules.

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

    An alternative method is to use a Gulp plugin created specifically for this problem. https://www.npmjs.com/package/gulp-ng-module-sort

    It allows you to sort your scripts by adding in a .pipe(ngModuleSort()) as such:

    var ngModuleSort = require('gulp-ng-module-sort');
    var concat = require('gulp-concat');
    
    gulp.task('angular-scripts', function() {
        return gulp.src('./src/app/**/*.js')
            .pipe(ngModuleSort())
            .pipe(concat('angularAppScripts.js))
            .pipe(gulp.dest('./dist/));
    });
    

    Assuming a directory convention of:

    |——— src/
    |   |——— app/
    |       |——— module1/
    |           |——— sub-module1/
    |               |——— sub-module1.js
    |           |——— module1.js
    |       |——— module2/
    |           |——— sub-module2/
    |               |——— sub-module2.js
    |           |——— sub-module3/
    |               |——— sub-module3.js
    |           |——— module2.js
    |   |——— app.js
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题