-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathgulpfile.js
More file actions
133 lines (120 loc) · 4.44 KB
/
Copy pathgulpfile.js
File metadata and controls
133 lines (120 loc) · 4.44 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
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
const gulp = require('gulp');
const cleanCSS = require('gulp-clean-css');
const htmlmin = require('gulp-html-minifier-terser');
const htmlclean = require('gulp-htmlclean');
const terser = require('gulp-terser');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('gulp-babel');
// 压缩css文件
const minify_css = () => (
gulp.src(['./public/**/*.css', '!./public/{lib,lib/**}', '!./public/{libs,libs/**}', '!./public/{media,media/**}'])
.pipe(sourcemaps.init())
.pipe(cleanCSS({ compatibility: 'ie8' }))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('./public'))
);
// 压缩html文件
const minify_html = () => (
gulp.src(['./public/**/*.html', '!./public/{lib,lib/**}', '!./public/{libs,libs/**}', '!./public/{media,media/**}'])
.pipe(htmlclean({
// 保护 SVG 和 mathjax 相关属性
protect: /<gulp-ignore[\s\S]*?<\/gulp-ignore>|<mjx-container[\s\S]*?<\/mjx-container>|<svg[\s\S]*?<\/svg>/gi,
}))
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
ignoreCustomFragments: [
/<gulp-ignore[\s\S]*?<\/gulp-ignore>/, // 跳过 gulp-ignore 标签
/<mjx-container[\s\S]*?<\/mjx-container>/, // 跳过 MathJax 容器
/<svg[\s\S]*?<\/svg>/, // 跳过 SVG 标签
],
}))
.pipe(gulp.dest('./public'))
)
// 压缩js文件
const minify_js = () => (
gulp.src(['./public/**/*.js', '!./public/**/*.min.js', '!./public/{lib,lib/**}', '!./public/{libs,libs/**}', '!./public/{media,media/**}'])
.pipe(sourcemaps.init())
.pipe(babel({
presets: ['@babel/preset-env']
}))
.pipe(terser({
ecma: 2015,
ie8: true,
safari10: true,
output: { comments: false }
}))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('./public'))
)
gulp.task('minify', gulp.parallel(
minify_html,
minify_css,
minify_js
))
gulp.task('default', gulp.series('minify'));
// (CSP Level 3 :Safari 15.4+ Chrome 59+ Firefox 58+ Edge 79+)
// // CSP inline hash
// const hashstream = require('inline-csp-hash');
// const crypto = require('crypto');
// const hash = (s) => crypto.createHash("sha256").update(s).digest('base64');
// function getRandStr(len) {
// var str = '';
// for (var i = 0; i < len; i++) {
// str += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
// }
// return str;
// }
// const nonce = getRandStr(15);
// // script White list [scripts in event handlers (eg onclick)]. 包含压缩的 inline js
// unsafe_script_list = [
// "this.media='all';this.onload=null",
// 'this.media="all",this.onload=null',
// "errorImgAvatar(this)",
// "errorImgCover(this)",
// "return false;",
// "return!1",
// "history.back()",
// "history.forward()",
// "window.location.reload()",
// "VolantisApp.scrolltoElement(volantis.dom.bodyAnchor)",
// "volantis.rightmenu.jump('prev')",
// "volantis.rightmenu.jump('next')"
// ]
// // script hash White list
// unsafe_script_hash = ["'sha256-MXV1jvkHrZruEyFEOrQRjKs9WlPZC1V/3RLoKrkoDFQ='"]
// unsafe_script_list.forEach(e => {
// unsafe_script_hash.push("'sha256-" + hash(e) + "'")
// });
// gulp.task('csp_hash', () => {
// return gulp.src(['./public/**/*.html', '!./public/{lib,lib/**}', '!./public/{libs,libs/**}', '!./public/{media,media/**}'])
// .pipe(hashstream({
// what: 'script:not([type="application/ld+json"])',
// replace_cb: (s, hashes) => {
// unsafe_script_hash.push.apply(unsafe_script_hash, hashes);
// unsafe_script_hash = Array.from(new Set(unsafe_script_hash))
// return s
// }
// }))
// ;
// });
// gulp.task('csp_replace', () => {
// return gulp.src(['./public/**/*.html', '!./public/{lib,lib/**}', '!./public/{libs,libs/**}', '!./public/{media,media/**}'])
// .pipe(hashstream({
// what: 'script:not([type="application/ld+json"])',
// replace_cb: (s, hashes) => {
// unsafe_script_hash = Array.from(new Set(unsafe_script_hash))
// s = s.replace(/<script.*?>/g, function (match) {
// return match.replace(/>/g, ` nonce='${nonce}'>`);
// });
// s = s.replace(/script-src 'self'[^;]*/, `script-src 'self' https: 'unsafe-hashes' 'nonce-${nonce}' 'strict-dynamic' ` + unsafe_script_hash.join(" "))
// return s
// }
// }))
// .pipe(gulp.dest('./public'))
// ;
// });
// gulp.task('csp', gulp.series('csp_hash', 'csp_replace'));
// gulp.task('default', gulp.series('minify', 'csp'));