Gulp任务似乎运行了两次,而不是一次

时间:2015-02-07 00:57:25

标签: javascript terminal gulp

问题

我的gulp任务似乎运行了两次而不是一次,试图解决这个问题我认为有些人认为罪魁祸首可能在于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');
});

终端

[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

2 个答案:

答案 0 :(得分:1)

您的scripts任务会写入watch任务正在观看的同一文件夹。然后,Watch会按指定运行scriptsstyles任务:

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

答案 1 :(得分:1)

您的问题是:您有两条相同的行保存文件并触发两次。

脚本任务

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

新人的其他可能问题。
我在这里写的经历可能对某人有帮助。

我将Dropbox用于我的项目并在那里工作。当Dropbox打开时,任务会运行两次,因为Dropbox会检测到文件更改并执行某些操作。特别是Typescript文件,我不知道为什么。