Skip to content

Commit

Permalink
do not build the service worker if the file is empty #104
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Oct 15, 2020
1 parent 77ad4d7 commit 8fb8103
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 150 deletions.
30 changes: 20 additions & 10 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@ VER=$(git rev-parse --short HEAD)
cd $DIR
#
#
fail() {

echo -e '\e[31mbuild failed\e[0m'
exit 1
}
#
#
# build config files
# process es6+ deps
node rollup.config.js
# minify
node terser.config.js
#
cat worker/dist/serviceworker.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/serviceworker.js
sed -i "s/build-date/$DATE/g" worker/dist/serviceworker.js && sed -i "s/build-id/$VER/g" worker/dist/serviceworker.js || fail
#
./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\
-- worker/src/browser.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.js
[ -s worker/dist/serviceworker.js ] || fail
#
./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\
-- worker/src/browser.administrator.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.administrator.js
# shellcheck disable=SC2002
sed "s/build-date/$DATE/g" worker/src/browser.js | sed "s/build-id/$VER/g" > worker/dist/browser.js || fail
#
./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\
-- worker/src/browser.sync.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.sync.js
sed "s/build-date/$DATE/g" worker/src/browser.administrator.js | sed "s/build-id/$VER/g" > worker/dist/browser.administrator.js || fail
#
./node_modules/terser/bin/terser --warn --comments all --beautify beautify=true,preamble='"/* do not edit! */"' --ecma=8\
-- worker/src/browser.uninstall.js | sed "s/build-date/$DATE/g" | sed "s/build-id/$VER/g" > worker/dist/browser.uninstall.js
sed "s/build-date/$DATE/g" worker/src/browser.sync.js | sed "s/build-id/$VER/g" > worker/dist/browser.sync.js || fail
#
node terser.config.js
sed "s/build-date/$DATE/g" worker/src/browser.uninstall.js | sed "s/build-id/$VER/g" > worker/dist/browser.uninstall.js || fail
#
# node terser.config.js
#
sha1sum worker/dist/serviceworker.js | awk '{print $1;}' | tee ./worker_version
48 changes: 27 additions & 21 deletions worker/dist/browser.administrator.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
/* do not edit! */
/**
* Service worker browser client
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
// @ts-check
// build d108aa3 2020-10-12 20:38:26-04:00
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("{scope}worker{debug}.js", {
scope: "{scope}"
}).catch((function(error) {
// console.log(error);
console.error("😭", error);
}));
}
/**
* Service worker browser client
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/

// @ts-check

// build 77ad4d7 2020-10-14 21:54:44-04:00

if ("serviceWorker" in navigator) {
navigator.serviceWorker.
register("{scope}worker{debug}.js", {scope: "{scope}"}).
// .then(function(registration) {

// console.log("🍻");
// })
catch(function(error) {
// console.log(error);
console.error("😭", error);
});
}
151 changes: 90 additions & 61 deletions worker/dist/browser.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,90 @@
/* do not edit! */
/**
* Service worker browser client
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
// @ts-check
// build d108aa3 2020-10-12 20:38:26-04:00
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("{scope}worker{debug}.js", {
scope: "{scope}"
}).catch((function(error) {
// console.log(error);
console.error("😭", error);
}));
if ("onbeforeinstallprompt" in window) {
let deferredPrompt;
let button;
const buttonHTML = '<div class="pwa-app-install pwa-app-install-bottom"><div class="alert alert-success"><div class=alert-body>' + "<button type=button class=close data-dismiss=alert aria-label=Close>" + "<span aria-hidden=true>&times;</span>" + "</button>" + "Click <a href=# data-action=install-pwa-app>here</a> to make this site available offline.";
const clickHandler = function(e) {
e.preventDefault();
e.stopPropagation();
deferredPrompt.prompt();
button.removeEventListener("click", clickHandler, false);
button = null;
// log the platforms provided as options in an install prompt
// console.log(deferredPrompt.platforms); // e.g., ["web", "android", "windows"]
deferredPrompt.userChoice.then((function(outcome) {
console.info(outcome);
// either "installed", "dismissed", etc.
}), (function(error) {
console.error("😭", error);
}));
// e.target.closest('[data-action=install-pwa-app]').removeEventListener('click', clickHandler, false)
};
const createButton = function() {
document.body.insertAdjacentHTML("beforeend", buttonHTML);
button = document.querySelector("a[data-action=install-pwa-app]");
button.addEventListener("click", clickHandler, false);
};
window.addEventListener("beforeinstallprompt", (function(e) {
// console.log("beforeinstallprompt", e);
deferredPrompt = e;
e.preventDefault();
// if ("getInstalledRelatedApps" in navigator) {
// navigator.getInstalledRelatedApps().then(function(relatedApps) {
// if (relatedApps.length == 0) {
// createButton();
// }
// });
// } else {
createButton();
// }
}));
}
}
/**
* Service worker browser client
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/

// @ts-check

// build 77ad4d7 2020-10-14 21:54:44-04:00

if ("serviceWorker" in navigator) {
navigator.serviceWorker.
register("{scope}worker{debug}.js", {
scope: "{scope}"
}).
// .then(function(registration) {

// console.log("🍻");
// })
catch(function (error) {
// console.log(error);
console.error("😭", error);
});

if ("onbeforeinstallprompt" in window) {
let deferredPrompt;
let button;

const buttonHTML =
'<div class="pwa-app-install pwa-app-install-bottom"><div class="alert alert-success"><div class=alert-body>' +
"<button type=button class=close data-dismiss=alert aria-label=Close>" +
"<span aria-hidden=true>&times;</span>" +
"</button>" +
"Click <a href=# data-action=install-pwa-app>here</a> to make this site available offline.";

const clickHandler = function (e) {
e.preventDefault();
e.stopPropagation();

deferredPrompt.prompt();

button.removeEventListener("click", clickHandler, false);
button = null;

// log the platforms provided as options in an install prompt
// console.log(deferredPrompt.platforms); // e.g., ["web", "android", "windows"]

deferredPrompt.userChoice.then(
function (outcome) {
console.info(outcome); // either "installed", "dismissed", etc.
},
function (error) {
console.error("😭", error);
}
);

// e.target.closest('[data-action=install-pwa-app]').removeEventListener('click', clickHandler, false)
};

const createButton = function () {
document.body.insertAdjacentHTML("beforeend", buttonHTML);
button = document.querySelector("a[data-action=install-pwa-app]");

button.addEventListener("click", clickHandler, false);
};

window.addEventListener("beforeinstallprompt", function (e) {
// console.log("beforeinstallprompt", e);

deferredPrompt = e;

e.preventDefault();

// if ("getInstalledRelatedApps" in navigator) {
// navigator.getInstalledRelatedApps().then(function(relatedApps) {
// if (relatedApps.length == 0) {
// createButton();
// }
// });
// } else {
createButton();
// }
});
}
}
72 changes: 40 additions & 32 deletions worker/dist/browser.sync.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
/* do not edit! */
/**
* Service worker browser client
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
// @ts-check
if ("serviceWorker" in navigator) {
if ("SyncManager" in window) {
navigator.serviceWorker.ready.then((function(reg) {
return reg.sync.getTags().then((function(tags) {
if (!tags.includes("{SYNC_API_TAG}")) {
reg.sync.register("{SYNC_API_TAG}");
}
}));
})).catch((function(error) {
// system was unable to register for a sync,
// this could be an OS-level restriction
console.error("cannot setup native sync api, using fallback 😭", error);
new Worker("{scope}sync-fallback{debug}.js");
}));
} else {
// serviceworker/sync not supported, use a worker instead
console.info("background sync api not supported, using fallback 😭");
new Worker("{scope}sync-fallback{debug}.js");
}
}
/**
* Service worker browser client
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/

// @ts-check

if ('serviceWorker' in navigator) {

if ('SyncManager' in window) {

navigator.serviceWorker.ready.then(function (reg) {

return reg.sync.getTags().then(function (tags) {

if (!tags.includes("{SYNC_API_TAG}")) {
reg.sync.register("{SYNC_API_TAG}");
}
})
}).
catch(function (error) {
// system was unable to register for a sync,
// this could be an OS-level restriction
console.error('cannot setup native sync api, using fallback 😭', error);
new Worker("{scope}sync-fallback{debug}.js");
});

} else {

// serviceworker/sync not supported, use a worker instead
console.info('background sync api not supported, using fallback 😭');
new Worker("{scope}sync-fallback{debug}.js");
}
}
1 change: 0 additions & 1 deletion worker/dist/browser.sync.min.js
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
"serviceWorker"in navigator&&("SyncManager"in window?navigator.serviceWorker.ready.then((function(n){return n.sync.getTags().then((function(e){e.includes("{SYNC_API_TAG}")||n.sync.register("{SYNC_API_TAG}")}))})).catch((function(n){console.error("cannot setup native sync api, using fallback 😭",n),new Worker("{scope}sync-fallback{debug}.js")})):new Worker("{scope}sync-fallback{debug}.js"));
48 changes: 26 additions & 22 deletions worker/dist/browser.uninstall.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
/* do not edit! */
/**
* Service worker browser client uninstall
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
// @ts-check
// build d108aa3 2020-10-12 20:38:26-04:00
if ("serviceWorker" in navigator && navigator.serviceWorker.controller) {
navigator.serviceWorker.getRegistration().then((function(registration) {
registration.unregister().then((function(result) {
if (result) {
console.info("The service worker has been successfully removed 😭");
}
}));
}));
}
/**
* Service worker browser client uninstall
* @package GZip Plugin
* @copyright Copyright (C) 2005 - 2018 Thierry Bela.
*
* dual licensed
*
* @license LGPL v3
* @license MIT License
*/
// @ts-check

// build 77ad4d7 2020-10-14 21:54:44-04:00

if ("serviceWorker" in navigator && navigator.serviceWorker.controller) {
navigator.serviceWorker.getRegistration().then(function (registration) {

registration.unregister().then(function (result) {

if (result) {

console.info('The service worker has been successfully removed 😭');
}
});
});
}
4 changes: 2 additions & 2 deletions worker/dist/serviceworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,14 +881,14 @@
* service worker build id
*/
buildid: {
value: "build-id",
value: "77ad4d7",
enumerable: true
},
/**
* service worker buid date
*/
builddate: {
value: "build-date",
value: "2020-10-14 21:54:44-04:00",
enumerable: true
},
/**
Expand Down
1 change: 1 addition & 0 deletions worker/dist/serviceworker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion worker_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
da39a3ee5e6b4b0d3255bfef95601890afd80709
c22fadaa841cf95cf62c214f8edb67521ea00d0e

0 comments on commit 8fb8103

Please sign in to comment.