Files
logsdu/esbuild.config.mjs

131 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2026-08-10 14:25:30 -03:00
// *************************************************************************
// * (C)opyright 2026 by Ruben Carlo Benante *
// * *
// * This program is free software; you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
// * the Free Software Foundation, either version 3 of the License, or *
// * (at your option) any later version. *
// * *
// * This program is distributed in the hope that it will be useful, *
// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
// * GNU General Public License for more details. *
// * *
// * You should have received a copy of the GNU General Public License *
// * along with this program. If not, see http://www.gnu.org/licenses/. *
// * *
// * Contact author at: *
// * Ruben Carlo Benante *
// * rcb@beco.cc *
// *************************************************************************
import esbuild from 'esbuild';
import process from 'process';
2026-08-10 15:23:17 -03:00
import { cp, mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
2026-08-10 14:25:30 -03:00
const banner = `/*
* logsdu - fills and submits a three-field academic portal login.
* Copyright (C) 2026 Ruben Carlo Benante (Dr. Beco) <rcb@beco.cc>
* Licensed under the GNU General Public License v3.0 or later.
* See https://www.gnu.org/licenses/ and the LICENSE file.
*
* THIS IS A GENERATED/BUNDLED FILE BY ESBUILD.
* To view the source, visit the repository of this extension.
*/
`;
const prod = process.argv[2] === 'production';
2026-08-10 15:23:17 -03:00
// Target browser. Firefox and Chrome disagree on exactly one manifest key, so
// the manifest is written per target rather than duplicated in the tree.
const target = process.env.TARGET === 'chrome' ? 'chrome' : 'firefox';
// Each target gets its own directory. They used to share one, and the result
// was that whichever build ran last silently won: loading the other browser's
// output then failed with a confusing manifest error. Separate directories
// mean both can exist at once and neither can be stale by accident.
const outdir = `build/${target}`;
2026-08-10 15:23:17 -03:00
/**
* Write the manifest for the target browser.
*
* Firefox MV3 runs the background as an event page ("scripts"); Chrome MV3
* requires a service worker and rejects "scripts" outright, so the two cannot
* simply coexist in one file. Chrome also has no use for the gecko block.
*/
async function writeManifest() {
const manifest = JSON.parse(await readFile('src/manifest.json', 'utf8'));
if (target === 'chrome') {
delete manifest.browser_specific_settings;
manifest.background = { service_worker: 'background.js' };
}
await writeFile(
`${outdir}/manifest.json`,
`${JSON.stringify(manifest, null, '\t')}\n`,
);
}
// Everything that is not TypeScript is copied verbatim into the output, so
// that the directory can be handed straight to about:debugging or to Chrome's
// "Load unpacked".
2026-08-10 14:25:30 -03:00
async function copyStatic() {
await mkdir(outdir, { recursive: true });
for (const name of await readdir('src')) {
2026-08-10 15:23:17 -03:00
if (name.endsWith('.html') || name.endsWith('.css')) {
2026-08-10 14:25:30 -03:00
await cp(`src/${name}`, `${outdir}/${name}`);
}
}
await cp('icons', `${outdir}/icons`, { recursive: true });
2026-08-10 15:23:17 -03:00
await writeManifest();
2026-08-10 14:25:30 -03:00
}
await copyStatic();
// In watch mode the static files must follow every rebuild, otherwise editing
// manifest.json or a .html file silently changes nothing in the output.
2026-08-10 14:25:30 -03:00
const staticPlugin = {
name: 'copy-static',
setup(build) {
build.onEnd(() => copyStatic());
},
};
const context = await esbuild.context({
banner: {
js: banner,
},
plugins: [staticPlugin],
2026-08-10 15:23:17 -03:00
// Five independent entry points: no shared runtime, no imports at load
2026-08-10 14:25:30 -03:00
// time. Content scripts and page-world scripts cannot be ES modules, so
// every bundle has to stand alone.
entryPoints: [
2026-08-10 15:23:17 -03:00
'src/background.ts',
2026-08-10 14:25:30 -03:00
'src/content.ts',
'src/injected.ts',
'src/options.ts',
'src/popup.ts',
],
bundle: true,
format: 'iife',
target: 'es2021',
logLevel: 'info',
sourcemap: prod ? false : 'inline',
treeShaking: true,
outdir,
2026-08-10 15:23:17 -03:00
// Deliberately never minified. addons.mozilla.org requires a separate
// source-code submission for any add-on whose uploaded code is minified or
// otherwise machine-generated, and that obligation would apply to every
// release from now on. The whole extension is a few tens of kilobytes, so
// the saving would not pay for the process, and shipping readable code
// makes the review -- and anyone auditing what handles their credentials --
// straightforward.
minify: false,
2026-08-10 14:25:30 -03:00
});
if (prod) {
await context.rebuild();
process.exit(0);
} else {
await context.watch();
}