Thứ Sáu, 23 tháng 12, 2016

Making gulp file cleaner with gulp-load-plugins

gulpfile.js without gulp-load-plugins:
var gulp = require('gulp');
var clean = require('gulp-clean');
var concat = require('gulp-concat');
var babel = require('gulp-babel');
var uglify = require('gulp-uglify');
var jshint = require('gulp-jshint');
var jscs =  require('gulp-jscs');

gulp.task('clean', function () {
    return gulp.src('build', {read: false})
        .pipe(clean());
});

gulp.task('build', ['clean'], function() {
     return gulp.src('source/**/*.js')
  .pipe(jshint())
  .pipe(jshint.reporter('jshint-stylish'))
  .pipe(jshint.reporter('fail'))
    .pipe(babel({
        presets: 'es2015'
    }))
 .pipe(concat('all.js'))
 .pipe(uglify())
    .pipe(gulp.dest('build'));
})

gulp.task('jshint', function () {
    return gulp.src('source/**/*.js')
        .pipe(jscs())
  .pipe(jshint())
  .pipe(jshint.reporter('jshint-stylish'))
  .pipe(jshint.reporter('fail'));
});

gulp.task('default', ['build'])

Install gulp-load-plugins:
npm install --save-dev gulp-load-plugins

gulpfile.js with gulp-load-plugins:
var gulp = require('gulp');
var $ = require('gulp-load-plugins')({lazy: true});

gulp.task('clean', function () {
    return gulp.src('build', {read: false})
        .pipe($.clean());
});

gulp.task('build', ['clean'], function() {
     return gulp.src('source/**/*.js')
  .pipe($.jshint())
  .pipe($.jshint.reporter('jshint-stylish'))
  .pipe($.jshint.reporter('fail'))
    .pipe($.babel({
        presets: 'es2015'
    }))
 .pipe($.concat('all.js'))
 .pipe($.uglify())
    .pipe(gulp.dest('build'));
})

gulp.task('jshint', function () {
    return gulp.src('source/**/*.js')
        .pipe($.jscs())
  .pipe($.jshint())
  .pipe($.jshint.reporter('jshint-stylish'))
  .pipe($.jshint.reporter('fail'));
});

gulp.task('default', ['build'])

Không có nhận xét nào :

Đăng nhận xét