theseus/bundled-addons/consent/build-inject.js

39 lines
2 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
// Builds inject.js — the page-inject script — from DuckDuckGo's autoconsent
// library (node_modules, MPL-2.0) plus the glue that connects it to this
// add-on over theseus.invoke / theseus.evalInPage. Also copies the compact
// rule bundle and the licence next to it, so the add-on tarball is self-
// contained and a rule update ships through the add-on channel.
//
// node bundled-addons/consent/build-inject.js
//
// Page-inject scripts run through `new Function("theseus", "require", src)`
// in the isolated world, with require() limited to "electron", so the
// library is inlined as a CommonJS module rather than required.
const fs = require("node:fs");
const path = require("node:path");
const here = __dirname;
const pkgDir = path.join(here, "..", "..", "node_modules", "@duckduckgo", "autoconsent");
const lib = fs.readFileSync(path.join(pkgDir, "dist", "autoconsent.cjs.js"), "utf8");
const version = JSON.parse(fs.readFileSync(path.join(pkgDir, "package.json"), "utf8")).version;
const glue = fs.readFileSync(path.join(here, "inject-glue.js"), "utf8");
const out = [
`// GENERATED by build-inject.js — do not edit. autoconsent ${version} (DuckDuckGo, MPL-2.0) + Theseus glue.`,
`(function () {`,
` const module = { exports: {} }; const exports = module.exports;`,
` ${lib.replace(/\n/g, "\n ")}`,
` const AutoConsentCtor = module.exports.default || module.exports.AutoConsent || module.exports;`,
` const AUTOCONSENT_VERSION = ${JSON.stringify(version)};`,
` ${glue.replace(/\n/g, "\n ")}`,
`})();`,
``,
].join("\n");
fs.writeFileSync(path.join(here, "inject.js"), out);
fs.mkdirSync(path.join(here, "rules"), { recursive: true });
fs.copyFileSync(path.join(pkgDir, "rules", "compact-rules.json"), path.join(here, "rules", "compact-rules.json"));
fs.copyFileSync(path.join(pkgDir, "LICENSE"), path.join(here, "LICENSE-autoconsent"));
fs.writeFileSync(path.join(here, "rules", "VERSION"), version + "\n");
console.log(`inject.js: ${out.length} bytes, autoconsent ${version}, rules copied`);