/
gulpfile.js
59 lines (51 loc) · 1.33 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
const fs = require('fs')
const path = require('path')
const ts = require('gulp-typescript')
const gulp = require('gulp')
const tsProject = ts.createProject('tsconfig.json')
const baseConfig = require('./tsconfig.json').compilerOptions
const webpack = require('webpack')
const webpackConfig = require('./webpack.config')
function cleanDirectory (directory) {
const files = fs.readdirSync(directory)
files.forEach(file => {
const filePath = path.join(directory, file)
const stat = fs.statSync(filePath)
if (stat.isFile()) {
fs.unlinkSync(filePath)
}
if (stat.isDirectory()) {
cleanDirectory(filePath)
}
})
}
function clean (cb) {
const distDir = path.join(__dirname, 'dist')
if (fs.existsSync(distDir)) {
cleanDirectory(distDir)
}
cb()
}
gulp.task('buildnode', () => {
return tsProject.src()
.pipe(ts({
...baseConfig,
module: 'CommonJS'
}))
.pipe(gulp.dest('dist/cjs'))
})
gulp.task('buildweb', () => {
return new Promise((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
return reject(err)
}
if (stats.hasErrors()) {
return reject(new Error(stats.compilation.errors.join('\n')))
}
return resolve()
})
})
})
exports.clean = clean
exports.build = gulp.series(clean, 'buildweb', 'buildnode')