-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-enhanced.js
More file actions
232 lines (189 loc) · 7.49 KB
/
build-enhanced.js
File metadata and controls
232 lines (189 loc) · 7.49 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node
/**
* Enhanced build script for APQ Debugger.
* Uses esbuild for fast ES-module bundling + minification of JS,
* and Clean-CSS for CSS minification.
* Outputs a ready-to-load extension into the extension_build/ folder.
*/
const fs = require('fs-extra');
const path = require('path');
const esbuild = require('esbuild');
const CleanCSS = require('clean-css');
const chokidar = require('chokidar');
// ── Configuration ─────────────────────────────────────────────────
const config = {
sourceDir: __dirname,
extDir: path.join(__dirname, 'extension_build'),
js: [
{
entry: 'js/sw/index.js',
outfile: 'service-worker.min.js',
},
{
entry: 'js/ui/index.js',
outfile: 'devtools.min.js',
},
],
css: {
input: 'frontend/devtools.css',
output: 'devtools.min.css',
options: {
level: {
1: { all: true, normalizeUrls: false },
2: {
all: false,
removeDuplicateRules: true,
removeDuplicateFontRules: true,
removeEmpty: true,
},
},
format: 'keep-breaks',
},
},
html: 'frontend/devtools.html',
manifest: 'manifest.json',
iconsDir: 'icons',
};
// CLI flags
const isDev = process.argv.includes('--dev');
const isWatch = process.argv.includes('--watch');
// ── JS bundling (esbuild) ─────────────────────────────────────────
async function bundleJS(entry, outfile) {
const inputPath = path.join(config.sourceDir, entry);
const outputPath = path.join(config.extDir, outfile);
if (!(await fs.pathExists(inputPath))) {
console.log(`❌ Entry not found: ${entry}`);
return;
}
console.log(`🔄 Bundling ${entry}...`);
const result = await esbuild.build({
entryPoints: [inputPath],
bundle: true,
outfile: outputPath,
format: 'iife',
target: ['chrome100'],
minify: !isDev,
sourcemap: isDev ? 'inline' : false,
drop: isDev ? [] : ['debugger'],
pure: isDev ? [] : ['console.info', 'console.debug'],
logLevel: 'warning',
metafile: true,
});
// Calculate combined input size from metafile
let inputSize = 0;
if (result.metafile) {
for (const src of Object.values(result.metafile.inputs)) {
inputSize += src.bytes;
}
}
const outputStat = await fs.stat(outputPath);
const reduction =
inputSize > 0 ? (((inputSize - outputStat.size) / inputSize) * 100).toFixed(1) : '?';
console.log(`✅ ${entry} → ${outfile}`);
console.log(
` ${inputSize} bytes (source) → ${outputStat.size} bytes (${reduction}% reduction)\n`
);
}
// ── CSS minification (Clean-CSS) ──────────────────────────────────
async function minifyCSS() {
const inputPath = path.join(config.sourceDir, config.css.input);
const outputPath = path.join(config.extDir, config.css.output);
if (!(await fs.pathExists(inputPath))) {
console.log(`❌ CSS not found: ${config.css.input}`);
return;
}
console.log(`🔄 Processing ${config.css.input}...`);
const code = await fs.readFile(inputPath, 'utf8');
const cleanCSS = new CleanCSS(config.css.options);
const result = cleanCSS.minify(code);
if (result.errors.length > 0) {
console.warn(`⚠️ CSS warnings:`, result.errors);
}
await fs.writeFile(outputPath, result.styles);
const originalSize = Buffer.byteLength(code, 'utf8');
const minifiedSize = Buffer.byteLength(result.styles, 'utf8');
const reduction = (((originalSize - minifiedSize) / originalSize) * 100).toFixed(1);
console.log(`✅ ${config.css.input} → ${config.css.output}`);
console.log(` ${originalSize} bytes → ${minifiedSize} bytes (${reduction}% reduction)\n`);
}
// ── Asset copying ─────────────────────────────────────────────────
async function copyAssets() {
const extDir = config.extDir;
// Copy icons
const iconsSrc = path.join(config.sourceDir, config.iconsDir);
const iconsDest = path.join(extDir, config.iconsDir);
if (await fs.pathExists(iconsSrc)) {
await fs.copy(iconsSrc, iconsDest);
console.log('✅ Copied icons/');
}
// Copy and patch manifest.json (sync version from package.json)
const manifestSrc = path.join(config.sourceDir, config.manifest);
const manifest = JSON.parse(await fs.readFile(manifestSrc, 'utf8'));
const pkg = JSON.parse(await fs.readFile(path.join(config.sourceDir, 'package.json'), 'utf8'));
manifest.version = pkg.version;
manifest.background.service_worker = 'service-worker.min.js';
manifest.devtools_page = 'devtools.html';
if (manifest.icons) {
for (const key of Object.keys(manifest.icons)) {
manifest.icons[key] = `icons/icon${key}.png`;
}
}
await fs.writeFile(path.join(extDir, 'manifest.json'), JSON.stringify(manifest, null, 2));
console.log('✅ Copied and patched manifest.json');
// Copy and patch HTML
const htmlSrc = path.join(config.sourceDir, config.html);
let html = await fs.readFile(htmlSrc, 'utf8');
html = html.replace(
/<link rel="stylesheet"[^>]*href=["'][^"']*devtools(\.min)?\.css["'][^>]*>/,
'<link rel="stylesheet" href="devtools.min.css">'
);
html = html.replace(
/<script[^>]*src=["'][^"']*devtools(\.min)?\.js["'][^>]*><\/script>/,
'<script src="devtools.min.js"></script>'
);
// Fix icon paths for the flat build output (../icons/ → icons/)
html = html.replace(/\.\.\/icons\//g, 'icons/');
await fs.writeFile(path.join(extDir, 'devtools.html'), html);
console.log('✅ Copied and patched devtools.html');
}
// ── Main build ────────────────────────────────────────────────────
async function build() {
console.log('🔨 Starting esbuild-powered build...\n');
const startTime = Date.now();
await fs.ensureDir(config.extDir);
// Bundle all JS entry points in parallel
await Promise.all(config.js.map(({ entry, outfile }) => bundleJS(entry, outfile)));
// Minify CSS
await minifyCSS();
// Copy static assets
await copyAssets();
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`\n🎉 Build completed in ${duration}s!`);
console.log('📦 Extension files are ready in the extension_build/ folder.');
}
// ── Watch mode ────────────────────────────────────────────────────
async function watch() {
console.log('👀 Starting watch mode...\n');
// Run an initial full build
await build();
const watcher = chokidar.watch(
['js/**/*.js', 'frontend/*.css', 'frontend/devtools.html', 'manifest.json', 'icons/*'],
{
ignored: [/\.min\.(js|css)$/, /node_modules/, /extension_build/],
persistent: true,
}
);
watcher.on('change', async (filePath) => {
const relativePath = path.relative(config.sourceDir, filePath);
console.log(`\n📝 File changed: ${relativePath}`);
await build();
});
console.log('Watching for changes... (Press Ctrl+C to stop)\n');
}
// ── CLI entry point ───────────────────────────────────────────────
if (isWatch) {
watch();
} else {
build();
}
module.exports = { build, watch };