stream-stub.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * 浏览器端 stream 桩模块
  3. *
  4. * webpack5 不再内置 node polyfill,sax 等依赖在模块加载阶段就会 require('stream'):
  5. * var Stream
  6. * try { Stream = require('stream').Stream } catch (ex) { Stream = function () {} }
  7. * SAXStream.prototype = Object.create(Stream.prototype, {...})
  8. *
  9. * 若把 stream 置空(fallback: { stream: false }),require('stream') 不会抛错,
  10. * 取到的 .Stream 是 undefined,上面这行就会抛
  11. * “Cannot read properties of undefined (reading 'prototype')”,导致整个应用启动失败。
  12. * 这里提供一个最小可用的 Stream 实现,保证模块加载期不报错。
  13. */
  14. function Stream() {}
  15. Stream.prototype.on = function () {
  16. return this;
  17. };
  18. Stream.prototype.once = function () {
  19. return this;
  20. };
  21. Stream.prototype.emit = function () {
  22. return false;
  23. };
  24. Stream.prototype.removeListener = function () {
  25. return this;
  26. };
  27. Stream.prototype.pipe = function () {
  28. return this;
  29. };
  30. Stream.prototype.write = function () {
  31. return true;
  32. };
  33. Stream.prototype.end = function () {
  34. return this;
  35. };
  36. exports.Stream = Stream;
  37. exports.Readable = Stream;
  38. exports.Writable = Stream;
  39. exports.Duplex = Stream;
  40. exports.Transform = Stream;
  41. exports.PassThrough = Stream;