มีงาน 'เสิร์ฟ' พร้อมฟังก์ชันนาฬิกาดังที่แสดงด้านล่าง:
gulp.task('serve', function() {
gulp.watch(['source/scripts/*.js'], ['concatjs', reload]);
gulp.watch(['source/styles/*.css'], ['concatcss', reload]);
gulp.watch(['app/static/*.html'], [reload]);
});
เมื่อฉันอัปเดตไฟล์ js หรือ css อึกจะดำเนินการเฉพาะงาน concatjs หรือ concatcss ตามลำดับ แต่ถ้าจะอัปเดตไฟล์ html อึกรันงานทั้งหมดแม้กระทั่งงานที่ไม่ได้ลงทะเบียนกับฟังก์ชัน watch และมันจะเกิดข้อผิดพลาด:
[18:26:31] Starting 'lesstocss'...
[18:26:31] Starting 'concatjs'...
[18:26:31] Starting 'concatcss'...
[18:26:31] Starting 'serve'...
[error] You tried to start Browsersync twice! To create multiple instances, use browserSync.create().init()
[18:26:31] Finished 'serve' after 6.1 ms
[18:26:31] Finished 'lesstocss' after 38 ms
[18:26:31] Finished 'concatjs' after 47 ms
[18:26:31] Finished 'concatcss' after 41 ms
บางทีอึกอาจใช้งานได้กับไฟล์ html ในอีกทางหนึ่ง? รหัสทั้งหมดที่ฉันใช้:
var gulp = require('gulp');
var concat = require('gulp-concat');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var less = require('gulp-less');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('lesstocss', function () {
return gulp.src('source/styles/*.less')
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write())
.pipe(gulp.dest('source/styles/'));
});
gulp.task('concatjs', function() {
return gulp.src(['source/scripts/mls.js', 'source/scripts/main.js'])
.pipe(concat('result.js'))
.pipe(gulp.dest('app/static/scripts/'));
});
gulp.task('concatcss', function() {
return gulp.src(['source/styles/mls.css', 'source/styles/main.css'])
.pipe(concat('result.css'))
.pipe(gulp.dest('app/static/styles/'));
})
gulp.task('serve', function() {
browserSync.init({
proxy: "localhost:3000",
browser: "google chrome",
notify: false,
open: 'local'
});
gulp.watch(['source/scripts/*.js'], ['concatjs', reload]);
gulp.watch(['source/styles/*.css'], ['concatcss', reload]);
gulp.watch(['app/static/*.html'], [reload]);
});
'app/static/*.html'
ไฟล์ใดไฟล์หนึ่งreload
ทำงานเมื่อคุณบันทึกการเปลี่ยนแปลง'source/scripts/*.js'
หรือ'source/styles/*.css'
หรือไม่reload
ของคุณคืออะไร และbrowserSync.init()
ของคุณตั้งค่าอย่างไร - person henry   schedule 15.09.2016'app/static/*.html'
ถูกเปลี่ยนแปลงreload
ใช้ได้กับไฟล์ทั้งหมดนั้น (js, css, html) เพิ่มรหัสในโพสต์แล้ว - person Makha   schedule 16.09.2016