Ver Fonte

fix: 补充 stream 桩模块文件,修复应用启动崩溃

xieyong há 6 dias atrás
pai
commit
b2c8a9f8ae
1 ficheiros alterados com 43 adições e 0 exclusões
  1. 43 0
      build/stream-stub.js

+ 43 - 0
build/stream-stub.js

@@ -0,0 +1,43 @@
+/**
+ * 浏览器端 stream 桩模块
+ *
+ * webpack5 不再内置 node polyfill,sax 等依赖在模块加载阶段就会 require('stream'):
+ *   var Stream
+ *   try { Stream = require('stream').Stream } catch (ex) { Stream = function () {} }
+ *   SAXStream.prototype = Object.create(Stream.prototype, {...})
+ *
+ * 若把 stream 置空(fallback: { stream: false }),require('stream') 不会抛错,
+ * 取到的 .Stream 是 undefined,上面这行就会抛
+ * “Cannot read properties of undefined (reading 'prototype')”,导致整个应用启动失败。
+ * 这里提供一个最小可用的 Stream 实现,保证模块加载期不报错。
+ */
+function Stream() {}
+
+Stream.prototype.on = function () {
+  return this;
+};
+Stream.prototype.once = function () {
+  return this;
+};
+Stream.prototype.emit = function () {
+  return false;
+};
+Stream.prototype.removeListener = function () {
+  return this;
+};
+Stream.prototype.pipe = function () {
+  return this;
+};
+Stream.prototype.write = function () {
+  return true;
+};
+Stream.prototype.end = function () {
+  return this;
+};
+
+exports.Stream = Stream;
+exports.Readable = Stream;
+exports.Writable = Stream;
+exports.Duplex = Stream;
+exports.Transform = Stream;
+exports.PassThrough = Stream;