-
Notifications
You must be signed in to change notification settings - Fork 551
Expand file tree
/
Copy pathcp-css.js
More file actions
122 lines (105 loc) · 4.31 KB
/
Copy pathcp-css.js
File metadata and controls
122 lines (105 loc) · 4.31 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
import fs from "fs";
import { camelCase } from "lodash-es";
import path from "path";
import { fileURLToPath } from "url";
import { mirrorStylesWithLayer, stripBom, writeLayeredCopy } from "./css-layer-utils.mjs";
const dirname = path.dirname(fileURLToPath(import.meta.url));
// Create directories if they don't exist
function ensureDirectoryExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
// Replace .scss imports with .css imports in compiled JS files
function fixScssImportsInJs(dir) {
const entries = fs.readdirSync(dir);
entries.forEach((entry) => {
const fullPath = path.join(dir, entry);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
fixScssImportsInJs(fullPath);
} else if (entry.endsWith(".js")) {
const content = fs.readFileSync(fullPath, "utf8");
const fixed = content.replace(/(['"])([^'"]*\.scss)\1/g, (match, quote, p) => {
return `${quote}${p.replace(/\.scss$/, ".css")}${quote}`;
});
if (fixed !== content) {
fs.writeFileSync(fullPath, fixed, "utf8");
}
}
});
}
// Strip Sass's leading UTF-8 BOM (compressed-mode output for non-ASCII) from
// every *.css under dir, in place — so the unlayered default exports
// (./components.css, ./styles/*) ship BOM-free. The layered mirror strips it
// separately via wrapInLayer.
function stripBomFromCssInDir(dir) {
for (const entry of fs.readdirSync(dir)) {
const full = path.join(dir, entry);
if (fs.statSync(full).isDirectory()) {
stripBomFromCssInDir(full);
} else if (entry.endsWith(".css")) {
const content = fs.readFileSync(full, "utf8");
const stripped = stripBom(content);
if (stripped !== content) fs.writeFileSync(full, stripped, "utf8");
}
}
}
// Copy CSS files from src to dist
function copyCssFiles() {
const srcDir = path.join(dirname, "dist", "components");
const distDir = path.join(dirname, "dist", "styles");
// Ensure the dist/styles directory exists
ensureDirectoryExists(distDir);
// Strip Sass's leading BOM from the unlayered sass output before copying, so
// the default exports (./components.css, ./styles/*) ship BOM-free.
stripBomFromCssInDir(srcDir);
const defaultsCssSrc = path.join(dirname, "dist", "openui-defaults.css");
if (fs.existsSync(defaultsCssSrc)) {
const content = fs.readFileSync(defaultsCssSrc, "utf8");
const stripped = stripBom(content);
if (stripped !== content) fs.writeFileSync(defaultsCssSrc, stripped, "utf8");
}
// Read all component directories
const components = fs.readdirSync(srcDir);
components.forEach((component) => {
const componentSrcPath = path.join(srcDir, component);
const componentStylesheetName = `${camelCase(component)}.css`;
// Skip if not a directory
if (!fs.statSync(componentSrcPath).isDirectory()) {
return;
}
const stylePath = path.join(componentSrcPath, componentStylesheetName);
const distFile = path.join(distDir, componentStylesheetName);
if (fs.existsSync(stylePath)) {
fs.copyFileSync(stylePath, distFile);
} else {
console.warn(`No stylesheet found for ${component}`);
}
});
const indexCSSContent = fs.readFileSync(path.join(srcDir, "index.css"), "utf8");
fs.writeFileSync(path.join(distDir, "index.css"), indexCSSContent);
const cssUtilsSrc = fs.readFileSync(path.join(dirname, "src", "cssUtils.scss"), "utf8");
fs.writeFileSync(path.join(distDir, "cssUtils.scss"), cssUtilsSrc);
const defaultsCssPath = path.join(dirname, "dist", "openui-defaults.css");
if (fs.existsSync(defaultsCssPath)) {
fs.copyFileSync(defaultsCssPath, path.join(distDir, "openui-defaults.css"));
}
// Emit the opt-in layered mirror (./layered-components.css and
// ./layered/styles/*). The default exports above stay unlayered — see
// README "Styling integration".
writeLayeredCopy(
path.join(srcDir, "index.css"),
path.join(dirname, "dist", "layered", "components", "index.css"),
);
mirrorStylesWithLayer(distDir, path.join(dirname, "dist", "layered", "styles"));
// Fix .scss imports in compiled JS to point to .css files instead
fixScssImportsInJs(path.join(dirname, "dist"));
}
try {
copyCssFiles();
console.log("CSS files copied successfully!");
} catch (error) {
console.error("Error copying CSS files:", error);
process.exit(1);
}