MeshChatX/scripts/bundle-electron-preload.cjs

40 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
"use strict";
/**
* Electron sandbox preloads cannot require() sibling modules. Bundle shellOrigin
* into a single preload script for packaged AppImage / desktop builds.
*/
const fs = require("fs");
const path = require("path");
const root = path.join(__dirname, "..");
const shellPath = path.join(root, "electron", "shellOrigin.js");
const preloadPath = path.join(root, "electron", "preload.js");
const outPath = path.join(root, "electron", "preload.bundle.js");
const shellSource = fs.readFileSync(shellPath, "utf8");
const preloadSource = fs.readFileSync(preloadPath, "utf8");
const shellBody = shellSource
.replace(/^"use strict";\s*/m, "")
.replace(/\nmodule\.exports\s*=\s*\{[\s\S]*$/m, "")
.trim();
const preloadBody = preloadSource
.replace(/const \{ isTrustedShellOrigin \} = require\("\.\/shellOrigin"\);\s*/m, "")
.trim();
const bundled = `// SPDX-License-Identifier: 0BSD
// Generated by scripts/bundle-electron-preload.cjs. Do not edit by hand.
"use strict";
${shellBody}
${preloadBody}
`;
fs.writeFileSync(outPath, bundled, "utf8");
console.log(`Bundled electron preload -> ${path.relative(root, outPath)}`);