📜  Gulp-Watch(1)

📅  最后修改于: 2023-12-03 15:01:05.457000             🧑  作者: Mango

Gulp-Watch

Introduction

Gulp-Watch is a plugin for Gulp, a popular task runner for front-end developers. The plugin provides a file-watching mechanism that enables Gulp to automatically execute tasks when files are added, deleted, or modified. This helps streamline the development process by automating repetitive tasks, such as compiling SCSS files, concatenating and minifying Javascript files, and compressing images.

Installation

To install Gulp-Watch, run the following command in your terminal:

npm install --save-dev gulp-watch
Usage

To use Gulp-Watch in your Gulpfile, you need to require the plugin and initialize a watcher. Here is an example Gulpfile that watches for changes in SCSS files and compiles them to CSS:

const gulp = require('gulp');
const watch = require('gulp-watch');
const sass = require('gulp-sass');

gulp.task('sass', () => {
  return gulp.src('src/scss/*.scss')
    .pipe(sass())
    .pipe(gulp.dest('dist/css'));
});

gulp.task('watch', () => {
  gulp.watch('src/scss/*.scss', gulp.series('sass'));
});

The above example creates two Gulp tasks. The first task, sass, compiles SCSS files to CSS and saves them to a dist/css directory. The second task, watch, initializes a watcher that listens for changes in any .scss file in the src/scss directory. When a change occurs, Gulp automatically runs the sass task, updating the CSS files in the dist/css directory.

Conclusion

Gulp-Watch is an invaluable tool for front-end developers, significantly reducing time and effort spent on repetitive tasks. By automating the build process, it allows developers to focus on writing cleaner, more efficient code.