/
gulpfile.js
182 lines (160 loc) · 3.77 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
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
const gulp = require('gulp');
const fs = require('fs');
const path = require('path')
const del = require('del');
const chalk = require('chalk');
let $ = require('gulp-load-plugins')({
// pattern: ['gulp-*'],
})
//请求地址
const httpBaseUrl = {
'dev':'http:127.0.0.1:9001'
}
function httpHandler(){
}
let env = 'dev' //开发环境 dev prod release
//环境设置
function setDevEnv(cd){
env = 'dev'
cd()
}
function setReleaseEnv(cd){
env = 'release'
cd()
}
function setProdEnv(cd){
env = 'prod'
cd()
}
//打印函数
function logHandler(type,info){
switch(type){
case 'log':
console.log(chalk.blue(info))
break;
}
}
//处理js文件
function jsHandler(cd,path = './src/**/*.js') {
return gulp.src(path)
.pipe($.replace('HTTP_BASE_URL', httpBaseUrl[env]))
.pipe($.mpNpm())
.pipe($.if(env!='dev',$.stripDebug())) //去除js文件中的console,同时也会将错误信息打印去掉,按需求配置
.pipe($.if(env!='dev',$.uglify({
output: {
annotations: false, //去除注释
}
})))
.pipe(gulp.dest('./dist/'));
}
//处理json文件
function jsonHandler(cd,path = './src/**/*.json') {
return gulp.src(path)
.pipe($.mpNpm({fullExtract: ['@vant/weapp/wxs']}))
.pipe(gulp.dest('./dist/'));
}
//处理less文件
function lessHandler(cd,path = './src/**/*.less') {
return gulp.src(path)
.pipe($.less()) //转换less到css
.pipe($.minifyCss()) //压缩
.pipe($.rename(function(path){
path.extname = ".wxss";
}))
.pipe(gulp.dest('./dist/'));
}
//处理wxml文件
function wxmlHandler(cd,path ='./src/**/*.wxml') {
return gulp.src(path)
.pipe(gulp.dest('./dist/'));
}
//删除文件操作
function delFileHandler(cb,filePath){
return del([filePath],cb);
}
//删除dist
function delDistFile(cb){
return delFileHandler(cb,'dist')
}
//监控文件变化
function watchFile(cb) {
const watcher = gulp.watch(['src']);
watcher.on('change', function (path, stats) {
logHandler('log',`File ${path} was changed`);
watchFileHandler('change',path)
});
watcher.on('add', function (path, stats) {
logHandler('log',`File ${path} was added`);
watchFileHandler('add',path)
});
watcher.on('unlink', function (path, stats) {
logHandler('log',`File ${path} was removed`);
let _path = path.replace('src/','dist/')
delFileHandler(_path)
});
cb();
}
//文件变化后的处理任务
function watchFileHandler(type,filePath){
const _extname = path.extname(filePath)
switch(_extname){
case '.less':
lessHandler(filePath)
break;
case '.js':
jsHandler(filePath)
break;
case '.json':
jsonHandler(filePath)
break;
case '.wxml':
wxmlHandler(filePath)
break;
default:
break;
}
}
//默认任务 dev
gulp.task('default', gulp.series(
setDevEnv,
delDistFile,
gulp.parallel(
jsHandler,
lessHandler,
jsonHandler,
wxmlHandler,
),
watchFile
))
//正式环境 release
gulp.task('release', gulp.series(
setReleaseEnv,
delDistFile,
gulp.parallel(
jsHandler,
lessHandler,
jsonHandler,
wxmlHandler,
),
watchFile
))
//生产环境 pro
gulp.task('prod', gulp.series(
setProdEnv,
delDistFile,
gulp.parallel(
jsHandler,
lessHandler,
jsonHandler,
wxmlHandler,
),
watchFile
))
// 文件监控任务
gulp.task('watch', gulp.series(
watchFile
))
//清除任务
gulp.task('clean',gulp.series(
delDistFile
))