-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
174 lines (150 loc) · 4.45 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import fs from 'fs';
import gulp from 'gulp';
import gutil from 'gulp-util';
import babel from 'gulp-babel';
import jsdoc from 'gulp-jsdoc3';
import rimraf from 'gulp-rimraf';
import flow from 'gulp-flowtype';
import eslint from 'gulp-eslint';
import replace from 'gulp-replace';
import coveralls from 'gulp-coveralls';
import { Server as KarmaServer } from 'karma';
import uglify from 'gulp-uglify';
import concat from 'gulp-concat';
import config from './package.json';
import jsdocConfig from './.jsdoc.json';
let currentEnv = process.env.NODE_ENV;
const isBuild = gutil.env.env === 'build';
function setEnv(env) {
currentEnv = process.env.NODE_ENV;
process.env.NODE_ENV = env;
}
function restoreEnv() {
process.env.NODE_ENV = currentEnv;
}
gulp.task('flow', () => (
gulp.src(config.files.sources)
.pipe(babel({
babelrc: false,
plugins: ['syntax-flow',
['module-alias', [
{ src: './src/lib/utils', expose: 'utils' },
{ src: './src/lib/models', expose: 'models' },
{ src: './src/lib', expose: 'lib' },
]]],
}))
.pipe(gulp.dest('flow-temp'))
.on('end', () => {
gulp.src('flow-temp/**/*.js')
.pipe(flow({
all: true,
abort: isBuild,
}));
})
));
gulp.task('flow:watch', gulp.parallel('flow', () => (
gulp.watch(config.files.sources, gulp.parallel('flow'))
)));
gulp.task('eslint:tests', () => (
gulp.src(config.files.tests.concat(['gulpfile.babel.js']))
.pipe(eslint({
envs: ['mocha', 'browser'],
rules: {
'import/no-extraneous-dependencies': 0,
'no-unused-expressions': 0,
'no-underscore-dangle': 0,
'flowtype/require-return-type': 0,
'flowtype/require-parameter-type': 0,
},
}))
.pipe(eslint.format())
.pipe(isBuild ? eslint.failAfterError() : gutil.noop())
));
gulp.task('eslint', gulp.series('eslint:tests', () => (
gulp.src(config.files.sources)
.pipe(eslint({
rules: {
'import/no-extraneous-dependencies': 0,
'import/extensions': 0,
'import/no-unresolved': 0,
},
}))
.pipe(eslint.format())
.pipe(isBuild ? eslint.failAfterError() : gutil.noop())
)));
gulp.task('eslint:watch', gulp.parallel('eslint', () => (
gulp.watch(config.files.sources, gulp.parallel('eslint'))
)));
gulp.task('karma', (done) => {
setEnv('test');
new KarmaServer({
configFile: `${process.cwd()}/karma.conf.js`,
singleRun: isBuild,
}, () => {
restoreEnv();
gulp.src('./coverage/lcov.info')
.pipe(replace(`${process.cwd()}/`, './'))
.pipe(gulp.dest('./coverage/'))
.on('end', done);
}).start();
});
gulp.task('clean', (done) => {
const dirsToClean = [config.files.es2015, config.files.docs];
const availableDirectories = [];
dirsToClean.forEach((dir) => {
try {
fs.statSync(dir);
availableDirectories.push(dir);
} catch (e) { gutil.noop(); }
});
if (availableDirectories.length > 0) {
return gulp.src(availableDirectories)
.pipe(rimraf())
.on('done', done);
}
return done();
});
gulp.task('build:es2015', gulp.series('clean', () => (
gulp.src(config.files.sources)
.pipe(babel({
babelrc: false,
plugins: ['babel-plugin-transform-flow-strip-types'],
}))
.pipe(gulp.dest(config.files.es2015))
)));
gulp.task('coveralls', () => (
gulp.src('coverage/lcov.info')
.pipe(coveralls())
));
gulp.task('build', () => (
gulp.src(config.files.sources)
.pipe(babel({
babelrc: false,
plugins: ['remove-comments', 'babel-plugin-transform-flow-strip-types',
['module-alias', [
{ src: './src/lib/utils', expose: 'utils' },
{ src: './src/lib/models', expose: 'models' },
{ src: './src/lib', expose: 'lib' },
]]],
}))
.pipe(gulp.dest(config.files.build))
));
gulp.task('build:watch', gulp.parallel('build', () => (
gulp.watch(config.files.sources, gulp.series('build'))
)));
gulp.task('build:dist', () => (
gulp.src(config.files.sources)
.pipe(babel())
.pipe(concat('bundle.js'))
.pipe(uglify({}))
.pipe(gulp.dest('./dist/dist'))
));
gulp.task('build:dist:watch', () => (
gulp.watch(config.files.sources, gulp.series('build:dist'))
));
gulp.task('docs', gulp.series('clean', 'build:es2015', () => (
gulp.src(config.files.docsSources)
.pipe(jsdoc(jsdocConfig))
.pipe(gulp.dest(config.files.docs))
)));
gulp.task('default', gulp.parallel('clean', 'karma', 'flow:watch', 'eslint:watch'));