Introduction to Run-Sequence
Run-sequence is a great tool for orchestrating task execution in Gulp. It allows you to define and run a series of tasks in a specific order, making it perfect for build processes and similar automation tasks. In this guide, we’ll dive deep into its dozens of useful APIs with code snippets.
API Examples
1. Basic Usage
The most basic usage involves importing run-sequence and running tasks in a predefined order.
const gulp = require('gulp'); const runSequence = require('run-sequence'); gulp.task('task1', function() { // Task 1 code }); gulp.task('task2', function() { // Task 2 code }); gulp.task('default', function(callback) { runSequence('task1', 'task2', callback); });
2. Running Tasks in Parallel
You can run tasks in parallel by passing an array of task names.
gulp.task('default', function(callback) { runSequence(['task1', 'task2'], 'task3', callback); });
3. Conditional Sequences
Run-sequence makes it simple to run tasks conditionally, based on various criteria.
gulp.task('default', function(callback) { const condition = someConditionCheck(); if(condition) { runSequence('task1', 'task2', callback); } else { runSequence('task3', callback); } });
4. Handling Errors
Handling errors within tasks is straightforward with the `on` method.
gulp.task('task1', function() { return gulp.src('src/*') .pipe(somePlugin()) .on('error', console.error.bind(console)); }); gulp.task('default', function(callback) { runSequence('task1', 'task2', callback); });
5. Run-Sequence Functional Example
Here is a full example of a Gulp application using run-sequence APIs.
const gulp = require('gulp'); const runSequence = require('run-sequence'); const clean = require('gulp-clean'); const sass = require('gulp-sass'); const concat = require('gulp-concat'); const uglyfly = require('gulp-uglyfly'); gulp.task('clean', function() { return gulp.src('dist', {read: false, allowEmpty: true}) .pipe(clean()); }); gulp.task('styles', function() { return gulp.src('src/sass/**/*.scss') .pipe(sass().on('error', sass.logError)) .pipe(gulp.dest('dist/css')); }); gulp.task('scripts', function() { return gulp.src('src/js/**/*.js') .pipe(concat('app.js')) .pipe(uglyfly()) .pipe(gulp.dest('dist/js')); }); gulp.task('default', function(callback) { runSequence('clean', ['styles', 'scripts'], callback); });
By using these methods, you can streamline your task running sequences and set up an efficient build process using Gulp.
Hash: 7793bedf7dfb425821731f4f399791aa92804006883dcf1a3bffe82256f2c667