为什么TypeError:stream.on不是函数

时间:2017-02-06 10:55:27

标签: javascript angularjs gulp uglifyjs

我一直致力于基于Angular JS的应用程序。我已经编写了一组gulp命令来uglify /从JS代码中删除注释。由于我想要正确地追踪异常,我用“泵”来包装任务。 (请参阅代码)并安装' pump'通过电源壳。 最后,当我尝试运行gulp命令时,它会向我显示此错误。谷歌搜索这对我不起作用。请指教我。

Gulpfile

// include plug-ins
'use strict';
var gulp = require('gulp');
var concat = require('gulp-concat');
var htmlbuild = require('gulp-htmlbuild');
var plugins = require('gulp-load-plugins')();
var es = require('event-stream');
var clean = require('gulp-clean');
var uglify = require('gulp-uglify');
var strip = require('gulp-strip-comments');
var pump = require('pump');

// pipe a glob stream into this and receive a gulp file stream 
var gulpSrc = function (opts) {
    var paths = es.through();
    var files = es.through();
    paths.pipe(es.writeArray(function (err, srcs) {
        var fixedFiles = [];
        for (var i=0; i < srcs.length; i++)
        {
            fixedFiles[i] = srcs[i].replace("~", ".");
            console.log(fixedFiles[i]);
        }
        gulp.src(fixedFiles, opts).pipe(files);
    }));

    return es.duplex(paths, files);
};

var jsBuild = es.pipeline(
  plugins.concat('all.js'),
  gulp.dest('./Scripts/')
);

gulp.task('clean', function () {
    return pump(gulp.src('./Scripts/all.js')
            .pipe(clean({ force: true })));
});

gulp.task('build', function () {
    return pump(gulp.src(['./Views/Home/Layout.cshtml'])
    .pipe(htmlbuild({
        js: htmlbuild.preprocess.js(function (block) {
            block.pipe(gulpSrc())
           .pipe(jsBuild);
      })
    }))
    );
});

// Minify 
gulp.task('minify', function () {
    return pump (gulp.src(['./Scripts/all.js']).
    pipe(uglify()).
    pipe(gulp.dest('./testing')));
});

// Strip Comments
gulp.task('StripComments', function () {
    return pump(gulp.src(['./Scripts/all.js'])
      .pipe(strip())
      .pipe(gulp.dest('./testing')));
});

gulp.task(pump('default', ['clean', 'build', 'StripComments'], function () { }));

电源 - 壳

PS C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain> gulp
C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:24
  stream.on('close', function () {
         ^

TypeError: stream.on is not a function
    at destroyer (C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:24:10)
    at C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:68:12
    at Array.map (native)
    at pump (C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\node_modules\pump\index.js:65:26)
    at Object.<anonymous> (C:\dev.net14\DevelopmentPhase2\MyCompany.WebMain\gulpfile.js:66:11)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

C:\ dev.net14 \ DevelopmentPhase2 \ MyCompany.WebMain \ node_modules \泵\ index.js

var once = require('once')
var eos = require('end-of-stream')
var fs = require('fs') // we only need fs to get the ReadStream and WriteStream prototypes

var noop = function () {}

var isFn = function (fn) {
  return typeof fn === 'function'
}

var isFS = function (stream) {
  if (!fs) return false // browser
  return (stream instanceof (fs.ReadStream || noop) || stream instanceof (fs.WriteStream || noop)) && isFn(stream.close)
}

var isRequest = function (stream) {
  return stream.setHeader && isFn(stream.abort)
}

var destroyer = function (stream, reading, writing, callback) {
  callback = once(callback)

  var closed = false
  stream.on('close', function () {
    closed = true
  })

  eos(stream, {readable: reading, writable: writing}, function (err) {
    if (err) return callback(err)
    closed = true
    callback()
  })

  var destroyed = false
  return function (err) {
    if (closed) return
    if (destroyed) return
    destroyed = true

    if (isFS(stream)) return stream.close() // use close for fs streams to avoid fd leaks
    if (isRequest(stream)) return stream.abort() // request.destroy just do .end - .abort is what we want

    if (isFn(stream.destroy)) return stream.destroy()

    callback(err || new Error('stream was destroyed'))
  }
}

var call = function (fn) {
  fn()
}

var pipe = function (from, to) {
  return from.pipe(to)
}

var pump = function () {
  var streams = Array.prototype.slice.call(arguments)
  var callback = isFn(streams[streams.length - 1] || noop) && streams.pop() || noop

  if (Array.isArray(streams[0])) streams = streams[0]
  if (streams.length < 2) throw new Error('pump requires two streams per minimum')

  var error
  var destroys = streams.map(function (stream, i) {
    var reading = i < streams.length - 1
    var writing = i > 0
    return destroyer(stream, reading, writing, function (err) {
      if (!error) error = err
      if (err) destroys.forEach(call)
      if (reading) return
      destroys.forEach(call)
      callback(error)
    })
  })

  return streams.reduce(pipe)
}

module.exports = pump

2 个答案:

答案 0 :(得分:0)

  

gulp.task(pump('default',['clean','build','StripComments'],function(){})))

您在此处没有流的情况下呼叫pump()

答案 1 :(得分:-2)

您应该定义如下的流:

var stream= fs.readStream('filename');

我更喜欢定义这样的流:

var stream= fs.createReadStream('filename');