javascript - gulp tasks could be combined to be faster? -
i have gulp file wrote , i'm new @ this. it's working have more tasks think need.
can me string javascript tasks 1 task?
i need 4 separate js files uglified when i'm done.
relevant code snippet gulpfile.js:
var gulp = require('gulp') uglify = require('gulp-uglify'), rename = require('gulp-rename'), concat = require('gulp-concat'), notify = require('gulp-notify'), cache = require('gulp-cache'), del = require('del'); gulp.task('clean', function(cb) { del(['css/*', 'js/min/*'], cb) }); gulp.task('featuretest', function() { return gulp.src('js/feature-test.js') .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) }); // file: '/js/excanvas.min.js' loaded via lte ie8 conditional statement gulp.task('excanvas', function() { return gulp.src('js/polyfills/excanvas.js') .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) }); // file: '/js/charts.min.js' used on few pages gulp.task('charts', function() { return gulp.src(['js/highcharts-4.0.1.js', 'js/usfa-theme.js']) .pipe(concat('charts.js')) .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) }); // file: '/js/main.min.js' concat of 'libs/jquery', few polyfills (except excanvas.js , highcharts) gulp.task('scripts', function() { return gulp.src(['js/libs/*.js', 'js/plugins/*.js', 'js/polyfills/*.js', '!js/excanvas.js', '!js/highcharts-4.0.1.js', 'js/custom.js']) .pipe(concat('main.js')) .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) .pipe(notify({ message: 'scripts task complete' })); }); gulp.task('watch', function() { // watch .js files gulp.watch('js/**/*.js', ['featuretest', 'excanvas', 'charts', 'scripts']); }); gulp.task('default', ['clean'], function() { gulp.start('featuretest', 'excanvas', 'charts', 'scripts'); });
what trying do:
var gulp = require('gulp') uglify = require('gulp-uglify'), rename = require('gulp-rename'), concat = require('gulp-concat'), notify = require('gulp-notify'), cache = require('gulp-cache'), del = require('del'); gulp.task('clean', function(cb) { del(['css/*', 'js/min/*'], cb) }); gulp.task('scripts', function() { // file: '/js/feature-test.js' loaded in doc <head> return gulp.src('js/feature-test.js') .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) // file: '/js/excanvas.min.js' loaded via lte ie8 conditional statement @ end of doc return gulp.src('js/polyfills/excanvas.js') .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) // file: '/js/charts.min.js' used on few pages , loaded when needed @ end of doc return gulp.src(['js/highcharts-4.0.1.js', 'js/usfa-theme.js']) .pipe(concat('charts.js')) .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) // file: '/js/main.min.js' concat of 'libs/jquery', few polyfills (except excanvas.js , high charts), loaded @ end of every doc return gulp.src(['js/libs/*.js', 'js/plugins/*.js', 'js/polyfills/*.js', '!js/excanvas.js', '!js/highcharts-4.0.1.js', 'js/custom.js']) .pipe(concat('main.js')) .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('js/min')) .pipe(notify({ message: 'scripts task complete' })); }); gulp.task('watch', function() { // watch .js files gulp.watch('js/**/*.js', ['scripts']); }); gulp.task('default', ['clean'], function() { gulp.start('scripts'); });
if you're asking question how combine separate operations single task, can combine streams gulp-util
.
var gulp = require('gulp') uglify = require('gulp-uglify'), util = require('gulp-util'); gulp.task('scripts', function() { var featuretest = gulp.src('js/feature-test.js')... var excanvas = gulp.src('js/polyfills/excanvas.js')... var charts = gulp.src(['js/highcharts-4.0.1.js', 'js/usfa-theme.js'])... var scripts = gulp.src(['js/libs/*.js', 'js/plugins/*.js', 'js/polyfills/*.js', '!js/excanvas.js', '!js/highcharts-4.0.1.js', 'js/custom.js'])... // combine streams return util.combine(featuretest, excanvas, charts, scripts); });
this give single task, it's not faster. if you're not forcing things sequential, gulp fast it's gonna be.
Comments
Post a Comment