我正在我的一个项目中尝试 Gulp,我想像以前使用 Grunt watch 一样运行它。这意味着,一旦完成所有操作,它就必须查看更少的文件和 js 文件、lint、合并、编译和刷新浏览器。
我设法让它与 gulp-browser-sync 一起工作,但由于某种原因它只能工作一次。我对我的 .less 文件进行了更改,然后浏览器重新加载。然后,第二次更改,它确实编译但没有重新加载发生。
这是日志:
[BS] Serving files from: ./
[09:47:26] Starting 'css-clean'...
[09:47:26] Finished 'css-clean' after 16 ms
[09:47:26] Starting 'styles'...
[BS] 1 file changed (styles.min.css)
[09:47:27] Finished 'styles' after 624 ms
[09:47:27] Starting 'styles-watch'...
[BS] Reloading Browsers...
当我第二次点击保存时:
[09:47:31] Starting 'css-clean'...
[09:47:31] Finished 'css-clean' after 3.39 ms
[09:47:31] Starting 'styles'...
[BS] 1 file changed (styles.min.css)
[09:47:32] Finished 'styles' after 362 ms
至于JS,它一直有效。没有任何问题,即使样式任务完成后,JS 更改仍然会正确触发重新加载。真的只有样式有问题。
这是 gulpfile.js
var gulp = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
less = require('gulp-less'),
minifycss = require('gulp-minify-css'),
concat = require('gulp-concat'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
notify = require('gulp-notify'),
path = require('path'),
streamqueue = require('streamqueue'),
clean = require('gulp-clean'),
browserSync = require('browser-sync').create(),
reload = browserSync.reload;
// Clean the CSS folder
gulp.task('css-clean', function () {
return gulp.src(['dist/css'], {read: false})
.pipe(clean());
});
// Clean the CSS folder
gulp.task('js-clean', function () {
return gulp.src(['dist/js'], {read: false})
.pipe(clean());
});
// Generate the CSS styles, clean before hand
gulp.task('styles', ['css-clean'], function() {
return streamqueue({ objectMode: true },
gulp.src(['./bower_components/uikit/less/uikit.less']),
gulp.src(['./src/less/main.less'])
)
.pipe(concat('styles.less'))
.pipe(less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(gulp.dest('dist/css'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest('dist/css'))
.pipe(browserSync.reload({stream:true}));
});
// create a task that ensures the `styles` task is complete before
// reloading browsers
gulp.task('styles-watch', ['styles'], browserSync.reload);
// Lint Task
gulp.task('lint', function() {
return gulp.src('src/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// Generate the scripts, clean before hand
gulp.task('scripts', ['js-clean', 'lint'], function() {
return streamqueue({ objectMode: true },
gulp.src(['./bower_components/jquery/dist/jquery.js']),
gulp.src(['./bower_components/modernizer/modernizr.js']),
gulp.src(['./bower_components/uikit/js/uikit.js']),
gulp.src(['./src/js/plugin.js']),
gulp.src(['./src/js/main.js'])
)
.pipe(concat('scripts.js'))
.pipe(gulp.dest('dist/js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('dist/js'))
.pipe(browserSync.reload({stream:true}));
});
// create a task that ensures the `scripts` task is complete before
// reloading browsers
gulp.task('scripts-watch', ['scripts'], browserSync.reload);
// Lint Task
gulp.task('alldone', ['scripts'], function() {
return gulp.src('src/js/*.js')
.pipe(notify({ message: 'Gulp tasks are completed!!' }));
});
// Static server
gulp.task('browsersync', function() {
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch("src/less/*.less", ['styles-watch']);
gulp.watch('src/js/*.js', ['lint', 'scripts-watch']);
gulp.watch("*.html").on('change', reload);
});
// Default Task
gulp.task('default', ['css-clean', 'js-clean', 'styles', 'lint', 'scripts', 'alldone']);
// Build Task
gulp.task('build', ['css-clean', 'js-clean', 'styles', 'lint', 'scripts', 'alldone']);
谢谢你的帮助!