Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ build/

# Binary files
.DS_Store

# Bundled files
*.bundle.js
31 changes: 17 additions & 14 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const { bundleCarbonCalculator } = require('./bundle.js');

// Check if archiver is available, if not, provide instructions
try {
Expand Down Expand Up @@ -44,7 +45,7 @@ function copyCommonFiles(bundlePath) {
const source = file;
const chromeDest = path.join(chromeDir, file);
const firefoxDest = path.join(firefoxDir, file);

if (fs.lstatSync(source).isDirectory()) {
// Copy directory recursively
copyDirectoryRecursive(source, chromeDest);
Expand Down Expand Up @@ -77,12 +78,12 @@ function copyDirectoryRecursive(src, dest) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest, { recursive: true });
}

const files = fs.readdirSync(src);
files.forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);

if (fs.lstatSync(srcPath).isDirectory()) {
copyDirectoryRecursive(srcPath, destPath);
} else {
Expand Down Expand Up @@ -122,30 +123,30 @@ function createZips() {
return new Promise((resolve, reject) => {
const chromeZip = fs.createWriteStream(path.join(buildDir, 'carbon-visualizer-chrome.zip'));
const firefoxZip = fs.createWriteStream(path.join(buildDir, 'carbon-visualizer-firefox.zip'));

const chromeArchive = archiver('zip', { zlib: { level: 9 } });
const firefoxArchive = archiver('zip', { zlib: { level: 9 } });

chromeZip.on('close', () => {
console.log('✅ Chrome extension packaged: build/carbon-visualizer-chrome.zip');
});

firefoxZip.on('close', () => {
console.log('✅ Firefox extension packaged: build/carbon-visualizer-firefox.zip');
});

chromeArchive.on('error', reject);
firefoxArchive.on('error', reject);

chromeArchive.pipe(chromeZip);
firefoxArchive.pipe(firefoxZip);

chromeArchive.directory(chromeDir, false);
firefoxArchive.directory(firefoxDir, false);

chromeArchive.finalize();
firefoxArchive.finalize();

Promise.all([
new Promise(resolve => chromeArchive.on('end', resolve)),
new Promise(resolve => firefoxArchive.on('end', resolve))
Expand Down Expand Up @@ -200,21 +201,23 @@ async function build() {
console.log('🚀 Building Carbon Visualizer extension for Chrome and Firefox...');

const bundlePath = bundleCoreCss();
await bundleCarbonCalculator();

createDirectories();
copyCommonFiles(bundlePath);
copyManifests();
patchFirefoxManifest();

await createZips();

console.log('\n🎉 Build complete!');
console.log('\n📦 Extension packages created:');
console.log(' Chrome: build/carbon-visualizer-chrome.zip');
console.log(' Firefox: build/carbon-visualizer-firefox.zip');
console.log('\n📋 Installation instructions:');
console.log(' Chrome: Load unpacked from build/chrome/');
console.log(' Firefox: Load temporary add-on from build/firefox/');

} catch (error) {
console.error('❌ Build failed:', error);
process.exit(1);
Expand Down
34 changes: 34 additions & 0 deletions bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node

const esbuild = require('esbuild');
const path = require('path');

async function bundleCarbonCalculator() {
try {
console.log('📦 Bundling CarbonCalculator.js with dependencies...');

await esbuild.build({
entryPoints: [path.join(__dirname, 'src/core/CarbonCalculator.js')],
bundle: true,
format: 'esm',
outfile: path.join(__dirname, 'src/core/CarbonCalculator.bundle.js'),
platform: 'browser',
target: 'es2020',
external: [],
minify: false,
sourcemap: false,
});

console.log('✅ CarbonCalculator.js bundled successfully');
} catch (error) {
console.error('❌ Bundling failed:', error);
process.exit(1);
}
}

if (require.main === module) {
bundleCarbonCalculator();
}

module.exports = { bundleCarbonCalculator };

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't have anything bundling our js for the browser, and this is the first time that we're importing a 3rd party npm package @tgwf/co, e.g:
import { co2 } from "@tgwf/co2";

Since deps from node_modules weren't being bundled, trying to import resulted in this error when running the extension in the browser: TypeError: Failed to resolve module specifier "@tgwf/co2".

This is kind of a quick and dirty solution that just bundles the carbon calculator with the underlying dependency. We could probably improve this by bundling each entry point js file along with their underlying deps, but that's bigger than the scope of this PR.

Loading