Skip to content

Commit

Permalink
Remove prefix as a function (#5829)
Browse files Browse the repository at this point in the history
* mark `prefix` as a function as a breaking change

* remove `prefix` as a function related code

* remove `prefix` as a function related tests

* update changelog
  • Loading branch information
RobinMalfait committed Oct 19, 2021
1 parent 36c880a commit 0c2f1a6
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Don't use pointer cursor on disabled buttons by default ([#5772](https://github.com/tailwindlabs/tailwindcss/pull/5772))
- Set default content value in preflight instead of within each before/after utility ([#5820](https://github.com/tailwindlabs/tailwindcss/pull/5820))
- Remove `prefix` as a function ([#5829](https://github.com/tailwindlabs/tailwindcss/pull/5829))

### Added

Expand Down
3 changes: 2 additions & 1 deletion src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ function* resolveMatchedPlugins(classCandidate, context) {
let candidatePrefix = classCandidate
let negative = false

const twConfigPrefix = context.tailwindConfig.prefix || ''
const twConfigPrefix = context.tailwindConfig.prefix

const twConfigPrefixLen = twConfigPrefix.length
if (candidatePrefix[twConfigPrefixLen] === '-') {
negative = true
Expand Down
4 changes: 0 additions & 4 deletions src/lib/setupContextUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
return identifier
}

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

return context.tailwindConfig.prefix + identifier
}

Expand Down
12 changes: 12 additions & 0 deletions src/util/normalizeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ export function normalizeConfig(config) {
return []
})()

// Normalize prefix option
if (typeof config.prefix === 'function') {
log.warn('prefix-function', [
'As of Tailwind CSS v3.0, `prefix` cannot be a function.',
'Update `prefix` in your configuration to be a string to eliminate this warning.',
// TODO: Add https://tw.wtf/prefix-function
])
config.prefix = ''
} else {
config.prefix = config.prefix ?? ''
}

// Normalize the `content`
config.content = {
files: (() => {
Expand Down
5 changes: 1 addition & 4 deletions src/util/prefixSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import parser from 'postcss-selector-parser'
import { tap } from './tap'

export default function (prefix, selector) {
const getPrefix =
typeof prefix === 'function' ? prefix : () => (prefix === undefined ? '' : prefix)

return parser((selectors) => {
selectors.walkClasses((classSelector) => {
tap(classSelector.value, (baseClass) => {
classSelector.value = `${getPrefix('.' + baseClass)}${baseClass}`
classSelector.value = `${prefix}${baseClass}`
})
})
}).processSync(selector)
Expand Down
9 changes: 0 additions & 9 deletions tests/prefix.fn.test.css

This file was deleted.

3 changes: 0 additions & 3 deletions tests/prefix.fn.test.html

This file was deleted.

25 changes: 0 additions & 25 deletions tests/prefix.fn.test.js

This file was deleted.

9 changes: 0 additions & 9 deletions tests/prefixSelector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ test('it prefixes classes with the provided prefix', () => {
expect(prefix('tw-', '.foo')).toEqual('.tw-foo')
})

test('it handles a function as the prefix', () => {
const prefixFunc = (selector) => {
return selector === '.foo' ? 'tw-' : ''
}

expect(prefix(prefixFunc, '.foo')).toEqual('.tw-foo')
expect(prefix(prefixFunc, '.bar')).toEqual('.bar')
})

test('it properly prefixes selectors with non-standard characters', () => {
expect(prefix('tw-', '.hello\\:world')).toEqual('.tw-hello\\:world')
expect(prefix('tw-', '.foo\\/bar')).toEqual('.tw-foo\\/bar')
Expand Down

0 comments on commit 0c2f1a6

Please sign in to comment.