-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathbuild-elements-styles.mjs
95 lines (78 loc) · 2.52 KB
/
build-elements-styles.mjs
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
import * as sass from "sass-embedded";
import postcss from "postcss";
import autoprefixer from "autoprefixer";
import { globby } from "globby";
import path from "path";
import { mkdirSync as makeDir } from "fs";
import { fileURLToPath } from "url";
import { writeFile } from "fs/promises";
const report = {
success: (s) => console.log("\x1b[32m%s\x1b[0m", s),
warn: (s) => console.warn("\x1b[33m%s\x1b[0m", s),
error: (s) => console.error("\x1b[31m%s\x1b[0m", s),
};
const STYLES = {
SRC: "projects/igniteui-angular-elements/src/themes",
DIST: "../dist/igniteui-angular-elements/browser/themes",
CONFIG: {
style: "compressed",
loadPaths: ["node_modules"],
sourceMap: false,
sourceMapEmbed: false,
},
};
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DEST_DIR = path.join.bind(null, path.resolve(__dirname, STYLES.DIST));
const stripComments = () => {
return {
postcssPlugin: "postcss-strip-comments",
OnceExit(root) {
root.walkComments((node) => node.remove());
},
};
};
stripComments.postcss = true;
const postProcessor = postcss([
autoprefixer({
cascade: false,
grid: true,
}),
stripComments,
]);
async function createFile(fileName, content) {
const outputFile = DEST_DIR(fileName);
makeDir(path.dirname(outputFile), { recursive: true });
await writeFile(outputFile, content, "utf-8");
}
async function buildThemes() {
const paths = await globby(`${STYLES.SRC}/**/*.scss`);
const compiler = await sass.initAsyncCompiler();
try {
await Promise.all(
paths.map(async (path) => {
const result = await compiler.compileAsync(path, STYLES.CONFIG);
const fileName = path.replace(/\.scss$/, ".css").replace(STYLES.SRC, "");
let outCss = postProcessor.process(result.css).css;
if (outCss.charCodeAt(0) === 0xfeff) {
outCss = outCss.substring(1);
}
outCss = outCss + "\n";
await createFile(fileName, outCss);
}),
);
} catch (err) {
await compiler.dispose();
report.error(err);
process.exit(1);
}
await compiler.dispose();
}
(async () => {
const startTime = new Date();
// Build theme presets
console.info("Building Elements themes...");
await buildThemes();
report.success(
`Themes generated in ${Math.round((Date.now() - startTime) / 1000)}s`,
);
})();