-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
110 lines (95 loc) · 3 KB
/
build.js
File metadata and controls
110 lines (95 loc) · 3 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
105
106
107
108
109
110
import * as esbuild from 'esbuild';
import { cpSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
const isWatch = process.argv.includes('--watch');
const isPackage = process.argv.includes('--package');
const entries = [
{ in: 'src/content/index.js', out: 'dist/content.js' },
{ in: 'src/background/index.js', out: 'dist/background.js' },
{ in: 'src/popup/popup.js', out: 'dist/popup.js' },
];
async function build() {
mkdirSync('dist', { recursive: true });
// Bundle JS entry points as IIFE (Chrome extension content scripts require this)
for (const entry of entries) {
const result = await esbuild.build({
entryPoints: [entry.in],
bundle: true,
format: 'iife',
outfile: entry.out,
minify: !isWatch,
sourcemap: isWatch ? 'inline' : false,
target: 'chrome120',
logLevel: 'info',
metafile: true,
});
// Report bundle sizes
if (result.metafile) {
for (const [name, info] of Object.entries(result.metafile.outputs)) {
console.log(` ${name}: ${(info.bytes / 1024).toFixed(1)} KB`);
}
}
}
// Copy static assets
cpSync('static/icons', 'dist/icons', { recursive: true });
cpSync('static/overlay.css', 'dist/overlay.css');
cpSync('src/popup/popup.html', 'dist/popup.html');
cpSync('src/popup/popup.css', 'dist/popup.css');
// Copy manifest.json, stripping debug content script for production
const manifest = JSON.parse(readFileSync('manifest.json', 'utf8'));
if (isPackage) {
manifest.content_scripts = manifest.content_scripts.filter(
(cs) => !cs.js.includes('debug.js'),
);
} else {
cpSync('src/debug/debug.js', 'dist/debug.js');
}
writeFileSync('dist/manifest.json', JSON.stringify(manifest, null, 2));
console.log('Build complete → dist/');
}
async function watch() {
// Initial build
await build();
// Watch mode for JS bundles
for (const entry of entries) {
const ctx = await esbuild.context({
entryPoints: [entry.in],
bundle: true,
format: 'iife',
outfile: entry.out,
minify: false,
sourcemap: 'inline',
target: 'chrome120',
logLevel: 'info',
});
await ctx.watch();
}
console.log('Watching for changes...');
}
async function packageExtension() {
await build();
const manifest = JSON.parse(readFileSync('dist/manifest.json', 'utf8'));
const zipName = `cesar-v${manifest.version}.zip`;
// Cross-platform zip: use PowerShell on Windows, zip on Unix
const { execSync } = await import('child_process');
if (process.platform === 'win32') {
execSync(
`powershell -Command "Compress-Archive -Path 'dist\\*' -DestinationPath '${zipName}' -Force"`
);
} else {
execSync(`cd dist && zip -r ../${zipName} .`);
}
console.log(`Packaged → ${zipName}`);
}
if (isWatch) {
watch().catch(console.error);
} else if (isPackage) {
packageExtension().catch((err) => {
console.error(err);
process.exit(1);
});
} else {
build().catch((err) => {
console.error(err);
process.exit(1);
});
}