forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (86 loc) · 3.2 KB
/
index.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
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
import path from 'path'
import fs from 'fs'
import _ from 'lodash'
import postcss from 'postcss'
import getModuleDependencies from './lib/getModuleDependencies'
import registerConfigAsDependency from './lib/registerConfigAsDependency'
import processTailwindFeatures from './processTailwindFeatures'
import formatCSS from './lib/formatCSS'
import resolveConfig from './util/resolveConfig'
import { defaultConfigFile } from './constants'
import defaultConfig from '../stubs/defaultConfig.stub.js'
import { flagEnabled } from './featureFlags'
import uniformColorPalette from './flagged/uniformColorPalette.js'
import extendedSpacingScale from './flagged/extendedSpacingScale.js'
import defaultLineHeights from './flagged/defaultLineHeights.js'
import extendedFontSizeScale from './flagged/extendedFontSizeScale.js'
function getDefaultConfigs(config) {
const configs = [defaultConfig]
if (flagEnabled(config, 'uniformColorPalette')) {
configs.unshift(uniformColorPalette)
}
if (flagEnabled(config, 'extendedSpacingScale')) {
configs.unshift(extendedSpacingScale)
}
if (flagEnabled(config, 'defaultLineHeights')) {
configs.unshift(defaultLineHeights)
}
if (flagEnabled(config, 'extendedFontSizeScale')) {
configs.unshift(extendedFontSizeScale)
}
return configs
}
function resolveConfigPath(filePath) {
// require('tailwindcss')({ theme: ..., variants: ... })
if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) {
return undefined
}
// require('tailwindcss')({ config: 'custom-config.js' })
if (_.isObject(filePath) && _.has(filePath, 'config') && _.isString(filePath.config)) {
return path.resolve(filePath.config)
}
// require('tailwindcss')({ config: { theme: ..., variants: ... } })
if (_.isObject(filePath) && _.has(filePath, 'config') && _.isObject(filePath.config)) {
undefined
}
// require('tailwindcss')('custom-config.js')
if (_.isString(filePath)) {
return path.resolve(filePath)
}
// require('tailwindcss')
try {
const defaultConfigPath = path.resolve(defaultConfigFile)
fs.accessSync(defaultConfigPath)
return defaultConfigPath
} catch (err) {
return undefined
}
}
const getConfigFunction = config => () => {
if (_.isUndefined(config) && !_.isObject(config)) {
return resolveConfig([...getDefaultConfigs(defaultConfig)])
}
// Skip this if Jest is running: https://github.com/facebook/jest/pull/9841#issuecomment-621417584
if (process.env.JEST_WORKER_ID === undefined) {
if (!_.isObject(config)) {
getModuleDependencies(config).forEach(mdl => {
delete require.cache[require.resolve(mdl.file)]
})
}
}
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
return resolveConfig([configObject, ...getDefaultConfigs(configObject)])
}
const plugin = postcss.plugin('tailwind', config => {
const plugins = []
const resolvedConfigPath = resolveConfigPath(config)
if (!_.isUndefined(resolvedConfigPath)) {
plugins.push(registerConfigAsDependency(resolvedConfigPath))
}
return postcss([
...plugins,
processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)),
formatCSS,
])
})
module.exports = plugin