【问题标题】:Rollup fails to transpile async/await - regeneratorRuntime is not definedRollup 无法转译 async/await - regeneratorRuntime 未定义
【发布时间】:2019-12-15 13:58:10
【问题描述】:

我想在汇总中使用 async/await。

我尝试在 stackoverflow 和 github 上搜索 babel 和汇总问题,但没有解决我的问题。

@babel/runtime/regenerator 被视为外部依赖项。我看到一个控制台错误:regeneratorRuntime is not defined。在您问之前,是的,我确实查看了有关此主题的所有其他帖子,但我找不到的帖子都没有解决此问题。

我尝试过使用@babel/polyfill,尽管它已被弃用并且人们说不要使用它。我在我的主要导入之前尝试过importing,我尝试过导入transform-runtime,但我什么都没做。

编译警告:

src/main.js → dist/bundle.js...
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
@babel/runtime/regenerator (imported by src/cronreader.js, src/animations.js)
created dist/bundle.js in 549ms

rollup.config.js:

import babel from 'rollup-plugin-babel'
import resolve from 'rollup-plugin-node-resolve'
import async from 'rollup-plugin-async';

export default {
    input: 'src/main.js',
    output: {
        file: 'dist/bundle.js',
        format: 'iife',
        globals: {
            "@babel/runtime/regenerator": "regeneratorRuntime",
            "@babel/runtime/helpers/asyncToGenerator": "asyncToGenerator"
        }
    },
    plugins: [
        async(),
        resolve({
            customResolveOptions: {
                moduleDirectory: 'src'
            }
        }),
        babel({
            runtimeHelpers: true,
            exclude: 'node_modules/**', // only transpile our source code
            presets: ["@babel/preset-env"],
            plugins: [
                "@babel/transform-runtime",
                "@babel/transform-regenerator",
                "@babel/transform-async-to-generator",
            ]
        })
    ]
}

package.json:

"devDependencies": {
    "@babel/core": "^7.5.5",
    "@babel/plugin-transform-async-to-generator": "^7.5.0",
    "@babel/plugin-transform-regenerator": "^7.4.5",
    "@babel/plugin-transform-runtime": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "@node-minify/cli": "^4.1.2",
    "@node-minify/crass": "^4.1.2",
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "node-minify": "^3.6.0",
    "node-sass": "^4.12.0",
    "rollup": "^1.18.0",
    "rollup-plugin-async": "^1.2.0",
    "rollup-plugin-babel": "^4.3.3",
    "rollup-plugin-node-resolve": "^5.2.0",
    "uglify-js": "^3.6.0"
  },
"scripts": {
    "build": "rollup -c rollup.config.js"
}
  "bundleDependencies": [
    "@babel/runtime"
  ]

没有 .babelrc 文件。

【问题讨论】:

  • 我遇到了一个非常相似的问题,即让它与 rollup 和 babel 一起工作。我还尝试了transform-runtime 和其他插件,都无济于事。 [ { plugins: [ babel({ babelrc: false, exclude: 'node_modules/**', presets: [ [ '@babel/preset-env', { corejs: 3, modules: false, useBuiltIns: 'usage', targets: { ie: '11', }, }, ], ], }), ], }, ];

标签: javascript asynchronous babeljs rollupjs


【解决方案1】:

不确定您是否已经解决了这个问题,但只是为了将来参考,这个答案为我做了https://stackoverflow.com/a/36821986/839273

我刚刚使用 npm i -D @babel/plugin-transform-runtime 安装了 transform-runtime,在 rollup.config.js 中启用了 runtimeHelpers,就像你在上面那样,然后添加了

["@babel/plugin-transform-runtime", {
   "regenerator": true
}]

到 .babelrc 中的插件节点 -> 为清楚起见,这是我完整的 .babelrc:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-flow"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "babel-plugin-inline-json-import",
    ["@babel/plugin-transform-runtime", {
      "regenerator": true
    }]
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 2018-04-11
    • 2017-06-09
    • 2018-03-05
    • 1970-01-01
    • 2015-05-12
    • 2021-10-05
    • 2021-10-21
    相关资源
    最近更新 更多