passchmop/esbuild.config.mjs

58 lines
1.9 KiB
JavaScript

import esbuild from 'esbuild';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';
const watch = process.argv.includes('--watch');
const __dirname = dirname(fileURLToPath(import.meta.url));
// isomorphic-git's `.` subpath export only points at the CJS build, which
// does a top-level `require('crypto')` that esbuild can't bundle for the
// browser. The package ships an ESM entry at ./index.js but the exports
// map hides it. Redirect the bare specifier to the ESM file by hand.
const isogitEsmPlugin = {
name: 'isogit-esm',
setup(build) {
const esmEntry = resolve(__dirname, 'node_modules/isomorphic-git/index.js');
build.onResolve({ filter: /^isomorphic-git$/ }, () => ({ path: esmEntry }));
},
};
const common = {
bundle: true,
format: 'iife',
target: ['firefox115'],
platform: 'browser',
mainFields: ['browser', 'module', 'main'],
conditions: ['browser', 'import', 'default'],
logLevel: 'info',
sourcemap: true,
minify: false,
define: {
'process.env.NODE_ENV': '"production"',
'global': 'globalThis',
},
plugins: [isogitEsmPlugin],
};
// Only the background bundle pulls in isomorphic-git and therefore needs
// the Buffer polyfill. popup/options talk to the background via runtime
// messages and stay tiny.
const polyfillInject = [resolve(__dirname, 'src/common/polyfills.ts')];
const entries = [
{ entryPoints: ['src/background/index.ts'], outfile: 'build/background.js', inject: polyfillInject },
{ entryPoints: ['src/popup/popup.ts'], outfile: 'build/popup.js' },
{ entryPoints: ['src/options/options.ts'], outfile: 'build/options.js' },
];
if (watch) {
for (const e of entries) {
const ctx = await esbuild.context({ ...common, ...e });
await ctx.watch();
}
console.log('watching…');
} else {
await Promise.all(entries.map(e => esbuild.build({ ...common, ...e })));
console.log('build ok');
}