-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
TailwindConfigParser.ts
206 lines (181 loc) · 7.03 KB
/
TailwindConfigParser.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import _ from 'lodash';
import {defaultTailwindConfig} from '../lib/defaultTailwindConfig';
import {TTailwindCSSConfig, TConfigDarkMode, TConfigPlugins} from '../types/config';
import {TConfigTheme, TThemeItems} from '../types/config';
import {baseVariants} from './constants/baseVariants';
import {tailwindColors} from './constants/tailwindColors';
/* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-return */
/**
* Parses the tailwind config object
*/
export class TailwindConfigParser {
private readonly _mode: string | undefined;
private readonly _prefix: string;
private readonly _separator: string;
private readonly _darkMode: TConfigDarkMode;
private readonly _themeConfig: TConfigTheme;
private _evaluatedTheme: TConfigTheme | null;
private readonly _pluginsConfig: TConfigPlugins;
constructor(tailwindConfig: TTailwindCSSConfig, plugins: TConfigPlugins) {
this._mode = tailwindConfig?.mode;
this._prefix = _.isEmpty(tailwindConfig?.prefix) ? '' : (tailwindConfig.prefix as string);
this._darkMode = _.isEmpty(tailwindConfig?.darkMode)
? false
: (tailwindConfig.darkMode as TConfigDarkMode);
this._separator = _.isEmpty(tailwindConfig.separator)
? ':'
: (tailwindConfig.separator as string);
this._themeConfig = {...defaultTailwindConfig.theme, ...tailwindConfig.theme};
this._evaluatedTheme = null;
this._pluginsConfig = plugins;
}
/**
* Gets the config prefix value
*/
public getPrefix = (): string => this._prefix;
/**
* Gets the config dark mode value
*/
public getDarkMode = (): TConfigDarkMode => this._darkMode;
/**
* Gets the config separator value
*/
public getSeparator = (): string => this._separator;
/**
* Gets the config plugins value
*/
public getPlugins = (): TConfigPlugins | null => {
const {pluginTypography, pluginCustomForms} = this._pluginsConfig;
return pluginTypography || pluginCustomForms ? this._pluginsConfig : null;
};
/**
* Gets the config theme object
*/
public getTheme = (): TThemeItems => {
// Check whether config was evaluated before; if yes, return cached result instead of re-calc
if (this._evaluatedTheme) {
return this._evaluatedTheme;
}
/** Evaluate function closures inside theme config and get the evaluated theme object */
const evaluateTheme = (valueSourceTheme?: TThemeItems): TThemeItems => {
// Pick the theme config items except theme.extend
const coreTheme = _.omit(this._themeConfig, 'extend');
// Iterate over theme object items and run the evaluator on it
const valueEvaluator = new ThemeClosuresEvaluator(coreTheme);
for (const [key, value] of Object.entries(this._themeConfig)) {
let evaluatorResult = valueEvaluator.evaluate(value, valueSourceTheme);
// Need to make sure that extensions for specific properties are considered
// For example when 'width' is extended, which is originally based on spacing
if (valueSourceTheme && _.isObject(evaluatorResult)) {
const sourceValue = valueSourceTheme[key as keyof TThemeItems];
evaluatorResult = {...(_.isObject(sourceValue) ? sourceValue : {}), ...evaluatorResult};
}
coreTheme[key as keyof TThemeItems] = evaluatorResult;
}
// Return the result of evaluation
return coreTheme;
};
/** Evaluate function closures inside theme.extend config and get the evaluated theme.extend object */
const evaluateThemeExtend = (): Partial<TConfigTheme['extend']> => {
// Get the theme.extend property value
const themeExtend = this._themeConfig.extend;
// Only when theme.extend exists...
if (themeExtend) {
// Iterate over every item and evaluate closures in it
const valueEvaluator = new ThemeClosuresEvaluator(themeExtend);
for (const [key, value] of Object.entries(themeExtend))
themeExtend[key as keyof TThemeItems] = valueEvaluator.evaluate(value);
}
// Return the result of the evaluation
return themeExtend;
};
// Merge theme with extensions
const themeWithMergedExtend = _.merge(evaluateTheme(), evaluateThemeExtend());
// Evaluate the theme again, however taking the values from the merge result
this._evaluatedTheme = evaluateTheme(themeWithMergedExtend);
delete this._evaluatedTheme?.extend;
// Return the evaluated theme
return this._evaluatedTheme;
};
/**
* Get the pseudoclass variants based on config.
*/
public getVariants = (): string[] => {
const variants = baseVariants;
// get responsive variants
const [mediaBreakpoints] = this.getThemeProperty('screens');
if (this.getDarkMode() == 'media' || this.getDarkMode() == 'class')
mediaBreakpoints.push('dark');
mediaBreakpoints.map((breakpoint: string) => {
if (!variants.includes(breakpoint)) {
variants.push(breakpoint);
}
});
return variants;
};
/**
* Get the value (and key) of a supplied theme property.
* @param themeProperty The theme property name
*/
public getThemeProperty = (
themeProperty: keyof TThemeItems,
): [string[], Array<string | Record<string, string>>] => {
return [
Object.keys(this.getTheme()[themeProperty]),
Object.values(this.getTheme()[themeProperty]),
];
};
}
/**
* The class responsible for evaluating the closures inside the config.
*/
class ThemeClosuresEvaluator {
constructor(private themeConfig: Partial<TThemeItems>) {}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public evaluate(value: any, valueSourceTheme?: TThemeItems): any {
// If a value is a function...
if (_.isFunction(value)) {
// evaluate the value by running the evaluator methods in this class.
return value({
colors: tailwindColors,
theme: this.makeThemePathResolver(valueSourceTheme || this.themeConfig),
negative: ThemeClosuresEvaluator.negative.bind(this),
breakpoints: ThemeClosuresEvaluator.breakpoints.bind(this),
});
}
// Otherwise, return the value.
else {
return value;
}
}
/**
* Creates evaluator for `theme()` functions/closures in config file
*/
private makeThemePathResolver =
(theme: Partial<TThemeItems>) =>
(path: string): Record<string, unknown> => {
return _.get(theme, _.trim(path, `'"`)) as Record<string, Record<string, string> | string>;
};
/**
* Evaluate `negative()` functions/closures
*/
private static negative(item: Record<string, string>): Record<string, string> {
const itemCopy = {...item};
for (const [key] of Object.entries(itemCopy)) {
itemCopy['-' + key] = itemCopy[key];
delete itemCopy[key];
}
return itemCopy;
}
/**
* Evaluate `breakpoints()` functions/closures
*/
private static breakpoints(item: Record<string, string>): Record<string, string> {
const itemCopy = {...item};
for (const [key] of Object.entries(itemCopy)) {
itemCopy['screen-' + key] = itemCopy[key];
delete itemCopy[key];
}
return itemCopy;
}
}