use vite define option to inject version number everywhere

This commit is contained in:
Bruno Windels 2021-10-07 15:09:16 +02:00
parent 216afd45cc
commit 923a1a2057
6 changed files with 15 additions and 11 deletions

View file

@ -10,6 +10,7 @@ function contentHash(str) {
module.exports = function injectServiceWorker(swFile) {
let root;
let version;
let manifestHref;
return {
name: "injectServiceWorker",
@ -17,11 +18,11 @@ module.exports = function injectServiceWorker(swFile) {
enforce: "post",
configResolved: config => {
root = config.root;
version = JSON.parse(config.define.HYDROGEN_VERSION); // unquote
},
generateBundle: async function(_, bundle) {
const absoluteSwFile = path.resolve(root, swFile);
const packageManifest = path.resolve(path.join(__dirname, "../../package.json"));
const version = JSON.parse(await fs.readFile(packageManifest, "utf8")).version;
let swSource = await fs.readFile(absoluteSwFile, {encoding: "utf8"});
const assets = Object.values(bundle).filter(a => a.type === "asset");
const cachedFileNames = assets.map(o => o.fileName).filter(fileName => fileName !== "index.html");

View file

@ -265,7 +265,7 @@ export class Platform {
}
get version() {
return window.HYDROGEN_VERSION;
return HYDROGEN_VERSION;
}
dispose() {

View file

@ -181,7 +181,7 @@ export class ServiceWorkerHandler {
}
get version() {
return window.HYDROGEN_VERSION;
return HYDROGEN_VERSION;
}
get buildHash() {

View file

@ -16,10 +16,6 @@
<link rel="alternate stylesheet" type="text/css" href="./ui/css/themes/bubbles/theme.css" title="Bubbles Theme">
</head>
<body class="hydrogen">
<script id="version" type="disabled">
window.HYDROGEN_VERSION = "%%VERSION%%";
window.HYDROGEN_GLOBAL_HASH = "%%GLOBAL_HASH%%";
</script>
<script id="main" type="module">
import {main} from "./main";
import {Platform} from "./Platform";

View file

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

View file

@ -1,5 +1,9 @@
const injectWebManifest = require("./scripts/build-plugins/manifest");
const injectServiceWorker = require("./scripts/build-plugins/service-worker");
const fs = require("fs");
const path = require("path");
const version = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8")).version;
export default {
public: false,
@ -22,5 +26,8 @@ export default {
plugins: [
injectWebManifest("assets/manifest.json"),
injectServiceWorker("sw.js")
]
],
define: {
"HYDROGEN_VERSION": JSON.stringify(version)
}
};