问题描述
我正在尝试运行和一个直接的ES6生成器:
I am trying to run Karma-babel-preprocessor and a straight forward ES6 generator:
//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):
From this I generated my test files (ES6 => ES5) with babel:
babel src --watch --out-dir tests
然后我运行 karma start
我收到错误:
Then I run karma start
I get error:
karma.conf.js中的相关位:
Relevant bits in 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'],
我能够使用许多ES6功能,包括箭头。只是没有继续发电机。
I am able to use many ES6 features including arrows. Just no go on Generators.
推荐答案
虽然我采取了不同的方法**在我的项目中使用Karma和Babel,我怀疑你遇到了同样的问题:没有加载 ,所以你没有得到它支持的功能(包括Babel用来使生成器工作的自定义再生器运行时)。
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).
一种方法是找到一种方法包括polyfill,也许是通过文件数组将它送到Karma:
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
...
另一种方法可能是使用Babel的 [编辑:重新阅读文档,除非你浏览/ webpack / etc,否则这将无效。处理变换器创建的 require()
调用];根据其文档,
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,
- 使用generator / async函数时,自动需要
babel-runtime / regenerator
。 - 自动要求
babel-runtime / core-js
并映射ES6静态方法和内置函数。 - 删除内联babel助手并改为使用
模块babel-runtime / helpers
。
- 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.
我没有这方面的经验,但我怀疑你会这样做,包括可选:['runtime']
选项来自您的 babelPreprocessor
配置中的Babel文档,即:
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'
},
...
( **我目前我们使用jspm + jspm-karma +一些配置来获取SystemJS中加载的Babel polyfill;询问是否相关,我会解释。)
这篇关于未定义RegeneratorRuntime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!