RegeneratorRuntime is not defined

后端 未结 5 541
渐次进展
渐次进展 2020-12-14 15:10

I am trying to run Karma-babel-preprocessor and a straight forward ES6 generator:

//require(\'babel/polyfill\');

  describe(\"how Generators work\", functio         


        
5条回答
  •  心在旅途
    2020-12-14 15:35

    While I'm taking a different approach** to using Karma with Babel in my project, I suspect you're having the same problem I was: the Babel polyfill is not being loaded, and so you're not getting the functionality it supports (including the custom regenerator runtime that Babel uses to make generators work).

    One approach would be to find a way to include the polyfill, perhaps by feeding it to Karma via the files array:

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

    An alternate approach may be to use Babel's runtime transformer [edit: on rereading the docs, this will not work unless you then browserify/webpack/etc. to process the require() calls created by the transformer]; per its docs,

    The runtime optional transformer does three things:

    • Automatically requires babel-runtime/regenerator when you use generators/async functions.
    • Automatically requires babel-runtime/core-js and maps ES6 static methods and built-ins.
    • Removes the inline babel helpers and uses the module babel-runtime/helpers instead.

    I have no experience with this, but I suspect you would do so by including the optional: ['runtime'] option from the Babel docs in your babelPreprocessor config, viz.:

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

    (** I'm currently using jspm + jspm-karma + some config to get the Babel polyfill to load in SystemJS; ask if relevant and I'll expound.)

提交回复
热议问题