MeshChatX/scripts/bundle-electron-preload.cjs

44 lines
1.4 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 = (() => {
let body = shellSource.replace(/^"use strict";\s*/m, "");
const preloadOnlyCut = body.indexOf("/**\n * Whether window.open should create");
if (preloadOnlyCut >= 0) {
return body.slice(0, preloadOnlyCut).trim();
}
return body.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)}`);