-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
215 lines (180 loc) · 5.26 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import "babel-polyfill";
import fs from 'fs';
import del from 'del';
import path from 'path';
import gulp from 'gulp';
import global from 'global-object';
import vinylPaths from 'vinyl-paths';
import childProcess from 'child_process';
import loadPlugins from 'gulp-load-plugins';
import npmScriptSync from 'gulp-npm-script-sync';
const $ = loadPlugins();
let exec = (command = '') => {
return new Promise((resolve, reject) => {
childProcess.exec(command, (error, stdout, stderror) => {
if (stdout) {
process.stdout.write(stdout);
}
if (stderror) {
process.stderr.write(stderror);
}
if (error !== null) {
reject(error);
}
resolve();
})
});
};
let options = {
eslint : {},
babel : {},
pkg : {}
};
const paths = {
root : path.normalize('.'),
dist : path.normalize('./dist/'),
src : path.normalize('./src/'),
js : path.join('./src/', '/**/*.js')
};
gulp.task('options', () => {
let promises = [],
readFile = (fileName, propertyName, resolve, reject) => {
fs.readFile(path.normalize(`${paths.root}/${fileName}`), {
encoding : 'UTF-8'
}, (error, content) => {
if (error) {
reject(error);
}
resolve(options[propertyName] = JSON.parse(content));
})
};
promises.push(new Promise((resolve, reject) => {
readFile('.babelrc', 'babel', resolve, reject);
}));
promises.push(new Promise((resolve, reject) => {
readFile('.eslintrc', 'eslint', resolve, reject);
}));
promises.push(new Promise((resolve, reject) => {
readFile('package.json', 'pkg', resolve, reject);
}));
return Promise.all(promises)
});
gulp.task('clean', () => {
return gulp.src(paths.dist)
.pipe(vinylPaths(del));
});
gulp.task('lint', gulp.series('options', () => {
return gulp.src(paths.js)
.pipe($.changed(paths.dist))
// eslint() attaches the lint output to the eslint property
// of the file object so it can be used by other modules.
.pipe($.eslint(options.eslint))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe($.eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failOnError last.
.pipe($.eslint.failOnError());
}));
gulp.task('build-es', gulp.series('options', () => {
return gulp.src(paths.js)
.pipe($.changed(paths.dist))
.pipe(gulp.dest(path.join(paths.dist, 'es')));
}));
gulp.task('build-commonjs', gulp.series('options', () => {
let babelOptionsCommonJs = options.babel;
babelOptionsCommonJs.plugins.push('transform-es2015-modules-commonjs');
return gulp.src(paths.js)
.pipe($.sourcemaps.init())
.pipe($.babel(babelOptionsCommonJs))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(path.join(paths.dist, 'commonjs')));
}));
gulp.task('build-amd', gulp.series('options', () => {
let babelOptions = options.babel;
babelOptions.plugins.push('transform-es2015-modules-amd');
return gulp.src(paths.js)
.pipe($.sourcemaps.init())
.pipe($.babel(babelOptions))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(path.join(paths.dist, 'amd')));
}));
gulp.task('build-umd', gulp.series('options', () => {
let babelOptions = options.babel;
babelOptions.plugins.push('transform-es2015-modules-umd');
return gulp.src(paths.js)
.pipe($.sourcemaps.init())
.pipe($.babel(babelOptions))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(path.join(paths.dist, 'umd')));
}));
gulp.task('build-systemjs', gulp.series('options', () => {
let babelOptionsSystemJs = options.babel;
babelOptionsSystemJs.plugins.push('transform-es2015-modules-systemjs');
return gulp.src(paths.js)
.pipe($.sourcemaps.init())
.pipe($.babel(babelOptionsSystemJs))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(path.join(paths.dist, 'systemjs')));
}));
gulp.task('build', gulp.series(
'lint',
gulp.parallel('build-es', 'build-commonjs', 'build-amd', 'build-umd', 'build-systemjs')
));
gulp.task('run-instance', gulp.series('build', () => {
let
invoked = false,
process = childProcess.fork(path.join(paths.dist, 'commonjs', 'test.js'));
return new Promise((resolve, reject) => {
process.on('error', (error) => {
if (invoked) {
return false;
}
invoked = true;
reject(error);
});
process.on('exit', (code) => {
if (invoked) {
return false;
}
invoked = true;
let error = code === 0 ? null : new Error(`exit code ${code}`);
if (error) {
reject(error);
}
resolve();
});
});
}));
gulp.task('watch', () => {
gulp.src(paths.js)
.pipe($.debug({
title : 'Started to watching files'
}));
return gulp.watch([
paths.js
], gulp.series(
'build',
'run-instance'
));
});
gulp.task('preversion', gulp.series('build', () => {
return exec('git add -A');
}));
gulp.task('postpublish', () => {
return exec('git push && git push --tags');
});
gulp.task('postversion', gulp.series('postpublish'));
gulp.task('prepublish', () => {
return exec('npm run build && git add -A');
});
gulp.task('test', gulp.series('run-instance'));
gulp.task(
'default',
gulp.series(
'build',
'run-instance',
'watch'
)
);
$.npmScriptSync(gulp);