Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 55aa371

Browse files
Add support for prefix as a function (#177)
* Refactor * Add support for prefix function This has the limitation that you must handle selector prefixes. The prefix function will get called with `.ml` before it gets called with `.ml-1`
1 parent d47902f commit 55aa371

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

src/lib/setupContext.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,15 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
401401
return '*'
402402
}
403403

404-
return options.respectPrefix ? context.tailwindConfig.prefix + identifier : identifier
404+
if (! options.respectPrefix) {
405+
return identifier
406+
}
407+
408+
if (typeof context.tailwindConfig.prefix === 'function') {
409+
return prefixSelector(context.tailwindConfig.prefix, `.${identifier}`).substr(1)
410+
}
411+
412+
return context.tailwindConfig.prefix + identifier
405413
}
406414

407415
return {

tests/prefix.fn.test.css

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.tw-ml-1 {
2+
margin-left: 0.25rem;
3+
}
4+
.flex {
5+
display: flex;
6+
}
7+
.tw-align-bottom {
8+
vertical-align: bottom
9+
}

tests/prefix.fn.test.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="flex"></div>
2+
<div class="tw-align-bottom"></div>
3+
<div class="tw-ml-1"></div>

tests/prefix.fn.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function run(input, config = {}) {
7+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
8+
}
9+
10+
test('prefix fn', () => {
11+
let config = {
12+
prefix(selector) {
13+
if (['.align-bottom', '.ml'].some(prefix => selector.startsWith(prefix))) {
14+
return 'tw-';
15+
}
16+
17+
return '';
18+
},
19+
purge: [path.resolve(__dirname, './prefix.fn.test.html')],
20+
corePlugins: { preflight: false },
21+
theme: {},
22+
}
23+
24+
let css = `
25+
@tailwind utilities;
26+
`
27+
28+
return run(css, config).then((result) => {
29+
let expectedPath = path.resolve(__dirname, './prefix.fn.test.css')
30+
let expected = fs.readFileSync(expectedPath, 'utf8')
31+
32+
expect(result.css).toMatchCss(expected)
33+
})
34+
})

0 commit comments

Comments
 (0)