-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathtailwindConfigUtils.js
80 lines (67 loc) · 2.49 KB
/
tailwindConfigUtils.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
79
80
const path = require('path')
/**
* Force loading tailwindcss module installed in current working directory.
* This is required for running the config viewer directly with npx without
* locally installing it
*/
const tailwindPackageJsonPath = require.resolve('tailwindcss/package.json', { paths: [__dirname, process.cwd()] })
const tailwindVersion = require(tailwindPackageJsonPath).version
const tailwindResolveConfigPath = require.resolve('tailwindcss/resolveConfig', { paths: [__dirname, process.cwd()] })
const resolveTailwindConfig = require(tailwindResolveConfigPath)
const flattenColorPalettePath = require.resolve('tailwindcss/lib/util/flattenColorPalette', { paths: [__dirname, process.cwd()] })
const flattenColorPalette = require(flattenColorPalettePath).default
const resolveConfigPath = configPath => path.resolve(process.cwd(), configPath)
const resolveConfig = config => {
return transformConfig(resolveTailwindConfig(config))
}
const transformConfig = config => {
config.tailwindVersion = tailwindVersion
config.theme = replaceWithOverrides(config.theme)
config.theme.colors = flattenColorPalette(config.theme.colors)
config.theme.backgroundColor = flattenColorPalette(config.theme.backgroundColor)
config.theme.textColor = flattenColorPalette(config.theme.textColor)
config.theme.borderColor = flattenColorPalette(config.theme.borderColor)
removeDisabledCorePlugins(config)
removeConfigProps(config, [
'variants',
'purge',
'plugins',
'corePlugins',
'target'
])
return config
}
const replaceWithOverrides = (theme) => {
if (theme.configViewer && theme.configViewer.themeReplacements) {
Object.keys(theme.configViewer.themeReplacements).forEach(key => {
theme = findAndReplaceRecursively(theme, key, theme.configViewer.themeReplacements[key])
})
}
return theme
}
function findAndReplaceRecursively (target, find, replaceWith) {
if (typeof target !== 'object') {
if (target === find) return replaceWith
return target
}
return Object.keys(target)
.reduce((carry, key) => {
const val = target[key]
carry[key] = findAndReplaceRecursively(val, find, replaceWith)
return carry
}, {})
}
const removeConfigProps = (config, props) => {
props.forEach(prop => delete config[prop])
}
const removeDisabledCorePlugins = (config) => {
Object.keys(config.corePlugins).forEach(key => {
if (config.corePlugins[key] === false) {
delete config.theme[key]
}
})
}
module.exports = {
resolveConfig,
resolveConfigPath,
}