-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
108 lines (95 loc) · 2.67 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Created by dima on 18/05/2017.
*/
import 'babel-polyfill';
import gulp from 'gulp';
import babel from 'gulp-babel';
import sourcemaps from 'gulp-sourcemaps';
import webserver from 'gulp-webserver';
import eslint from 'gulp-eslint';
import browserify from 'browserify';
import del from 'del';
import gutil from 'gulp-util';
import uglify from 'gulp-uglify';
import gulpif from 'gulp-if';
import babelify from 'babelify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
const buildStrategy = (gutil.env.build === 'browser' ? gutil.env.build : 'console');
let defaultTasks;
let taskStrategy;
gutil.env.production = !!gutil.env.production;
if (buildStrategy === 'browser') {
defaultTasks = ['lint', 'webserver'];
taskStrategy = 'browserify';
} else {
defaultTasks = ['lint', 'watch'];
taskStrategy = 'compile';
}
const config = {
paths: [
'**/*.js',
'**/*.jsx',
'!node_modules/**/*.js',
'!build/**/*.js',
'!docs/**/*.js',
'!example/**/*.js',
'!lib/**/*.js',
'!lib/**/*.jsx',
'!**/bundle.js',
],
rules: {
extends: 'airbnb',
},
};
// Task for clean building directory
gulp.task('clean', () => del('app/dist'));
// Task for linting all JavaScript code.
gulp.task('lint', () => gulp.src(config.paths)
.pipe(eslint())
.pipe(eslint.format()),
);
// Task for linting and fixing all JavaScript code.
gulp.task('lint:fix', () => {
const fixRules = Object.assign({}, config.rules, { fix: true });
return gulp.src(config.paths, { base: '.' })
.pipe(eslint(fixRules))
.pipe(gulp.dest('.'))
.pipe(eslint.format());
});
// compile ES6 with babel
gulp.task('compile', () => gulp.src(['src/classes/*.js', 'src/*.js'], { base: './src/' })
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['es2015', 'stage-2'],
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('app/dist')));
gulp.task('browserify', () => {
browserify({ debug: true })
.transform(babelify, {
sourceMaps: true,
})
.require('src/test.js', { entry: true })
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(gulpif(gutil.env.production, uglify()))
.pipe(gulpif(!gutil.env.production, sourcemaps.write('.')))
.pipe(gulp.dest('app/dist'));
});
// start webserver to test project
gulp.task('webserver', ['watch'], () => {
gulp.src('./app')
.pipe(webserver({
livereload: true,
open: true,
}));
});
// start watch task and recompile/lint on changes
gulp.task('watch', [taskStrategy], () => {
gulp.watch(['src/**/*.js', 'app/*.html'], ['lint', taskStrategy]);
});
gulp.task('build', [taskStrategy]);
gulp.task('default', defaultTasks);