Skip to content

Commit dac5911

Browse files
committed
Provide a function for prefixing utilities in plugins
1 parent 90982f9 commit dac5911

File tree

7 files changed

+49
-14
lines changed

7 files changed

+49
-14
lines changed

__tests__/applyClassPrefix.test.js renamed to __tests__/prefixTree.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import postcss from 'postcss'
2-
import applyClassPrefix from '../src/util/applyClassPrefix'
2+
import prefixTree from '../src/util/prefixTree'
33

44
test('it prefixes classes with the provided prefix', () => {
55
const input = postcss.parse(`
@@ -12,7 +12,7 @@ test('it prefixes classes with the provided prefix', () => {
1212
.tw-apple, .tw-pear { color: green; }
1313
`
1414

15-
const result = applyClassPrefix(input, 'tw-').toResult()
15+
const result = prefixTree(input, 'tw-').toResult()
1616
expect(result.css).toEqual(expected)
1717
expect(result.warnings().length).toBe(0)
1818
})
@@ -36,7 +36,7 @@ test('it handles a function as the prefix', () => {
3636
return ''
3737
}
3838

39-
const result = applyClassPrefix(input, prefixFunc).toResult()
39+
const result = prefixTree(input, prefixFunc).toResult()
4040
expect(result.css).toEqual(expected)
4141
expect(result.warnings().length).toBe(0)
4242
})

__tests__/processPlugins.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,28 @@ test('plugins can add multiple sets of utilities and components', () => {
393393
}
394394
`)
395395
})
396+
397+
test("plugins can apply the user's chosen prefix", () => {
398+
const [, utilities] = processPlugins({
399+
plugins: [
400+
function({ rule, addUtilities, prefix }) {
401+
addUtilities([
402+
rule(prefix('.skew-12deg'), {
403+
transform: 'skewY(-12deg)',
404+
}),
405+
])
406+
},
407+
],
408+
options: {
409+
prefix: 'tw-',
410+
},
411+
})
412+
413+
expect(css(utilities)).toMatchCss(`
414+
@variants {
415+
.tw-skew-12deg {
416+
transform: skewY(-12deg)
417+
}
418+
}
419+
`)
420+
})

src/lib/substituteTailwindAtRules.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import postcss from 'postcss'
33
import container from '../generators/container'
44
import utilityModules from '../utilityModules'
5-
import applyClassPrefix from '../util/applyClassPrefix'
5+
import prefixTree from '../util/prefixTree'
66
import generateModules from '../util/generateModules'
77
import processPlugins from '../util/processPlugins'
88

@@ -29,7 +29,7 @@ export default function(config) {
2929
nodes: pluginComponents,
3030
})
3131

32-
applyClassPrefix(tailwindComponentTree, unwrappedConfig.options.prefix)
32+
prefixTree(tailwindComponentTree, unwrappedConfig.options.prefix)
3333

3434
tailwindComponentTree.walk(node => (node.source = atRule.source))
3535
pluginComponentTree.walk(node => (node.source = atRule.source))
@@ -54,7 +54,7 @@ export default function(config) {
5454
nodes: pluginUtilities,
5555
})
5656

57-
applyClassPrefix(tailwindUtilityTree, unwrappedConfig.options.prefix)
57+
prefixTree(tailwindUtilityTree, unwrappedConfig.options.prefix)
5858

5959
tailwindUtilityTree.walk(node => (node.source = atRule.source))
6060
pluginUtilityTree.walk(node => (node.source = atRule.source))

src/util/applyClassPrefix.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/util/prefixSelector.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function(prefix, selector) {
2+
const getPrefix = typeof prefix === 'function' ? prefix : () => prefix
3+
4+
return `.${getPrefix(selector)}${selector.slice(1)}`
5+
}

src/util/prefixTree.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import prefixSelector from './prefixSelector'
2+
3+
export default function(css, prefix) {
4+
css.walkRules(rule => {
5+
rule.selectors = rule.selectors.map(selector => prefixSelector(prefix, selector))
6+
})
7+
8+
return css
9+
}

src/util/processPlugins.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash'
22
import postcss from 'postcss'
33
import escapeClassName from '../util/escapeClassName'
4+
import prefixSelector from '../util/prefixSelector'
45
import wrapWithVariants from '../util/wrapWithVariants'
56

67
function defineRule(selector, properties) {
@@ -41,6 +42,9 @@ export default function(config) {
4142
addComponents: components => {
4243
pluginComponents.push(...components)
4344
},
45+
prefix: selector => {
46+
return prefixSelector(config.options.prefix, selector)
47+
},
4448
})
4549
})
4650

0 commit comments

Comments
 (0)