1 | const path = require('path');
|
2 | const gulp = require('gulp');
|
3 | const gutil = require('gulp-util');
|
4 | const plumber = require('gulp-plumber');
|
5 | const through = require('through2');
|
6 | const lebab = require('lebab');
|
7 |
|
8 | const transformOptions = [
|
9 | 'arg-spread',
|
10 | 'arrow',
|
11 | 'class',
|
12 | 'commonjs',
|
13 | 'default-param',
|
14 | 'exponent',
|
15 | 'for-of',
|
16 | 'includes',
|
17 | 'let',
|
18 |
|
19 | 'no-strict',
|
20 | 'obj-method',
|
21 | 'obj-shorthand',
|
22 | 'template',
|
23 | ];
|
24 |
|
25 | function toES6() {
|
26 | return through.obj(function render(file, enc, cb) {
|
27 | if (file.isNull()) {
|
28 | this.push(file);
|
29 | return cb();
|
30 | }
|
31 |
|
32 | if (file.isStream()) {
|
33 | this.emit('error', new gutil.PluginError('commonjs2es6', 'Streaming not supported'));
|
34 | }
|
35 |
|
36 | try {
|
37 | gutil.log(file.path);
|
38 | const result = lebab.transform(file.contents.toString('utf8'), transformOptions);
|
39 | file.contents = new Buffer(result.code);
|
40 | } catch (err) {
|
41 | this.emit('error', new gutil.PluginError('commonjs2es6', err.toString()));
|
42 | }
|
43 |
|
44 | this.push(file);
|
45 | return cb();
|
46 | });
|
47 | }
|
48 |
|
49 |
|
50 | gulp.task(
|
51 | 'commonjs2es6',
|
52 | () => gulp.src(path.resolve(__dirname, '../spec/**/*.js'))
|
53 | .pipe(plumber())
|
54 | .pipe(toES6())
|
55 | .on('error', (err) => {
|
56 | gutil.log(gutil.colors.red(err.message));
|
57 | })
|
58 | .pipe(gulp.dest(path.resolve(__dirname, '../spec/')))
|
59 | );
|