-
Notifications
You must be signed in to change notification settings - Fork 38
/
gulpfile.js
195 lines (172 loc) · 5.15 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
const config = require('./config')
const path = require('path')
const chalk = require('chalk')
const gulp = require('gulp')
const gulpif = require('gulp-if')
const htmlmin = require('gulp-htmlmin')
const fileinclude = require('gulp-file-include')
const sass = require('gulp-sass')
const postcss = require('gulp-postcss')
const cleanCSS = require('gulp-clean-css')
const plumber = require('gulp-plumber')
const notify = require('gulp-notify')
const cache = require('gulp-cache')
const imagemin = require('gulp-imagemin')
const pngquant = require('imagemin-pngquant')
const uglify = require('gulp-uglify')
const eslint = require('gulp-eslint')
const stripDebug = require('gulp-strip-debug')
const babel = require('gulp-babel')
const sequence = require('gulp-sequence')
const zip = require('gulp-zip')
const del = require('del')
// webpack
const webpack = require('webpack')
const webpackStream = require('webpack-stream')
const webpackConfig = require('./webpack.config.js')
// server
const browserSync = require('browser-sync').create()
const reload = browserSync.reload
// NODE_ENV
const env = process.env.NODE_ENV || 'development'
const condition = env === 'production'
function respath(dir) {
return path.join(__dirname, './', dir)
}
function onError(error) {
const title = error.plugin + ' ' + error.name
const msg = error.message
const errContent = msg.replace(/\n/g, '\\A ')
notify.onError({
title: title,
message: errContent,
sound: true
})(error)
this.emit('end')
}
function cbTask(task) {
return new Promise((resolve, reject) => {
del(respath('dist'))
.then(paths => {
console.log(chalk.green(`
-----------------------------
Clean tasks are completed
-----------------------------`))
sequence(task, () => {
console.log(chalk.green(`
-----------------------------
All tasks are completed
-----------------------------`))
resolve('completed')
})
})
})
}
gulp.task('html', () => {
return gulp.src(config.dev.html)
.pipe(plumber(onError))
.pipe(fileinclude({
prefix: '@@',
basepath: respath('src/include/')
}))
.pipe(gulpif(condition, htmlmin({
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true
})))
.pipe(gulp.dest(config.build.html))
})
gulp.task('styles', () => {
return gulp.src(config.dev.styles)
.pipe(plumber(onError))
.pipe(sass())
.pipe(gulpif(condition, cleanCSS({debug: true})))
.pipe(postcss('./.postcssrc.js'))
.pipe(gulp.dest(config.build.styles))
})
gulp.task('images', () => {
return gulp.src(config.dev.images)
.pipe(plumber(onError))
.pipe(cache(imagemin({
progressive: true, // 无损压缩JPG图片
svgoPlugins: [{removeViewBox: false}], // 不移除svg的viewbox属性
use: [pngquant()] // 使用pngquant插件进行深度压缩
})))
.pipe(gulp.dest(config.build.images))
})
gulp.task('eslint', () => {
return gulp.src(config.dev.script)
.pipe(plumber(onError))
.pipe(gulpif(condition, stripDebug()))
.pipe(eslint({ configFle: './.eslintrc' }))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
})
const useEslint = config.useEslint ? ['eslint'] : [];
gulp.task('script', useEslint, () => {
return gulp.src(config.dev.script)
.pipe(plumber(onError))
.pipe(gulpif(condition, babel({
presets: ['env']
})))
.pipe(gulpif(config.useWebpack, webpackStream(webpackConfig, webpack)))
.pipe(gulpif(condition, uglify()))
.pipe(gulp.dest(config.build.script))
})
gulp.task('static', () => {
return gulp.src(config.dev.static)
.pipe(gulp.dest(config.build.static))
})
gulp.task('clean', () => {
del('./dist').then(paths => {
console.log('Deleted files and folders:\n', paths.join('\n'));
});
})
gulp.task('watch', () => {
gulp.watch(config.dev.allhtml, ['html']).on('change', reload)
gulp.watch(config.dev.styles, ['styles']).on('change', reload)
gulp.watch(config.dev.script, ['script']).on('change', reload)
gulp.watch(config.dev.images, ['images']).on('change', reload)
gulp.watch(config.dev.static, ['static']).on('change', reload)
})
gulp.task('zip', () => {
return gulp.src(config.zip.path)
.pipe(plumber(onError))
.pipe(zip(config.zip.name))
.pipe(gulp.dest(config.zip.dest))
})
gulp.task('server', () => {
const task = ['html', 'styles', 'script', 'images', 'static']
cbTask(task).then(() => {
browserSync.init(config.server)
console.log(chalk.cyan(' Server complete.\n'))
gulp.start('watch')
})
})
gulp.task('build', () => {
const task = ['html', 'styles', 'script', 'images', 'static']
cbTask(task).then(() => {
console.log(chalk.cyan(' Build complete.\n'))
if (config.productionZip) {
gulp.start('zip', () => {
console.log(chalk.cyan(' Zip complete.\n'))
})
}
})
})
gulp.task('default', () => {
console.log(chalk.green(
`
Build Setup
开发环境: npm run dev
生产环境: npm run build
执行压缩: gulp zip
编译页面: gulp html
编译脚本: gulp script
编译样式: gulp styles
语法检测: gulp eslint
压缩图片: gulp images
`
))
})