使用带有Babel的es7函数

fgo*_*lez 2 javascript browserify gulp babeljs ecmascript-7

我想在我的项目中包含es7函数,以开始在其中使用fetch等待异步方式.

我正在使用gulp,browserify和babelify(7.2.0),阅读一些文档,我看到说babelify使用es7函数的方法是将这一行包含在babelify转换中:

optional: ['runtime', 'es7.asyncFunctions']
Run Code Online (Sandbox Code Playgroud)

所以我的整个任务如下:

gulp.task('js',function(){



var bundleStream = browserify({

    entries:[config.paths.mainJs],
    debug: true,

    transform: [babelify.configure({
        presets:["es2015","react"],
        optional: ['runtime', 'es7.asyncFunctions']
    })]


}).transform("browserify-shim")
    .bundle()
    .on('error',console.error.bind(console))




bundleStream
    .pipe(source('compiled.js'))
    .pipe(buffer())
   // .pipe(uglify())
    .pipe(rename('compiled.min.js'))
    .pipe(gulp.dest(config.paths.dist + '/js'))


});
Run Code Online (Sandbox Code Playgroud)

不幸的是,运行任务时出现以下错误:

"未知选项:解析文件时的base.optional:"

谷歌搜索有点我看到babelify 7.x确实使用babel 6.0,显然这个参数可选在babel 6.0中不再存在.

我不想降级我的babelify版本来使这项工作,但我想包括es7功能与babelify版本7,有人知道如何做到这一点?

任何帮助将非常感激,因为没有太多关于它的信息

为了以防万一,请查找我的package.json文件:

"dependencies": {
"bootstrap": "^3.3.5",
"history": "^1.13.0",
"jquery": "^2.1.4",
"jquery-ui": "^1.10.4",
"jquery.easing": "^1.3.2",
"moment": "^2.10.2",
"react": "^0.14.3",
"react-bootstrap": "^0.28.1",
"react-dom": "^0.14.3",
"react-router": "^1.0.2",
"reflux": "^0.3.0"
},
"devDependencies": {
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"babelify": "^7.2.0",
"browserify": "^9.0.8",
"browserify-shim": "^3.8.11",
"gulp": "^3.9.0",
"gulp-concat": "^2.6.0",
"gulp-connect": "^2.2.0",
"gulp-open": "^1.0.0",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.5.1",
"jest-cli": "^0.8.0",
"reactify": "^1.1.0",
"regenerator": "^0.8.42",
"streamify": "^0.2.5",
"uglify-js": "^2.4.20",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.1.2"
 },
"browser": {
"jquery": "src/main/webapp/js/libs/jquery-1.11.1.min.js",
"x": "./vendor/x.js"
 },
"browserify": {
"transform": [
  "browserify-shim"
]
 },
 "browserify-shim": {
   "jquery": "$"
   }
Run Code Online (Sandbox Code Playgroud)

log*_*yth 7

optional: ['runtime', 'es7.asyncFunctions']
Run Code Online (Sandbox Code Playgroud)

是你如何配置Babel 5.你正在使用Babel 6,所以它会

plugins: ['transform-runtime', 'transform-async-to-generator']
Run Code Online (Sandbox Code Playgroud)

需要注意的一点是,不建议通过Babelify配置Babel.相反,最好.babelrc在应用程序的根目录中使用JSON 创建一个文件,例如

{
  presets:["es2015","react"],
  plugins: ['transform-runtime', 'transform-async-to-generator']
}
Run Code Online (Sandbox Code Playgroud)

npm install --save-dev babel-plugin-transform-runtime babel-plugin-transform-async-to-generator