Concat scripts in order with Gulp

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

    With gulp-useref you can concatenate every script declared in your index file, in the order in which you declare it.

    https://www.npmjs.com/package/gulp-useref

    var $ = require('gulp-load-plugins')();
    gulp.task('jsbuild', function () {
      var assets = $.useref.assets({searchPath: '{.tmp,app}'});
      return gulp.src('app/**/*.html')
        .pipe(assets)
        .pipe($.if('*.js', $.uglify({preserveComments: 'some'})))
        .pipe(gulp.dest('dist'))
        .pipe($.size({title: 'html'}));
    });
    

    And in the HTML you have to declare the name of the build file you want to generate, like this:

    
        
        
        
    

    In your build directory you will have the reference to main.min.js which will contain vendor.js, test.js, and main.js

提交回复
热议问题