Skip to content

Commit 32e8117

Browse files
fix: adjust css file loading (#7)
* fix: adjust css file paths * fix: load all the things * chore: delete styles.css * fix: load styles one time only * chore: bundle styles during build * chore: remove panel css from configs, load in bundle
1 parent 90f3f33 commit 32e8117

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

build.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function createDirectories() {
3939
}
4040

4141
// Copy common files
42-
function copyCommonFiles() {
42+
function copyCommonFiles(bundlePath) {
4343
commonFiles.forEach(file => {
4444
const source = file;
4545
const chromeDest = path.join(chromeDir, file);
@@ -55,6 +55,22 @@ function copyCommonFiles() {
5555
fs.copyFileSync(source, firefoxDest);
5656
}
5757
});
58+
59+
// Copy the bundled CSS to the extension's src/styles directory
60+
if (bundlePath && fs.existsSync(bundlePath)) {
61+
const chromeStylesDir = path.join(chromeDir, 'src/styles');
62+
const firefoxStylesDir = path.join(firefoxDir, 'src/styles');
63+
64+
if (!fs.existsSync(chromeStylesDir)) {
65+
fs.mkdirSync(chromeStylesDir, { recursive: true });
66+
}
67+
if (!fs.existsSync(firefoxStylesDir)) {
68+
fs.mkdirSync(firefoxStylesDir, { recursive: true });
69+
}
70+
71+
fs.copyFileSync(bundlePath, path.join(chromeStylesDir, 'core-bundle.css'));
72+
fs.copyFileSync(bundlePath, path.join(firefoxStylesDir, 'core-bundle.css'));
73+
}
5874
}
5975

6076
function copyDirectoryRecursive(src, dest) {
@@ -137,13 +153,55 @@ function createZips() {
137153
});
138154
}
139155

156+
// Bundle all CSS files (core + panels)
157+
function bundleCoreCss() {
158+
const srcCssDir = 'src/styles';
159+
const buildCssDir = path.join(buildDir, 'css');
160+
const files = [
161+
'tokens/color.css',
162+
'tokens/size.css',
163+
'tokens/typography.css',
164+
'themes/default.css',
165+
'generic/typography.css',
166+
'core.css',
167+
'welcome.css',
168+
'results.css'
169+
];
170+
171+
// Create build/css directory if it doesn't exist
172+
if (!fs.existsSync(buildCssDir)) {
173+
fs.mkdirSync(buildCssDir, { recursive: true });
174+
}
175+
176+
let bundledCss = '/* Core CSS Bundle for Carbon Visualizer Extension */\n';
177+
bundledCss += '/* This file combines all design system CSS files in the correct dependency order */\n\n';
178+
179+
files.forEach(file => {
180+
const filePath = path.join(srcCssDir, file);
181+
try {
182+
const content = fs.readFileSync(filePath, 'utf-8');
183+
bundledCss += `/* ===== ${file} ===== */\n`;
184+
bundledCss += content;
185+
bundledCss += '\n\n';
186+
} catch (error) {
187+
console.error(`⚠️ Warning: Could not read CSS file ${filePath}:`, error.message);
188+
}
189+
});
190+
191+
const bundleOutputPath = path.join(buildCssDir, 'core-bundle.css');
192+
fs.writeFileSync(bundleOutputPath, bundledCss);
193+
console.log(`✅ Bundled core CSS: ${bundleOutputPath}`);
194+
return bundleOutputPath;
195+
}
196+
140197
// Main build function
141198
async function build() {
142199
try {
143200
console.log('🚀 Building Carbon Visualizer extension for Chrome and Firefox...');
144-
201+
202+
const bundlePath = bundleCoreCss();
145203
createDirectories();
146-
copyCommonFiles();
204+
copyCommonFiles(bundlePath);
147205
copyManifests();
148206
patchFirefoxManifest();
149207

src/core/ExtensionManager.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,24 @@ class ExtensionManager {
5353
}
5454

5555
async loadCoreCSS() {
56-
if (document.getElementById('carbon-visualizer-core-css')) return;
56+
// Check if core CSS bundle is already loaded
57+
if (document.getElementById('carbon-visualizer-core-css-bundle')) {
58+
return;
59+
}
5760

5861
try {
59-
const cssUrl = this.browserAPI.runtime.getURL('src/styles/core.css');
62+
// Load the bundled core CSS file
63+
const cssUrl = this.browserAPI.runtime.getURL('src/styles/core-bundle.css');
6064
const response = await fetch(cssUrl);
6165
const css = await response.text();
6266

67+
// Create and append the style tag
6368
const style = document.createElement('style');
64-
style.id = 'carbon-visualizer-core-css';
69+
style.id = 'carbon-visualizer-core-css-bundle';
6570
style.textContent = css;
6671
document.head.appendChild(style);
6772
} catch (error) {
68-
console.error(error);
73+
console.error('Failed to load core CSS bundle:', error);
6974
}
7075
}
7176

src/core/Panel.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ class Panel {
1313
const configs = {
1414
welcome: {
1515
htmlFile: 'src/panels/welcome/welcome.html',
16-
cssFile: 'src/styles/styles.css',
1716
jsFile: 'src/panels/welcome/welcome.js',
1817
containerId: 'carbon-visualizer-welcome-panel',
1918
className: 'cv-panel--welcome'
2019
},
2120
results: {
2221
htmlFile: 'src/panels/results/results.html',
23-
cssFile: 'src/panels/results/results.css',
2422
jsFile: 'src/panels/results/results.js',
2523
containerId: 'carbon-visualizer-results-panel',
2624
className: 'cv-panel--results'
@@ -39,31 +37,13 @@ class Panel {
3937
// Reset visibility state
4038
this.isVisible = false;
4139

42-
// Load panel-specific CSS
43-
await this.loadPanelCSS();
44-
45-
// Load and inject HTML
40+
// Load and inject HTML (CSS is already bundled and loaded by ExtensionManager)
4641
await this.loadPanelHTML();
4742

4843
// Load panel-specific JavaScript
4944
await this.loadPanelJS();
5045
}
5146

52-
async loadPanelCSS() {
53-
try {
54-
const cssUrl = this.browserAPI.runtime.getURL(this.config.cssFile);
55-
const response = await fetch(cssUrl);
56-
const css = await response.text();
57-
58-
const style = document.createElement('style');
59-
style.id = `carbon-visualizer-${this.type}-css`;
60-
style.textContent = css;
61-
document.head.appendChild(style);
62-
} catch (error) {
63-
console.error(error);
64-
}
65-
}
66-
6747
async loadPanelHTML() {
6848
try {
6949
const htmlUrl = this.browserAPI.runtime.getURL(this.config.htmlFile);

src/styles/styles.css

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)