-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
78 lines (70 loc) · 2.23 KB
/
build.js
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
const config = require("./config");
const StyleDictionary = require("style-dictionary");
const { fileHeader, createPropertyFormatter } = StyleDictionary.formatHelpers;
StyleDictionary.extend(config)
.registerFormat({
name: "css/json",
formatter({ dictionary, options }) {
const { outputReferences, selector = ":root" } = options;
const tokens = dictionary.allTokens.reduce((prev, token) => {
let { name } = token;
const queryMatch = /{media\.(\w+)\.value}/.exec(
token.original.attributes?.media
);
if (queryMatch) {
name = token.name.replace(RegExp(`-${queryMatch[1]}$`), "");
}
// TODO: respect outputReferences
prev[name] ??= [];
prev[name].push({
value: token.value,
selector,
media: token.attributes?.media,
});
return prev;
}, {});
return JSON.stringify(tokens, null, 2);
},
})
.registerFormat({
name: "css/variables-with-media-queries",
formatter({ dictionary, file, options }) {
const { outputReferences, selector = ":root" } = options;
const propertyFormatter = createPropertyFormatter({
outputReferences,
dictionary,
format: "css",
formatting: {},
themeable: false,
});
const groupedTokens = dictionary.allTokens.reduce((prev, t) => {
const token = { ...t };
const breakpoint = token.attributes?.media ?? "_";
prev[breakpoint] ??= [];
const queryMatch = /{media\.(\w+)\.value}/.exec(
token.original.attributes?.media
);
if (queryMatch) {
token.name = token.name.replace(RegExp(`-${queryMatch[1]}$`), "");
}
prev[breakpoint].push(token);
return prev;
}, {});
const tokens = (bp) =>
`${selector} {\n${groupedTokens[bp]
.map(propertyFormatter)
.filter(Boolean)
.join("\n")}\n}`;
return (
fileHeader({ file }) +
Object.keys(groupedTokens)
.map((query) =>
query === "_"
? tokens(query) + "\n"
: `@media ${query} {\n${tokens(query)}\n}`
)
.join("")
);
},
})
.buildAllPlatforms();