【问题标题】:gulp.dest() does not result in file updatesgulp.dest() 不会导致文件更新
【发布时间】:2016-06-08 13:31:17
【问题描述】:

给定以下目录结构:

<project>
 |- src
 |- gen
 |- target

我们有一个 Gulp 构建链,它为我们执行整个前端构建。中间结果放在gen目录,最终结果放在target目录。

在开发过程中,我们希望监控 target 目录并将更改同步到包含我们基于 Grails 的应用程序的单独目录。为此,我们使用以下 sn-p:

'use strict';

var debug         = require('gulp-debug');
var config        = require('../config');
var gulp          = require('gulp');
var watch         = require('gulp-watch');

module.exports = {
  //command: 'prod',
  //desc: 'Builds "production" version',
  run: function(){
     gulp.task('watch', ['server'], function() {

        // Copy files from the 'target' directory to the framework directory while developing
        var source = config.dist.root,
           destination = config.fw.root;

        gulp.src(source + '/**/*', {base: source})
           .pipe(debug({title: 'unicorn:'}))
           .pipe(watch(source + '/**/*', {base: source}))
           .pipe(debug({title: 'minotaur:'}))
           .pipe(gulp.dest(destination))
           .pipe(debug({title: 'centaur:'}));

     });
  }
};

当我更新一个源文件时,构建链会触发并将更新的结果放在 target 目录中。但更新不会同步到单独的 Grails 目录。当我检查日志时,我看到了:

[14:29:42] Rebundle...
[14:29:42] minotaur: target\web-app\portal\js\appLoader.js
[14:29:43] minotaur: target\web-app\portal\js\appLoader.js

似乎文件在 target 目录中重新生成,并且重新生成被 gulp-watch 包拾取。但是文件不是由 gulp.dest() 函数写入的?!

这里可能发生了什么?

【问题讨论】:

    标签: node.js gulp gulp-watch


    【解决方案1】:

    经过反复试验,您似乎无法在管道中间使用watch()。相反,您应该将其用作管道的头部(而不是 gulp.src())。将单个管道拆分为两个单独的管道解决了这个问题。

    因此(为简洁起见,删除了debug() 语句):

    gulp.src(source + '/**/*', {base: source})
           .pipe(watch(source + '/**/*', {base: source}))
           .pipe(gulp.dest(destination));
    

    变成这样:

    gulp.src(source + '/**/*', {base: source})
           .pipe(gulp.dest(destination));
    
    watch(source + '/**/*', {base: source})
           .pipe(gulp.dest(destination));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多