diff --git a/gulp/tasks/gen-service-worker.js b/gulp/tasks/gen-service-worker.js index 77b5eda134..74a8e39382 100755 --- a/gulp/tasks/gen-service-worker.js +++ b/gulp/tasks/gen-service-worker.js @@ -13,6 +13,7 @@ TODO: */ var gulp = require('gulp'); var crypto = require('crypto'); +var file = require('gulp-file'); var fs = require('fs'); var path = require('path'); var swPrecache = require('sw-precache'); @@ -53,58 +54,58 @@ function md5(filename) { } gulp.task('gen-service-worker', () => { - // Create fingerprinted versions of our dependencies. - staticFingerprinted.forEach(fn => { - var parts = path.parse(fn); - var hash = md5(rootDir + '/' + parts.name + parts.ext); - var url = '/static/' + parts.name + '-' + hash + parts.ext; - var fpath = rootDir + '/' + parts.name + parts.ext; - dynamicUrlToDependencies[url] = [fpath]; - }); + var genPromise = null; + if (DEV) { + var devBase = 'console.warn("Service worker caching disabled in development")'; + genPromise = Promise.resolve(devBase); + } else { + // Create fingerprinted versions of our dependencies. + staticFingerprinted.forEach(fn => { + var parts = path.parse(fn); + var hash = md5(rootDir + '/' + parts.name + parts.ext); + var url = '/static/' + parts.name + '-' + hash + parts.ext; + var fpath = rootDir + '/' + parts.name + parts.ext; + dynamicUrlToDependencies[url] = [fpath]; + }); - panelsFingerprinted.forEach(panel => { - var fpath = panelDir + '/ha-panel-' + panel + '.html'; - var hash = md5(fpath); - var url = '/frontend/panels/' + panel + '-' + hash + '.html'; - dynamicUrlToDependencies[url] = [fpath]; - }); + panelsFingerprinted.forEach(panel => { + var fpath = panelDir + '/ha-panel-' + panel + '.html'; + var hash = md5(fpath); + var url = '/frontend/panels/' + panel + '-' + hash + '.html'; + dynamicUrlToDependencies[url] = [fpath]; + }); - var options = { - navigateFallback: '/', - navigateFallbackWhitelist: [/^((?!(static|api|local|service_worker.js|manifest.json)).)*$/], - dynamicUrlToDependencies: dynamicUrlToDependencies, - staticFileGlobs: [ - rootDir + '/icons/favicon.ico', - rootDir + '/icons/favicon-192x192.png', - rootDir + '/webcomponents-lite.min.js', - rootDir + '/fonts/roboto/Roboto-Light.ttf', - rootDir + '/fonts/roboto/Roboto-Medium.ttf', - rootDir + '/fonts/roboto/Roboto-Regular.ttf', - rootDir + '/fonts/roboto/Roboto-Bold.ttf', - rootDir + '/images/card_media_player_bg.png', - ], - stripPrefix: '..', - replacePrefix: 'static', - verbose: true, - }; + var options = { + navigateFallback: '/', + navigateFallbackWhitelist: [/^((?!(static|api|local|service_worker.js|manifest.json)).)*$/], + dynamicUrlToDependencies: dynamicUrlToDependencies, + staticFileGlobs: [ + rootDir + '/icons/favicon.ico', + rootDir + '/icons/favicon-192x192.png', + rootDir + '/webcomponents-lite.min.js', + rootDir + '/fonts/roboto/Roboto-Light.ttf', + rootDir + '/fonts/roboto/Roboto-Medium.ttf', + rootDir + '/fonts/roboto/Roboto-Regular.ttf', + rootDir + '/fonts/roboto/Roboto-Bold.ttf', + rootDir + '/images/card_media_player_bg.png', + ], + stripPrefix: '..', + replacePrefix: 'static', + verbose: true, + }; - var devBase = 'console.warn("Service worker caching disabled in development")'; + genPromise = swPrecache.generate(options); + } var swHass = fs.readFileSync(path.resolve(__dirname, '../service-worker.js.tmpl'), 'UTF-8'); - var genPromise = DEV ? Promise.resolve(devBase) : swPrecache.generate(options); - - genPromise = genPromise.then(swString => swString + '\n' + swHass); - // Fix this // if (!DEV) { // genPromise = genPromise.then( // swString => uglifyJS.minify(swString, { fromString: true }).code); // } - genPromise.then(swString => { - fs.writeFileSync(path.resolve(config.build_dir, 'service_worker.js'), swString); - }); - - return genPromise; + return genPromise.then(swString => swString + '\n' + swHass) + .then(swString => file('service_worker.js', swString) + .pipe(gulp.dest(config.build_dir))); }); diff --git a/package.json b/package.json index 841b657edc..de91e63cd4 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "eslint-plugin-react": "^7.0.0", "gulp": "^3.9.1", "gulp-babel": "^7.0.0", + "gulp-file": "^0.3.0", "gulp-filter": "^5.0.1", "gulp-html-minifier": "^0.1.8", "gulp-if": "^2.0.2", diff --git a/yarn.lock b/yarn.lock index 3bd8689cb7..417cf3c858 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3631,6 +3631,13 @@ gulp-babel@^7.0.0: through2 "^2.0.0" vinyl-sourcemaps-apply "^0.2.0" +gulp-file@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/gulp-file/-/gulp-file-0.3.0.tgz#e8c4d763f126fb3332fc416e3d1ef46ed67d8d0d" + dependencies: + gulp-util "^2.2.14" + through2 "^0.4.1" + gulp-filter@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/gulp-filter/-/gulp-filter-5.0.1.tgz#5d87f662e317e5839ef7650e620e6c9008ff92d0" @@ -3696,7 +3703,7 @@ gulp-uglify@^3.0.0: uglify-js "^3.0.5" vinyl-sourcemaps-apply "^0.2.0" -gulp-util@^2.2.20: +gulp-util@^2.2.14, gulp-util@^2.2.20: version "2.2.20" resolved "https://registry.yarnpkg.com/gulp-util/-/gulp-util-2.2.20.tgz#d7146e5728910bd8f047a6b0b1e549bc22dbd64c" dependencies: @@ -5339,6 +5346,10 @@ object-keys@^1.0.8: version "1.0.11" resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.11.tgz#c54601778ad560f1142ce0e01bcca8b56d13426d" +object-keys@~0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-0.4.0.tgz#28a6aae7428dd2c3a92f3d95f21335dd204e0336" + object.defaults@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/object.defaults/-/object.defaults-1.1.0.tgz#3a7f868334b407dea06da16d88d5cd29e435fecf" @@ -7241,6 +7252,13 @@ through2-filter@^2.0.0: through2 "~2.0.0" xtend "~4.0.0" +through2@^0.4.1: + version "0.4.2" + resolved "https://registry.yarnpkg.com/through2/-/through2-0.4.2.tgz#dbf5866031151ec8352bb6c4db64a2292a840b9b" + dependencies: + readable-stream "~1.0.17" + xtend "~2.1.1" + through2@^0.5.0: version "0.5.1" resolved "https://registry.yarnpkg.com/through2/-/through2-0.5.1.tgz#dfdd012eb9c700e2323fd334f38ac622ab372da7" @@ -7890,6 +7908,12 @@ xmlhttprequest-ssl@1.5.3: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" +xtend@~2.1.1: + version "2.1.2" + resolved "https://registry.yarnpkg.com/xtend/-/xtend-2.1.2.tgz#6efecc2a4dad8e6962c4901b337ce7ba87b5d28b" + dependencies: + object-keys "~0.4.0" + xtend@~3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/xtend/-/xtend-3.0.0.tgz#5cce7407baf642cba7becda568111c493f59665a"