pistacchio
pistacchio

Reputation: 58883

Multiple streams in Gulp

I have three js files: 1.js, 2.e6.js and 3.js

I want to pass 2.e6.js throu babel, then concat it between the other two (1 -> 2 -> 3), then uglify everything.

How can I achieve this with Gulp?

EXAMPLE

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

gulp.task('webdist', function() {
    gulp.src(['./1.js', './2.e6.js', './3.js']) // <-   2.e6.js should pass throu .pipe(babel({ presets: ['es2015'] }))
        .pipe(concat('out.js'))
        .pipe(gulp.dest('./dist/'));
});

Upvotes: 0

Views: 119

Answers (1)

Elfayer
Elfayer

Reputation: 4561

You could try something like this. I didn't test it, tell me if it doesn't do what you expect or if there are errors.

var gulp = require('gulp');
var babel = require('gulp-babel');
var rename = require("gulp-rename");
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

gulp.task('babelCompilation', function() {
  return gulp.src('./2.e6.js')
    .pipe(babel({
      presets: ['es2015']
    }))
    .pipe(rename({
      suffix: ".compiled"
    }))
    .pipe(gulp.dest('./'));
});

gulp.task('default', ['babelCompilation'], function() {
  return gulp.src(['./1.js', './2.e6.compiled.js', './3.js'])
    .pipe(concat('out.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist/'));
});

You just have to call gulp on a console, it'll start the gulp.task('default', ...);.

I just added a task dependency that does the babel compilation.

Upvotes: 1

Related Questions