forked from zhuanggenhua/BoardGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostcss-tailwind-legacy-translate.js
More file actions
62 lines (49 loc) · 1.94 KB
/
Copy pathpostcss-tailwind-legacy-translate.js
File metadata and controls
62 lines (49 loc) · 1.94 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
const LEGACY_TRANSLATE_SUPPORT_QUERY = 'not (translate: 0)';
const splitTranslateTokens = (value) =>
value
.replace(/\)(?=(var|calc|min|max|clamp)\()/g, ') ')
.match(/var\([^)]*\)|calc\([^)]*\)|min\([^)]*\)|max\([^)]*\)|clamp\([^)]*\)|[^\s]+/g)
?? [];
const buildTransformTranslateValue = (value) => {
const tokens = splitTranslateTokens(value.trim()).filter(Boolean);
if (tokens.length === 0 || tokens[0] === 'none') {
return null;
}
if (tokens.length === 1) {
return `translate(${tokens[0]})`;
}
return `translate(${tokens[0]}, ${tokens[1]})`;
};
const tailwindLegacyTranslateFallback = () => ({
postcssPlugin: 'tailwind-legacy-translate-fallback',
OnceExit(root, { AtRule }) {
root.walkRules((rule) => {
if (rule.parent?.type === 'atrule' && rule.parent.name === 'supports' && rule.parent.params === LEGACY_TRANSLATE_SUPPORT_QUERY) {
return;
}
const translateDecl = rule.nodes?.find(
(node) => node.type === 'decl' && node.prop === 'translate',
);
if (!translateDecl || rule.nodes?.some((node) => node.type === 'decl' && node.prop === 'transform')) {
return;
}
const fallbackTransformValue = buildTransformTranslateValue(translateDecl.value);
if (!fallbackTransformValue) {
return;
}
const fallbackRule = rule.clone();
fallbackRule.append({
prop: 'transform',
value: fallbackTransformValue,
});
const supportsRule = new AtRule({
name: 'supports',
params: LEGACY_TRANSLATE_SUPPORT_QUERY,
});
supportsRule.append(fallbackRule);
rule.after(supportsRule);
});
},
});
tailwindLegacyTranslateFallback.postcss = true;
export default tailwindLegacyTranslateFallback;