当我更改sass文件时,我正试图让gulpfile观看重新加载。目前,我必须在终端中关闭gulp cmd并刷新以使其正常工作。
我在这里想念什么?
谢谢!
let gulp = require('gulp');
let sass = require('gulp-sass');
let browserSync = require('browser-sync').create();
gulp.task('styles', function() {
gulp.src("app/scss/*.scss")
.pipe(sass())
.pipe(gulp.dest("app/css"))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('serve', function () {
browserSync.init({
server: {
baseDir: './app'
}
});
gulp.watch('app/scss/*.scss', ['styles']);
gulp.watch('app/**/*.html').on('change', browserSync.reload)
});
gulp.task('default', ['styles', 'serve']);
答案 0 :(得分:0)
如果您使用Gulp4。请使用以下代码:
let gulp = require('gulp');
let sass = require('gulp-sass');
let browserSync = require('browser-sync');
gulp.task('styles', function() {
return gulp.src('app/scss/*.scss')
.pipe(sass())
.pipe(gulp.dest('app/css')) //removed semicolon
.pipe(browserSync.reload({ stream:true }));
});
// watch Sass files for changes, run the Sass preprocessor with the 'sass' task and reload
gulp.task('serve', gulp.series('styles', function() {
browserSync({
server: {
baseDir: './app'
}
});
gulp.watch('app/scss/*.scss', gulp.series('styles'));
gulp.watch('app/css/*.css', browserSync.reload);
gulp.watch('app/**/*.html', browserSync.reload);
}));
如果您使用旧的Gulp:
let gulp = require("gulp");
let sass = require("gulp-sass");
let browserSync = require("browser-sync").create();
let runSequence = require('run-sequence'); // npm install run-sequence --save-dev
gulp.task("styles", function () {
gulp
.src("app/scss/*.scss")
.pipe(sass())
.pipe(gulp.dest('app/css'))
});
// Static server
gulp.task("serve", function () {
browserSync.init({
server: {
baseDir: "./"
}
});
});
//watch for files changes
gulp.task('watch', ['styles', 'serve'], function () {
// sass files changes
gulp.watch('app/scss/*.scss', ['styles']);
// CSS files changes
gulp.watch('app/css/*.css', browserSync.reload);
// html files changes
ulp.watch('app/**/*.html', browserSync.reload)
});
// The default task (called when you run 'gulp' from cli)
gulp.task('default', function (callback) {
runSequence(['styles', 'serve', 'watch'],
callback
)
})