6
6
// Date: 02.Dec.2021
7
7
//
8
8
// ==============================================================================
9
+ /* eslint-disable max-lines */
9
10
/* eslint-disable no-console */
10
11
11
12
//==============================================================================
12
13
// Directories and files.
13
14
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"
17
17
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/`
20
21
21
22
// 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
23
30
24
31
// App JS file.
25
32
const appJS = "app.js"
26
33
34
+ // App, map file name
35
+ const appJSMap = appJS + ".map"
36
+
27
37
// Manifest filename (original).
28
38
const manifestJSON = "manifest.json"
29
39
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
+
30
49
// Changelog file.
31
50
const changelogPath = "./CHANGELOG.md"
32
51
@@ -43,6 +62,13 @@ const spagoOutdir = "./output"
43
62
// Server.
44
63
const serveDir = outDir
45
64
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
+
46
72
//==============================================================================
47
73
// JS requires
48
74
@@ -91,7 +117,7 @@ function generateTimestamp() {
91
117
}
92
118
93
119
//==============================================================================
94
- // Replace Version
120
+ // Replace Version and the PWA scope path.
95
121
96
122
function scanChangelogVersion ( ) {
97
123
let version = ""
@@ -109,19 +135,55 @@ function scanChangelogVersion() {
109
135
return version
110
136
}
111
137
112
- function replaceVersion ( dirName , version ) {
138
+ function processManifest ( dirName , version , scopePath ) {
113
139
return src ( dirName + "/" + manifestJSON )
114
140
. pipe (
115
141
replace (
116
142
/ " v e r s i o n " : \s + " [ 0 - 9 ] + .[ 0 - 9 ] + .[ 0 - 9 ] + " , / gu,
117
- ' "version": "' + version + '",'
143
+ ` "version": "${ version } ",`
118
144
)
119
145
)
146
+ . pipe (
147
+ replace ( / " s t a r t _ u r l " : " [ \S ] + " , / gu, `"start_url": "${ scopePath } ",` )
148
+ )
149
+ . pipe ( replace ( / " i d " : " [ \S ] + " , / gu, `"id": "${ scopePath } ",` ) )
150
+ . pipe ( replace ( / " s c o p e " : " [ \S ] + " , / gu, `"scope": "${ scopePath } ",` ) )
151
+ . pipe ( replace ( / " a c t i o n " : " [ \S ] + " , / gu, `"action": "${ scopePath } ",` ) )
152
+
120
153
. pipe ( dest ( dirName ) )
121
154
}
122
155
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 ( / s c o p e : " [ \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 )
125
187
}
126
188
127
189
//==============================================================================
@@ -144,7 +206,7 @@ function runSpago() {
144
206
// Run tailwindcss.
145
207
async function runTailwind ( ) {
146
208
return (
147
- exec ( `tailwindcss -i src/input.css -o ${ outDir } /notoy-pwa.css ` ) ,
209
+ exec ( `tailwindcss -i ${ tailwindInput } -o ${ outDir } /${ tailwindOutput } ` ) ,
148
210
( error , stdout , stderr ) => {
149
211
if ( error ) {
150
212
console . error ( `exec error: ${ error } ` )
@@ -175,23 +237,28 @@ function runHTTPS(cb) {
175
237
//==============================================================================
176
238
// Return a list of all files in the directory `.http`, as a comma and newline
177
239
// Separated list.
178
- function getListOfFiles ( ) {
240
+ function getListOfFiles ( dir ) {
179
241
let listOfFiles = new filelist . FileList ( )
180
242
listOfFiles . include ( outDir + "/**" )
181
243
const outdirNoSlashes = outDir . replace ( / ^ [ . / \\ ] * / gu, "" )
182
244
183
245
const addedFiles = listOfFiles
184
246
. 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
+ ] )
187
254
return [ ...new Set ( addedFiles ) ]
188
255
}
189
256
190
257
//==============================================================================
191
258
// 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 )
195
262
. pipe (
196
263
replace (
197
264
/ c o n s t m a n i f e s t \s * = \s * \[ \s * L I S T _ O F _ F I L E S \s * \] / gu,
@@ -201,12 +268,20 @@ function copyServiceWorker() {
201
268
. pipe (
202
269
replace (
203
270
/ c o n s t v e r s i o n \s * = \s * T I M E S T A M P / gu,
204
- ' const version = "Notoy-PWA-' + generateTimestamp ( ) + '"'
271
+ ` const version = "${ appName } -` + generateTimestamp ( ) + '"'
205
272
)
206
273
)
207
274
. pipe ( dest ( outDir ) )
208
275
}
209
276
277
+ function copyServiceWorkerGitHub ( ) {
278
+ return copyServiceWorker ( navScopeGitHub )
279
+ }
280
+
281
+ function copyServiceWorkerNavScopePWA ( ) {
282
+ return copyServiceWorker ( navScopePWA )
283
+ }
284
+
210
285
//==============================================================================
211
286
// Run Esbuild von JS files.
212
287
function processJS ( file ) {
@@ -231,11 +306,11 @@ function processJS(file) {
231
306
}
232
307
233
308
function processSW ( ) {
234
- return processJS ( "sw.js" )
309
+ return processJS ( serviceWorkerJS )
235
310
}
236
311
237
312
function processApp ( ) {
238
- return processJS ( "app.js" )
313
+ return processJS ( appJS )
239
314
}
240
315
241
316
//==============================================================================
@@ -272,9 +347,20 @@ const cleanTarget = parallel(cleanOutput, cleanHTTP)
272
347
const bundleTarget = series (
273
348
parallel ( runSpago , copyAssets , runTailwind ) ,
274
349
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 ,
276
362
processApp ,
277
- series ( copyServiceWorker , processSW )
363
+ series ( copyServiceWorkerGitHub , processSW )
278
364
)
279
365
)
280
366
@@ -285,6 +371,10 @@ exports.clean = cleanTarget
285
371
286
372
// eslint-disable-next-line no-undef
287
373
exports . bundle = bundleTarget
374
+
375
+ // eslint-disable-next-line no-undef
376
+ exports . bundleGitHub = bundleTargetGitHub
377
+
288
378
// eslint-disable-next-line no-undef
289
379
exports . serve = serveTarget
290
380
0 commit comments