1

使用 babel-preset-env 并定位节点 8 时,Babel 不是 polyfiling array.values()。

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "node": "8"
      },
      "useBuiltIns": true,
      "debug": true
    }]
  ]
}

代码:

import 'babel-polyfill';
const arr = [1,2,3];
console.log(arr.values);
const it = arr.values();
console.log('arr.values it next', it.next().value);

我使用 npm 脚本编译它:

"compile": "babel src --out-dir ./dist --source-maps",

编译输出为:

babel src --out-dir ./dist --source-maps

babel-preset-env: `DEBUG` option

Using targets:
{
  "node": "8"
}

Modules transform: commonjs

Using plugins:
src/server.js -> dist/server.js

节点 dist/server.js 输出:

undefined
error: uncaughtException and caused stop application   Error: TypeError: arr.values is not a function

当我尝试使用 babel-node 运行它或在 .babelrc 中定位节点 4 时,有什么奇怪的,array.values 被正确地填充了

编译输出:

> babel src --out-dir ./dist --source-maps

babel-preset-env: `DEBUG` option

Using targets:
{
  "node": "4"
}

Modules transform: commonjs

Using plugins:
  check-es2015-constants {"node":"4"}
  transform-es2015-arrow-functions {"node":"4"}
  transform-es2015-block-scoping {"node":"4"}
  transform-es2015-classes {"node":"4"}
  transform-es2015-destructuring {"node":"4"}
  transform-es2015-for-of {"node":"4"}
  transform-es2015-function-name {"node":"4"}
  transform-es2015-object-super {"node":"4"}
  transform-es2015-parameters {"node":"4"}
  transform-es2015-spread {"node":"4"}
  transform-es2015-sticky-regex {"node":"4"}
  transform-es2015-unicode-regex {"node":"4"}
  transform-regenerator {"node":"4"}
  transform-exponentiation-operator {"node":"4"}
  transform-async-to-generator {"node":"4"}
  syntax-trailing-function-commas {"node":"4"}

Using polyfills:
  es6.typed.array-buffer {"node":"4"}
  es6.typed.int8-array {"node":"4"}
  es6.typed.uint8-array {"node":"4"}
  es6.typed.uint8-clamped-array {"node":"4"}
  es6.typed.int16-array {"node":"4"}
  es6.typed.uint16-array {"node":"4"}
  es6.typed.int32-array {"node":"4"}
  es6.typed.uint32-array {"node":"4"}
  es6.typed.float32-array {"node":"4"}
  es6.typed.float64-array {"node":"4"}
  es6.map {"node":"4"}
  es6.set {"node":"4"}
  es6.weak-map {"node":"4"}
  es6.weak-set {"node":"4"}
  es6.reflect.apply {"node":"4"}
  es6.reflect.construct {"node":"4"}
  es6.reflect.define-property {"node":"4"}
  es6.reflect.delete-property {"node":"4"}
  es6.reflect.get {"node":"4"}
  es6.reflect.get-own-property-descriptor {"node":"4"}
  es6.reflect.get-prototype-of {"node":"4"}
  es6.reflect.has {"node":"4"}
  es6.reflect.is-extensible {"node":"4"}
  es6.reflect.own-keys {"node":"4"}
  es6.reflect.prevent-extensions {"node":"4"}
  es6.reflect.set {"node":"4"}
  es6.reflect.set-prototype-of {"node":"4"}
  es6.promise {"node":"4"}
  es6.symbol {"node":"4"}
  es6.function.name {"node":"4"}
  es6.regexp.flags {"node":"4"}
  es6.regexp.match {"node":"4"}
  es6.regexp.replace {"node":"4"}
  es6.regexp.split {"node":"4"}
  es6.regexp.search {"node":"4"}
  es6.array.from {"node":"4"}
  es7.array.includes {"node":"4"}
  es7.object.values {"node":"4"}
  es7.object.entries {"node":"4"}
  es7.object.get-own-property-descriptors {"node":"4"}
  es7.string.pad-start {"node":"4"}
  es7.string.pad-end {"node":"4"}

问题可能出在哪里?

4

0 回答 0