【问题标题】:Unable to Transpile Async/Await for Browser with Babel 7无法使用 Babel 7 为浏览器转译异步/等待
【发布时间】:2019-05-02 23:45:16
【问题描述】:

我正在尝试转换 async/await 语句以在浏览器中使用(早在 IE11 中)。我正在使用 Rollup 和 Babel 7,但是当我实际运行代码时,Chrome 中不断出现错误。我觉得它与插件和/或其配置有关,但已经停滞不前。

这是我的 .babelrc 文件:

{
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-async-to-generator",
    "@babel/plugin-transform-runtime"
  ]
}

这里是我安装的包:

  "devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/plugin-transform-async-to-generator": "^7.1.0",
    "@babel/plugin-transform-runtime": "^7.1.0",
    "@babel/preset-env": "^7.1.6",
    "babel-jest": "^21.2.0",
    "concurrently": "^3.5.1",
    "express": "^4.16.2",
    "jest-cli": "^21.2.1",
    "prettier": "^1.15.3",
    "rollup": "^0.67.3",
    "rollup-plugin-babel": "^4.0.3",
    "rollup-plugin-uglify": "^3.0.0"
  },
  "dependencies": {
    "@babel/runtime": "^7.1.5"
  }

在浏览器中,我不断收到此错误:

Uncaught TypeError: Cannot read property 'mark' of undefined

...这被追溯到我的代码中的一行,如下所示:

_regeneratorRuntime.mark(function _callee2() {

我不认为我需要导入诸如 Babel polyfill 之类的东西,因为我已经阅读了有关如何实现所有内容的内容,但我可能是错的。任何方向将不胜感激。

【问题讨论】:

  • regeneratorRuntime 未定义,使用:babeljs.io/docs/en/babel-plugin-transform-runtime
  • 我已经在使用它了。 @babel/plugin-transform-runtime 在我的package.json 和我的.babelrc 中被引用。
  • @AlexMacArthur 运行时需要进入编译输出,我认为它不应该是 devdependeny。
  • 本文深入探讨了这个问题link.medium.com/JS0UfDQghS
  • 谢谢大家!事实证明,我从 Rollup 迁移到 Webpack,这似乎解决了我的问题,即使具有相同的依赖项。我确定我的 rollup.config.js 文件有些奇怪,但我可以改用 Webpack。

标签: javascript ecmascript-6 babeljs rollup


【解决方案1】:

从这个答案 https://stackoverflow.com/a/36821986/9816567 ,它建议您在转换运行时插件上使用此配置

{
  "plugins": [
    ["transform-runtime", {
      "polyfill": false,
      "regenerator": true
    }]
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多