This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
116 lines (102 loc) · 3.03 KB
/
gulpfile.babel.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
import { dest, parallel, series, src, watch } from 'gulp'
import { parse, stringify } from 'userscript-meta'
import { readFileSync, existsSync, writeFile } from 'node:fs'
import cleanCSS from 'gulp-clean-css'
import flatmap from 'gulp-flatmap'
import htmlMinifier from 'gulp-html-minifier-terser'
import replace from 'gulp-replace'
// paths
const paths = {
css: {
dest: 'tempfiles/',
src: 'userscripts/src/css/*.css'
},
handlebars: {
dest: 'tempfiles/',
src: 'userscripts/src/handlebars/*.hbs'
},
meta: {
dest: 'userscripts/meta/',
src: 'userscripts/meta/'
},
userscripts: {
dest: 'userscripts/',
src: 'userscripts/*.user.js'
}
}
// minify
const minifyCSS = () => {
return src(paths.css.src)
.pipe(cleanCSS({}))
.pipe(dest(paths.css.dest))
}
const minifyHBS = () => {
return src(paths.handlebars.src)
.pipe(htmlMinifier({
collapseBooleanAttributes: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
processScripts: ['text/x-handlebars-template'],
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
sortAttributes: true,
sortClassName: true
}))
.pipe(dest(paths.handlebars.dest))
}
// replace
const replaceCSS = () => {
return src(paths.userscripts.src)
.pipe(flatmap((stream, file) => {
const fileName = file.stem.replace('.user', '')
return existsSync(`tempfiles/${fileName}.css`)
? src(file.path)
.pipe(replace(/(?<=(css: )')(.*?)(?=')/g, readFileSync(`tempfiles/${fileName}.css`, 'utf8')))
.pipe(dest(paths.userscripts.dest))
: stream
}))
}
const replaceHBS = () => {
return src(paths.userscripts.src)
.pipe(flatmap((stream, file) => {
const fileName = file.stem.replace('.user', '')
return existsSync(`tempfiles/${fileName}.hbs`)
? src(file.path)
.pipe(replace(/(?<=(const template = )')(.*?)(?=')/g, readFileSync(`tempfiles/${fileName}.hbs`, 'utf8')))
.pipe(dest(paths.userscripts.dest))
: stream
}))
}
// bump meta
const bumpMeta = (callback) => {
return src(paths.userscripts.src)
.pipe(flatmap((stream, file) => {
const fileName = file.basename.replace('.user', '.meta')
const contents = file.contents.toString('utf8')
const { name, author, namespace, description, copyright, license, version } = parse(contents)
const meta = stringify({ name, author, namespace, description, copyright, license, version })
writeFile(`${paths.meta.src}${fileName}`, meta, callback)
return stream
}))
}
// watch
const watchUserJS = () => {
watch(paths.userscripts.src, {
ignoreInitial: false
}, series(bumpMeta))
}
const watchCSS = () => {
watch(paths.css.src, {
ignoreInitial: false
}, series(minifyCSS, replaceCSS))
}
const watchHBS = () => {
watch(paths.handlebars.src, {
ignoreInitial: false
}, series(minifyHBS, replaceHBS))
}
// export
const _default = parallel(watchUserJS, watchCSS, watchHBS)
export { _default as default }