-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathgulpfile.js
148 lines (130 loc) · 3.58 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
const gulp = require('gulp')
const rename = require('gulp-rename')
const del = require('del')
const through = require('through2')
const colors = require('ansi-colors')
const log = require('fancy-log')
const postcss = require('gulp-postcss')
const pxtorpx = require('postcss-px2rpx')
const base64 = require('postcss-font-base64')
const htmlmin = require('gulp-htmlmin')
const sass = require('gulp-sass')
const jsonminify = require('gulp-jsonminify')
const combiner = require('stream-combiner2')
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const cssnano = require('gulp-cssnano')
const runSequence = require('run-sequence')
const sourcemaps = require('gulp-sourcemaps')
const filter = require('gulp-filter')
const jdists = require('gulp-jdists')
const src = './client'
const dist = './dist'
const isProd = process.env.NODE_ENV === 'production' || false; // 'development'
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))
}
// task start
gulp.task('json', () => {
return gulp.src(`${src}/**/*.json`).pipe(isProd ? jsonminify() : through.obj()).pipe(gulp.dest(dist))
})
gulp.task('wxml', () => {
return gulp
.src(`${src}/**/*.wxml`)
.pipe(gulp.dest(dist))
})
gulp.task('wxs', () => {
return gulp.src(`${src}/**/*.wxs`).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('images', () => {
return gulp.src(`${src}/images/**`).pipe(gulp.dest(`${dist}/images`))
})
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('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')
})