我正在尝试运行Karma-babel-预处理器和一个直接的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“。
Karma.conf.js中的相关bits:
// 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特性,包括箭头。只是不要开发电机。
发布于 2015-03-11 04:31:36
虽然我在我的项目中使用Karma与Babel不同的方法,但我怀疑您有相同的问题:巴贝尔聚脂填料没有被加载,所以您没有得到它支持的功能(包括Babel用来使生成器工作的自定义重新生成器运行时)。
一种方法是找到一种方法来包含这个多边形填充,也许可以通过文件数组将它提供给Karma:
files: [
'path/to/browser-polyfill.js', // edited: polyfill => browser-polyfill per P.Brian.Mackey's answer
...另一种方法可能是使用Babel的运行变压器编辑:在重新读取文档时,这将无法工作,除非您随后浏览/webpack/等以处理由转换器创建的require()调用;根据其文档,
runtime可选转换器执行三项任务:
babel-runtime/regenerator。babel-runtime/core-js和映射ES6静态方法和内置。module babel-runtime/helpers。我对此没有经验,但我怀疑您会这样做,方法是在您的optional: ['runtime']配置中包括Babel文档中的babelPreprocessor选项,即:
'babelPreprocessor': {
options: {
optional: ['runtime'], // per http://babeljs.io/docs/usage/options/
sourceMap: 'inline'
},
...(**我目前正在使用jspm +jspm-业力+一些配置,以便在SystemJS中加载Babel填充;询问是否相关,我将进行详细说明。)
发布于 2015-12-10 06:56:04
节点js Env -更新的2015年12月
此问题已被回答,除非在NodeJS环境中运行,否则请参阅已接受的答案。
如果您和我一样,有相同的错误消息:'ReferenceError: regeneratorRuntime是未定义的,而是在NodeJS环境中运行Babel,那么简单地执行以下操作可能解决您的问题:
npm install babel-polyfill --save然后,在受影响模块的顶部插入以下required语句,以获得必需(生成器)行为:
require("babel-polyfill");这应该是您所需要的,只需导入模块就可以在运行时添加所需的填充行为。
发布于 2018-12-10 20:03:51
类似于arcseldon发布的文章,我在NodeJS环境中运行Babel,并获得了相同的错误消息'ReferenceError: regeneratorRuntime未定义‘。虽然安装babel-poly填充是可行的,但我选择了@babel/运行时。
它需要用两种方式安装..。首先作为dev依赖项:
npm install --save-dev @babel/plugin-transform-runtime第二,作为生产依赖:
npm install --save @babel/runtime然后,需要在.babelrc文件中添加一个简单的内容:
{
"plugins": ["@babel/plugin-transform-runtime"]
}这些添加使ES6在没有ReferenceError的情况下具有创作功能。
https://stackoverflow.com/questions/28976748
复制相似问题