Skip to content

Commit

Permalink
Support passing config object under config key
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Aug 2, 2019
1 parent 5cad391 commit 0bf8495
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
38 changes: 38 additions & 0 deletions __tests__/customConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,44 @@ test('custom config path can be passed using `config` property in an object', ()
})
})

test('custom config can be passed under the `config` property', () => {
return postcss([
tailwind({
config: {
theme: {
screens: {
mobile: '400px',
},
},
},
}),
])
.process(
`
@responsive {
.foo {
color: blue;
}
}
`,
{ from: undefined }
)
.then(result => {
const expected = `
.foo {
color: blue;
}
@media (min-width: 400px) {
.mobile\\:foo {
color: blue;
}
}
`

expect(result.css).toMatchCss(expected)
})
})

test('tailwind.config.js is picked up by default', () => {
return inTempDirectory(() => {
fs.writeFileSync(
Expand Down
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ import { defaultConfigFile } 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
}

if (_.isObject(filePath) && _.has(filePath, 'config')) {
// 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)
Expand All @@ -43,7 +52,10 @@ const getConfigFunction = config => () => {
delete require.cache[require.resolve(config)]
}

return resolveConfig([_.isObject(config) ? config : require(config), defaultConfig])
return resolveConfig([
_.isObject(config) ? _.get(config, 'config', config) : require(config),
defaultConfig,
])
}

const plugin = postcss.plugin('tailwind', config => {
Expand Down

0 comments on commit 0bf8495

Please sign in to comment.