Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.

Commit 6e212ed

Browse files
Automatically build GitHub bundle
1 parent ad681df commit 6e212ed

8 files changed

+160
-164
lines changed

gulpfile.js

+113-23
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,46 @@
66
// Date: 02.Dec.2021
77
//
88
// ==============================================================================
9+
/* eslint-disable max-lines */
910
/* eslint-disable no-console */
1011

1112
//==============================================================================
1213
// Directories and files.
1314

14-
// Path to HTTPS certificate and key
15-
const httpsCertificate = "../https_cert.pem"
16-
const httpsCertificateKey = "../https_cert-key.pem"
15+
// Name of the app.
16+
const appName = "Notoy-PWA"
1717

18-
// HTTPS port to use
19-
const httpsPort = 1234
18+
// The navigation scope of the PWA, the root path to the service worker.
19+
const navScopePWA = "/"
20+
const navScopeGitHub = `/${appName}/http/`
2021

2122
// Service worker, JS file.
22-
const serviceWorkerJS = "./src/service_worker/sw.js"
23+
const serviceWorkerJS = "sw.js"
24+
25+
// Service worker, map file name.
26+
const serviceWorkerJSMap = serviceWorkerJS + ".map"
27+
28+
// Service worker, full path.
29+
const serviceWorkerJSPath = "./src/service_worker/" + serviceWorkerJS
2330

2431
// App JS file.
2532
const appJS = "app.js"
2633

34+
// App, map file name
35+
const appJSMap = appJS + ".map"
36+
2737
// Manifest filename (original).
2838
const manifestJSON = "manifest.json"
2939

40+
// Directory to exclude in the list of files to cache. Only the directory itself, not it's content!
41+
const excludeDir = "/icons"
42+
43+
// CSS file to process by TailwindCSS.
44+
const tailwindInput = "src/input.css"
45+
46+
// Name of the CSS file TailwindCSS produced in directory `outDir`.
47+
const tailwindOutput = appName.toLowerCase() + ".css"
48+
3049
// Changelog file.
3150
const changelogPath = "./CHANGELOG.md"
3251

@@ -43,6 +62,13 @@ const spagoOutdir = "./output"
4362
// Server.
4463
const serveDir = outDir
4564

65+
// Path to HTTPS certificate and key
66+
const httpsCertificate = "../https_cert.pem"
67+
const httpsCertificateKey = "../https_cert-key.pem"
68+
69+
// HTTPS port to use
70+
const httpsPort = 1234
71+
4672
//==============================================================================
4773
// JS requires
4874

@@ -91,7 +117,7 @@ function generateTimestamp() {
91117
}
92118

93119
//==============================================================================
94-
// Replace Version
120+
// Replace Version and the PWA scope path.
95121

96122
function scanChangelogVersion() {
97123
let version = ""
@@ -109,19 +135,55 @@ function scanChangelogVersion() {
109135
return version
110136
}
111137

112-
function replaceVersion(dirName, version) {
138+
function processManifest(dirName, version, scopePath) {
113139
return src(dirName + "/" + manifestJSON)
114140
.pipe(
115141
replace(
116142
/"version":\s+"[0-9]+.[0-9]+.[0-9]+",/gu,
117-
'"version": "' + version + '",'
143+
`"version": "${version}",`
118144
)
119145
)
146+
.pipe(
147+
replace(/"start_url": "[\S]+",/gu, `"start_url": "${scopePath}",`)
148+
)
149+
.pipe(replace(/"id": "[\S]+",/gu, `"id": "${scopePath}",`))
150+
.pipe(replace(/"scope": "[\S]+",/gu, `"scope": "${scopePath}",`))
151+
.pipe(replace(/"action": "[\S]+",/gu, `"action": "${scopePath}",`))
152+
120153
.pipe(dest(dirName))
121154
}
122155

123-
function replaceVersionOutdir() {
124-
return replaceVersion(outDir, scanChangelogVersion())
156+
function processManifestOutdirGitHub() {
157+
return processManifest(outDir, scanChangelogVersion(), navScopeGitHub)
158+
}
159+
function processManifestOutdir() {
160+
return processManifest(outDir, scanChangelogVersion(), navScopePWA)
161+
}
162+
163+
//==============================================================================
164+
// Replace the PWA scope path in index.html.
165+
function processIndexHTML(scopePath) {
166+
const newUrlRex = new RegExp(
167+
`new URL\\("[\\S]+${serviceWorkerJS}", import\\.meta\\.url\\)`,
168+
"gu"
169+
)
170+
return src(outDir + "/index.html")
171+
.pipe(
172+
replace(
173+
newUrlRex,
174+
`new URL("${scopePath}${serviceWorkerJS}", import.meta.url)`
175+
)
176+
)
177+
.pipe(replace(/scope: "[\S]+",/gu, `scope: "${scopePath}",`))
178+
.pipe(dest(outDir))
179+
}
180+
181+
function processIndexHTMLGitHub() {
182+
return processIndexHTML(navScopeGitHub)
183+
}
184+
185+
function processIndexHTMLPWA() {
186+
return processIndexHTML(navScopePWA)
125187
}
126188

127189
//==============================================================================
@@ -144,7 +206,7 @@ function runSpago() {
144206
// Run tailwindcss.
145207
async function runTailwind() {
146208
return (
147-
exec(`tailwindcss -i src/input.css -o ${outDir}/notoy-pwa.css`),
209+
exec(`tailwindcss -i ${tailwindInput} -o ${outDir}/${tailwindOutput}`),
148210
(error, stdout, stderr) => {
149211
if (error) {
150212
console.error(`exec error: ${error}`)
@@ -175,23 +237,28 @@ function runHTTPS(cb) {
175237
//==============================================================================
176238
// Return a list of all files in the directory `.http`, as a comma and newline
177239
// Separated list.
178-
function getListOfFiles() {
240+
function getListOfFiles(dir) {
179241
let listOfFiles = new filelist.FileList()
180242
listOfFiles.include(outDir + "/**")
181243
const outdirNoSlashes = outDir.replace(/^[./\\]*/gu, "")
182244

183245
const addedFiles = listOfFiles
184246
.toArray()
185-
.map((e) => '"' + e.toString().replace(outdirNoSlashes, "") + '"')
186-
.concat(['"/"', '"/sw.js.map"', '"/app.js.map"'])
247+
.filter((e) => e !== outdirNoSlashes + excludeDir)
248+
.map((e) => '"' + e.replace(outdirNoSlashes + "/", dir) + '"')
249+
.concat([
250+
`"${dir}"`,
251+
`"${dir}${appJSMap}"`,
252+
`"${dir}${serviceWorkerJSMap}"`,
253+
])
187254
return [...new Set(addedFiles)]
188255
}
189256

190257
//==============================================================================
191258
// Copy service worker to ./http
192-
function copyServiceWorker() {
193-
const listOfFiles = getListOfFiles()
194-
return src(serviceWorkerJS)
259+
function copyServiceWorker(dir) {
260+
const listOfFiles = getListOfFiles(dir)
261+
return src(serviceWorkerJSPath)
195262
.pipe(
196263
replace(
197264
/const manifest\s*=\s*\[\s*LIST_OF_FILES\s*\]/gu,
@@ -201,12 +268,20 @@ function copyServiceWorker() {
201268
.pipe(
202269
replace(
203270
/const version\s*=\s*TIMESTAMP/gu,
204-
'const version = "Notoy-PWA-' + generateTimestamp() + '"'
271+
`const version = "${appName}-` + generateTimestamp() + '"'
205272
)
206273
)
207274
.pipe(dest(outDir))
208275
}
209276

277+
function copyServiceWorkerGitHub() {
278+
return copyServiceWorker(navScopeGitHub)
279+
}
280+
281+
function copyServiceWorkerNavScopePWA() {
282+
return copyServiceWorker(navScopePWA)
283+
}
284+
210285
//==============================================================================
211286
// Run Esbuild von JS files.
212287
function processJS(file) {
@@ -231,11 +306,11 @@ function processJS(file) {
231306
}
232307

233308
function processSW() {
234-
return processJS("sw.js")
309+
return processJS(serviceWorkerJS)
235310
}
236311

237312
function processApp() {
238-
return processJS("app.js")
313+
return processJS(appJS)
239314
}
240315

241316
//==============================================================================
@@ -272,9 +347,20 @@ const cleanTarget = parallel(cleanOutput, cleanHTTP)
272347
const bundleTarget = series(
273348
parallel(runSpago, copyAssets, runTailwind),
274349
parallel(
275-
replaceVersionOutdir,
350+
processManifestOutdir,
351+
processIndexHTMLPWA,
352+
processApp,
353+
series(copyServiceWorkerNavScopePWA, processSW)
354+
)
355+
)
356+
357+
const bundleTargetGitHub = series(
358+
parallel(runSpago, copyAssets, runTailwind),
359+
parallel(
360+
processManifestOutdirGitHub,
361+
processIndexHTMLGitHub,
276362
processApp,
277-
series(copyServiceWorker, processSW)
363+
series(copyServiceWorkerGitHub, processSW)
278364
)
279365
)
280366

@@ -285,6 +371,10 @@ exports.clean = cleanTarget
285371

286372
// eslint-disable-next-line no-undef
287373
exports.bundle = bundleTarget
374+
375+
// eslint-disable-next-line no-undef
376+
exports.bundleGitHub = bundleTargetGitHub
377+
288378
// eslint-disable-next-line no-undef
289379
exports.serve = serveTarget
290380

0 commit comments

Comments
 (0)