-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-css.js
More file actions
89 lines (73 loc) · 3.12 KB
/
Copy pathbuild-css.js
File metadata and controls
89 lines (73 loc) · 3.12 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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get current directory for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Read all CSS files from src/styles and concatenate them
const stylesDir = path.join(__dirname, 'src', 'styles');
const distDir = path.join(__dirname, 'dist');
// Ensure dist directory exists
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir, { recursive: true });
}
// Function to resolve @import statements recursively
function resolveImports(cssContent, currentDir) {
const importRegex = /@import\s+['"](\.\/[^'"]+)['"];/g;
return cssContent.replace(importRegex, (match, importPath) => {
try {
const resolvedPath = path.resolve(currentDir, importPath);
if (fs.existsSync(resolvedPath)) {
const importedContent = fs.readFileSync(resolvedPath, 'utf8');
// Add a comment to show where this content came from
const comment = `\n/* === ${path.basename(resolvedPath)} === */\n`;
// Recursively resolve imports in the imported file
return comment + resolveImports(importedContent, path.dirname(resolvedPath)) + '\n';
} else {
console.warn(`Warning: Could not resolve import: ${importPath}`);
return `\n/* Could not resolve import: ${importPath} */\n`;
}
} catch (error) {
console.warn(`Warning: Error processing import ${importPath}:`, error.message);
return `\n/* Error processing import: ${importPath} */\n`;
}
});
}
// Read main CSS file and resolve all imports
const mainCSSPath = path.join(stylesDir, 'tablix.css');
let mainCSS = fs.readFileSync(mainCSSPath, 'utf8');
// Resolve all @import statements
const resolvedCSS = resolveImports(mainCSS, stylesDir);
// Create combined CSS with header
const combinedCSS = `/**
* TablixJS CSS Bundle
* Complete styles for TablixJS table library
* All @import statements have been resolved and inlined
*/
${resolvedCSS}`;
// Write combined CSS to dist
fs.writeFileSync(path.join(distDir, 'tablixjs.css'), combinedCSS);
// Read theme CSS files and copy them individually
const themesDir = path.join(stylesDir, 'themes');
if (fs.existsSync(themesDir)) {
const themeFiles = fs.readdirSync(themesDir).filter(file => file.endsWith('.css'));
themeFiles.forEach(themeFile => {
const themeCSS = fs.readFileSync(path.join(themesDir, themeFile), 'utf8');
const themeName = path.basename(themeFile, '.css');
const themeWithHeader = `/**
* TablixJS ${themeName.charAt(0).toUpperCase() + themeName.slice(1)} Theme
*/
${themeCSS}`;
fs.writeFileSync(path.join(distDir, `tablixjs-theme-${themeName}.css`), themeWithHeader);
});
console.log('CSS files built successfully:');
console.log('- tablixjs.css (main styles with inlined imports)');
themeFiles.forEach(themeFile => {
const themeName = path.basename(themeFile, '.css');
console.log(`- tablixjs-theme-${themeName}.css`);
});
} else {
console.log('CSS files built successfully:');
console.log('- tablixjs.css (main styles with inlined imports)');
console.warn('Warning: themes directory not found');
}