-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgulpfile.js
243 lines (222 loc) · 7.24 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* eslint no-console: 0 */
const path = require('path');
const chalk = require('chalk');
const moment = require('moment');
const del = require('del');
const gulp = require('gulp');
const less = require('gulp-less');
const changed = require('gulp-changed');
const rename = require('gulp-rename');
const file = require('gulp-file');
const babel = require('gulp-babel');
const addLessImport = require('./gulp-plugin-add-less-import');
const wxImport = require('./gulp-plugin-wx');
const uploadCdn = require('./gulp-plugin-cdn');
const config = require('./src/common/config');
const uploadFile = require('./bin/upload_assets');
gulp.task('copy-less-config', copyLessConfig);
gulp.task('transform-less', transformLess);
gulp.task('delete-dist-theme', deleteDistThemeFile);
gulp.task('copy-wx-file', copyWxFile);
gulp.task('clean', clean);
gulp.task('babel', babelJs);
gulp.task('watch', watchTaskFunc);
gulp.task('cdn', cdnTask);
gulp.task('less', gulp.series('copy-less-config', 'transform-less', 'delete-dist-theme'));
gulp.task('build', gulp.parallel('cdn', 'less', 'copy-wx-file', 'babel'));
gulp.task('dev', gulp.series('build', 'watch'));
// 上传静态文件(图片资源)
function cdnTask(done) {
gulp.src('assets/**/*', { allowEmpty: true })
.pipe(uploadCdn())
.on('error', err => console.error(err.stack || err))
.on('finish', done);
}
// 拷贝 less 配置文件
function copyLessConfig(done) {
gulp.src('src/theme/config.less', { allowEmpty: true })
.pipe(file('config.less', `@cdn: ~'${config.cdn}';`))
.pipe(changed('src/theme'))
.pipe(gulp.dest('src/theme'))
.on('finish', done);
}
// 监听任务
function watchTaskFunc(done) {
// 所有的事件:add, addDir, change, unlink, unlinkDir, ready, raw, error
// 监听
gulp.watch(['src/**/**', 'assets/**/*'], { allowEmpty: true })
.on('all', watch)
.on('ready', () => {
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.green('编译完成,请打开微信开发者工具查看调试...')}`);
});
}
function watch(type, filePath) {
changeLog({ type, filePath });
const imgDirR = /^(assets)/;
// 静态资源(图片资源)
if (imgDirR.test(filePath)) {
if (type !== 'unlink' && type !== 'unlinkDir') {
uploadFile(filePath);
}
return;
}
const r = /(wxml|json|wxs)$/;
const cssR = /(less)$/;
const jsR = /(js)$/;
const { dir, ext, name } = path.parse(filePath);
const distDir = dir.replace('src', 'dist');
// 删除
if (type === 'unlink' || type === 'unlinkDir') {
// 删除 js, json, wxml, wxs 文件
if (r.test(ext)) {
del(filePath.replace('src', 'dist'));
return;
}
// 删除 wxss 样式文件
if (cssR.test(ext)) {
del(`${dir.replace('src', 'dist')}${name}.wxss`);
return;
}
del(filePath.replace('src', 'dist'));
return;
}
// 复制 json, wxml 文件
if (r.test(ext)) {
gulp.src([filePath], { allowEmpty: true })
.pipe(changed(distDir))
.pipe(gulp.dest(distDir))
.on('finish', () => {
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.magenta('复制完成:')}${chalk.green(`${distDir}/${name}${ext}`)}`);
});
return;
}
// 编译 JS 文件
if (jsR.test(ext)) {
gulp.src([filePath, '!src/**/*/*.default.js'], { allowEmpty: true })
.pipe(wxImport({
wxPath: path.join(__dirname, './src/utils/wx.js')
}))
.pipe(babel())
.on('error', err => console.error(err.stack || err))
.pipe(changed(distDir))
.pipe(gulp.dest(distDir))
.on('finish', () => {
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.magenta('编译完成:')}${chalk.green(`${distDir}/${name}${ext}`)}`);
});
return;
}
// 编译 less 样式文件
if (cssR.test(ext)) {
// 全局样式处理
if (name === 'app') {
gulp.src('src/components/**/*.less')
.pipe(addLessImport({
themePath: path.join(__dirname, './src/theme/index.less'),
commomPath: path.join(__dirname, './src/app.less'),
}))
.pipe(less({
paths: [path.join(__dirname, './src/theme')]
}))
.on('error', err => console.error(err.stack || err))
.pipe(rename((path) => {
path.extname = '.wxss';
}))
.pipe(gulp.dest('dist/components/'))
.on('finish', () => {
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.magenta('编译完成:')}${chalk.green('dist/components/**.wxss')}`);
});
}
gulp.src(filePath)
.pipe(addLessImport({
themePath: path.join(__dirname, './src/theme/index.less'),
commomPath: path.join(__dirname, './src/app.less'),
}))
.pipe(less({
paths: [path.join(__dirname, './src/theme')],
}))
.on('error', err => console.error(err.stack || err))
.pipe(rename((path) => {
path.extname = '.wxss';
}))
.pipe(gulp.dest(distDir))
.on('finish', () => {
if (distDir.indexOf('dist/theme') !== -1) {
del(['dist/theme']);
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.magenta('修改theme:')}${chalk.green(`${distDir}/${name}${ext}`)}`);
return;
}
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.magenta('编译完成:')}${chalk.green(`${distDir}/${name}.wxss`)}`);
});
return;
}
}
// 拷贝 微信 json, wxml, wxs 文件
function copyWxFile(done) {
gulp.src([
'src/*.json',
'src/**/*/*.json',
'src/**/*/*.wxml',
'src/**/*/*.wxs',
])
.pipe(changed('./dist'))
.pipe(gulp.dest('./dist'))
.on('finish', done);
}
// babel 转移 Js 文件 开启 async/await 等新特性
function babelJs(done) {
gulp.src([
'src/*.js',
'src/**/*/*.js',
'!src/**/*/*.default.js',
])
.pipe(wxImport({
wxPath: path.join(__dirname, './src/utils/wx.js')
}))
.pipe(babel())
.on('error', err => console.error(err.stack || err))
.pipe(changed('./dist'))
.pipe(gulp.dest('./dist'))
.on('finish', done);
}
// 编译 less 文件
function transformLess(done) {
gulp.src(['./src/**/*/*.less', './src/*.less'])
.pipe(addLessImport({
themePath: path.join(__dirname, './src/theme/index.less'),
commomPath: path.join(__dirname, './src/app.less'),
}))
.pipe(less({
paths: [path.join(__dirname, './src/theme')],
}))
.on('error', err => console.error(err.stack || err))
.pipe(rename((path) => {
path.extname = '.wxss';
}))
.pipe(gulp.dest('./dist'))
.on('finish', done);
}
// 删除 输出目录中主题样式文件
function deleteDistThemeFile(done) {
del(['dist/theme']).then(() => done());
}
// 删除 输出目录
function clean(done) {
del([
'dist/**/*',
'!dist/**/*.config.json',
]).then(() => done());
}
// 打印监听日志
function changeLog(event) {
// add, addDir, change, unlink, unlinkDir, ready, raw, error
const actionName = {
change: '改变文件',
add: '添加文件',
addDir: '添加目录',
unlink: '删除文件',
unlinkDir: '删除文件夹',
error: '发生错误',
ready: '完成'
};
console.log(`[${chalk.gray(moment().format('HH:mm:ss'))}] ${chalk.cyan(`${actionName[event.type] || event.type}:`)}${chalk.green(event.filePath)}`);
}