-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
161 lines (138 loc) · 3.92 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
const gulp = require('gulp')
const rename = require('gulp-rename')
const del = require('del')
const postcss = require('gulp-postcss')
const pxtorpx = require('postcss-px2rpx')
// 字体文件转化为base64
const base64 = require('postcss-font-base64')
const htmlmin = require('gulp-htmlmin')
const sass = require('gulp-sass')
const combiner = require('stream-combiner2')
const jsonminify = require('gulp-jsonminify')
const cssnano = require('gulp-cssnano')
const runSequence = require('run-sequence')
const colors = require('ansi-colors')
const log = require('fancy-log')
const filter = require('gulp-filter')
// 引入需要用到的 npm 包
// 生产发布打包流程
const sourcemaps = require('gulp-sourcemaps') // 方便本地debuge代码
const jdists = require('gulp-jdists')
const through = require('through2')
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const argv = require('minimist')(process.argv.slice(2))
// 判断 gulp --type prod 命名 type 是否是生产打包
const isProd = argv.type === 'prod'
const src = './client'
const dist = './dist'
const handleError = (err) => {
console.log('\n')
log(colors.red('Error!'))
log('fileName: ' + colors.red(err.fileName))
log('lineNumber: ' + colors.red(err.lineNumber))
log('message: ' + err.message)
log('plugin: ' + colors.yellow(err.plugin))
}
// Gulp 是以 task 为核心的打包工具,
// 针对不同的文件类型(比如通过正则过滤)
// 可以配置不同的流程控制。
gulp.task('wxml', () => {
return gulp
.src(`${src}/**/*.wxml`)
.pipe(gulp.dest(dist))
})
gulp.task('wxss', () => {
const combined = combiner.obj([
gulp.src(`${src}/**/*.{wxss,scss}`),
sass().on('error', sass.logError),
postcss([pxtorpx(), base64()]),
isProd
? cssnano({
autoprefixer: false,
discardComments: {removeAll: true}
})
: through.obj(),
rename((path) => (path.extname = '.wxss')),
gulp.dest(dist)
])
combined.on('error', handleError)
})
gulp.task('js', () => {
const f = filter((file) => !/(mock)/.test(file.path))
gulp
.src(`${src}/**/*.js`)
.pipe(isProd ? f : through.obj())
.pipe(
isProd
? jdists({
trigger: 'prod'
})
: jdists({
trigger: 'dev'
})
)
.pipe(isProd ? through.obj() : sourcemaps.init())
.pipe(
babel({
presets: ['env']
})
)
.pipe(
isProd
? uglify({
compress: true
})
: through.obj()
)
.pipe(isProd ? through.obj() : sourcemaps.write('./'))
.pipe(gulp.dest(dist))
})
gulp.task('wxs', () => {
return gulp.src(`${src}/**/*.wxs`).pipe(gulp.dest(dist))
})
gulp.task('images', () => {
return gulp.src(`${src}/images/**`).pipe(gulp.dest(`${dist}/images`))
})
gulp.task('json', () => {
return gulp.src(`${src}/**/*.json`).pipe(isProd ? jsonminify() : through.obj()).pipe(gulp.dest(dist))
})
// 聚合类
gulp.task('watch', () => {
;['wxml', 'wxss', 'js', 'json', 'wxs'].forEach((v) => {
gulp.watch(`${src}/**/*.${v}`, [v])
})
gulp.watch(`${src}/images/**`, ['images'])
gulp.watch(`${src}/**/*.scss`, ['wxss'])
})
gulp.task('clean', () => {
return del(['./dist/**'])
})
gulp.task('dev', ['clean'], () => {
runSequence('json', 'images', 'wxml', 'wxss', 'js', 'wxs', 'cloud', 'watch')
})
gulp.task('build', ['clean'], () => {
runSequence('json', 'images', 'wxml', 'wxss', 'js', 'wxs', 'cloud')
})
// cloud-functions 处理方法
const cloudPath = './server/cloud-functions'
gulp.task('cloud', () => {
return gulp
.src(`${cloudPath}/**`)
.pipe(
isProd
? jdists({
trigger: 'prod'
})
: jdists({
trigger: 'dev'
})
)
.pipe(gulp.dest(`${dist}/cloud-functions`))
})
gulp.task('watch:cloud', () => {
gulp.watch(`${cloudPath}/**`, ['cloud'])
})
gulp.task('cloud:dev', () => {
runSequence('cloud', 'watch:cloud')
})