gulp怎么运行html文件,如果gulp-watch监视html文件,它会运行所有任务

安毅
2023-12-01

有一项任务'服务'具有如下所示的手表功能:

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文件时,gulp相应地只执行concatjs或concatcss任务。但是如果要更新html文件gulp执行所有任务,即使是没有注册到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

也许gulp以另一种方式使用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]);

});

 类似资料: