diff --git a/gulp/common/md5.js b/gulp/common/md5.js new file mode 100644 index 0000000000..29e80d8bb4 --- /dev/null +++ b/gulp/common/md5.js @@ -0,0 +1,8 @@ +const fs = require('fs'); +const crypto = require('crypto'); + +module.exports = function md5(filename) { + return crypto.createHash('md5') + .update(fs.readFileSync(filename)).digest('hex'); +}; + diff --git a/gulp/config.js b/gulp/config.js index ff4ef9a25f..185db09813 100644 --- a/gulp/config.js +++ b/gulp/config.js @@ -1,7 +1,8 @@ var path = require('path'); module.exports = { - static_dir: path.resolve(__dirname, '../..'), polymer_dir: path.resolve(__dirname, '..'), build_dir: path.resolve(__dirname, '../build'), + output: path.resolve(__dirname, '../hass_frontend'), + output_es5: path.resolve(__dirname, '../hass_frontend_es5'), }; diff --git a/gulp/tasks/gen-index-html.js b/gulp/tasks/gen-index-html.js new file mode 100644 index 0000000000..36411fceca --- /dev/null +++ b/gulp/tasks/gen-index-html.js @@ -0,0 +1,44 @@ +const gulp = require('gulp'); +const replace = require('gulp-batch-replace'); +const path = require('path'); +const url = require('url'); +const config = require('../config'); +const md5 = require('../common/md5.js'); + +const buildReplaces = { + '/home-assistant-polymer/build/core.js': 'core.js', + '/home-assistant-polymer/src/home-assistant.html': 'frontend.html', +}; + +function generateIndex(es6) { + const targetPath = es6 ? config.output : config.output_es5; + const targetUrl = es6 ? '/frontend_latest/' : '/frontend_es5/'; + + const toReplace = [ + ['/home-assistant-polymer/hass_frontend/mdi.html', + `/static/mdi-${md5(path.resolve(config.output, 'mdi.html'))}.html`], + ['/home-assistant-polymer/build-temp/compatibility.js', + `/static/compatibility-${md5(path.resolve(config.output_es5, 'compatibility.js'))}.js`], + ]; + + if (!es6) { + toReplace.push([ + '/service_worker.js', '/service_worker_es5.js' + ]); + } + + for (const [replaceSearch, filename] of Object.entries(buildReplaces)) { + const parsed = path.parse(filename); + const hash = md5(path.resolve(targetPath, filename)); + toReplace.push([ + replaceSearch, + url.resolve(targetUrl, `${parsed.name}-${hash}${parsed.ext}`)]); + } + + gulp.src(path.resolve(config.polymer_dir, 'index.html')) + .pipe(replace(toReplace)) + .pipe(gulp.dest(targetPath)); +} + +gulp.task('gen-index-html-es5', generateIndex.bind(null, /* es6= */ false)); +gulp.task('gen-index-html', generateIndex.bind(null, /* es6= */ true)); diff --git a/gulp/tasks/gen-service-worker.js b/gulp/tasks/gen-service-worker.js index b18f5f4d6d..362f7b83fe 100755 --- a/gulp/tasks/gen-service-worker.js +++ b/gulp/tasks/gen-service-worker.js @@ -12,11 +12,11 @@ TODO: - Fix minifying the stream */ const gulp = require('gulp'); -const crypto = require('crypto'); const file = require('gulp-file'); const fs = require('fs'); const path = require('path'); const swPrecache = require('sw-precache'); +const md5 = require('../common/md5.js'); const DEV = !!JSON.parse(process.env.BUILD_DEV || 'true'); @@ -45,11 +45,6 @@ const panelsFingerprinted = [ 'dev-mqtt', 'kiosk', ]; -function md5(filename) { - return crypto.createHash('md5') - .update(fs.readFileSync(filename)).digest('hex'); -} - function processStatic(fn, rootDir, urlDir) { const parts = path.parse(fn); const base = parts.dir.length > 0 ? parts.dir + '/' + parts.name : parts.name; @@ -117,7 +112,7 @@ function generateServiceWorker(es6) { } ], stripPrefix: baseRootDir, - replacePrefix: 'static', + replacePrefix: '/static', verbose: true, // Allow our users to refresh to get latest version. clientsClaim: true, diff --git a/index.html b/index.html new file mode 100644 index 0000000000..4546133dc3 --- /dev/null +++ b/index.html @@ -0,0 +1,121 @@ + + +
+ +