Gulp 如何使用怀表?

Gulp 如何使用怀表?,gulp,gulp-watch,Gulp,Gulp Watch,使用GulpWatch插件的正确方法是什么 ... var watch = require('gulp-watch'); function styles() { return gulp.src('app/styles/*.less') .pipe(watch('app/styles/*.less')) .pipe(concat('main.css')) .pipe(less()) .pipe(gulp.dest('build')); } gulp.task

使用GulpWatch插件的正确方法是什么

...
var watch = require('gulp-watch');

function styles() {
  return gulp.src('app/styles/*.less')
    .pipe(watch('app/styles/*.less'))
    .pipe(concat('main.css'))
    .pipe(less())
    .pipe(gulp.dest('build'));
}

gulp.task('styles', styles);
运行
gulp styles
时,我看不到任何结果

gulp.task('less', function() {
  gulp.src('app/styles/*.less')
    .pipe(less())
    .pipe(gulp.dest('build'));
});


gulp.task('watch', function() {
  gulp.watch(['app/styles/*.less'], ['less'])
});
  • 在shell中(例如苹果或Linux的终端或iTerm),导航到gulpfile.js所在的文件夹。例如,如果您将它与WordPress主题一起使用,那么gulpfile.js应该位于主题的根目录中。因此,使用
    cd/path/to/your/wordpress/theme
    导航到那里
  • 然后键入
    gulpwatch
    并按enter键
  • 如果正确配置了
    gulpfile.js
    (参见下面的示例),您将看到如下输出:

    [15:45:50] Using gulpfile /path/to/gulpfile.js
    [15:45:50] Starting 'watch'...
    [15:45:50] Finished 'watch' after 11 ms
    
    每次保存文件时,您都会立即在此处看到新的输出

    这里是一个功能性的
    gulpfile.js

    var gulp = require('gulp'),
        watch = require('gulp-watch'),
        watchLess = require('gulp-watch-less'),
        pug = require('gulp-pug'),
        less = require('gulp-less'),
        minifyCSS = require('gulp-csso'),
        concat = require('gulp-concat'),
        sourcemaps = require('gulp-sourcemaps');
    
    gulp.task('watch', function () {
        gulp.watch('source/less/*.less', ['css']);
    });
    
    gulp.task('html', function(){
        return gulp.src('source/html/*.pug')
        .pipe(pug())
        .pipe(gulp.dest('build/html'))
    });
    
    gulp.task('css', function(){
    return gulp.src('source/less/*.less')
        .pipe(less())
        .pipe(minifyCSS())
        .pipe(gulp.dest('build/css'))
    });
    
    gulp.task('js', function(){
        return gulp.src('source/js/*.js')
        .pipe(sourcemaps.init())
        .pipe(concat('app.min.js'))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('build/js'))
    });
    
    gulp.task('default', [ 'html', 'js', 'css', 'watch']);
    

    注意:无论你在哪里看到
    source/whatever/
    这是一个你需要创建或更新的路径,以反映你在各个文件中使用的路径。

    这不是你使用gulp watch插件的方式