-
Notifications
You must be signed in to change notification settings - Fork 10
/
gulpfile.js
113 lines (103 loc) · 2.47 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
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
const del = require('delete')
// const glob = require('glob')
const gulp = require('gulp')
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')
// const concat = require('gulp-concat')
// const logger = require('gulplog')
// 命令行传参
const cmdParams = {
watch: process.argv.includes('--watch')
}
// 打包后的目录
const buildFrom = './plugin/'
// const buildTo = './plugin/build/'
const buildTo = './miniprogram_dist/'
const buildConfig = [
{
name: 'index',
from: buildFrom + 'index.js',
to: buildTo,
watch: true
},
{
name: 'modules',
from: buildFrom + 'modules/*.min.js',
to: buildTo + 'modules/',
watch: true
},
{
name: 'WxCanvas2d',
from: buildFrom + 'WxCanvas2d.js',
to: buildTo,
babel: true,
uglify: true,
watch: true
},
{
name: 'series',
from: buildFrom + 'series/*.js',
to: buildTo + 'series/',
babel: true,
uglify: true,
watch: true
},
{
name: 'utils',
from: buildFrom + 'utils/*.js',
to: buildTo + 'utils/',
babel: true,
uglify: true,
watch: true
},
{
name: 'types',
from: buildFrom + 'index.d.ts',
to: buildTo,
babel: false,
uglify: false,
watch: true
}
]
// 清空之前的打包文件
function clear (paths = [buildTo]) {
return function clear (cb) {
del(paths, cb)
}
}
function buildItem (config) {
return {
[config.name]: cb => {
let res = gulp.src(config.from)
config.babel && (res = res.pipe(babel()))
config.uglify && (res = res.pipe(uglify()))
return res.pipe(gulp.dest(config.to))
}
}[config.name]
}
// 执行构建
function build (cb) {
return gulp.parallel(
...buildConfig.map(config => buildItem(config))
)
}
// 监听
if (cmdParams.watch) {
buildConfig.forEach(config => {
if (config.watch) {
const paths = config.from.split('/')
const last = paths[paths.length - 1]
const fileScope = '/' + (paths[paths.length - 2] === '**' ? '**/' + last : last)
gulp.watch([
config.from
], gulp.series(
clear([buildTo + fileScope]),
buildItem(config)
))
}
})
}
exports.default = gulp.series(
clear(),
build()
)