- 在您的 shell(例如 Apple 或 Linux 的终端或 iTerm)中,导航到 gulpfile.js 所在的文件夹。 例如,如果您将它与 WordPress 主题一起使用,则您的 gulpfile.js 应该位于主题的根目录中。因此使用
cd /path/to/your/wordpress/theme 导航到那里。
- 然后输入
gulp watch 并回车。
如果您的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/ 的任何地方,您都需要创建或更新以反映您用于相应文件的路径.