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

Add support for prefix as a function #177

Merged
merged 2 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/lib/setupContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,15 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
return '*'
}

return options.respectPrefix ? context.tailwindConfig.prefix + identifier : identifier
if (! options.respectPrefix) {
return identifier
}

if (typeof context.tailwindConfig.prefix === 'function') {
return prefixSelector(context.tailwindConfig.prefix, `.${identifier}`).substr(1)
}

return context.tailwindConfig.prefix + identifier
}

return {
Expand Down
9 changes: 9 additions & 0 deletions tests/prefix.fn.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.tw-ml-1 {
margin-left: 0.25rem;
}
.flex {
display: flex;
}
.tw-align-bottom {
vertical-align: bottom
}
3 changes: 3 additions & 0 deletions tests/prefix.fn.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="flex"></div>
<div class="tw-align-bottom"></div>
<div class="tw-ml-1"></div>
34 changes: 34 additions & 0 deletions tests/prefix.fn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const postcss = require('postcss')
const tailwind = require('../src/index.js')
const fs = require('fs')
const path = require('path')

function run(input, config = {}) {
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
}

test('prefix fn', () => {
let config = {
prefix(selector) {
if (['.align-bottom', '.ml'].some(prefix => selector.startsWith(prefix))) {
return 'tw-';
}

return '';
},
purge: [path.resolve(__dirname, './prefix.fn.test.html')],
corePlugins: { preflight: false },
theme: {},
}

let css = `
@tailwind utilities;
`

return run(css, config).then((result) => {
let expectedPath = path.resolve(__dirname, './prefix.fn.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')

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