-
Notifications
You must be signed in to change notification settings - Fork 12
/
gulpfile.js
233 lines (217 loc) · 6.57 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
/*
* @Author: Ryan
* @Date: 2019-05-06 14:50:28
* @Last Modified by: Ryan
* @Last Modified time: 2020-03-11 18:38:33
*/
'use strict'
/* = Gulp
-------------------------------------------------------------- */
// include package
const { series, parallel, src, dest, watch } = require('gulp')
const sass = require('gulp-sass')
const autoprefixer = require('autoprefixer')
const uglify = require('gulp-uglify')
const imagemin = require('gulp-imagemin')
const pngquant = require('imagemin-pngquant')
const connect = require('gulp-connect')
const proxy = require('http-proxy-middleware')
const sourcemaps = require('gulp-sourcemaps')
const changed = require('gulp-changed')
const fileinclude = require('gulp-file-include')
const rename = require('gulp-rename')
const babel = require('gulp-babel')
const base64 = require('gulp-base64')
const postcss = require('gulp-postcss')
const pxtoviewport = require('postcss-px-to-viewport')
const pxtorem = require('postcss-pxtorem')
const del = require('del')
const through2 = require('through2')
const gulpWebpack = require('webpack-stream')
// include config
const config = require('./gulp.env')
// sass compiler
sass.compiler = require('node-sass')
/* = Task List
-------------------------------------------------------------- */
// html
function html () {
return src(config.paths.html + '/**/!(m_)*.html')
.pipe(config.isDev ? changed(config.pathsDev.html) : through2.obj())
.pipe(
fileinclude({
prefix: '@@',
basepath: '@file'
})
)
.pipe(dest(config.pathsDev.html))
.pipe(connect.reload())
}
exports.html = html
// styles
let processors
switch (config.cssUnit) {
case 'rem':
processors = [pxtorem(config.pxtoremConfig)]
break;
case 'viewport':
processors = [pxtoviewport(config.pxtoviewportConfig)]
break;
default:
processors = ''
break;
}
function styles () {
return src(config.paths.css + '/*.scss')
.pipe(config.isDev ? sourcemaps.init() : through2.obj())
.pipe(
sass({
outputStyle: config.sassStyle
}).on('error', sass.logError)
)
.pipe(postcss([autoprefixer(config.autoprefixerConfig)]))
.pipe(processors ? postcss(processors) : through2.obj())
.pipe(base64(config.base64Config))
.pipe(
sass({
outputStyle: config.sassStyle
}).on('error', sass.logError)
)
.pipe(config.sourceMap ? sourcemaps.write('maps') : through2.obj())
.pipe(dest(config.pathsDev.css))
.pipe(connect.reload())
}
exports.styles = styles
// images
function images () {
return src([
config.paths.image + '/**/*',
'!' + config.paths.image + '/sprite/*'
])
.pipe(!config.isDev ? changed(config.pathsDev.image) : through2.obj())
.pipe(
!config.isDev
? imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ quality: 75, progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{ removeViewBox: true },
{ cleanupIDs: false }
]
})
])
: through2.obj()
)
.pipe(dest(config.pathsDev.image))
.pipe(connect.reload())
}
exports.images = images
// scripts
const webpackConfig = require('./webpack.config')
function scripts () {
return src([config.paths.script + '/*.js'])
.pipe(config.isDev ? sourcemaps.init() : through2.obj())
.pipe(changed(config.pathsDev.script))
.pipe(config.useWebpack ? gulpWebpack(webpackConfig) : through2.obj())
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(uglify())
.pipe(
rename({
suffix: '.min'
})
)
.pipe(config.sourceMap ? sourcemaps.write('maps') : through2.obj())
.pipe(dest(config.pathsDev.script))
.pipe(connect.reload())
}
exports.scripts = scripts
// local server
let serverOptions = {
name: 'ENV:' + config.env,
root: config.pathsDev.html,
host: '0.0.0.0',
port: 8000,
livereload: true
}
if (config.proxyOptions.changeOrigin) {
serverOptions = {
name: 'ENV:' + config.env,
root: config.pathsDev.html,
host: '0.0.0.0',
port: 8000,
livereload: true,
middleware: () => {
return [proxy('/api', config.proxyOptions)]
}
}
}
function server () {
connect.server(serverOptions)
}
exports.server = server
// copy css
function copycss () {
return src([config.paths.html + '/lib/*.css']).pipe(
dest(config.pathsDev.css)
)
}
exports.copycss = copycss
// copy js
function copyjs () {
return src([config.paths.html + '/lib/*.js']).pipe(
dest(config.pathsDev.script)
)
}
exports.copyjs = copyjs
// watch
function watchList () {
watch(config.paths.html + '/**/*.html', series(html))
watch(config.paths.css + '/*.scss', series(styles))
watch(config.paths.image + '/**/*', series(images))
watch(config.paths.script + '/*.js', series(scripts))
watch(config.paths.html + '/lib/*.css', series(copycss))
watch(config.paths.html + '/lib/*.js', series(copyjs))
}
exports.watch = watchList
// clean
function clean () {
return del([config.pathsDev.html + '/**']).then(() => {
console.log(config.env + '项目初始化清理完成...')
})
}
exports.clean = clean
// default
exports.default = series(parallel(server, watchList))
// init/build
exports.init = series(
clean,
html,
styles,
images,
scripts,
copycss,
copyjs,
done => {
console.log(config.env + '项目初始化构建完成...')
done()
}
)
// const webpackConfig = require('./webpack.config')
// function assets () {
// return new Promise((resolve, reject) => {
// webpack(webpackConfig, (err, stats) => {
// if (err) {
// return reject(err)
// }
// if (stats.hasErrors()) {
// return reject(new Error(stats.compilation.errors.join('\n')))
// }
// resolve()
// })
// })
// }
// exports.assets = assets