gpt4 book ai didi

javascript - Rollup 无法转译异步/等待 - regeneratorRuntime 未定义

转载 作者:搜寻专家 更新时间:2023-11-01 04:34:29 29 4
gpt4 key购买 nike

我想将 async/await 与 rollup 结合使用。

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

@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 文件。

最佳答案

不确定您是否已经解决了这个问题,但只是为了将来引用,这个答案对我有用 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
}]
]
}

关于javascript - Rollup 无法转译异步/等待 - regeneratorRuntime 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57402722/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com