|
1 | | -import process from 'node:process' |
2 | 1 | import type { Plugin, ResolvedConfig } from 'vite' |
| 2 | +import type { Context } from './context' |
| 3 | +import process from 'node:process' |
3 | 4 | import fse from 'fs-extra' |
4 | | -import YAML from 'yaml' |
5 | 5 | import resolveConfig from 'tailwindcss/resolveConfig.js' |
6 | | -import type { Context } from './context' |
| 6 | +import YAML from 'yaml' |
7 | 7 |
|
8 | 8 | export default (ctx: Context): Plugin => { |
9 | 9 | let config: ResolvedConfig |
@@ -43,34 +43,35 @@ async function buildTailwindConfig() { |
43 | 43 | const tailwindConfigFile = await import(`${process.cwd()}/tailwind.config.js`) |
44 | 44 | const tailwindConfig = resolveConfig(tailwindConfigFile.default) |
45 | 45 |
|
46 | | - const resolveThemeValue = (key: any) => (key in tailwindConfig.theme ? tailwindConfig.theme[key] : undefined); |
| 46 | + const resolveThemeValue = (key: any) => (key in tailwindConfig.theme ? tailwindConfig.theme[key] : undefined) |
47 | 47 | const evaluateAndSerializeConfig = (obj: any): any => { |
48 | 48 | if (typeof obj === 'function') { |
49 | 49 | try { |
50 | 50 | // Try evaluating the function with the theme resolver context |
51 | | - return obj({ theme: resolveThemeValue }); |
52 | | - } catch (error) { |
53 | | - console.error(`Error resolving function: ${error}`); |
54 | | - return '[Unresolvable Function]'; // Fallback if evaluation fails |
| 51 | + return obj({ theme: resolveThemeValue }) |
| 52 | + } |
| 53 | + catch (error) { |
| 54 | + console.error(`Error resolving function: ${error}`) |
| 55 | + return '[Unresolvable Function]' // Fallback if evaluation fails |
55 | 56 | } |
56 | 57 | } |
57 | 58 |
|
58 | 59 | if (Array.isArray(obj)) { |
59 | | - return obj.map(evaluateAndSerializeConfig); // Recursively handle arrays |
| 60 | + return obj.map(evaluateAndSerializeConfig) // Recursively handle arrays |
60 | 61 | } |
61 | 62 |
|
62 | 63 | if (typeof obj === 'object' && obj !== null) { |
63 | 64 | return Object.keys(obj).reduce((acc: Record<string, any>, key) => { |
64 | | - acc[key] = evaluateAndSerializeConfig(obj[key]); // Recursively handle objects |
65 | | - return acc; |
66 | | - }, {}); |
| 65 | + acc[key] = evaluateAndSerializeConfig(obj[key]) // Recursively handle objects |
| 66 | + return acc |
| 67 | + }, {}) |
67 | 68 | } |
68 | 69 |
|
69 | | - return obj; // Return the value if it's not a function or an object |
| 70 | + return obj // Return the value if it's not a function or an object |
70 | 71 | } |
71 | 72 |
|
72 | 73 | // Evaluate and serialize the resolved config |
73 | | - const evaluatedConfig = evaluateAndSerializeConfig(tailwindConfig.theme); |
| 74 | + const evaluatedConfig = evaluateAndSerializeConfig(tailwindConfig.theme) |
74 | 75 |
|
75 | 76 | return YAML.stringify(evaluatedConfig) |
76 | 77 | } |
|
0 commit comments