-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuild-styles.js
More file actions
137 lines (118 loc) · 4.75 KB
/
Copy pathbuild-styles.js
File metadata and controls
137 lines (118 loc) · 4.75 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
const fs = require('fs');
const path = require('path');
const sass = require('sass');
const postcss = require('postcss');
const autoprefixer = require('autoprefixer');
const ROOT = path.resolve(__dirname, '..');
const COMPONENTS = path.join(ROOT, 'src');
const ES_DIR = path.join(ROOT, 'es');
const LIB_DIR = path.join(ROOT, 'lib');
const NODE_MODULES = path.join(ROOT, 'node_modules');
function mkdirp(dir) {
fs.mkdirSync(dir, { recursive: true });
}
async function processWithPostcss(css) {
const result = await postcss([autoprefixer]).process(css, { from: undefined });
return result.css;
}
// 1. Base CSS: copy the runtime theme bundle from @tiny-design/tokens.
// Also copies the per-tier (foundation/semantic) and per-component slice files
// plus the slice manifest so inject-style-imports.js can wire components to slices.
function copyBaseCss() {
const baseSrc = require.resolve('@tiny-design/tokens/dist/css/base.css');
const tokensCssDir = path.dirname(baseSrc);
const foundationSrc = path.join(tokensCssDir, 'foundation.css');
const semanticSrc = path.join(tokensCssDir, 'semantic.css');
const componentsSrcDir = path.join(tokensCssDir, 'components');
const manifestSrc = path.join(tokensCssDir, 'component-deps.json');
for (const dir of [ES_DIR, LIB_DIR]) {
const outDir = path.join(dir, 'style');
const componentsOutDir = path.join(outDir, 'components');
mkdirp(outDir);
mkdirp(componentsOutDir);
fs.copyFileSync(baseSrc, path.join(outDir, 'base.css'));
fs.copyFileSync(foundationSrc, path.join(outDir, 'foundation.css'));
fs.copyFileSync(semanticSrc, path.join(outDir, 'semantic.css'));
fs.copyFileSync(manifestSrc, path.join(outDir, 'component-deps.json'));
for (const file of fs.readdirSync(componentsSrcDir)) {
if (!file.endsWith('.css')) continue;
fs.copyFileSync(path.join(componentsSrcDir, file), path.join(componentsOutDir, file));
}
}
const sliceCount = fs.readdirSync(componentsSrcDir).filter((f) => f.endsWith('.css')).length;
console.log(
` es/style/{base,foundation,semantic}.css + lib/style/{base,foundation,semantic}.css copied`
);
console.log(` es/style/components/*.css + lib/style/components/*.css (${sliceCount} slices)`);
}
// 2. Per-component CSS: compile each component's style/index.scss entry
async function buildComponentCss() {
const entries = fs.readdirSync(COMPONENTS, { withFileTypes: true });
let count = 0;
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (entry.name === 'style' || entry.name.startsWith('_')) continue;
const styleDir = path.join(COMPONENTS, entry.name, 'style');
const directPath = path.join(styleDir, 'index.scss');
if (!fs.existsSync(directPath)) {
continue;
}
const result = sass.compile(directPath, {
loadPaths: [COMPONENTS, NODE_MODULES],
});
const css = await processWithPostcss(result.css);
for (const dir of [ES_DIR, LIB_DIR]) {
const outDir = path.join(dir, entry.name, 'style');
mkdirp(outDir);
fs.writeFileSync(path.join(outDir, 'index.css'), css);
}
count++;
}
console.log(` ${count} component CSS files compiled`);
}
// 3. Copy SCSS sources to es/ and lib/
function copyScss() {
// Copy components/style/** to es/style/ and lib/style/
copyDirRecursive(path.join(COMPONENTS, 'style'), path.join(ES_DIR, 'style'));
copyDirRecursive(path.join(COMPONENTS, 'style'), path.join(LIB_DIR, 'style'));
// Copy per-component style/*.scss to es/ and lib/
const entries = fs.readdirSync(COMPONENTS, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (entry.name === 'style' || entry.name.startsWith('_')) continue;
const styleDir = path.join(COMPONENTS, entry.name, 'style');
if (!fs.existsSync(styleDir)) continue;
const scssFiles = fs.readdirSync(styleDir).filter((f) => f.endsWith('.scss'));
for (const file of scssFiles) {
for (const dir of [ES_DIR, LIB_DIR]) {
const outDir = path.join(dir, entry.name, 'style');
mkdirp(outDir);
fs.copyFileSync(path.join(styleDir, file), path.join(outDir, file));
}
}
}
console.log(' SCSS sources copied to es/ and lib/');
}
function copyDirRecursive(src, dest) {
mkdirp(dest);
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirRecursive(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
async function main() {
console.log('Building styles...\n');
copyBaseCss();
await buildComponentCss();
copyScss();
console.log('\nStyles done.');
}
main().catch((err) => {
console.error(err);
process.exit(1);
});