Babel 预设环境未针对正确的环境

Babel preset env not targeting correct env

我可能对 babel-preset-env 的实际作用感到困惑。我的假设是它会为我的目标浏览器转换我的代码,包括任何必要的 polyfill。不过在我的代码中似乎并非如此...

我的代码:

import "babel-polyfill"
...
var k = Object.values({ x: 's' });

我的 babel 设置:

    "presets": [
    [ "env", {
      "useBuiltIns": true,
      "targets": {
        "browsers": ["last 2 versions", "IE 10"]
      }
    }]
  ]

转译后的代码仍然包含 Object.values 并且 IE 10 安静地死掉了(它确实转译了 - 我可以看到代码的其余部分发生了变化 - 只是目标似乎错了)。

Package.json(其中一些导入未使用):

"core-js": "^2.5.1",
"opentype.js": "^0.7.2",
"svg.draggable.js": "^2.2.1",
"svg.js": "^2.6.3",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.24.1",
"clean-webpack-plugin": "^0.1.16",
"compression-webpack-plugin": "^1.0.0",
"copy-webpack-plugin": "^4.0.1",
"imports-loader": "^0.7.1",
"mocha": "^3.5.0",
"webpack": "^3.4.1",
"webpack-dev-server": "^2.8.2",
"webpack-merge": "^4.1.0"

babel-preset-env 在其默认状态下仅处理转换语法,不填充标准库功能。

听起来您需要 useBuiltins: true 在您的配置中。您还需要按照那里的其他说明安装 core-js 并为 babel-polyfill 添加导入。

或者你可以只加载 babel-polyfill 本身而不依赖 babel-preset-env