-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
420 lines (377 loc) · 12.2 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
const gulp = require('gulp')
const gulpif = require('gulp-if')
const replace = require('gulp-replace')
const rename = require('gulp-rename')
const crypto = require('crypto')
const hashMD5 = crypto.createHash('md5')
const hash512 = crypto.createHash('sha512')
const through2 = require('through2')
const htmlmin = require('gulp-htmlmin')
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const terser = require('gulp-terser')
const purify = require('gulp-purifycss')
const autoprefixer = require('gulp-autoprefixer')
const cleanCSS = require('gulp-clean-css')
const sass = require('gulp-sass')
const postcss = require('gulp-postcss')
const sorting = require('postcss-sorting')
sass.compiler = require('node-sass')
const svgmin = require('gulp-svgmin')
const clean = require('gulp-clean')
const destPath = 'dist'
function html() {
return gulp.src('src/index.html')
.pipe(replace(/src=["']?js\/passwd\.js["']?/g, function() {
return `src="js/passwd.js?v=${digest.get('passwd.js').sha512}"`
// ` integrity="sha512-${digest.get('passwd.js').sha512}"` +
// ` crossorigin="anonymous"`
}))
.pipe(replace(/href=["']?css\/style\.css["']?/, function() {
return `href="css/style.css?v=${digest.get('style.css').sha512}"`
// ` integrity="sha512-${digest.get('style.css').sha512}"` +
// ` crossorigin="anonymous"`
}))
.pipe(through2.obj(shortenCSSQueries))
// .pipe(through2.obj(inpsectFile))
.pipe(htmlmin({
collapseBooleanAttributes: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
removeAttributeQuotes: true,
removeComments: true,
sortAttributes: true,
sortClassName: true
}))
.pipe(gulp.dest(`${destPath}`))
}
function babelify(cb) {
gulp.src('src/js/passwd.js')
.pipe(babel({
presets: ['@babel/preset-env']
}))
.pipe(gulp.dest(`${destPath}/js/`))
cb();
}
function compress() {
return gulp.src('src/**/*.js')
.pipe(through2.obj(shortenCSSQueries))
.pipe(through2.obj(shortenLongJSWords))
.pipe(gulpif(/src\/js/, babel({
presets: ['@babel/preset-env']
})))
.pipe(terser({
compress: {
drop_console: true,
passes: 10,
reduce_vars: false,
// pure_funcs: ['shuffle', 'pickPosition'],
// pure_getters : true,
// unused: "keep_assign"
},
mangle: {
toplevel: true,
reserved: ['addAll'],
properties: {
// builtins: true,
// keep_quoted: 'strict',
// regex: /^(?!.*length).*$/g,
reserved: ['waitUntil', 'respondWith', 'request', 'type', 'addAll'],
// undeclared: true
},
},
toplevel: true,
}))
.pipe(through2.obj(computeHash))
// .pipe(rename(addHashToFileName))
.pipe(gulp.dest(`${destPath}/`))
}
function compress2() {
return gulp.src('src/sw.js')
.pipe(terser({
compress: {
drop_console: true,
passes: 10,
reduce_vars: false,
// keep_fnames: 'addAll'
},
mangle: {
toplevel: true,
reserved: ['addAll'],
properties: {
reserved: ['waitUntil', 'respondWith', 'request', 'type', 'addAll'],
},
},
// toplevel: true,
}))
.pipe(gulp.dest(`${destPath}/`))
}
function shortenLongJSWords(file, enc, cb) {
if(file.isBuffer()) {
let contents = file.contents.toString(enc)
const words = [
'totalMin',
'totalMax',
'passwordLength',
'uppercaseMin',
'uppercaseMax',
'lowercaseMin',
'lowercaseMax',
'digitsMin',
'digitsMax',
'specialCharactersMin',
'specialCharactersMax',
'numShuffles',
'includeUppercase',
'includeLowercase',
'includeDigits',
'includeSpecialCharacters',
'excludeSimiliarCharacters',
'excludeAmbiguousCharacters'
]
changed = 0
for(let i in words) {
const r = new RegExp('config\\.' + words[i], 'g')
contents = contents.replace(r, 'config.' + setShortName(changed++) )
}
file.contents = Buffer.from(contents, enc)
cb(null, file)
}
}
function css() {
return gulp.src('src/css/style.css')
.pipe(purify(['src/js/*.js', 'src/*.html']))
.pipe(through2.obj(shortenCSSQueries))
.pipe(autoprefixer())
.pipe(cleanCSS({
level: {
1: { all: true },
2: {}
}
}))
.pipe(through2.obj(computeHash))
// .pipe(rename(addHashToFileName))
.pipe(gulp.dest(`${destPath}/css/`))
}
function scss_compile() {
return gulp.src('src/css/style.scss')
.pipe(through2.obj(shortenCSSQueries))
.pipe(through2.obj(inpsectFile))
.pipe(sass().on('error', sass.logError))
// .pipe(purify(['src/js/*.js', 'src/*.html']))
.pipe(postcss([
sorting({
"order": ["custom-properties", "dollar-variables", "declarations", "rules", "at-rules"],
"properties-order": [
"content",
"position",
"top", "bottom", "left", "right",
"width",
"max-width",
"min-width",
"height",
"max-height",
"min-height",
"display",
"flex-direction",
"flex-wrap",
"flex-grow",
"flex-shrink",
"justify-content",
"align-items",
"align-self",
"margin",
"margin-left", "margin-right", "margin-bottom", "margin-left",
"padding",
"padding-left", "padding-right", "padding-bottom", "padding-left",
"background",
"color",
"box-shadow",
"border",
"border-radius",
"outline",
"opacity",
"cursor",
"font-family",
"font-size",
"font-weight",
"text-align",
"white-space",
"list-style",
"transform-origin",
"transform",
"transition",
"animation",
"overflow",
"z-index",
"pointer-events",
],
"unspecified-properties-position": "bottomAlphabetical"
})
]))
.pipe(autoprefixer())
.pipe(cleanCSS({
level: {
1: { all: true },
2: {}
}
}))
.pipe(through2.obj(computeHash))
// .pipe(rename(addHashToFileName))
.pipe(gulp.dest(`${destPath}/css/`))
}
function svg(cb) {
gulp.src('src/img/*.svg')
.pipe(svgmin())
.pipe(gulp.dest(`${destPath}/img/`))
cb()
}
function icons() {
return gulp.src('src/img/icons/*')
.pipe(gulp.dest(`${destPath}/img/icons/`))
}
function pwa() {
return gulp.src(['src/manifest.json', 'src/browserconfig.xml', 'src/robots.txt'])
.pipe(gulp.dest(`${destPath}/`))
}
function openGraph() {
return gulp.src(['src/img/og.*', 'src/img/screenshot.*'])
.pipe(gulp.dest(`${destPath}/img/`))
}
function copy(cb) {
gulp.src('src/img/*')
.pipe(gulp.dest(`${destPath}/img/`))
cb()
}
function cleanBuild(cb) {
gulp.src(destPath, { read: false, allowEmpty: true })
.pipe(clean())
cb()
}
function terse(cb) {
gulp.src('src/js/*.js')
.pipe(terser({
compress: {
drop_console: true,
},
mangle: {
toplevel: true,
}
}))
.pipe(gulp.dest('dist/js/'))
cb()
}
const digest = new Map()
function computeHash(file, _, cb) {
if(file.isBuffer()) {
hashMD5.update(file.contents.toString())
digest.set(file.basename, {
md5: hashMD5.copy().digest('base64'),
sha512: hash512.copy().digest('base64'),
})
console.log(digest.get(file.basename), file.basename)
cb(null, file)
}
}
function addHashToFileName(path) {
path.basename += '-' + digest.get(path.basename + path.extname).sha512
}
const cssQueries = new Map()
const cssQueryRegex = /(class|id)(?:=)['"](?<css>[^'"]*)['"]/g
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
const regexes = {
html: `(%CSS_TYPE%=)(['"])(%CSS_QUERY%)(['"])`,
js: `(['"])(%CSS_TYPE%)(%CSS_QUERY%)(['"])`,
css: `(%CSS_TYPE%)(%CSS_QUERY%)`,
scss: `(%CSS_TYPE%)(%CSS_QUERY%)`
}
let cssQueriesCount = 0
function findCSSQueries(cb) {
gulp.src('src/index.html')
.pipe(replace(cssQueryRegex, function(match, p1, p2, offset, string) {
if( cssQueries.has(p2) ) {
return string
}
p2.split(' ').forEach(el => {
if( el.length > 0 ) {
cssQueries.set(el, {
name: setShortName(cssQueriesCount),
type: p1
})
cssQueriesCount += 1
}
})
return string
}))
cb()
}
function setShortName(i) {
if( i < chars.length ) {
return chars[i]
}
return chars[i % chars.length] + setShortName(Math.floor(i / chars.length))
}
function shortenCSSQueries(file, enc, cb) {
if(file.isBuffer()) {
const ext = file.extname.substring(1)
if( ext in regexes) {
let content = file.contents.toString(enc)
if(ext === 'html') {
content = content.replace(cssQueryRegex, function(match, p1, p2, offset, string) {
query = p2.split(' ')
query = query.filter(el => el.length > 0)
query = query.map(el => {
if( el.length > 0 && cssQueries.has(el)) {
let r = cssQueries.get(el)
return p1 === r.type ? r.name : el
}
return el
})
return `${p1}="${query.join(' ')}"`
})
} else {
cssQueries.forEach(function(value, key) {
let cssType = value.type === 'class' ? '\\.' : '#'
const newSubStr = ext.match('css') ? `$1${value.name}` : `$1$2${value.name}$4`
let initializeRegexTemplate = regexes[ext].replace('%CSS_TYPE%', cssType)
initializeRegexTemplate = initializeRegexTemplate.replace('%CSS_QUERY%', key)
let r = new RegExp(initializeRegexTemplate, 'g')
content = content.replace(r, newSubStr)
})
}
file.contents = Buffer.from(content, enc)
}
cb(null, file)
}
}
function inpsectFile(file, enc, cb) {
console.log(enc)
cb(null, file)
}
function test(cb) {
var s = gulp.src('src/js/passwd.js')
.pipe(through2.obj(computeHash))
.pipe(rename(addHashToFileName))
cb()
}
exports.test = gulp.series(compress2)
exports.babelify = babelify
exports.compress = compress
exports.css = css
exports.svg = svg
exports.copy = copy
exports.clean = cleanBuild
exports.html = html
exports.terser = terse
exports.build = gulp.series(
findCSSQueries,
compress,
scss_compile,
svg,
icons,
pwa,
openGraph,
html
)