Skip to content

Commit b14d279

Browse files
authored
Merge pull request #186 from tailwindcss/options-master
Add "prefix" and "important" options to config
2 parents b806cd1 + 891271f commit b14d279

File tree

4 files changed

+79
-41
lines changed

4 files changed

+79
-41
lines changed

__tests__/applyClassPrefix.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import postcss from 'postcss'
2+
import applyClassPrefix from '../src/util/applyClassPrefix'
3+
4+
test('it prefixes classes with the provided prefix', () => {
5+
const input = postcss.parse(`
6+
.foo { color: red; }
7+
.apple, .pear { color: green; }
8+
`)
9+
10+
const expected = `
11+
.tw-foo { color: red; }
12+
.tw-apple, .tw-pear { color: green; }
13+
`
14+
15+
const result = applyClassPrefix(input, 'tw-').toResult()
16+
expect(result.css).toEqual(expected)
17+
expect(result.warnings().length).toBe(0)
18+
})

src/lib/generateUtilities.js

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import _ from 'lodash'
2+
import postcss from 'postcss'
3+
import applyClassPrefix from '../util/applyClassPrefix'
24
import backgroundColors from '../generators/backgroundColors'
35
import backgroundPositions from '../generators/backgroundPositions'
46
import backgroundSize from '../generators/backgroundSize'
@@ -39,50 +41,61 @@ import zIndex from '../generators/zIndex'
3941

4042
export default function(config) {
4143
return function(css) {
42-
const options = config()
44+
const unwrappedConfig = config()
4345

4446
css.walkAtRules('tailwind', atRule => {
4547
if (atRule.params === 'utilities') {
46-
const utilities = _.flatten([
47-
lists(options),
48-
forms(options),
49-
textSizes(options),
50-
textWeights(options),
51-
textFonts(options),
52-
textColors(options),
53-
textLeading(options),
54-
textTracking(options),
55-
textAlign(options),
56-
textWrap(options),
57-
textStyle(options),
58-
verticalAlign(options),
59-
backgroundColors(options),
60-
backgroundPositions(options),
61-
backgroundSize(options),
62-
borderStylesReset(options),
63-
borderWidths(options),
64-
borderColors(options),
65-
borderStyles(options),
66-
rounded(options),
67-
display(options),
68-
position(options),
69-
overflow(options),
70-
sizing(options),
71-
spacing(options),
72-
shadows(options),
73-
flex(options),
74-
floats(options),
75-
visibility(options),
76-
zIndex(options),
77-
opacity(options),
78-
userSelect(options),
79-
pointerEvents(options),
80-
resize(options),
81-
cursor(options),
82-
])
48+
const utilities = postcss.root({
49+
nodes: _.flatten([
50+
lists(unwrappedConfig),
51+
forms(unwrappedConfig),
52+
textSizes(unwrappedConfig),
53+
textWeights(unwrappedConfig),
54+
textFonts(unwrappedConfig),
55+
textColors(unwrappedConfig),
56+
textLeading(unwrappedConfig),
57+
textTracking(unwrappedConfig),
58+
textAlign(unwrappedConfig),
59+
textWrap(unwrappedConfig),
60+
textStyle(unwrappedConfig),
61+
verticalAlign(unwrappedConfig),
62+
backgroundColors(unwrappedConfig),
63+
backgroundPositions(unwrappedConfig),
64+
backgroundSize(unwrappedConfig),
65+
borderStylesReset(unwrappedConfig),
66+
borderWidths(unwrappedConfig),
67+
borderColors(unwrappedConfig),
68+
borderStyles(unwrappedConfig),
69+
rounded(unwrappedConfig),
70+
display(unwrappedConfig),
71+
position(unwrappedConfig),
72+
overflow(unwrappedConfig),
73+
sizing(unwrappedConfig),
74+
spacing(unwrappedConfig),
75+
shadows(unwrappedConfig),
76+
flex(unwrappedConfig),
77+
floats(unwrappedConfig),
78+
visibility(unwrappedConfig),
79+
zIndex(unwrappedConfig),
80+
opacity(unwrappedConfig),
81+
userSelect(unwrappedConfig),
82+
pointerEvents(unwrappedConfig),
83+
resize(unwrappedConfig),
84+
cursor(unwrappedConfig),
85+
]),
86+
})
8387

84-
atRule.before(container(options))
85-
atRule.before(responsive(utilities))
88+
if (_.get(unwrappedConfig, 'options.important', false)) {
89+
utilities.walkDecls(decl => (decl.important = true))
90+
}
91+
92+
const tailwindClasses = postcss.root({
93+
nodes: [...container(unwrappedConfig), responsive(utilities)],
94+
})
95+
96+
applyClassPrefix(tailwindClasses, _.get(unwrappedConfig, 'options.prefix', ''))
97+
98+
atRule.before(tailwindClasses)
8699
atRule.remove()
87100
}
88101
})

src/util/applyClassPrefix.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function(css, prefix) {
2+
css.walkRules(rule => {
3+
rule.selectors = rule.selectors.map(selector => `.${prefix}${selector.slice(1)}`)
4+
})
5+
return css
6+
}

src/util/responsive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash'
12
import postcss from 'postcss'
23
import cloneNodes from './cloneNodes'
34

@@ -6,5 +7,5 @@ export default function responsive(rules) {
67
.atRule({
78
name: 'responsive',
89
})
9-
.append(cloneNodes(rules))
10+
.append(cloneNodes(_.isArray(rules) ? rules : rules.nodes))
1011
}

0 commit comments

Comments
 (0)