-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
71 lines (65 loc) · 2.04 KB
/
gulpfile.js
File metadata and controls
71 lines (65 loc) · 2.04 KB
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
const { watch, task, series, dest, src } = require('gulp');
const changedInPlace = require('gulp-changed-in-place');
const ts = require('gulp-typescript');
const terser = require('gulp-terser');
const merge = require('merge-stream');
const eslint = require('gulp-eslint');
const clean = require('gulp-clean');
const tsconfig = require('./tsconfig.json');
const tsProject = ts.createProject('./tsconfig.json');
const getFiles = async () => {
return new Promise((res, rej) => {
const stream = tsProject.src();
const data = [];
stream.on('data', (file) => {
data.push(file.path);
});
stream.on('end', () => {
res(data);
});
stream.on('error', (err) => {
rej(err);
});
});
};
let files = [];
const getAllFiles = async () => {
files = await getFiles();
};
const cleanJsAndDeclarations = async () => {
return src(
files.reduce((arr, file) => {
const fileName = file.replace('.ts', '');
arr = arr.concat([`${fileName}.js`, `${fileName}.d.ts`]);
return arr;
}, []),
{
allowEmpty: true,
}
).pipe(clean());
};
const compileTypeScript = (onlyChanged = true) => {
let tsResult = tsProject.src();
if (onlyChanged) {
tsResult = tsResult.pipe(changedInPlace());
}
tsResult = tsResult
.pipe(eslint())
.pipe(eslint.failAfterError())
.pipe(tsProject());
return merge([
tsResult.dts,
tsResult.js.pipe(
terser({
keep_classnames: true,
keep_fnames: true,
mangle: true,
})
),
]).pipe(dest(tsconfig.compilerOptions.outDir));
};
const compileIncrementalFiles = () => compileTypeScript();
const compileAllFiles = () => compileTypeScript(false);
const watchFiles = async () => watch(files, series([compileIncrementalFiles]));
task('default', series([getAllFiles, cleanJsAndDeclarations, compileAllFiles]));
task('watch', series([task('default'), watchFiles]));