UNPKG

1.46 kBJavaScriptView Raw
1const path = require('path');
2const gulp = require('gulp');
3const gutil = require('gulp-util');
4const plumber = require('gulp-plumber');
5const through = require('through2');
6const lebab = require('lebab');
7
8const transformOptions = [
9 'arg-spread',
10 'arrow',
11 'class',
12 'commonjs',
13 'default-param',
14 'exponent',
15 'for-of',
16 'includes',
17 'let',
18 // 'multi-var',
19 'no-strict',
20 'obj-method',
21 'obj-shorthand',
22 'template',
23];
24
25function 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// 这个脚本不能多次执行,因此在临时文件夹内进行
50gulp.task(
51 'commonjs2es6',
52 () => gulp.src(path.resolve(__dirname, '../spec/**/*.js')) // FIXME modify this to specify target
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/'))) // FIXME modify this to specify target
59);