简体   繁体   English

使用带有 React 和 Webpacker 的 Monaco Editor 的 regeneratorRuntime 错误

[英]regeneratorRuntime error using Monaco Editor with React and Webpacker

I am trying to get Monaco Editor to work in my application to allow editing of YAML.我正在尝试让 Monaco Editor 在我的应用程序中工作,以允许编辑 YAML。 I can get it work, but the console is complaining that regeneratorRuntime is undefined, so the web workers are likely not working.我可以让它工作,但控制台抱怨 regeneratorRuntime 未定义,所以 web 工作人员可能无法工作。

iterator.js:18 Uncaught ReferenceError: regeneratorRuntime is not defined
at iterator.js:18
at Module../node_modules/monaco-editor/esm/vs/base/common/iterator.js (iterator.js:524)
at __webpack_require__ (bootstrap:19)
at Module../node_modules/monaco-editor/esm/vs/base/common/lifecycle.js (lifecycle.js:1)
at __webpack_require__ (bootstrap:19)
at Module../node_modules/monaco-editor/esm/vs/base/common/worker/simpleWorker.js (simpleWorker.js:1)
at __webpack_require__ (bootstrap:19)
at Module../node_modules/monaco-editor/esm/vs/editor/editor.worker.js (editor.worker.js:1)
at __webpack_require__ (bootstrap:19)
at bootstrap:83

I'm no babel expert but I think that I have it set up correctly.我不是 babel 专家,但我认为我的设置正确。 Here is my .babelrc file:这是我的.babelrc文件:

    {
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "entry"
      }
    ],
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/syntax-dynamic-import", 
    "@babel/plugin-proposal-class-properties", 
    "babel-plugin-styled-components",
    [
      "@babel/plugin-transform-runtime"
    ]
  ]
}

and just a few relevant deps from package.json以及来自 package.json 的一些相关部门

"@babel/core": "^7.14.3",
"@babel/plugin-transform-runtime": "^7.14.3",
"@babel/preset-env": "^7.14.4",
"@babel/preset-react": "^7.13.13",
"@babel/runtime": "^7.14.0",
"babel-loader": "^8.2.2",

I'm also using Webpacker 5.4.0.我也在使用Webpacker 5.4.0。

To get the editor working I set up a custom config file:为了让编辑器正常工作,我设置了一个自定义配置文件:

// custom.js
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = {
  plugins: [
    new MonacoWebpackPlugin({
      languages: ["yaml"],
      publicPath: "/packs/"
    }),
  ],
};

which is merged with Webpackers webpack config in environment.js:它与 environment.js 中的 Webpackers webpack 配置合并:

// environment.js
const { environment } = require('@rails/webpacker');
const customConfig = require('./custom');

environment.config.merge(customConfig)

module.exports = environment;

Then I just import the editor as usual and it loads just fine, except for the error in the console.然后我像往常一样导入编辑器,它加载得很好,除了控制台中的错误。 (I'll leave this out as I don't think it is relevant to the issue); (我将把它排除在外,因为我认为它与问题无关);

Does anyone have any idea about what could be happening?有谁知道会发生什么? I have tried adding @babel/plugin-transform-regenerator to my babelrc file, but no luck.我尝试将@babel/plugin-transform-regenerator添加到我的 babelrc 文件中,但没有运气。 Anyway, this is included in @babel/preset-env ..无论如何,这包含在@babel/preset-env ..

I would be grateful for any help!如果有任何帮助,我将不胜感激!

I got it working in the end.我最终让它工作了。 One of the things I did not mention was my Rails version (5.1.6).我没有提到的一件事是我的 Rails 版本(5.1.6)。 I upgraded this to 5.2.x so that it is in sync with webpacker 5.4.我将它升级到 5.2.x,以便它与 webpacker 5.4 同步。 This brought in some changes to the structure, including new babel and postcss files.这给结构带来了一些变化,包括新的 babel 和 postcss 文件。

I also installed corejs and regenerator-runtime and now everything is working perfectly.我还安装了 corejs 和 regenerator-runtime,现在一切正常。

Lesson learnt, check compatibility before upgrading anything!吸取教训,在升级任何东西之前检查兼容性!

I have found a solution: add targets: {esmodules: true} to @babel/preset-env .我找到了解决方案:将targets: {esmodules: true}添加到@babel/preset-env For example:例如:

    {
      "presets": [
        [
          "@babel/preset-env",
          {
            "useBuiltIns": "entry",
             targets: {
              esmodules: true,
             },
          }
        ],
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM