【问题标题】:RegeneratorRuntime is not definedRegeneratorRuntime 未定义
【发布时间】:2015-05-12 15:50:23
【问题描述】:

我正在尝试运行 Karma-babel-preprocessor 和一个简单的 ES6 生成器:

//require('babel/polyfill');

  describe("how Generators work", function() {
    it("will allow generator functions", function() {
      /*function * numbers() {
        yield 1;
        yield 2;
        yield 3;
      };*/


      let numbers = {
        [Symbol.iterator]:function*(){
            yield 1;
            yield 2;
            yield 3;
          }
      }

      let sum = 0;

      for(n of numbers){
        sum += n;
      }

      expect(sum).toBe(6);
    });
  });

由此我使用 babel 生成了我的测试文件(ES6 => ES5):

babel src --watch --out-dir tests

然后我运行karma start 我得到错误:

ReferenceError: regeneratorRuntime is not defined”。

karma.conf.js 中的相关位:

  // list of files / patterns to load in the browser
    files: [
      'test-main.js',
      {pattern: 'tests/*.js', included: true}
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
            'src/*.js': ['babel']
    },
        'babelPreprocessor': {
      options: {
        sourceMap: 'inline'
      },
      filename: function(file) {
        return file.originalPath.replace(/\.js$/, '.es5.js');
      },
      sourceFileName: function(file) {
        return file.originalPath;
      }
    },


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],

Full project on github

我可以使用许多 ES6 特性,包括箭头。只是不要继续使用生成器。

【问题讨论】:

    标签: javascript karma-runner babeljs karma-babel-preprocessor


    【解决方案1】:

    如果你使用 React,从 create-react-app 添加 polyfill 对我有用。

    yarn add --dev react-app-polyfill
    

    然后将以下行添加到webpack.config.js

    entry: {
      app: [
        'react-app-polyfill/ie9', // Only if you want to support IE 9
        'react-app-polyfill/stable',
        './src/index.jsx',
      ],
    },
    

    react-app-polyfill GitHub page 上查看更多示例。

    【讨论】:

      【解决方案2】:

      与 arcseldon 的帖子类似,我在 NodeJS 环境中运行 Babel 并收到相同的错误消息“ReferenceError: regeneratorRuntime is not defined”。虽然安装 babel-polyfill 确实有效,但我选择了 @babel/plugin-transform-runtime。

      @babel/plugin-transform-runtime

      它需要通过两种方式安装...首先作为开发依赖:

      npm install --save-dev @babel/plugin-transform-runtime
      

      第二个作为生产依赖项:

      npm install --save @babel/runtime
      

      然后需要在 .babelrc 文件中添加一个简单的内容:

      {
        "plugins": ["@babel/plugin-transform-runtime"]
      }
      

      这些添加提供了没有 ReferenceError 的 ES6 创作功能。

      【讨论】:

      • 我试图让一些 Jest 单元测试在 circleci 上正确运行,并且不得不使用它来让它们通过。
      【解决方案3】:

      虽然我采用不同的方法**在我的项目中使用带有 Babel 的 Karma,但我怀疑你遇到了与我相同的问题:Babel polyfill 没有被加载,所以你没有得到它支持的功能(包括 Babel 用来使生成器工作的自定义再生器运行时)。

      一种方法是找到一种包含 polyfill 的方法,也许是通过 files 数组将它提供给 Karma:

      files: [
        'path/to/browser-polyfill.js', // edited: polyfill => browser-polyfill per P.Brian.Mackey's answer
        ...
      

      另一种方法可能是使用 Babel 的 runtime transformer [编辑:在重新阅读文档时,除非您随后使用 browserify/webpack/etc,否则这将不起作用。处理由转换器创建的require() 调用];根据其文档,

      runtime 可选转换器做了三件事:

      • 在您使用生成器/异步函数时自动需要 babel-runtime/regenerator
      • 自动需要 babel-runtime/core-js 并映射 ES6 静态方法和内置函数。
      • 删除内联 babel 帮助程序并改用 module babel-runtime/helpers

      我没有这方面的经验,但我怀疑你可以通过在 babelPreprocessor 配置中包含 Babel 文档中的 optional: ['runtime'] 选项来做到这一点,即:

      'babelPreprocessor': {
        options: {
          optional: ['runtime'],  // per http://babeljs.io/docs/usage/options/
          sourceMap: 'inline'
        },
      ...
      

      (** 我目前正在使用 jspm + jspm-karma + 一些配置来让 Babel polyfill 加载到 SystemJS;询问是否相关,我会解释。)

      【讨论】:

      • 我尝试了这两个选项。没有一个对我有用。我添加了node_modules/babel/polyfill.js 和错误:Uncaught ReferenceError: module is not defined。我相信指的是这个文件中包含module 的单行。我添加了越来越多的 dir/*.js 目录,只是在没有任何工作的情况下更深入地了解它。您列出的第二个选项似乎没有影响,同样的错误。
      • 您的回答非常接近,肯定让我走上了正轨。有一些问题,所以我发布了一个答案来帮助识别它们。
      • 太棒了!我已经在我的答案中编辑了 polyfill 路径以避免混淆其他人。
      【解决方案4】:

      我修改了karma.conf.js 以添加browser-polyfill,正如Docs Link提到的

      files: [
                  'node_modules/babel/browser-polyfill.js',
            'test-main.js',
            {pattern: 'tests/*.js', included: true}
          ],
      

      修改后,以下单元测试在 Karma 中工作:

        describe("how Generators work", function() {
          it("will allow generator functions", function() {
           /*function* numbers(){
             yield 1;
             yield 2;
             yield 3;
           };*///Simplified syntax does not work
      
            let numbers = {
              [Symbol.iterator]:function*(){
                  yield 1;
                  yield 2;
                  yield 3;
                }
            }
      
            let sum = 0;
      
            for(let num of numbers){
              sum += num;
            }
      
            expect(sum).toBe(6);
          });
        });
      

      【讨论】:

      • 整个 karma.conf 看起来如何?
      • 要包含的文件似乎变成了 'node_modules/babel-polyfill/browser.js'
      【解决方案5】:

      Node js 环境 - 2015 年 12 月更新

      这个问题已经回答了,请查看接受的答案,除非在 NodeJS 环境中运行。

      如果像我一样,你也有同样的错误信息:'ReferenceError: regeneratorRuntime is not defined' 但是在 NodeJS 环境中运行 Babel,那么简单地执行以下操作可能会解决你的问题:

      npm install babel-polyfill --save
      

      然后在受影响模块的顶部插入以下 require 语句以获得所需的(生成器)行为:

      require("babel-polyfill");
      

      这应该是你所需要的,只需导入模块在运行时添加所需的 polyfill 行为。

      【讨论】:

      • 我试图让 FuseBox 捆绑一个 React 应用程序,我必须这样做才能使其正常工作。
      猜你喜欢
      • 2020-05-29
      • 2016-02-05
      • 2016-10-29
      • 2020-09-20
      • 2019-05-02
      相关资源
      最近更新 更多