-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
318 lines (275 loc) · 7.78 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
var gulp = require('gulp');
var webpack = require('webpack-stream');
var del = require("del");
var connect = require('gulp-connect');
// 引入组件
var minifycss = require('gulp-minify-css'),//css压缩
concat = require('gulp-concat'),//文件合并
uglify = require('gulp-uglify'),//js压缩
htmlmin = require('gulp-htmlmin'),
notify = require('gulp-notify'),//提示信息
rev = require('gulp-rev'),
revC = require('gulp-rev-collector') ;
const eslint = require('gulp-eslint');
var src = {
html: "src/html/*.html", // html 文件
vendor: ["src/vendor/**/*", "bower_components/**/*"], // vendor 目录和 bower_components
jspath: ["src/js/**/*.js", "src/js/*.js"],
style: "src/css/*.css", // style 目录下所有 xx/index.less
assets: "src/assets/**/*", // 图片等应用资源
statichtml: "src/statichtml/*.html" // 静态html(python)
};
var dist = {
root: "dist/",
html: "dist/",
style: "dist/static/css",
js: "dist/static/js",
vendor: "dist/static/vendor",
assets: "dist/static/assets",
assetsjs: "dist/static/assets/js",
statichtml: "dist/static/html/" // 静态html(python)
};
var bin = {
root: "bin/",
html: "bin/",
style: "bin/static/css",
js: "bin/static/js",
vendor: "bin/static/vendor",
assets: "bin/static/assets",
assetsjs: "bin/static/assets/js",
statichtml: "bin/static/html/" // 静态html(python)
};
/**
* ----------------------------------------------------
* tasks
* ----------------------------------------------------
*/
/**
* clean build dir
*/
function cleandist(done) {
del.sync(dist.root);
done();
}
function cleanhtml(done) {
del.sync("dist/*.html");
del.sync("dist/static/html/*.html");
del.sync("dist/static/js");
del.sync("dist/static/css");
done();
}
/**
* [cleanBin description]
* @return {[type]} [description]
*/
function cleanBin(done) {
del.sync(bin.root);
done();
}
/**
* [copyVendor description]
* @return {[type]} [description]
*/
function copyVendor() {
return gulp.src(src.vendor)
.pipe(gulp.dest(dist.vendor));
}
function copybuildVendor() {
return gulp.src(src.vendor)
.pipe(gulp.dest(bin.vendor));
}
/**
* [copyAssets description]
* @return {[type]} [description]
*/
function copyAssets() {
return gulp.src(src.assets)
.pipe(gulp.dest(dist.assets));
}
function copybuildAssets() {
return gulp.src(src.assets)
.pipe(gulp.dest(bin.assets));
}
// 合并、压缩js文件
function buildjs() {
return gulp.src('src/assets/js/*.js')
// .pipe(concat('all.js'))
.pipe(gulp.dest(bin.assetsjs))
// .pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest(bin.assetsjs))
.pipe(notify({message: 'js task ok'}));
}
/**
* [style description]
* @param {Function} done [description]
* @return {[type]} [description]
*/
function style() {
return gulp.src('src/css/*.css')
.pipe(concat('main.css'))
.pipe(minifycss())
.pipe(rev())
.pipe(gulp.dest(bin.style))
.pipe(rev.manifest())
.pipe(gulp.dest(bin.style))
.pipe(notify({message: 'css task ok'}));
}
function styledev() {
return gulp.src('src/css/*.css')
.pipe(concat('main.css'))
.pipe(rev())
.pipe(gulp.dest(dist.style))
.pipe(rev.manifest())
.pipe(gulp.dest(dist.style))
.pipe(notify({message: 'css task ok'}));
}
//Html替换css、js引用文件版本
function revHtml() {
return gulp.src(['dist/**/*.json', src.html])
.pipe(revC({
replaceReved: true,
}))
.pipe(gulp.dest(dist.html));
}
//Html替换css、js引用文件版本
function revstaticHtml() {
return gulp.src(src.statichtml)
.pipe(gulp.dest(dist.statichtml));
}
function revbuildstaticHtml() {
var options = {
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
minifyJS: true,//压缩页面JS
minifyCSS: true//压缩页面CSS
};
return gulp.src(['bin/**/*.json', src.statichtml])
.pipe(htmlmin(options))
.pipe(revC())
.pipe(gulp.dest(bin.statichtml));
}
function revbuildHtml() {
var options = {
removeComments: true,//清除HTML注释
collapseWhitespace: true,//压缩HTML
collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
minifyJS: true,//压缩页面JS
minifyCSS: true//压缩页面CSS
};
return gulp.src(['bin/**/*.json', src.html])
.pipe(htmlmin(options))
.pipe(revC())
.pipe(gulp.dest(bin.html));
}
/**
* [connectServer description]
* @return {[type]} [description]
*/
function connectServer(done) {
connect.server({
root: dist.root,
port: 8080,
livereload: true,
});
done();
}
/**
* [watch description]
* @return {[type]} [description]
*/
function watch() {
gulp.watch("src/**/*.js",gulp.series( webpackDevelopment,revHtml));
gulp.watch("src/**/*.css", gulp.series( styledev,revHtml));
gulp.watch(src.html, revHtml);
gulp.watch("dist/**/*").on('change', function (file) {
gulp.src('dist/')
.pipe(connect.reload());
});
}
/**
* default task
*/
gulp.task("default", gulp.series(
//cleandist,
cleanhtml,
checkjs,
// copyAssets,
gulp.parallel(copyVendor, styledev, webpackDevelopment),
revstaticHtml,
revHtml,
connectServer,
watch
));
/**
* production build task
*/
gulp.task("build", gulp.series(
cleanBin,
checkjs,
copybuildAssets,
buildjs,
gulp.parallel(copybuildVendor, style, webpackProduction),
revbuildHtml,
revbuildstaticHtml,
function (done) {
console.log('build success');
done();
}
));
/**
* [handleError description]
* @param {[type]} err [description]
* @return {[type]} [description]
*/
function handleError(err) {
if (err.message) {
console.log(err.message)
} else {
console.log(err)
}
this.emit('end')
}
/**
* [reload description]
* @return {[type]} [description]
*/
function reload() {
connect.reload();
}
function checkjs() {
return gulp.src(src.jspath)
.pipe(eslint())
.pipe(eslint.formatEach('compact', process.stderr))
.pipe(eslint.failAfterError());
}
exports.checkjs = checkjs;
function webpackDevelopment() {
return gulp.src('src/js/index.js')
.pipe(webpack({
config: require('./webpack.dev.js'),
}))
.pipe(rev())
.pipe(gulp.dest(dist.js))
.pipe(rev.manifest())
.pipe(gulp.dest(dist.js))
.pipe(notify({message: 'devjs task ok'}));
}
function webpackProduction() {
return gulp.src('src/js/index.js')
.pipe(webpack({
config: require('./webpack.prod.js'),
}))
.pipe(rev())
.pipe(gulp.dest(bin.js))
.pipe(rev.manifest())
.pipe(gulp.dest(bin.js))
.pipe(notify({message: 'projs task ok'}));
}