mirror of
https://github.com/tailwindlabs/tailwindcss.git
synced 2025-12-08 21:36:08 +00:00
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
import path from 'path'
|
|
import fs from 'fs'
|
|
|
|
import _ from 'lodash'
|
|
import postcss from 'postcss'
|
|
|
|
import registerConfigAsDependency from './lib/registerConfigAsDependency'
|
|
import processTailwindFeatures from './processTailwindFeatures'
|
|
import formatNodes from './lib/formatNodes'
|
|
import resolveConfig from './util/resolveConfig'
|
|
import { defaultConfigFile } from './constants'
|
|
|
|
import defaultConfig from '../stubs/defaultConfig.stub.js'
|
|
|
|
function resolveConfigPath(filePath) {
|
|
if (_.isObject(filePath)) {
|
|
return undefined
|
|
}
|
|
|
|
if (!_.isUndefined(filePath)) {
|
|
return path.resolve(filePath)
|
|
}
|
|
|
|
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([defaultConfig])
|
|
}
|
|
|
|
if (!_.isObject(config)) {
|
|
delete require.cache[require.resolve(config)]
|
|
}
|
|
|
|
return resolveConfig([_.isObject(config) ? config : require(config), defaultConfig])
|
|
}
|
|
|
|
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)),
|
|
formatNodes,
|
|
])
|
|
})
|
|
|
|
module.exports = plugin
|