2017-10-15 13:52:51 -04:00
|
|
|
import path from 'path'
|
2019-03-04 16:19:07 -05:00
|
|
|
import fs from 'fs'
|
2017-10-15 13:52:51 -04:00
|
|
|
|
2017-08-26 14:36:40 -05:00
|
|
|
import _ from 'lodash'
|
2017-08-27 18:42:10 -04:00
|
|
|
|
2019-08-11 12:32:46 +03:00
|
|
|
import getModuleDependencies from './lib/getModuleDependencies'
|
2017-11-03 09:54:15 -04:00
|
|
|
import registerConfigAsDependency from './lib/registerConfigAsDependency'
|
2018-06-22 20:52:59 -04:00
|
|
|
import processTailwindFeatures from './processTailwindFeatures'
|
2019-04-17 08:07:42 -04:00
|
|
|
import formatCSS from './lib/formatCSS'
|
2019-02-05 20:36:54 -05:00
|
|
|
import resolveConfig from './util/resolveConfig'
|
2020-09-07 19:56:35 -04:00
|
|
|
import getAllConfigs from './util/getAllConfigs'
|
2019-03-15 07:08:34 -05:00
|
|
|
import { defaultConfigFile } from './constants'
|
2019-03-14 15:51:21 -05:00
|
|
|
import defaultConfig from '../stubs/defaultConfig.stub.js'
|
2020-08-05 13:59:59 -04:00
|
|
|
|
2019-03-04 16:19:07 -05:00
|
|
|
function resolveConfigPath(filePath) {
|
2019-08-02 08:31:46 -04:00
|
|
|
// require('tailwindcss')({ theme: ..., variants: ... })
|
2019-08-02 08:14:07 -04:00
|
|
|
if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) {
|
2019-03-04 16:19:07 -05:00
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 08:31:46 -04:00
|
|
|
// require('tailwindcss')({ config: 'custom-config.js' })
|
|
|
|
|
if (_.isObject(filePath) && _.has(filePath, 'config') && _.isString(filePath.config)) {
|
2019-08-02 08:07:42 -04:00
|
|
|
return path.resolve(filePath.config)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 08:31:46 -04:00
|
|
|
// require('tailwindcss')({ config: { theme: ..., variants: ... } })
|
|
|
|
|
if (_.isObject(filePath) && _.has(filePath, 'config') && _.isObject(filePath.config)) {
|
2020-08-27 13:05:06 -04:00
|
|
|
return undefined
|
2019-08-02 08:31:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// require('tailwindcss')('custom-config.js')
|
2019-08-02 08:14:07 -04:00
|
|
|
if (_.isString(filePath)) {
|
2019-03-04 16:19:07 -05:00
|
|
|
return path.resolve(filePath)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 08:31:46 -04:00
|
|
|
// require('tailwindcss')
|
2019-03-04 16:19:07 -05:00
|
|
|
try {
|
2019-03-15 07:08:34 -05:00
|
|
|
const defaultConfigPath = path.resolve(defaultConfigFile)
|
2019-03-04 16:19:07 -05:00
|
|
|
fs.accessSync(defaultConfigPath)
|
|
|
|
|
return defaultConfigPath
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-03 09:54:15 -04:00
|
|
|
|
2020-10-16 15:39:44 -04:00
|
|
|
const getConfigFunction = (config) => () => {
|
2020-10-08 11:21:39 -04:00
|
|
|
if (_.isUndefined(config)) {
|
2020-09-04 19:58:02 -04:00
|
|
|
return resolveConfig([...getAllConfigs(defaultConfig)])
|
2017-10-26 09:29:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 16:37:03 -04:00
|
|
|
// 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)) {
|
2020-10-16 15:39:44 -04:00
|
|
|
getModuleDependencies(config).forEach((mdl) => {
|
2020-04-29 16:37:03 -04:00
|
|
|
delete require.cache[require.resolve(mdl.file)]
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-03-04 16:19:07 -05:00
|
|
|
}
|
2017-11-03 09:54:15 -04:00
|
|
|
|
2019-10-11 13:39:05 -04:00
|
|
|
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
|
2019-10-12 13:41:28 -04:00
|
|
|
|
2020-09-04 19:58:02 -04:00
|
|
|
return resolveConfig([...getAllConfigs(configObject)])
|
2019-03-04 16:19:07 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-15 16:13:48 -05:00
|
|
|
module.exports = function (config) {
|
2019-03-04 16:19:07 -05:00
|
|
|
const plugins = []
|
|
|
|
|
const resolvedConfigPath = resolveConfigPath(config)
|
2018-06-22 21:01:18 -04:00
|
|
|
|
2019-03-04 16:19:07 -05:00
|
|
|
if (!_.isUndefined(resolvedConfigPath)) {
|
|
|
|
|
plugins.push(registerConfigAsDependency(resolvedConfigPath))
|
2017-10-15 13:52:51 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-15 16:13:48 -05:00
|
|
|
return {
|
|
|
|
|
postcssPlugin: 'tailwindcss',
|
|
|
|
|
plugins: [
|
|
|
|
|
...plugins,
|
|
|
|
|
processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)),
|
|
|
|
|
formatCSS,
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-28 15:02:50 -05:00
|
|
|
|
2020-11-15 16:13:48 -05:00
|
|
|
module.exports.postcss = true
|