【问题标题】:Gulp 4 - Error: write after endGulp 4 - 错误:结束后写入
【发布时间】:2016-06-18 21:44:48
【问题描述】:

我在查看 index.html 中的更改时遇到错误(CONFIG.APP.INDEX 中的完整路径)。我所有的任务都在单独的文件中。这是tasks/watch.ts,例如:

import * as CONFIG from '../config';

export default done => {
  // other watches
  gulp.watch(CONFIG.APP.INDEX, gulp.series('inject'));
};

第一次更改任务正常执行,但第二次更改我收到此错误:

c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:203
  var er = new Error('write after end');
           ^
Error: write after end
    at writeAfterEnd (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:203:12)
    at DestroyableTransform.Writable.write (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:239:20)
    at DestroyableTransform.ondata (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:531:20)
    at emitOne (events.js:77:13)
    at DestroyableTransform.emit (events.js:169:7)
    at readableAddChunk (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:198:18)
    at DestroyableTransform.Readable.push (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:157:10)
    at DestroyableTransform.Transform.push (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:123:32)
    at afterTransform (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:79:51)
    at TransformState.afterTransform (c:\~\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:58:12)
    at c:\~\node_modules\vinyl-fs\lib\src\getContents\bufferFile.js:18:5
    at c:\~\node_modules\graceful-fs\graceful-fs.js:78:16
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:404:3)

tasks/inject.ts 任务:

declare var require;
const gulp = require('gulp');
const plugins = require('gulp-load-plugins')();
import * as CONFIG from '../config';

export default done => {
  return gulp
    .src(CONFIG.APP.INDEX)

    .pipe(require('../util/inject/fixes').default) // <--- PROBLEM IS HERE
    // other stuff...
    .pipe(gulp.dest(CONFIG.DST.BUILD))
    .on('error', plugins.util.log);
};

util/inject/fixes.ts 任务

declare var require;
const plugins = require('gulp-load-plugins')();

// errors even with this...
export default plugins.util.noop();

任务从gulpfile.ts/index.ts 加载,如下所示:

fs.readdirSync('./gulpfile.ts/tasks').map(file => {
  let name = file.replace(/\.ts$/, '');
  let task = require(path.join(path.resolve('.'), 'gulpfile.ts', 'tasks', file));
  gulp.task(name, task.default);
});

我已设法确定错误的来源,但不知道是什么原因造成的,也不知道如何解决。只有在观看index.html在第一次更改和任务执行后时才会出现问题。手动运行task正常(gulp inject),其他所有watch和task正常。

【问题讨论】:

    标签: node.js typescript gulp gulp-watch gulp-4


    【解决方案1】:

    我怀疑您对fixes.ts 的实现是在重复使用相同的noop() 结果,而不是在每次运行任务时都重新创建结果。

    尝试将fixes.ts 转换为返回一个工厂函数,该函数将在每次调用任务时返回一个新的noop 实例。

    util/inject/fixes.ts

    declare var require;
    const plugins = require('gulp-load-plugins')();
    
    // Return a function that will return a new `noop` instance each time:
    export default () => {
        return plugins.util.noop();
    };
    

    就上下文而言,我刚刚在我的项目中遇到了类似的问题,我不小心重用了 gulp 流并收到“结束后写入”错误。

    我怀疑您的代码对 noop() 结果做同样的事情,因为该值将被缓存为该模块的值。

    不正确版本的 gulpfile.js - 我的 gulp.dest() 调用的结果每次都被重复使用。

    let gulp = require('gulp');
    let merge = require('merge-stream');
    
    let _path = require('path');
    
    let files = {
        'html': {
            src: _path.join('public', 'index.html'),
            dest: gulp.dest('public')  // <-- WRONG use of `gulp.dest()`, causes result to be reused and gives "write after end" error
        },
        'files': 'html': {
            src: _path.join('files', '*'),
            dest: gulp.dest('files')  // <-- WRONG use of`gulp.dest()`
        },
    };
    
    gulp.task('copy', function(){
        let html = gulp.src(files.html.src)
            .pipe(files.html.dest);   // <-- `gulp.dest()` should be here
    
        let files = gulp.src(files.files.src)
            .pipe(files.files.dest);  // <-- `gulp.dest()` should be here
    
        return merge(html, files);
    });
    
    
    gulp.task('copy-watch', function(){
        let srcList = Object.keys(files).map(i => files[i].src);
        gulp.watch(srcList, ['copy']);
    });
    

    固定版本的 gulpfile.js - 每次运行任务时都会调用 gulp.dest()

    let files = {
        'html': {
            src: _path.join('public', 'index.html'),
            dest: 'public'  // <-- removed `gulp.dest()` call from here
        },
        'files': 'html': {
            src: _path.join('files', '*'),
            dest: 'files'  // <-- removed `gulp.dest()` call from here
        },
    };
    
    gulp.task('copy', function(){
        let html = gulp.src(files.html.src)
            .pipe(gulp.dest(files.html.dest));  // <-- CORRECT use of`gulp.dest()`
    
        let files = gulp.src(files.files.src)
            .pipe(gulp.dest(files.files.dest)); // <-- CORRECT use of `gulp.dest()`
    
        return merge(html, files);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 2020-06-28
      • 2018-09-23
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多