Gulp tasks appear to be running twice, instead of once

限于喜欢 提交于 2020-01-17 09:15:27

问题


Problem

My gulp tasks seem to be running twice instead of once, trying to fix thatI think some have suggested that the culprit might lie in the default task in Gulpfile.js

Gulpfile.js

// Include Gulp
var gulp = require('gulp');

// All of your plugins
var autoprefixer = require('gulp-autoprefixer');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var livereload = require('gulp-livereload');
var minify = require('gulp-minify-css');
var notify = require('gulp-notify');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');

// Compile CSS, Autoprefix
gulp.task('styles', function() {
  return gulp.src('assets/css/style.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('assets/css'))
    // .pipe(rename({suffix: '.min'}))
    // .pipe(minify())
    .pipe(gulp.dest('assets/css'))
    .pipe(notify({ message: "Watson: I've organized your files for you." }));
});

// Lint, Concatenate and Minify JavaScript
gulp.task('scripts', function() {
  return gulp.src('assets/js/scripts.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default'))
    // .pipe(concat('scripts.js'))
    .pipe(gulp.dest('assets/js'))
    // .pipe(rename({suffix: '.min'}))
    // .pipe(uglify())
    .pipe(gulp.dest('assets/js'))
    .pipe(notify({ message: "Watson: I've done your dirty laundry." }));
});

// Watch files for changes
gulp.task('watch', function() {
    gulp.watch('assets/js/*.js', ['scripts', 'styles']);
    gulp.watch('assets/css/**/*.scss', ['styles']);
});

// Default Task
gulp.task('default', function() {
    gulp.start('styles', 'scripts', 'watch');
});

Terminal

[18:53:57] Using gulpfile ~/Desktop/Projects/legislature/Gulpfile.js
[18:53:57] Starting 'default'...
[18:53:57] Starting 'styles'...
[18:53:57] Starting 'scripts'...
[18:53:58] Starting 'watch'...
[18:53:58] Finished 'watch' after 26 ms
[18:53:58] Finished 'default' after 49 ms
[18:53:58] gulp-notify: [Gulp notification] Watson: I've organized your files for you.
[18:53:58] Finished 'styles' after 262 ms
[18:53:58] gulp-notify: [Gulp notification] Watson: I've done your dirty laundry.
[18:53:58] Finished 'scripts' after 256 ms
[18:53:58] Starting 'scripts'...
[18:53:58] Starting 'styles'...
[18:53:58] gulp-notify: [Gulp notification] Watson: I've done your dirty laundry.
[18:53:58] Finished 'scripts' after 232 ms
[18:53:58] gulp-notify: [Gulp notification] Watson: I've organized your files for you.
[18:53:58] Finished 'styles' after 232 ms

回答1:


Your scripts task writes to the same folder that watch task is watching. Watch then runs the scripts and styles tasks as specified:

gulp.watch('assets/js/*.js', ['scripts', 'styles']);



回答2:


Your problem is: you have two identical line that saves the file and triggers twice.

In your scripts task

.pipe(gulp.dest('assets/js'))

Other possible problem for newcomers.
I write here my experience maybe helps somebody.

I use Dropbox for my project and work there. When Dropbox is open, tasks run twice because Dropbox detects file change and does something. Specially Typescript files, i don't know why.



来源:https://stackoverflow.com/questions/28377303/gulp-tasks-appear-to-be-running-twice-instead-of-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!