Repository navigation
Expand file tree
/
Copy pathbuild-css.mjs
More file actions
111 lines (93 loc) · 2.85 KB
/
Copy pathbuild-css.mjs
File metadata and controls
111 lines (93 loc) · 2.85 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
import { spawnSync } from "node:child_process";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import browserslist from "browserslist";
import { watch } from "chokidar";
import { browserslistToTargets, bundleAsync } from "lightningcss";
const repository = resolve(fileURLToPath(new URL("..", import.meta.url)));
const watchMode = process.argv.includes("--watch");
const targets = browserslistToTargets(browserslist());
const terrazzoCli = resolve(
repository,
"node_modules/@terrazzo/cli/bin/cli.js",
);
await build();
if (watchMode) {
const watcher = watch(
["src/**/*.css", "tokens/**/*.json", "terrazzo.config.js"],
{
cwd: repository,
ignoreInitial: true,
ignored: ["src/styles/generated/tokens.css"],
},
);
let queued = false;
watcher.on("all", (_event, filename) => {
if (queued) return;
queued = true;
setTimeout(async () => {
queued = false;
try {
await build();
} catch (error) {
console.error(error);
}
}, 50);
});
console.log("Watching CSS and customization token sources…");
}
async function build() {
buildTokens();
const angularCss = await compile("src/index.css", !watchMode);
const preflightCss = await compile("src/preflight.css", !watchMode);
write("dist/angular.css", angularCss);
write("docs/static/css/angular.css", angularCss);
write("docs/static/css/preflight.css", preflightCss);
const tailwindMarkers = angularCss.match(
/(?:@apply|@theme|@custom-variant|--tw-)/g,
);
if (tailwindMarkers) {
throw new Error(
`Compiled AngularCSS contains Tailwind implementation markers: ${[
...new Set(tailwindMarkers),
].join(", ")}`,
);
}
console.log(
`Built AngularCSS (${angularCss.length} bytes) and documentation preflight (${preflightCss.length} bytes).`,
);
}
function buildTokens() {
const result = spawnSync(
process.execPath,
[terrazzoCli, "build", "--config", "terrazzo.config.js", "--silent"],
{ cwd: repository, encoding: "utf8" },
);
if (result.status !== 0) {
process.stderr.write(result.stdout);
process.stderr.write(result.stderr);
throw new Error(`Terrazzo exited with status ${result.status}`);
}
}
async function compile(filename, minify) {
const result = await bundleAsync({
filename: resolve(repository, filename),
minify,
sourceMap: false,
targets,
});
for (const warning of result.warnings) console.warn(warning.message);
return result.code.toString();
}
function write(filename, contents) {
const output = resolve(repository, filename);
mkdirSync(dirname(output), { recursive: true });
if (
readFileSync(output, { encoding: "utf8", flag: "a+" }) ===
contents.toString()
) {
return;
}
writeFileSync(output, contents);
}