Skip to content

Commit 8646c94

Browse files
committed
Prepend a shadow lookup table when no @tailwind rules are in the tree
1 parent c252e33 commit 8646c94

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

__tests__/applyAtRule.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const { utilities: defaultUtilities } = processPlugins(
1313
)
1414

1515
function run(input, config = resolvedDefaultConfig, utilities = defaultUtilities) {
16-
return postcss([substituteClassApplyAtRules(config, utilities)]).process(input, {
16+
return postcss([
17+
substituteClassApplyAtRules(config, () => ({
18+
utilities,
19+
})),
20+
]).process(input, {
1721
from: undefined,
1822
})
1923
}

__tests__/applyComplexClasses.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ test('you can apply utility classes that do not actually exist as long as they w
259259
})
260260
})
261261

262+
test('the shadow lookup is only used if no @tailwind rules were in the source tree', () => {
263+
const input = `
264+
@tailwind base;
265+
.foo { @apply mt-4; }
266+
`
267+
268+
return run(input).catch(e => {
269+
expect(e).toMatchObject({ name: 'CssSyntaxError' })
270+
})
271+
})
272+
262273
test.skip('you can apply utility classes without using the given prefix', () => {
263274
const input = `
264275
.foo { @apply .tw-mt-4 .mb-4; }

src/flagged/applyComplexClasses.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import substituteResponsiveAtRules from '../lib/substituteResponsiveAtRules'
88
import convertLayerAtRulesToControlComments from '../lib/convertLayerAtRulesToControlComments'
99
import substituteScreenAtRules from '../lib/substituteScreenAtRules'
1010

11-
function hasInject(css) {
12-
let foundInject = false
11+
function hasAtRule(css, atRule) {
12+
let foundAtRule = false
1313

14-
css.walkAtRules('apply', () => {
15-
foundInject = true
14+
css.walkAtRules(atRule, () => {
15+
foundAtRule = true
1616
return false
1717
})
1818

19-
return foundInject
19+
return foundAtRule
2020
}
21+
2122
function applyUtility(rule, className, replaceWith) {
2223
const processedSelectors = rule.selectors.map(selector => {
2324
const processor = selectorParser(selectors => {
@@ -85,7 +86,7 @@ function buildUtilityMap(css) {
8586
index,
8687
utilityName,
8788
rule: rule.clone({ parent: rule.parent }),
88-
containsApply: hasInject(rule),
89+
containsApply: hasAtRule(rule, 'apply'),
8990
})
9091
index++
9192
})
@@ -153,10 +154,10 @@ function makeExtractUtilityRules(css) {
153154
}
154155
}
155156

156-
function themagic(css, lookupTree) {
157+
function processApplyAtRules(css, lookupTree) {
157158
const extractUtilityRules = makeExtractUtilityRules(lookupTree)
158159

159-
while (hasInject(css)) {
160+
while (hasAtRule(css, 'apply')) {
160161
css.walkRules(rule => {
161162
const injectRules = []
162163

@@ -222,6 +223,11 @@ function themagic(css, lookupTree) {
222223

223224
export default function applyComplexClasses(config, getProcessedPlugins) {
224225
return function(css) {
226+
// Tree already contains @tailwind rules, don't prepend default Tailwind tree
227+
if (hasAtRule(css, 'tailwind')) {
228+
return processApplyAtRules(css, css)
229+
}
230+
225231
return postcss([
226232
substituteTailwindAtRules(config, getProcessedPlugins()),
227233
evaluateTailwindFunctions(config),
@@ -242,7 +248,7 @@ export default function applyComplexClasses(config, getProcessedPlugins) {
242248
// if css already contains tailwind, css is the lookup tree
243249
const lookupTree = _.tap(css.clone(), tree => tree.prepend(result.root))
244250

245-
return themagic(css, lookupTree)
251+
return processApplyAtRules(css, lookupTree)
246252
})
247253
}
248254
}

src/lib/substituteTailwindAtRules.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,15 @@ export default function(
4949
}
5050

5151
if (atRule.params === 'base') {
52-
atRule.before(updateSource(pluginBase, atRule.source))
53-
atRule.remove()
52+
atRule.after(updateSource(pluginBase, atRule.source))
5453
}
5554

5655
if (atRule.params === 'components') {
57-
atRule.before(updateSource(pluginComponents, atRule.source))
58-
atRule.remove()
56+
atRule.after(updateSource(pluginComponents, atRule.source))
5957
}
6058

6159
if (atRule.params === 'utilities') {
62-
atRule.before(updateSource(pluginUtilities, atRule.source))
63-
atRule.remove()
60+
atRule.after(updateSource(pluginUtilities, atRule.source))
6461
}
6562

6663
if (atRule.params === 'screens') {

src/processTailwindFeatures.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export default function(getConfig) {
4040
convertLayerAtRulesToControlComments(config),
4141
substituteScreenAtRules(config),
4242
substituteClassApplyAtRules(config, getProcessedPlugins),
43+
function(css) {
44+
css.walkAtRules('tailwind', rule => rule.remove())
45+
},
4346
purgeUnusedStyles(config),
4447
]).process(css, { from: _.get(css, 'source.input.file') })
4548
}

0 commit comments

Comments
 (0)