javascript - GULP tap.js - 无法读取属性

标签 javascript build gulp

我最近打算使用 GULP 进行构建。但是我在尝试运行我的“脚本”清理时遇到了困难。

错误

D:\GitHub\ASGARD\baseThemeV2\src>gulp default
[23:13:03] Using gulpfile D:\GitHub\ASGARD\baseThemeV2\src\gulpfile.js
[23:13:03] Starting 'lint:js'...
[23:13:03] Starting 'build:plugins'...
[23:13:03] Starting 'build:js'...
[23:13:03] Starting 'build:less'...
[23:13:03] Starting 'build:images'...
[23:13:03] Starting 'build:svgs'...
[23:13:03] Starting 'build:fonts'...
[23:13:03] Starting 'build:favicon'...
[23:13:03] Finished 'build:favicon' after 2.19 ms
[23:13:03] Finished 'build:plugins' after 78 ms
D:\GitHub\ASGARD\baseThemeV2\src\node_modules\gulp-tap\lib\tap.js:60
    if (obj instanceof baseStream && !obj._readableState.ended) {
                                                        ^

TypeError: Cannot read property 'ended' of undefined
    at DestroyableTransform.modifyFile (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\gulp-tap\lib\tap.js:60:57)
    at DestroyableTransform.Transform._read (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:182:10)
    at DestroyableTransform.Transform._write (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:170:83)
    at doWrite (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:406:64)
    at writeOrBuffer (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:395:5)
    at DestroyableTransform.Writable.write (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:322:11)
    at DestroyableTransform.ondata (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:612:20)
    at emitOne (events.js:96:13)
    at DestroyableTransform.emit (events.js:188:7)
    at addChunk (D:\GitHub\ASGARD\baseThemeV2\src\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:284:12)

D:\GitHub\ASGARD\baseThemeV2\src>

我正在使用的 Gulp“命令”是 我认为错误是“.pipe(tap(function (file, t) {”——但我不知道如何“不”使用它。

// Lint, minify, and concatenate scripts
gulp.task('build:js',  function() {
    var jsTasks = lazypipe()
        .pipe(header, banner.full, { package : package })
        .pipe(gulp.dest, paths.scripts.output)
        .pipe(rename, { suffix: '.min' })
        .pipe(uglify)
        .pipe(header, banner.min, { package : package })
        .pipe(gulp.dest, paths.scripts.output);

    return gulp.src(paths.scripts.input)
        .pipe(plumber())
        .pipe(tap(function (file, t) {
            if ( file.isDirectory() ) {
                var name = file.relative + '.js';
                return gulp.src(file.path + '/*.js')
                    .pipe(concat(name))
                    .pipe(jsTasks());
            }
        }))
        .pipe(jsTasks());
});

最佳答案

问题出在这行代码中:

if (obj instanceof baseStream && !obj._readableState.ended) {
  obj.on('end', next);
  return obj.on('data', data = function() {
    obj.removeListener('end', next);
    obj.removeListener('data', data);
    return next();
  });
} else {
  return next();
}

obj 中没有属性 _readableState 因此当我们尝试读取 ended 时我们会得到一个错误。通过一些小的谷歌搜索,我们能够找到类似的 issue discussed on github ,这证实了 _readableState 确实不是流中必须的。

Streams are not required to have a _readableState state property. They will get this if the implementation inherits from stream.Readable, but otherwise there is no such guarantee.

如果我们继续 to the Node.js docs我们发现有几种类型的流:

There are four fundamental stream types within Node.js:

  • 可读 - 可以从中读取数据的流(例如 fs.createReadStream())。
  • 可写 - 可以写入数据的流(例如 fs.createWriteStream())。
  • 双工 - 可读和可写的流(例如 net.Socket)。
  • 转换 - 双工流,可以在写入和读取数据时修改或转换数据(例如 zlib.createDeflate())。

回到代码,obj 是您在回调中返回的内容:

obj = lambda(inst.file, utils(this, inst.file), inst);

即:

return gulp.src(file.path + '/*.js')
    .pipe(concat(name))
    .pipe(jsTasks());

它是流,但不是可读流吗?如果是这样,代码将失败。

Gulp 使用 Vinyl 流,您可能需要将其转换为“普通”流。这gulp plugin似乎可以完成这项工作。

附言此外,我建议您查看 tap 提供的示例,您确定需要在您的情况下返回流吗?

关于javascript - GULP tap.js - 无法读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164574/

相关文章:

javascript - 按照教程没有得到 dojo 循环依赖错误

javascript - 数据表无法与 Bootstrap 一起正常工作

javascript - 如何打印json属性名称?

android - Gradle 为特定设备构建 flavor

Android Studio gradle 构建时间过长

gulp.spritesmith 抑制生成的 css 文件中的 "icon-"前缀

javascript - Ace.js 中的换行行为,考虑缩进吗?

java - <dependency> 标签中的 <type> 标签是什么意思?

javascript - 使用 gulp-usemin 更新多个 html 文件

node.js - 在 Docker VM 中,Gulp-Watch 似乎不适用于从主机操作系统托管的卷