2024-08-16 15:30:44 +05:30
|
|
|
/*
|
2025-01-17 17:05:05 +00:00
|
|
|
Copyright 2025 New Vector Ltd.
|
2024-08-16 15:30:44 +05:30
|
|
|
Copyright 2024 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2025-01-17 17:05:05 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2024-08-16 15:30:44 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from "fs/promises";
|
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This rollup plugin makes it possible to use the serviceworker with the dev server.
|
|
|
|
* The service worker is located in `/src/platform/web/sw.js` and it contains some
|
|
|
|
* fields that need to be replaced with sensible values.
|
|
|
|
*
|
|
|
|
* We have a plugin that does this during build (see `./service-worker.js`).
|
|
|
|
* This plugin does more or less the same but for dev.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function transformServiceWorkerInDevServer() {
|
|
|
|
// See https://vitejs.dev/config/shared-options.html#define
|
|
|
|
// Comes from vite.config.js
|
|
|
|
let define;
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: "hydrogen:transformServiceWorkerInDevServer",
|
|
|
|
apply: "serve",
|
|
|
|
enforce: "pre",
|
|
|
|
|
|
|
|
configResolved(resolvedConfig) {
|
|
|
|
// store the resolved config
|
|
|
|
define = resolvedConfig.define;
|
|
|
|
},
|
|
|
|
|
|
|
|
async load(id) {
|
|
|
|
if (!id.includes("sw.js")) return null;
|
|
|
|
let code = await readServiceWorkerCode();
|
|
|
|
for (const [key, value] of Object.entries(define)) {
|
|
|
|
code = code.replaceAll(key, value);
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read service worker code from `src/platform/web/sw.js`
|
|
|
|
* @returns code as string
|
|
|
|
*/
|
|
|
|
async function readServiceWorkerCode() {
|
|
|
|
const resolvedLocation = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
"../../",
|
|
|
|
"./src/platform/web/sw.js"
|
|
|
|
);
|
|
|
|
const data = await fs.readFile(resolvedLocation, { encoding: "utf-8" });
|
|
|
|
return data;
|
|
|
|
}
|