简体   繁体   English

gulp watch为什么要运行所有任务?

[英]Why does gulp watch run all the tasks?

I have a gulpfile.js to compile my sass and javascript. 我有一个gulpfile.js来编译我的sass和javascript。 At the bottom of the file I have a task that watches for changes in any of the sass or javascript files: 在文件的底部,我有一个任务,负责监视任何sass或javascript文件中的更改:

// Load plugins
var gulp = require('gulp'),
    sass = require('gulp-ruby-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    minifycss = require('gulp-minify-css'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    clean = require('gulp-clean'),
    concat = require('gulp-concat'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload');

// Styles
gulp.task('styles', function() {
    return gulp.src('sass/**/*.scss')
        .pipe(sass({ style: 'expanded', }))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(gulp.dest('web'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(minifycss())
        .pipe(gulp.dest('web'));
});

// Scripts
gulp.task('scripts', function() {
    return gulp.src('js/**/*.js')
        .pipe(jshint('node_modules/gulp-jshint/.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('web'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest('web'));
});

// Images
gulp.task('images', function() {
    return gulp.src('img/**/*')
        .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
        .pipe(gulp.dest('web'));
});

// Clean
gulp.task('clean', function() {
    return gulp.src(['web'], {read: false})
        .pipe(clean());
});

// Default task
gulp.task('default', ['clean'], function() {
    gulp.start('styles', 'scripts', 'images');
});

// Start livereload
gulp.task('start-livereload', function () {
    server.listen(35729, function (err) {
        if (err) {
            return console.log(err);
        }
    });
});

// Watch
gulp.task('dev', ['start-livereload'], function() {

    console.log('running');

    // Watch .scss files
    gulp.watch('sass/**/*.scss', {mode: 'poll'}, ['styles']);

    // Watch .js files
    gulp.watch('js/**/*.js', {mode: 'poll'}, ['scripts']);

    // Watch image files
    gulp.watch('img/**/*', {mode: 'poll'}, ['images']);

    // Create LiveReload server
    var server = livereload();

    // Watch any files in dist/, reload on change
    gulp.watch(['web/**']).on('change', function(file) {
        server.changed(file.path);
    });

});

When I run gulp dev in terminal and change a sass file both the sass compiler and javascript compiler run. 当我在终端中运行gulp dev并更改sass文件时,sass编译器和javascript编译器都会运行。 The same thing happens when I change a javascript file. 当我更改一个javascript文件时,也会发生同样的事情。

How can I change it so that I only compile Sass when a sass file is changed and only compile javascript when a javascript file is changed? 如何更改它,以便仅在更改sass文件时才编译Sass,而仅在更改javascript文件时才编译javascript?

Have you noticed, you watch for file changes in the source AND destination directory? 您是否注意到,您正在监视源目标目录中的文件更改? So, maybe, if the source is changed and gets compiled, the destination directory gets touched. 因此,也许,如果更改了源文件并对其进行了编译,则将触及目标目录。 And this will inherit the watch task of the destination directory. 这将继承目标目录的监视任务。

Also, you set the destination in styles and scripts task twice: 另外,您还要在stylesscripts任务中设置目标两次:

.pipe(gulp.dest('web'));

With best regards. 最诚挚的问候。

I write here my experience, maybe helps someone. 我在这里写下我的经验,也许可以帮助某人。

I use Dropbox for my project and work there. 我将Dropbox用于我的项目并在那里工作。 When Dropbox is open, tasks run twice because Dropbox detects file change and does something, that triggers the task again. 当Dropbox打开时,任务会运行两次,因为Dropbox会检测到文件更改并执行某些操作,从而再次触发任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM