项目作者: babel

项目描述 :
Babel的Gulp插件
高级语言: JavaScript
项目地址: git://github.com/babel/gulp-babel.git
创建时间: 2014-09-30T11:11:00Z
项目社区:https://github.com/babel/gulp-babel

开源协议:MIT License

下载


This readme is for gulp-babel v8 + Babel v7
Check the 7.x branch for docs with Babel v6 usage

gulp-babel npm Build Status

Use next generation JavaScript, today, with Babel

Issues with the output should be reported on the Babel issue tracker.

Install

Install gulp-babel if you want to get the pre-release of the next version of gulp-babel.

  1. # Babel 7
  2. $ npm install --save-dev gulp-babel @babel/core @babel/preset-env
  3. # Babel 6
  4. $ npm install --save-dev gulp-babel@7 babel-core babel-preset-env

Usage

  1. const gulp = require('gulp');
  2. const babel = require('gulp-babel');
  3. gulp.task('default', () =>
  4. gulp.src('src/app.js')
  5. .pipe(babel({
  6. presets: ['@babel/preset-env']
  7. }))
  8. .pipe(gulp.dest('dist'))
  9. );

API

babel([options])

options

See the Babel options, except for sourceMaps and filename which is handled for you. Also, keep in mind that options will be loaded from config files that apply to each file.

Source Maps

Use gulp-sourcemaps like this:

  1. const gulp = require('gulp');
  2. const sourcemaps = require('gulp-sourcemaps');
  3. const babel = require('gulp-babel');
  4. const concat = require('gulp-concat');
  5. gulp.task('default', () =>
  6. gulp.src('src/**/*.js')
  7. .pipe(sourcemaps.init())
  8. .pipe(babel({
  9. presets: ['@babel/preset-env']
  10. }))
  11. .pipe(concat('all.js'))
  12. .pipe(sourcemaps.write('.'))
  13. .pipe(gulp.dest('dist'))
  14. );

Babel Metadata

Files in the stream are annotated with a babel property, which contains the metadata from babel.transform().

Example

  1. const gulp = require('gulp');
  2. const babel = require('gulp-babel');
  3. const through = require('through2');
  4. function logBabelMetadata() {
  5. return through.obj((file, enc, cb) => {
  6. console.log(file.babel.test); // 'metadata'
  7. cb(null, file);
  8. });
  9. }
  10. gulp.task('default', () =>
  11. gulp.src('src/**/*.js')
  12. .pipe(babel({
  13. // plugin that sets some metadata
  14. plugins: [{
  15. post(file) {
  16. file.metadata.test = 'metadata';
  17. }
  18. }]
  19. }))
  20. .pipe(logBabelMetadata())
  21. )

Runtime

If you’re attempting to use features such as generators, you’ll need to add transform-runtime as a plugin, to include the Babel runtime. Otherwise, you’ll receive the error: regeneratorRuntime is not defined.

Install the runtime:

  1. $ npm install --save-dev @babel/plugin-transform-runtime
  2. $ npm install --save @babel/runtime

Use it as plugin:

  1. const gulp = require('gulp');
  2. const babel = require('gulp-babel');
  3. gulp.task('default', () =>
  4. gulp.src('src/app.js')
  5. .pipe(babel({
  6. plugins: ['@babel/transform-runtime']
  7. }))
  8. .pipe(gulp.dest('dist'))
  9. );

License

MIT © Sindre Sorhus