Concat scripts in order with Gulp

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

    Another thing that helps if you need some files to come after a blob of files, is to exclude specific files from your glob, like so:

    [
      '/src/**/!(foobar)*.js', // all files that end in .js EXCEPT foobar*.js
      '/src/js/foobar.js',
    ]
    

    You can combine this with specifying files that need to come first as explained in Chad Johnson's answer.

    0 讨论(0)
  • 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:

    <!-- build:js js/main.min.js -->
        <script src="js/vendor/vendor.js"></script>
        <script src="js/modules/test.js"></script>
        <script src="js/main.js"></script>
    

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

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

    In my gulp setup, I'm specifying the vendor files first and then specifying the (more general) everything, second. And it successfully puts the vendor js before the other custom stuff.

    gulp.src([
      // vendor folder first
      path.join(folder, '/vendor/**/*.js'),
      // custom js after vendor
      path.join(folder, '/**/*.js')
    ])    
    
    0 讨论(0)
  • 2020-11-28 02:40

    I have used the gulp-order plugin but it is not always successful as you can see by my stack overflow post gulp-order node module with merged streams. When browsing through the Gulp docs I came across the streamque module which has worked quite well for specifying order of in my case concatenation. https://github.com/gulpjs/gulp/blob/master/docs/recipes/using-multiple-sources-in-one-task.md

    Example of how I used it is below

    var gulp         = require('gulp');
    var concat       = require('gulp-concat');
    var handleErrors = require('../util/handleErrors');
    var streamqueue  = require('streamqueue');
    
    gulp.task('scripts', function() {
        return streamqueue({ objectMode: true },
            gulp.src('./public/angular/config/*.js'),
            gulp.src('./public/angular/services/**/*.js'),
            gulp.src('./public/angular/modules/**/*.js'),
            gulp.src('./public/angular/primitives/**/*.js'),
            gulp.src('./public/js/**/*.js')
        )
            .pipe(concat('app.js'))
            .pipe(gulp.dest('./public/build/js'))
            .on('error', handleErrors);
    });
    
    0 讨论(0)
  • 2020-11-28 02:42

    I have my scripts organized in different folders for each package I pull in from bower, plus my own script for my app. Since you are going to list the order of these scripts somewhere, why not just list them in your gulp file? For new developers on your project, it's nice that all your script end-points are listed here. You can do this with gulp-add-src:

    gulpfile.js

    var gulp = require('gulp'),
        less = require('gulp-less'),
        minifyCSS = require('gulp-minify-css'),
        uglify = require('gulp-uglify'),
        concat = require('gulp-concat'),
        addsrc = require('gulp-add-src'),
        sourcemaps = require('gulp-sourcemaps');
    
    // CSS & Less
    gulp.task('css', function(){
        gulp.src('less/all.less')
            .pipe(sourcemaps.init())
            .pipe(less())
            .pipe(minifyCSS())
            .pipe(sourcemaps.write('source-maps'))
            .pipe(gulp.dest('public/css'));
    });
    
    // JS
    gulp.task('js', function() {
        gulp.src('resources/assets/bower/jquery/dist/jquery.js')
        .pipe(addsrc.append('resources/assets/bower/bootstrap/dist/js/bootstrap.js'))
        .pipe(addsrc.append('resources/assets/bower/blahblah/dist/js/blah.js'))
        .pipe(addsrc.append('resources/assets/js/my-script.js'))
        .pipe(sourcemaps.init())
        .pipe(concat('all.js'))
        .pipe(uglify())
        .pipe(sourcemaps.write('source-maps'))
        .pipe(gulp.dest('public/js'));
    });
    
    gulp.task('default',['css','js']);
    

    Note: jQuery and Bootstrap added for demonstration purposes of order. Probably better to use CDNs for those since they are so widely used and browsers could have them cached from other sites already.

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

    merge2 looks like the only working and maintained ordered stream merging tool at the moment.

    Update 2020

    The APIs are always changing, some libraries become unusable or contain vulnerabilities, or their dependencies contain vulnerabilities, that are not fixed for years. For text files manipulations you'd better use custom NodeJS scripts and popular libraries like globby and fs-extra along with other libraries without Gulp, Grunt, etc wrappers.

    import globby from 'globby';
    import fs from 'fs-extra';
    
    async function bundleScripts() {
        const rootPaths = await globby('./source/js/*.js');
        const otherPaths = (await globby('./source/**/*.js'))
            .filter(f => !rootFiles.includes(f));
        const paths = rootPaths.concat(otherPaths);
    
        const files = Promise.all(
            paths.map(
                // Returns a Promise
                path => fs.readFile(path, {encoding: 'utf8'})
            )
        );
    
        let bundle = files.join('\n');
        bundle = uglify(bundle);
        bundle = whatever(bundle);
        bundle = bundle.replace(/\/\*.*?\*\//g, '');
    
        await fs.outputFile('./build/js/script.js', bundle, {encoding: 'utf8'});
    }
    
    bundleScripts.then(() => console.log('done');
    
    0 讨论(0)
提交回复
热议问题