forked from tailwindlabs/tailwindcss
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.postcss7.js
83 lines (67 loc) · 2.46 KB
/
index.postcss7.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
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 getAllConfigs from './util/getAllConfigs'
import { supportedConfigFiles } from './constants'
import defaultConfig from '../stubs/defaultConfig.stub.js'
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)) {
return undefined
}
// require('tailwindcss')('custom-config.js')
if (_.isString(filePath)) {
return path.resolve(filePath)
}
// require('tailwindcss')
for (const configFile of supportedConfigFiles) {
try {
const configPath = path.resolve(configFile)
fs.accessSync(configPath)
return configPath
} catch (err) {}
}
return undefined
}
const getConfigFunction = (config) => () => {
if (_.isUndefined(config)) {
return resolveConfig([...getAllConfigs(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([...getAllConfigs(configObject)])
}
const plugin = postcss.plugin('tailwindcss', (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