是否可能在es6中写一个gulpfile? [英] Is it possible write a gulpfile in es6?

查看:114
本文介绍了是否可能在es6中写一个gulpfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何在ES6中编写我的gulp文件,以便我可以使用 import 而不是 require 并使用 function()中的 => 语法?

Question: How can I write my gulp file in ES6 so I can use import instead of require and use => syntax over function()?

我可以使用io.js或节点任何版本。

gulpfile.js

import gulp from "./node_modules/gulp/index.js";
gulp.task('hello-world', =>{
    console.log('hello world');
});

错误:

import gulp from "./node_modules/gulp/index.js";
^^^^^^
SyntaxError: Unexpected reserved word







gulp.task('hello-world', =>{
                         ^^
SyntaxError: Unexpected token =>

node_modules / gulp / bin / gulp.js 根据这个#!/ usr / bin / env node --harmony com / questions / 29836529 / can-i-run-a-gulp-task-written-in-es6-using-node> stack

Inside the node_modules/gulp/bin/gulp.js i've changed the first line to #!/usr/bin/env node --harmony as asked in this stack

推荐答案

是的,您可以使用 babel

确保你有最新版本的gulp-cli。

Make sure you've got the latest version of the gulp-cli.

npm install -g gulp-cli

安装babel作为项目的依赖。

Install babel as a dependency of the project.

npm install --save-dev babel

重命名 gulpfile.js gulpfile.babel.js

您的gulpfile可能看起来像这样:

Your gulpfile might look something like this:

import gulp from 'gulp';

gulp.task('default', () => {
  // do something
});

Babel 6.0 +更新
正如 Eric Bronniman ,还有一些额外的让这个工作与最新版本的宝贝一起工作。以下是这些说明:

Update for Babel 6.0+ As correctly pointed out by Eric Bronniman, there are a few extra steps involved in getting this to work with the latest version of babel. Here are those instructions:

同样,请确保您有最新版本的gulp-cli

npm安装-g gulp-cli

Again, make sure you've got the latest version of gulp-cli
npm install -g gulp-cli

然后安装gulp,babel核心和es2015预设

npm install --save-dev gulp babel-core babel-preset-es2015

Then install gulp, babel core, and the es2015 presets
npm install --save-dev gulp babel-core babel-preset-es2015

然后,将以下内容添加到.babelrc文件中或者你的package.json

Then, either add the following to a .babelrc file or to your package.json

"babel": {
  "presets": [
    "es2015"
  ]
}

您的 gulpfile.js应该命名为 gulpfile.babel.js

这篇关于是否可能在es6中写一个gulpfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆