-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
45 lines (40 loc) · 1.95 KB
/
Copy pathgulpfile.js
File metadata and controls
45 lines (40 loc) · 1.95 KB
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
const gulp = require('gulp');
const cleancss = require('gulp-clean-css');
const uglify = require('gulp-uglify-es').default;
const htmlmin = require('gulp-html-minifier-terser');
// 压缩 dist 目录下的 css 文件。
// 可接受参数的文档:https://github.com/jakubpawlowicz/clean-css#constructor-options。
gulp.task('minify-css', () => {
return gulp.src('./dist/**/*.css') // 处理 dist 目录下所有的 css 文件,下同。
.pipe(cleancss({ compatibility: 'ie8' })) // 兼容到 IE8。
.pipe(gulp.dest('./dist'));
});
// 压缩 dist 目录下的 js 文件。
gulp.task('minify-js', () => {
return gulp.src('./dist/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./dist'));
});
// 压缩 dist 目录下的 html 文件。
// 可接受参数的文档:https://github.com/terser/html-minifier-terser#options-quick-reference。
gulp.task('minify-html', () => {
return gulp.src('./dist/**/*.html')
.pipe(htmlmin({
removeComments: true, // 移除注释。
removeEmptyAttributes: true, // 移除值为空的参数。
removeRedundantAttributes: true, // 移除值跟默认值匹配的属性。
collapseBooleanAttributes: true, // 省略布尔属性的值。
collapseWhitespace: true, // 移除空格和空行。
minifyCSS: true, // 压缩 HTML 中的 CSS。
minifyJS: true, // 压缩 HTML 中的 JS。
minifyURLs: true, // 压缩 HTML 中的链接。
ignoreCustomFragments: [
/<pre class="mermaid">[\s\S]*?<\/pre>/ // 忽略 Mermaid 块,避免压缩破坏流程图语法。
]
}))
.pipe(gulp.dest('./dist'));
});
// 默认任务。不带任务名运行 gulp 时执行的任务。
gulp.task('default', gulp.parallel(
'minify-css', 'minify-js', 'minify-html'
));