mirror of
https://github.com/home-assistant/frontend.git
synced 2025-05-01 00:37:20 +00:00

Co-authored-by: Bram Kragten <mail@bramkragten.nl> Co-authored-by: Paul Bottein <paul.bottein@gmail.com> Co-authored-by: Steve Repsher <steverep@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Joakim Sørensen <ludeeus@ludeeus.dev> Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: puddly <32534428+puddly@users.noreply.github.com> Co-authored-by: Erik Montnemery <erik@montnemery.com> Co-authored-by: Bagira <bagdi.istvan@gmail.com> Co-authored-by: Ben Randall <veleek@gmail.com> Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com> Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: Chris <31055115+darthsebulba04@users.noreply.github.com> Co-authored-by: Aaron Carson <aaron@aaroncarson.co.uk> Co-authored-by: David F. Mulcahey <david.mulcahey@me.com> Co-authored-by: Yosi Levy <37745463+yosilevy@users.noreply.github.com> Co-authored-by: Alex van den Hoogen <alex3305@gmail.com> Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> Co-authored-by: Jani Lahti <jani.lahti@iki.fi> Co-authored-by: RoboMagus <68224306+RoboMagus@users.noreply.github.com> Co-authored-by: Brynley McDonald <brynley+github@zephire.nz> Co-authored-by: Denis Shulyaka <Shulyaka@gmail.com> Co-authored-by: KablammoNick <nick@kablammo.net> Co-authored-by: Allen Porter <allen@thebends.org> Co-authored-by: Philip Allgaier <mail@spacegaier.de>
95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
// Generate service worker.
|
|
// Based on manifest, create a file with the content as service_worker.js
|
|
const gulp = require("gulp");
|
|
const path = require("path");
|
|
const fs = require("fs-extra");
|
|
const workboxBuild = require("workbox-build");
|
|
const sourceMapUrl = require("source-map-url");
|
|
const paths = require("../paths.js");
|
|
|
|
const swDest = path.resolve(paths.app_output_root, "service_worker.js");
|
|
|
|
const writeSW = (content) => fs.outputFileSync(swDest, content.trim() + "\n");
|
|
|
|
gulp.task("gen-service-worker-app-dev", (done) => {
|
|
writeSW(
|
|
`
|
|
console.debug('Service worker disabled in development');
|
|
|
|
self.addEventListener('install', (event) => {
|
|
// This will activate the dev service worker,
|
|
// removing any prod service worker the dev might have running
|
|
self.skipWaiting();
|
|
});
|
|
`
|
|
);
|
|
done();
|
|
});
|
|
|
|
gulp.task("gen-service-worker-app-prod", async () => {
|
|
// Read bundled source file
|
|
const bundleManifestLatest = require(path.resolve(
|
|
paths.app_output_latest,
|
|
"manifest.json"
|
|
));
|
|
let serviceWorkerContent = fs.readFileSync(
|
|
paths.app_output_root + bundleManifestLatest["service_worker.js"],
|
|
"utf-8"
|
|
);
|
|
|
|
// Delete old file from frontend_latest so manifest won't pick it up
|
|
fs.removeSync(
|
|
paths.app_output_root + bundleManifestLatest["service_worker.js"]
|
|
);
|
|
fs.removeSync(
|
|
paths.app_output_root + bundleManifestLatest["service_worker.js.map"]
|
|
);
|
|
|
|
// Remove ES5
|
|
const bundleManifestES5 = require(path.resolve(
|
|
paths.app_output_es5,
|
|
"manifest.json"
|
|
));
|
|
fs.removeSync(paths.app_output_root + bundleManifestES5["service_worker.js"]);
|
|
fs.removeSync(
|
|
paths.app_output_root + bundleManifestES5["service_worker.js.map"]
|
|
);
|
|
|
|
const workboxManifest = await workboxBuild.getManifest({
|
|
// Files that mach this pattern will be considered unique and skip revision check
|
|
// ignore JS files + translation files
|
|
dontCacheBustURLsMatching: /(frontend_latest\/.+|static\/translations\/.+)/,
|
|
|
|
globDirectory: paths.app_output_root,
|
|
globPatterns: [
|
|
"frontend_latest/*.js",
|
|
// Cache all English translations because we catch them as fallback
|
|
// Using pattern to match hash instead of * to avoid caching en-GB
|
|
// 'v' added as valid hash letter because in dev we hash with 'dev'
|
|
"static/translations/**/en-+([a-fv0-9]).json",
|
|
// Icon shown on splash screen
|
|
"static/icons/favicon-192x192.png",
|
|
"static/icons/favicon.ico",
|
|
// Common fonts
|
|
"static/fonts/roboto/Roboto-Light.woff2",
|
|
"static/fonts/roboto/Roboto-Medium.woff2",
|
|
"static/fonts/roboto/Roboto-Regular.woff2",
|
|
"static/fonts/roboto/Roboto-Bold.woff2",
|
|
],
|
|
});
|
|
|
|
for (const warning of workboxManifest.warnings) {
|
|
console.warn(warning);
|
|
}
|
|
|
|
// remove source map and add WB manifest
|
|
serviceWorkerContent = sourceMapUrl.removeFrom(serviceWorkerContent);
|
|
serviceWorkerContent = serviceWorkerContent.replace(
|
|
"WB_MANIFEST",
|
|
JSON.stringify(workboxManifest.manifestEntries)
|
|
);
|
|
|
|
// Write new file to root
|
|
fs.writeFileSync(swDest, serviceWorkerContent);
|
|
});
|