我想在我的项目中包含es7函数,以开始在其中使用fetch等待异步方式。
我正在使用gulp,browserify和babelify(7.2.0),阅读一些文档,我看到说babelify使用es7函数的方法是将这一行包括在babelify转换中:
optional: ['runtime', 'es7.asyncFunctions']
所以我的整个任务如下:
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'))
});
不幸的是,我在运行任务时遇到以下错误:
“未知选项:解析文件时的base.optional:”
谷歌搜索了一下,我看到babelify 7.x确实使用了babel 6.0,显然这个参数可选在babel 6.0中不再存在。
我不想降级我的babelify版本以使其工作,但我希望在babelify版本7中包含es7函数,有人知道该怎么做吗?
任何帮助都会非常感激,因为那里没有太多关于它的信息
以防万一,请查找我的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": "$"
}
答案 0 :(得分:7)
optional: ['runtime', 'es7.asyncFunctions']
是你如何配置Babel 5.你正在使用Babel 6,所以它将是
plugins: ['transform-runtime', 'transform-async-to-generator']
需要注意的一点是,不建议通过Babelify配置Babel。相反,最好在应用程序的根目录中使用JSON创建一个.babelrc
文件,例如。
{
presets:["es2015","react"],
plugins: ['transform-runtime', 'transform-async-to-generator']
}
和npm install --save-dev babel-plugin-transform-runtime babel-plugin-transform-async-to-generator