简体   繁体   English

如何在gulpfile.js中使用babel-polyfill

[英]How to use babel-polyfill in gulpfile.js

In Babel docs they just say that to include import "babel-polyfill"; 在Babel 文档中,他们只是说要包括import "babel-polyfill"; so that I can use ES6 generators but after I included that line in my gulpfile.js I still gen an exception : Uncaught ReferenceError: regeneratorRuntime is not defined This is my gulpfile.js 这样我就可以使用ES6生成器,但是在将该行包含在gulpfile.js中之后,我仍然生成了一个异常: Uncaught ReferenceError: regeneratorRuntime is not defined这是我的gulpfile.js

import 'babel-polyfill';

var gulp = require("gulp"),
babel = require("gulp-babel"),
concat = require('gulp-concat'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify');

gulp.task("babel", function() {
return gulp.src(jsSrc)
    .pipe(concat('Main.js'))
    .pipe(babel())
    .pipe(gulp.dest(jsDest))
    .pipe(rename('Main-min.js'))
    .pipe(uglify())
    .pipe(gulp.dest(jsDest));
});

jsSrc have maines6.js and other .js files. jsSrc具有maines6.js和其他.js文件。 In maines6.js here is my generator: maines6.js这是我的生成器:

function* anotherGenerator(i) {
  yield i + 1;
  yield i + 2;
  yield i + 3;
}

I don't know how to use this.. can you help me? 我不知道该怎么用..你能帮我吗?

Since you're just using gulp and not some sort of module bundler(webpack for eg) 由于您仅使用gulp而不是某种模块捆绑器(例如webpack)

You should follow this guide https://github.com/babel/gulp-babel#runtime 您应该遵循本指南 https://github.com/babel/gulp-babel#runtime

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

and then use it like this 然后像这样使用它

 
 
 
  
  .pipe(babel({ plugins: ['transform-runtime'] }))
 
  

Should do the trick :) 应该做的把戏:)

EDIT: 编辑:

Seems that babel-plugin-transform-runtime add require calls to the transformed file, so I guess you'll need to use a module loader. 似乎babel-plugin-transform-runtime add需要调用已转换的文件,因此我想您需要使用模块加载器。 I would suggest webpack, although there are alternative like browserify and jspm. 我建议使用webpack,尽管还有其他选择,例如browserify和jspm。

You'll need 你需要

 npm install -g webpack npm install babel-loader babel-core babel-polyfill babel-preset-es2015 --save-dev 

Then you'll need to create a webpack.config.js file. 然后,您需要创建一个webpack.config.js文件。 Here's a very primitive setup. 这是一个非常原始的设置。

 module.exports = { context: __dirname + '/app', entry: ['babel-polyfill', './entries/index.js'], output: { path: 'dist', filename: '[name].js' }, module: { loaders: [ { test: /\\.js/, exclude: /node_modules/, loader: 'babel', query: { presets: ['es2015'] } } ] } }; 

With the config above the file structure should looke like this 通过上面的配置,文件结构应如下所示

 project/ node_modules/ app/ entries/ main.js dist/ main.js webpack.config.js package.json 

Then just run webpack from your command line. 然后只需从命令行运行webpack If you want to get the minified version run webpack -p 如果要获取缩小版本,请运行webpack -p

to include the polyfill you need to require it at the top of the entry point to your application. 包括polyfill,您需要在应用程序入口点的顶部要求它。

import 'babel/polyfill' would need to go at the top of your jsSrc entry file import 'babel/polyfill'将需要放在jsSrc条目文件的顶部

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

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