replace global hash in given chunks

This commit is contained in:
Bruno Windels 2021-12-09 12:15:17 +01:00
parent c344032c0a
commit 8e4da396ea
4 changed files with 24 additions and 6 deletions

View File

@ -15,6 +15,7 @@ module.exports = {
"no-unused-vars": "warn"
},
"globals": {
"HYDROGEN_VERSION": "readonly"
"HYDROGEN_VERSION": "readonly",
"HYDROGEN_GLOBAL_HASH": "readonly"
}
};

View File

@ -8,7 +8,7 @@ function contentHash(str) {
return hasher.digest();
}
module.exports = function injectServiceWorker(swFile, otherUncachedFiles) {
module.exports = function injectServiceWorker(swFile, otherUncachedFiles, globalHashChunkReplaceMap) {
const swName = path.basename(swFile);
let root;
let version;
@ -43,6 +43,7 @@ module.exports = function injectServiceWorker(swFile, otherUncachedFiles) {
const globalHash = getBuildHash(cachedFileNames, uncachedFileContentMap);
const sw = bundle[swName];
sw.code = replaceConstsInServiceWorker(sw.code, version, globalHash, assets);
replaceGlobalHashPlaceholderInChunks(assets, globalHashChunkReplaceMap, globalHash);
console.log(`\nBuilt ${version} (${globalHash})`);
}
};
@ -115,3 +116,14 @@ function replaceConstsInServiceWorker(swSource, version, globalHash, assets) {
swSource = replaceStringInSource("NOTIFICATION_BADGE_ICON", assets.find(a => a.name === "icon.png").fileName);
return swSource;
}
function replaceGlobalHashPlaceholderInChunks(assets, globalHashChunkReplaceMap, globalHash) {
for (const [name, placeholder] of Object.entries(globalHashChunkReplaceMap)) {
const chunk = assets.find(a => a.type === "chunk" && a.name === name);
if (!chunk) {
throw new Error(`could not find chunk ${name} to replace global hash placeholder`);
}
console.log(placeholder, globalHash);
chunk.code = chunk.code.replaceAll(placeholder, `"${globalHash}"`);
}
}

View File

@ -15,10 +15,10 @@ limitations under the License.
*/
export function hydrogenGithubLink(t) {
if (HYDROGEN_VERSION) {
if (HYDROGEN_VERSION && HYDROGEN_GLOBAL_HASH) {
return t.a({target: "_blank",
href: `https://github.com/vector-im/hydrogen-web/releases/tag/v${HYDROGEN_VERSION}`},
`Hydrogen v${HYDROGEN_VERSION} (${window.HYDROGEN_GLOBAL_HASH}) on Github`);
`Hydrogen v${HYDROGEN_VERSION} (${HYDROGEN_GLOBAL_HASH}) on Github`);
} else {
return t.a({target: "_blank", href: "https://github.com/vector-im/hydrogen-web"},
"Hydrogen on Github");

View File

@ -4,6 +4,7 @@ const flexbugsFixes = require("postcss-flexbugs-fixes");
const fs = require("fs");
const path = require("path");
const GLOBAL_HASH_PLACEHOLDER = "hydrogen-global-hash-placeholder-4cf32306-5d61-4262-9a57-c9983f472c3c";
const injectWebManifest = require("./scripts/build-plugins/manifest");
const injectServiceWorker = require("./scripts/build-plugins/service-worker");
@ -50,10 +51,14 @@ export default {
// important this comes before service worker
// otherwise the manifest and the icons it refers to won't be cached
injectWebManifest("assets/manifest.json"),
injectServiceWorker("./src/platform/web/sw.js", ["index.html"]),
injectServiceWorker("./src/platform/web/sw.js", ["index.html"], {
// replace global hash placeholder in index chunk
"index": JSON.stringify(GLOBAL_HASH_PLACEHOLDER)
}),
],
define: {
"HYDROGEN_VERSION": JSON.stringify(version)
"HYDROGEN_VERSION": JSON.stringify(version),
"HYDROGEN_GLOBAL_HASH": JSON.stringify(GLOBAL_HASH_PLACEHOLDER)
},
css: {
postcss: {