-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
51 lines (43 loc) · 1.43 KB
/
build.ts
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
const fs = require("fs").promises;
const removeCommon = (text: string) => {
const regex = /:root[\s\S]*?@-webkit-keyframes/;
return text.replace(regex, "@-webkit-keyframes");
};
const removeWebkitKeyframes = (text: string) => {
const regex =
/@-webkit-keyframes\s+[\w-]+\s*\{[^{}]*?(?:\{[^{}]*?\}[^{}]*?)*\}/g;
return text.replace(regex, "");
};
const removeCSSClass = (text: string) => {
const regex = /\.[\w-]+(?:\.[\w-]+)*\s*\{[^{}]*?(?:\{[^{}]*?\}[^{}]*?)*\}/g;
return text.replace(regex, "");
};
const convertCSSKeyframesToJS = (text: string) => {
const regex = /@keyframes\s+(\w+)\s*\{([^{}]*?(?:\{[^{}]*?\}[^{}]*?)*)\}/g;
let result;
let output = "";
while ((result = regex.exec(text)) !== null) {
const name = result[1];
const keyframes = result[2].trim();
output += `export const ${name} = keyframes\`\n${keyframes}\n\`;\n\n`;
}
return output;
};
const processCSSFile = async () => {
try {
const string = await fs.readFile("animate.css", "utf8");
const cleanedContent = convertCSSKeyframesToJS(
removeCSSClass(removeWebkitKeyframes(removeCommon(string)))
);
await fs.writeFile(
"index.ts",
"import { keyframes } from 'styled-components';\n\n" + cleanedContent
);
console.log(
"CSS keyframes have been converted to JavaScript and saved as keyframes.js"
);
} catch (error) {
console.error("Error processing file:", error);
}
};
processCSSFile();