-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcss-builder.js
More file actions
271 lines (223 loc) · 8.96 KB
/
css-builder.js
File metadata and controls
271 lines (223 loc) · 8.96 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env node
/**
* CSS Builder Script for EmbedPress
*
* This script compiles SCSS files to CSS without going through Vite
* to avoid the JavaScript module processing issues.
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import sass from 'sass';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
class CSSBuilder {
constructor() {
this.srcDir = path.join(__dirname, '../src');
this.assetsDir = path.join(__dirname, '../assets');
this.cssOutputDir = path.join(this.assetsDir, 'css');
this.watchMode = process.argv.includes('--watch');
this.outputFiles = new Map(); // Track files to merge by output name
}
/**
* Build all CSS files
*/
async buildAll() {
console.log('🎨 Building CSS files...');
// Ensure output directory exists
fs.mkdirSync(this.cssOutputDir, { recursive: true });
// Clear the output files map for fresh build
this.outputFiles.clear();
// Queue files for building (same output names will be merged)
this.queueFile('src/Shared/styles/admin.scss', 'admin.build.css');
// this.queueFile('src/Blocks/EmbedPress/src/style.scss', 'blocks.style.build.css');
// this.queueFile('src/Blocks/EmbedPress/src/editor.scss', 'blocks.editor.build.css');
// this.queueFile('src/Blocks/embedpress-pdf/src/style.scss', 'blocks.style.build.css');
// this.queueFile('src/Blocks/embedpress-pdf/src/editor.scss', 'blocks.editor.build.css');
// this.queueFile('src/Blocks/document/style.scss', 'blocks.style.build.css');
// this.queueFile('src/Blocks/document/editor.scss', 'blocks.editor.build.css');
// Build all queued files
await this.buildQueuedFiles();
// Build component styles
await this.buildComponentStyles();
console.log('✅ CSS build completed!');
}
/**
* Queue a file for building (allows merging files with same output name)
*/
queueFile(inputPath, outputName) {
if (!this.outputFiles.has(outputName)) {
this.outputFiles.set(outputName, []);
}
this.outputFiles.get(outputName).push(inputPath);
}
/**
* Build all queued files, merging those with same output names
*/
async buildQueuedFiles() {
for (const [outputName, inputPaths] of this.outputFiles.entries()) {
if (inputPaths.length === 1) {
// Single file, build normally
await this.buildFile(inputPaths[0], outputName);
} else {
// Multiple files, merge them
await this.buildMergedFile(inputPaths, outputName);
}
}
}
/**
* Build a single SCSS file
*/
async buildFile(inputPath, outputName) {
const fullInputPath = path.join(__dirname, '..', inputPath);
const outputPath = path.join(this.cssOutputDir, outputName);
try {
if (!fs.existsSync(fullInputPath)) {
console.warn(`⚠️ SCSS file not found: ${inputPath}`);
return;
}
console.log(`📝 Building: ${inputPath} → ${outputName}`);
const result = sass.compile(fullInputPath, {
style: 'expanded',
sourceMap: true,
loadPaths: [
path.join(__dirname, '../src'),
path.join(__dirname, '../src/Shared/styles'),
path.join(__dirname, '../node_modules')
]
});
// Write CSS file
fs.writeFileSync(outputPath, result.css);
// Write source map
if (result.sourceMap) {
fs.writeFileSync(`${outputPath}.map`, JSON.stringify(result.sourceMap));
}
console.log(`✅ Built: ${outputName} (${(result.css.length / 1024).toFixed(2)} KB)`);
} catch (error) {
console.error(`❌ Error building ${inputPath}:`, error.message);
}
}
/**
* Build and merge multiple SCSS files into a single output file
*/
async buildMergedFile(inputPaths, outputName) {
const outputPath = path.join(this.cssOutputDir, outputName);
console.log(`📝 Merging ${inputPaths.length} files → ${outputName}`);
let mergedCSS = `/**
* EmbedPress Merged CSS: ${outputName}
* Generated on: ${new Date().toISOString()}
* Source files: ${inputPaths.join(', ')}
*/\n\n`;
let totalSize = 0;
let successfulBuilds = 0;
for (const inputPath of inputPaths) {
const fullInputPath = path.join(__dirname, '..', inputPath);
try {
if (!fs.existsSync(fullInputPath)) {
console.warn(`⚠️ SCSS file not found: ${inputPath}`);
continue;
}
console.log(` 📄 Processing: ${inputPath}`);
const result = sass.compile(fullInputPath, {
style: 'expanded',
sourceMap: false, // Disable source maps for merged files
loadPaths: [
path.join(__dirname, '../src'),
path.join(__dirname, '../src/Shared/styles'),
path.join(__dirname, '../node_modules')
]
});
mergedCSS += `/* === ${path.basename(inputPath)} === */\n${result.css}\n\n`;
totalSize += result.css.length;
successfulBuilds++;
} catch (error) {
console.error(`❌ Error building ${inputPath}:`, error.message);
}
}
if (successfulBuilds > 0) {
// Write merged CSS file
fs.writeFileSync(outputPath, mergedCSS);
console.log(`✅ Merged: ${outputName} (${successfulBuilds}/${inputPaths.length} files, ${(totalSize / 1024).toFixed(2)} KB)`);
} else {
console.error(`❌ Failed to merge any files for ${outputName}`);
}
}
/**
* Build component styles
*/
async buildComponentStyles() {
const componentStyles = [
'src/Shared/components/UI/Button/Button.scss',
'src/Shared/components/UI/Input/Input.scss',
'src/Shared/components/UI/Toggle/Toggle.scss'
];
// Create a combined components CSS file
let combinedCSS = `/**
* EmbedPress Component Styles
* Generated on: ${new Date().toISOString()}
*/\n\n`;
for (const stylePath of componentStyles) {
const fullPath = path.join(__dirname, '..', stylePath);
if (fs.existsSync(fullPath)) {
try {
const result = sass.compile(fullPath, {
style: 'expanded',
loadPaths: [
path.join(__dirname, '../src'),
path.join(__dirname, '../src/Shared/styles')
]
});
combinedCSS += `/* === ${path.basename(stylePath)} === */\n${result.css}\n\n`;
} catch (error) {
console.warn(`⚠️ Error compiling ${stylePath}:`, error.message);
}
}
}
const outputPath = path.join(this.cssOutputDir, 'components.build.css');
fs.writeFileSync(outputPath, combinedCSS);
console.log(`✅ Built: components.build.css (${(combinedCSS.length / 1024).toFixed(2)} KB)`);
}
/**
* Watch for changes and rebuild
*/
watch() {
console.log('👀 Watching for CSS changes...');
const watchPaths = [
path.join(this.srcDir, 'Shared/styles'),
path.join(this.srcDir, 'Blocks'),
path.join(this.srcDir, 'Shared/components')
];
watchPaths.forEach(watchPath => {
if (fs.existsSync(watchPath)) {
fs.watch(watchPath, { recursive: true }, (_, filename) => {
if (filename && (filename.endsWith('.scss') || filename.endsWith('.css'))) {
console.log(`🔄 File changed: ${filename}`);
this.buildAll();
}
});
}
});
}
/**
* Run the CSS builder
*/
async run() {
console.log('🚀 Starting CSS Builder...');
await this.buildAll();
if (this.watchMode) {
this.watch();
// Keep the process running
process.on('SIGINT', () => {
console.log('\n👋 CSS Builder stopped');
process.exit(0);
});
console.log('👀 Watching for changes... (Press Ctrl+C to stop)');
}
}
}
// Run the CSS builder
const cssBuilder = new CSSBuilder();
cssBuilder.run().catch(error => {
console.error('❌ CSS Builder failed:', error);
process.exit(1);
});