Newer
Older
gnexus-creds-extension / tools / bundle-background.js
#!/usr/bin/env node
/**
 * Bundle src/api.js + src/background.js into a single classic (non-module)
 * background.js for Firefox MV3, which does not support background.service_worker
 * or type:"module" (event-page scripts run as classic scripts sharing one scope).
 *
 * Usage: node tools/bundle-background.js <build-dir>
 */
const fs = require("fs");
const path = require("path");

const dir = process.argv[2];
if (!dir) {
  console.error("build dir argument required");
  process.exit(1);
}

const apiPath = path.join(dir, "src", "api.js");
const bgPath = path.join(dir, "src", "background.js");

// Strip ES module keywords so the result is valid as a classic script.
const api = fs.readFileSync(apiPath, "utf8").replace(/^export /gm, "");
const bg = fs.readFileSync(bgPath, "utf8").replace(
  /^\s*import\s+.*\s+from\s+["']\.\/api\.js["'];\s*\n/m,
  ""
);

// api.js function declarations are hoisted into the shared classic scope,
// so background.js can reference them without an import.
fs.writeFileSync(bgPath, api.trimEnd() + "\n\n" + bg);
fs.rmSync(apiPath);