Escape class names provided in @apply

Without this, can't mixin stuff like .w-1/4.
This commit is contained in:
Adam Wathan 2017-10-06 15:40:40 -04:00
parent 082fd3a389
commit 9a880d43ea
3 changed files with 20 additions and 6 deletions

View File

@ -1,13 +1,27 @@
import postcss from 'postcss'
import _ from 'lodash'
import findMixin from '../util/findMixin'
import escapeClassName from '../util/escapeClassName'
function normalizeClassNames(classNames) {
return classNames.map((className) => {
return `.${escapeClassName(_.trimStart(className, '.'))}`
})
}
export default postcss.plugin('tailwind-apply', function(css) {
return function(css) {
css.walkRules(function(rule) {
rule.walkAtRules('apply', atRule => {
const mixins = postcss.list.space(atRule.params)
const mixins = normalizeClassNames(postcss.list.space(atRule.params))
/*
* Don't wreck CSSNext-style @apply rules:
* http://cssnext.io/features/#custom-properties-set-apply
*
* These are deprecated in CSSNext but still playing it safe for now.
* We might consider renaming this at-rule.
*/
const [customProperties, classes] = _.partition(mixins, mixin => {
return _.startsWith(mixin, '--')
})

View File

@ -1,9 +1,6 @@
import _ from 'lodash'
import postcss from 'postcss'
function escapeSelector(selector) {
return selector.replace(/([^A-Za-z0-9\-])/g, "\\$1")
}
import escapeClassName from './escapeClassName'
export default function(className, properties) {
const decls = _.map(properties, (value, property) => {
@ -14,6 +11,6 @@ export default function(className, properties) {
})
return postcss.rule({
selector: `.${escapeSelector(className)}`,
selector: `.${escapeClassName(className)}`,
}).append(decls)
}

View File

@ -0,0 +1,3 @@
export default function escapeClassName(className) {
return className.replace(/([^A-Za-z0-9\-])/g, "\\$1")
}