Skip to content

Commit 17fbcdc

Browse files
committed
feat: integrate carbon calculator with page speed results
- adds esbuild to bundle carbon calculator dependencies - calls carbon calculator function from welcome panel with page speed results
1 parent fb76408 commit 17fbcdc

File tree

6 files changed

+552
-20
lines changed

6 files changed

+552
-20
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ build/
66

77
# Binary files
88
.DS_Store
9+
10+
# Bundled files
11+
*.bundle.js

build.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const fs = require('fs');
44
const path = require('path');
55
const archiver = require('archiver');
6+
const { bundleCarbonCalculator } = require('./bundle.js');
67

78
// Check if archiver is available, if not, provide instructions
89
try {
@@ -44,7 +45,7 @@ function copyCommonFiles(bundlePath) {
4445
const source = file;
4546
const chromeDest = path.join(chromeDir, file);
4647
const firefoxDest = path.join(firefoxDir, file);
47-
48+
4849
if (fs.lstatSync(source).isDirectory()) {
4950
// Copy directory recursively
5051
copyDirectoryRecursive(source, chromeDest);
@@ -77,12 +78,12 @@ function copyDirectoryRecursive(src, dest) {
7778
if (!fs.existsSync(dest)) {
7879
fs.mkdirSync(dest, { recursive: true });
7980
}
80-
81+
8182
const files = fs.readdirSync(src);
8283
files.forEach(file => {
8384
const srcPath = path.join(src, file);
8485
const destPath = path.join(dest, file);
85-
86+
8687
if (fs.lstatSync(srcPath).isDirectory()) {
8788
copyDirectoryRecursive(srcPath, destPath);
8889
} else {
@@ -122,30 +123,30 @@ function createZips() {
122123
return new Promise((resolve, reject) => {
123124
const chromeZip = fs.createWriteStream(path.join(buildDir, 'carbon-visualizer-chrome.zip'));
124125
const firefoxZip = fs.createWriteStream(path.join(buildDir, 'carbon-visualizer-firefox.zip'));
125-
126+
126127
const chromeArchive = archiver('zip', { zlib: { level: 9 } });
127128
const firefoxArchive = archiver('zip', { zlib: { level: 9 } });
128-
129+
129130
chromeZip.on('close', () => {
130131
console.log('✅ Chrome extension packaged: build/carbon-visualizer-chrome.zip');
131132
});
132-
133+
133134
firefoxZip.on('close', () => {
134135
console.log('✅ Firefox extension packaged: build/carbon-visualizer-firefox.zip');
135136
});
136-
137+
137138
chromeArchive.on('error', reject);
138139
firefoxArchive.on('error', reject);
139-
140+
140141
chromeArchive.pipe(chromeZip);
141142
firefoxArchive.pipe(firefoxZip);
142-
143+
143144
chromeArchive.directory(chromeDir, false);
144145
firefoxArchive.directory(firefoxDir, false);
145-
146+
146147
chromeArchive.finalize();
147148
firefoxArchive.finalize();
148-
149+
149150
Promise.all([
150151
new Promise(resolve => chromeArchive.on('end', resolve)),
151152
new Promise(resolve => firefoxArchive.on('end', resolve))
@@ -200,21 +201,23 @@ async function build() {
200201
console.log('🚀 Building Carbon Visualizer extension for Chrome and Firefox...');
201202

202203
const bundlePath = bundleCoreCss();
204+
await bundleCarbonCalculator();
205+
203206
createDirectories();
204207
copyCommonFiles(bundlePath);
205208
copyManifests();
206209
patchFirefoxManifest();
207-
210+
208211
await createZips();
209-
212+
210213
console.log('\n🎉 Build complete!');
211214
console.log('\n📦 Extension packages created:');
212215
console.log(' Chrome: build/carbon-visualizer-chrome.zip');
213216
console.log(' Firefox: build/carbon-visualizer-firefox.zip');
214217
console.log('\n📋 Installation instructions:');
215218
console.log(' Chrome: Load unpacked from build/chrome/');
216219
console.log(' Firefox: Load temporary add-on from build/firefox/');
217-
220+
218221
} catch (error) {
219222
console.error('❌ Build failed:', error);
220223
process.exit(1);

bundle.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
3+
const esbuild = require('esbuild');
4+
const path = require('path');
5+
6+
async function bundleCarbonCalculator() {
7+
try {
8+
console.log('📦 Bundling CarbonCalculator.js with dependencies...');
9+
10+
await esbuild.build({
11+
entryPoints: [path.join(__dirname, 'src/core/CarbonCalculator.js')],
12+
bundle: true,
13+
format: 'esm',
14+
outfile: path.join(__dirname, 'src/core/CarbonCalculator.bundle.js'),
15+
platform: 'browser',
16+
target: 'es2020',
17+
external: [],
18+
minify: false,
19+
sourcemap: false,
20+
});
21+
22+
console.log('✅ CarbonCalculator.js bundled successfully');
23+
} catch (error) {
24+
console.error('❌ Bundling failed:', error);
25+
process.exit(1);
26+
}
27+
}
28+
29+
if (require.main === module) {
30+
bundleCarbonCalculator();
31+
}
32+
33+
module.exports = { bundleCarbonCalculator };
34+

0 commit comments

Comments
 (0)