Gulp, concat and ouglify files and vendor concat

I want to execute and strip my JS files using gulp. After that I want to reconcile the result with the vendor folder which includes jquery, bootstrap, ...

How can I concatenate files from vendor folder after uglify my js code?

Here is my current gulp file:

var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

gulp.task('scripts', function() {
  gulp.src([
          './src/resources/assets/js/**/*.js',
          '!./src/resources/assets/js/vendor/**/*.js'
      ])
    .pipe(concat('main.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./public/js/'));
});

gulp.task('default', ['scripts'], function() {
});

      

+3


source to share


1 answer


If I understand your question correctly, you want your js.files to be bundled with libraries, but keep the concat version of your own js?

I would do this, you can keep both files:

var runSequence = require('run-sequence');

gulp.task('myJs', function() {
  gulp.src([
          './src/resources/assets/js/**/*.js',
          '!./src/resources/assets/js/vendor/**/*.js'
      ])
    .pipe(concat('myJs.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./public/js/'));
});

gulp.task('allJs', function() {
    gulp.src([
          './src/resources/assets/js/vendor/**/*.js',
          './public/js/myJs.js'
      ])
    .pipe(concat('allJs.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./public/js/'));
})


gulp.task('default', function() {
  runSequence('myJs',
              'allJs',
              );
});

      



or that you only save one file:

var merge = require('merge2');

gulp.task('scripts', function() {
    // Get your js
    var myJs = gulp.src([
        src + 'my/*.js',
        src + '!not/my/*.js'])

    // Get vendor js
    var vendorJs = gulp.src(src + 'not/my/*.js')

    return merge(myJs, vendorJs)
        .pipe(concat('main.js'))
        .pipe(uglify())
        .pipe(gulp.dest(dest));
});

      

+6


source







All Articles