【问题标题】:aurelia-cli error when using buffer.js - global undefined使用 buffer.js 时出现 aurelia-cli 错误 - 全局未定义
【发布时间】:2019-01-31 11:20:56
【问题描述】:

使用 aurelia-cli - SystemJS bundler 选项创建了一个新项目。

从 npm 安装了 htmlparser2 模块,该模块具有 buffer.js 作为依赖项。

尝试导入 htmlparser2 时出现以下错误:

bluebird.core.js:3434 Error: global is not defined
  Evaluating http://localhost:9000/buffer/index

在检查 vendor-bundle -> 这是产生错误的行:

Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  ? global.TYPED_ARRAY_SUPPORT
  : typedArraySupport()

用angualar-cli找到了similar issue,解决方法是手动打开节点全局

Node global is turned off. It works fine if I manually turn it on again.

问题是如何使用 aurelia-cli 做到这一点?有什么建议吗?


来自 vendor-bundle 的更大代码 sn-p

define('buffer/index',['require','exports','module','base64-js','ieee754','isarray'],function (require, exports, module) {/*!
 * The buffer module from node.js, for the browser.
 *
 * @author   Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
 * @license  MIT
 */
/* eslint-disable no-proto */

'use strict'

var base64 = require('base64-js')
var ieee754 = require('ieee754')
var isArray = require('isarray')

exports.Buffer = Buffer
exports.SlowBuffer = SlowBuffer
exports.INSPECT_MAX_BYTES = 50

/**
 * If `Buffer.TYPED_ARRAY_SUPPORT`:
 *   === true    Use Uint8Array implementation (fastest)
 *   === false   Use Object implementation (most compatible, even IE6)
 *
 * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
 * Opera 11.6+, iOS 4.2+.
 *
 * Due to various browser bugs, sometimes the Object implementation will be used even
 * when the browser supports typed arrays.
 *
 * Note:
 *
 *   - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
 *     See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
 *
 *   - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
 *
 *   - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
 *     incorrect length in some situations.

 * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
 * get the Object implementation, which is slower but behaves correctly.
 */
Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  ? global.TYPED_ARRAY_SUPPORT
  : typedArraySupport()

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    我相信你使用的是 cli 内置捆绑器(我写的),而不是 webpack。

    是的,目前不支持 nodejs 全局变量 global。 nodejs 全局变量 processBuffer 也有类似的问题。

    cli doc 有一个补丁支持processBuffer

    import process from 'process';
    window.process = process;
    import {Buffer} from 'buffer';
    window.Buffer = Buffer;
    

    您可以尝试为global再添加一个补丁。

    window.global = window;
    

    好的,为什么 cli 有问题

    cli 的跟踪算法使用 rjs(requirejs 优化器)解析器,它有点老,不检测全局变量(技术上它不做变量范围分析)。

    我有另一个 WIP 捆绑器,名为 dumber,它通过检测全局变量的新解析器解决了限制。它会根据需要在模块级别自动修补 nodejs 全局变量。

    从长远来看,我们将删除 cli 内置 bundler 的代码,然后将dumber 包装起来并使其向后兼容。

    【讨论】:

    • 对 buffer.js 的以下修改也可以解决问题:Buffer.TYPED_ARRAY_SUPPORT = typeof global !== 'undefined' &amp;&amp; global.TYPED_ARRAY_SUPPORT !== undefined ? global.TYPED_ARRAY_SUPPORT : typedArraySupport() 因为typedArraySupport() 根本不依赖于global
    • 这对我不起作用。我有一个非常相似的问题,除了在导入 aws-sdk 时发生
    猜你喜欢
    • 2018-10-26
    • 2022-06-14
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多