-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
138 lines (127 loc) · 3.9 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
const gulp = require("gulp");
const clean = require("gulp-clean");
const cleanCSS = require('gulp-clean-css');
const htmlclean = require('gulp-htmlclean');
const htmlmin = require('gulp-html-minifier-terser');
const fontmin = require('gulp-fontmin');
const shell = require("gulp-shell");
const terser = require('gulp-terser');
const workbox = require("workbox-build");
const fs = require('fs');
const path = require('path')
const axios = require('axios').default;
// clean
gulp.task("clean", function () {
return gulp.src("./public", { read: false, allowEmpty: true })
.pipe(clean());
});
// 生成 sw
gulp.task('generate-service-worker', () => {
return workbox.injectManifest({
swSrc: './sw-template.js',
swDest: './public/sw.js',
globDirectory: './public',
globPatterns: [
"**/*.{html,css,js,json,woff2}"
],
modifyURLPrefix: {
"": "./"
}
});
});
// 压缩js
gulp.task('compress', () =>
gulp.src(['./public/**/*.js', '!./public/**/*.min.js'])
.pipe(terser())
.pipe(gulp.dest('./public'))
)
//压缩css
gulp.task('minify-css', () => {
return gulp.src(['./public/**/*.css'])
.pipe(cleanCSS({
compatibility: 'ie11'
}))
.pipe(gulp.dest('./public'));
});
//压缩html
gulp.task('minify-html', () => {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
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
minifyURLs: true //压缩页面URL
}))
.pipe(gulp.dest('./public'))
});
//压缩字体
function minifyFont(text, cb) {
gulp
.src('./public/fonts/*.ttf') //原字体所在目录
.pipe(fontmin({
text: text
}))
.pipe(gulp.dest('./public/fontsdest/')) //压缩后的输出目录
.on('end', cb);
}
gulp.task('minify-font', (cb) => {
var buffers = [];
gulp
.src(['./public/**/*.html']) //HTML文件所在目录请根据自身情况修改
.on('data', function(file) {
buffers.push(file.contents);
})
.on('end', function() {
var text = Buffer.concat(buffers).toString('utf-8');
minifyFont(text, cb);
});
});
// url 是资源地址,如,http://wximg.233.com/attached/image/20160815/20160815162505_0878.png
// filepath 是文件下载的本地目录
// name 是下载后的文件名
async function downloadFile(url, filepath, name) {
if (!fs.existsSync(filepath)) {
fs.mkdirSync(filepath);
}
const mypath = path.resolve(filepath, name);
const writer = fs.createWriteStream(mypath);
const response = await axios({
url,
method: "GET",
responseType: "stream",
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
}
function getBingWallpaperInfoFile(cb){
const uri = 'https://efs.coding.net/p/lab/shared-depot/BingWallpaper/git/raw/master/BingWallpaper/BingWallpaperInfo.json'
downloadFile(uri,'./public/bing','BingWallpaperInfo.json').then((res)=>{
cb()
})
}
gulp.task('download', (cb)=>{
getBingWallpaperInfoFile(cb)
})
// 运行 gulp build 命令时依次执行以下任务
gulp.task("build", gulp.series(
// "clean",
"generate-service-worker",
"compress",
"minify-css",
"minify-html",
"minify-font",
"download",
));