-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
181 lines (153 loc) · 5.01 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
const del = require('del')
const favicons = require('favicons')
const faviconsConfig = require('./favicons.config')
const fs = require('fs')
const gulp = require('gulp')
const imagemin = require('gulp-imagemin')
const inject = require('gulp-inject')
const packageJson = require('./package.json')
const svitsConfig = require('./svits.config.json')
const routerUrls = require('./.routify/urlIndex.json')
const rename = require('gulp-rename')
const replace = require('gulp-string-replace')
const webp = require('gulp-webp')
const { SitemapStream, streamToPromise } = require('sitemap')
const { createWriteStream } = require('fs')
const updateServiceWorker = () => {
const root = 'dist'
const skip = [
'favicons',
]
const flatDeep = arr => arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val) : val), [])
const tree = (root) => fs.readdirSync(root, { withFileTypes: true })
.filter(element => ! skip.includes(element.name) && ! element.name.endsWith('.map'))
.map(element => element.isDirectory()
? tree(`${root}/${element.name}`)
: `${root}/${element.name}`)
const listAllFiles = flatDeep(fs.readdirSync(root, { withFileTypes: true })
.filter(dir => dir.isDirectory() && ! skip.includes(dir.name))
.map(dir => tree(`${root}/${dir.name}`)))
.map(path => path.substring(root.length))
const filesToPreCache = [
'/',
'/index.html',
...listAllFiles,
]
return gulp.src(`${root}/service-worker.js`)
.pipe(replace(/'cache-.*'/, `'cache-${packageJson.name}-${(+new Date).toString(36)}'`))
.pipe(replace(/filesToPreCache = \[(.*\n)*\]/, `filesToPreCache = [\n '${filesToPreCache.join('\',\n \'')}'\n]`))
.pipe(gulp.dest(root))
}
exports.updateServiceWorker = updateServiceWorker
const generateFavicons = () => {
faviconsConfig.appName = svitsConfig.name
faviconsConfig.appShortName = svitsConfig.shortName
faviconsConfig.appDescription = svitsConfig.description
faviconsConfig.developerName = svitsConfig.author.name
faviconsConfig.developerURL = svitsConfig.author.url
faviconsConfig.version = packageJson.version
faviconsConfig.url = svitsConfig.hostname
return gulp.src('./favicon.*')
.pipe(favicons.stream(faviconsConfig))
.pipe(gulp.dest(`dist${faviconsConfig.path}`))
}
exports.generateFavicons = generateFavicons
const clean = () => {
return del([
'dist',
'public/**/*.webp',
])
}
exports.clean = clean
const generateWebps = (path) => {
return gulp.src(`${path}/**/*`)
.pipe(webp({
quality: 80,
}))
.pipe(gulp.dest(path))
}
const generateWebpsPublic = () => generateWebps('public')
exports.generateWebpsPublic = generateWebpsPublic
const generateWebpsDist = () => generateWebps('dist')
exports.generateWebpsDist = generateWebpsDist
const injectFavicons = () => {
return gulp.src('dist/index.html')
.pipe(inject(gulp.src([`dist${faviconsConfig.path}${faviconsConfig.html}`]), {
starttag: '<!-- inject:favicons -->',
transform: (filepath, file) => {
return file.contents.toString()
}
}))
.pipe(gulp.dest('dist'))
}
exports.injectFavicons = injectFavicons
const metadataContent = (cfg) => {
return `<title>${cfg.name}</title>
<meta name="description" content="${cfg.description}" />`
}
const injectMetadata = () => {
return gulp.src('dist/index.html')
.pipe(replace('<!-- inject:metadata -->', metadataContent(svitsConfig)))
.pipe(gulp.dest('dist'))
}
exports.injectMetadata = injectMetadata
const replaceRobotsTXT = () => {
return gulp.src('dist/robots.txt')
.pipe(replace('{{host}}', svitsConfig.hostname))
.pipe(gulp.dest('dist'))
}
exports.replaceRobotsTXT = replaceRobotsTXT
const cleanFaviconsHTML = () => {
return del([
'dist/favicons/*.html',
])
}
exports.cleanFaviconsHTML = cleanFaviconsHTML
const optimizeImages = () => {
return gulp.src('dist/**/*.+(jpeg|jpg|png|gif)')
.pipe(imagemin())
.pipe(gulp.dest('dist'))
}
const generateSitemapXML = () => {
const smStream = new SitemapStream({
hostname: svitsConfig.hostname,
})
const urls = routerUrls.map(url => {
const urlObj = {
url: url === '/index' ? '/' : url.split('/index').join(''),
changefreq: 'daily',
priority: 0.7,
}
const configUrl = svitsConfig.sitemapUrls.filter(url => url.url === urlObj.url)[0]
return { ...urlObj, ...configUrl }
})
console.log(urls)
smStream.pipe(createWriteStream('./dist/sitemap.xml'))
urls.forEach(url => {
if (svitsConfig.routifyRuntimeConfig && svitsConfig.routifyRuntimeConfig.useHash && url.url !== '/') {
url.url = `/#${url.url}`
}
smStream.write(url)
})
return smStream.end()
}
exports.generateSitemapXML = generateSitemapXML
exports.optimizeImages = optimizeImages
const dev = gulp.series(
clean,
generateWebpsPublic,
)
exports.dev = dev
const prod = gulp.series(
optimizeImages,
generateWebpsDist,
generateFavicons,
injectFavicons,
injectMetadata,
cleanFaviconsHTML,
updateServiceWorker,
generateSitemapXML,
replaceRobotsTXT,
)
exports.prod = prod
exports.default = dev