diff --git a/src/lib/substituteClassApplyAtRules.js b/src/lib/substituteClassApplyAtRules.js index 623d49490..7d506d7a2 100644 --- a/src/lib/substituteClassApplyAtRules.js +++ b/src/lib/substituteClassApplyAtRules.js @@ -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, '--') }) diff --git a/src/util/defineClass.js b/src/util/defineClass.js index 4f1e4dfac..5a21d4687 100644 --- a/src/util/defineClass.js +++ b/src/util/defineClass.js @@ -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) } diff --git a/src/util/escapeClassName.js b/src/util/escapeClassName.js new file mode 100644 index 000000000..052a4de8b --- /dev/null +++ b/src/util/escapeClassName.js @@ -0,0 +1,3 @@ +export default function escapeClassName(className) { + return className.replace(/([^A-Za-z0-9\-])/g, "\\$1") +}