-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
104 lines (86 loc) · 3.06 KB
/
Copy pathbuild.js
File metadata and controls
104 lines (86 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
// MoxMox build script — bundles src/ into dist/chrome/ and dist/firefox/.
import * as esbuild from 'esbuild';
import { readFileSync, writeFileSync, mkdirSync, cpSync, rmSync, existsSync } from 'fs';
import { join } from 'path';
const BROWSERS = ['chrome', 'firefox'];
const ROOT = import.meta.dirname;
// Entry points bundled by esbuild (IIFE, no module runtime needed).
const BUNDLE_ENTRIES = ['background.js', 'content.js', 'content-main.js', 'popup.js'];
// Files copied as-is (no bundling).
const COPY_FILES = ['popup.html', 'styles.css'];
function mergeManifests(browser) {
const base = JSON.parse(readFileSync(join(ROOT, 'manifests', 'base.json'), 'utf-8'));
const override = JSON.parse(readFileSync(join(ROOT, 'manifests', `${browser}.json`), 'utf-8'));
return { ...base, ...override };
}
async function buildBrowser(browser) {
const dist = join(ROOT, 'dist', browser);
// Clean and create dist dir.
rmSync(dist, { recursive: true, force: true });
mkdirSync(dist, { recursive: true });
// Bundle entry points.
for (const entry of BUNDLE_ENTRIES) {
await esbuild.build({
entryPoints: [join(ROOT, 'src', entry)],
bundle: true,
format: 'iife',
outfile: join(dist, entry),
target: 'es2020',
minify: false,
sourcemap: false,
});
}
// Copy non-bundled files.
for (const file of COPY_FILES) {
const src = join(ROOT, 'src', file);
if (existsSync(src)) {
cpSync(src, join(dist, file));
}
}
// Copy icons.
if (existsSync(join(ROOT, 'icons'))) {
cpSync(join(ROOT, 'icons'), join(dist, 'icons'), { recursive: true });
}
// Merge and write manifest.
const manifest = mergeManifests(browser);
writeFileSync(join(dist, 'manifest.json'), JSON.stringify(manifest, null, 2) + '\n');
console.log(`✔ Built ${browser} → dist/${browser}/`);
}
async function main() {
const args = process.argv.slice(2).filter(a => !a.startsWith('-'));
const targets = args.length > 0 ? args : BROWSERS;
for (const browser of targets) {
if (!BROWSERS.includes(browser)) {
console.error(`Unknown browser: ${browser}. Use: ${BROWSERS.join(', ')}`);
process.exit(1);
}
await buildBrowser(browser);
}
}
await main().catch(err => {
console.error(err);
process.exit(1);
});
// ─── Watch mode ─────────────────────────────────────────────────────
if (process.argv.includes('--watch')) {
const { watch } = await import('fs');
const dirs = [join(ROOT, 'src'), join(ROOT, 'manifests'), join(ROOT, 'icons')];
let timer = null;
function rebuild() {
clearTimeout(timer);
timer = setTimeout(async () => {
try {
for (const browser of BROWSERS) await buildBrowser(browser);
} catch (e) {
console.error('Build error:', e.message);
}
}, 100);
}
for (const dir of dirs) {
if (existsSync(dir)) {
watch(dir, { recursive: true }, rebuild);
}
}
console.log('\n👀 Watching for changes… (Ctrl+C to stop)');
}