阅读代码后分析如下:

阅读代码后分析如下:
模块 pomelo-protocol 的代码,试图兼容node.js与浏览器端,其区分方法是 判断module是否是一个object。
('object' === typeof module ? module.exports : (this.Protocol = {}),'object' === typeof module ? Buffer : Uint8Array, this);

在node.js中,module是一个object,而在浏览器端,早期版本的component实现 把module的函数自身作为最后一个参数(命名为module)
  if (!module.exports) {
    module.exports = {};
    module.client = module.component = true;
     module.call(this, module.exports, require.relative(resolved), module);
  }

所以typeof(module)得到的是一个function。

但是随着component的更新,component改变了这个特性:
  if (!module._resolving && !module.exports) {
    var mod = {};
    mod.exports = {};
    mod.client = mod.component = true;
    module._resolving = true;
    module.call(this, mod.exports, require.relative(resolved), mod);
    delete module._resolving;
    module.exports = mod.exports;
  }

可以看到最后一个参数现在是一个新创建出来的Object,所以现在在浏览器上,pomelo-protocol也认为现在正在node.js环境中,于是就报错了。

一个 临时的workaround办法是,在require("promelo-protocol")之前,先准备好Buffer,代码如下:

window.Buffer = Uint8Array;
var protocol = require('pomelo-protocol');
window.Protocol = protocol;
delete window.Buffer;
这样问题暂时消除了。当然,最靠谱的办法还是在pomelo-protocol中修改识别环境的办法。稍后我会向pomelo提交pull-request帮助解决这一问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值