-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
88 lines (68 loc) · 2.1 KB
/
gulpfile.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
const _ = require('lodash')
const gulp = require('gulp')
const path = require('path')
const merge = require('merge2')
const rimraf = require('rimraf')
const ts = require('gulp-typescript')
const TscWatch = require('tsc-watch/client')
const sourcemaps = require('gulp-sourcemaps')
const syncDirectory = require('sync-directory')
const { replaceTscAliasPaths } = require('tsc-alias')
const project = ts.createProject('./tsconfig.client.json', {
declaration: true,
isolatedModules: false,
})
const SOURCE_DIR = project.config.compilerOptions.rootDir ?? 'src'
const OUT_DIR = project.config.compilerOptions.outDir ?? 'lib'
const sourceDir = path.resolve('./', SOURCE_DIR)
const outDir = path.resolve('./', OUT_DIR)
const clean = async (cb) => {
rimraf(outDir, cb)
}
const build = (cb) => {
return gulp.series(buildClient, copyFiles, postBuild, buildServer)(cb)
}
const buildClient = () => {
const compiled = project.src().pipe(sourcemaps.init()).pipe(project())
return merge(compiled.dts, compiled.js.pipe(sourcemaps.write())).pipe(
gulp.dest(outDir),
)
}
const replaceTsAliasPaths = () =>
replaceTscAliasPaths({
configFile: './tsconfig.client.json',
})
const postBuild = (done) =>
gulp.series((cb) => replaceTsAliasPaths().finally(cb))(done)
const buildServer = () => {
const project = ts.createProject('./tsconfig.json', {
isolatedModules: false,
})
const compiled = project.src().pipe(project())
return merge(compiled.dts, compiled.js).pipe(gulp.dest(outDir))
}
const syncDirectories = async (watch = false) => {
syncDirectory.async(sourceDir, outDir, {
watch,
verbose: 1,
exclude: [/.*\.(ts|tsx)$/],
})
}
const watch = async () => {
syncDirectories(true)
const watch = new TscWatch()
watch.on('success', async () => {
await postBuild()
})
watch.start('--build')
}
const copyFiles = (cb) => {
syncDirectories(false).finally(cb)
}
gulp.task('build', build)
gulp.task('watch', watch)
gulp.task('clean', clean)
gulp.task('build-server', buildServer)
gulp.task('build-client', buildClient)
gulp.task('post-build', postBuild)
gulp.task('copy-files', copyFiles)