Merge pull request #1062 from tailwindcss/config-path-object-syntax
Support passing config path via object
This commit is contained in:
commit
2a462e7c64
2 changed files with 123 additions and 3 deletions
|
|
@ -68,6 +68,71 @@ test('custom config can be passed as an object', () => {
|
|||
})
|
||||
})
|
||||
|
||||
test('custom config path can be passed using `config` property in an object', () => {
|
||||
return postcss([tailwind({ config: path.resolve(`${__dirname}/fixtures/custom-config.js`) })])
|
||||
.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('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(
|
||||
|
|
@ -106,3 +171,42 @@ test('tailwind.config.js is picked up by default', () => {
|
|||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('tailwind.config.js is picked up by default when passing an empty object', () => {
|
||||
return inTempDirectory(() => {
|
||||
fs.writeFileSync(
|
||||
path.resolve(defaultConfigFile),
|
||||
`module.exports = {
|
||||
theme: {
|
||||
screens: {
|
||||
mobile: '400px',
|
||||
},
|
||||
},
|
||||
}`
|
||||
)
|
||||
|
||||
return postcss([tailwind({})])
|
||||
.process(
|
||||
`
|
||||
@responsive {
|
||||
.foo {
|
||||
color: blue;
|
||||
}
|
||||
}
|
||||
`,
|
||||
{ from: undefined }
|
||||
)
|
||||
.then(result => {
|
||||
expect(result.css).toMatchCss(`
|
||||
.foo {
|
||||
color: blue;
|
||||
}
|
||||
@media (min-width: 400px) {
|
||||
.mobile\\:foo {
|
||||
color: blue;
|
||||
}
|
||||
}
|
||||
`)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
22
src/index.js
22
src/index.js
|
|
@ -13,14 +13,27 @@ import { defaultConfigFile } from './constants'
|
|||
import defaultConfig from '../stubs/defaultConfig.stub.js'
|
||||
|
||||
function resolveConfigPath(filePath) {
|
||||
if (_.isObject(filePath)) {
|
||||
// require('tailwindcss')({ theme: ..., variants: ... })
|
||||
if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
if (!_.isUndefined(filePath)) {
|
||||
// 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)
|
||||
|
|
@ -39,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 => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue