laravelgulpgulp-concat

Gulp-concat: wrap all js in a unique $(document)


Is it possible to wrap all added js in a $(document).ready(function(){ as first line and }); as last line in cases using gulp-concat?


Solution

  • you can use gulp-concat-util for this

    it does concat all files, and optionally add header line & footer line to concatenated text

    var concat = require('gulp-concat-util');
    
    gulp.task('concat:dist', function() {
      gulp.src('scripts/{,*/}*.js')
        .pipe(concat(pkg.name + '.js', {process: function(src) { return (src.trim() + '\n').replace(/(^|\n)[ \t]*('use strict'|"use strict");?\s*/g, '$1'); }}))
        .pipe(concat.header('(function(window, document, undefined) {\n\'use strict\';\n'))
        .pipe(concat.footer('\n})(window, document);\n'))
        .pipe(gulp.dest('dist'));
    });