Skip to content

Commit 165787d

Browse files
committed
Clean stuff up
1 parent 20a6b60 commit 165787d

File tree

4 files changed

+46
-72
lines changed

4 files changed

+46
-72
lines changed

src/jit/lib/generateRules.js

+5-57
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import selectorParser from 'postcss-selector-parser'
33
import parseObjectStyles from '../../util/parseObjectStyles'
44
import isPlainObject from '../../util/isPlainObject'
55
import prefixSelector from '../../util/prefixSelector'
6-
import nameClass from '../../util/nameClass'
7-
import {
8-
updateAllClasses,
9-
asValue,
10-
asList,
11-
asColor,
12-
asAngle,
13-
asLength,
14-
asLookupValue,
15-
} from '../../util/pluginUtils'
6+
import { updateAllClasses } from '../../util/pluginUtils'
167

178
let classNameParser = selectorParser((selectors) => {
189
return selectors.first.filter(({ type }) => type === 'class').pop().value
@@ -224,58 +215,15 @@ function* resolveMatches(candidate, context) {
224215
// }
225216

226217
for (let matchedPlugins of resolveMatchedPlugins(classCandidate, context)) {
227-
let pluginHelpers = {
228-
candidate: classCandidate,
229-
theme: context.tailwindConfig.theme,
230-
}
231-
232-
let asMap = {
233-
any: asValue,
234-
list: asList,
235-
color: asColor,
236-
angle: asAngle,
237-
length: asLength,
238-
lookup: asLookupValue,
239-
}
240-
241218
let matches = []
242219
let [plugins, modifier] = matchedPlugins
243220

244221
for (let [sort, plugin] of plugins) {
245222
if (typeof plugin === 'function') {
246-
if (sort.version === 2) {
247-
let { type = 'any' } = sort.options
248-
let value = asMap[type](modifier, sort.options.values)
249-
250-
if (value === undefined) {
251-
continue
252-
}
253-
254-
let includedRules = []
255-
let ruleSets = []
256-
.concat(
257-
plugin(value, {
258-
includeRules(rules) {
259-
includedRules.push(...rules)
260-
},
261-
})
262-
)
263-
.map((declaration) => ({
264-
[nameClass(sort.identifier, modifier)]: declaration,
265-
}))
266-
267-
for (let ruleSet of [...includedRules, ...ruleSets]) {
268-
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
269-
for (let rule of rules) {
270-
matches.push([{ ...sort, options: { ...sort.options, ...options } }, rule])
271-
}
272-
}
273-
} else {
274-
for (let ruleSet of [].concat(plugin(modifier, pluginHelpers))) {
275-
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
276-
for (let rule of rules) {
277-
matches.push([{ ...sort, options: { ...sort.options, ...options } }, rule])
278-
}
223+
for (let ruleSet of [].concat(plugin(modifier))) {
224+
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
225+
for (let rule of rules) {
226+
matches.push([{ ...sort, options: { ...sort.options, ...options } }, rule])
279227
}
280228
}
281229
}

src/jit/lib/setupContext.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import corePlugins from '../corePlugins'
2222
import isPlainObject from '../../util/isPlainObject'
2323
import escapeClassName from '../../util/escapeClassName'
2424

25+
import nameClass from '../../util/nameClass'
26+
import { coerceValue } from '../../util/pluginUtils'
27+
2528
import * as sharedState from './sharedState'
2629

2730
let contextMap = sharedState.contextMap
@@ -529,10 +532,31 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
529532
let prefixedIdentifier = prefixIdentifier(identifier, options)
530533
let rule = utilities[identifier]
531534

532-
let withOffsets = [
533-
{ sort: offset, layer: 'utilities', options, identifier, version: 2 },
534-
rule,
535-
]
535+
function wrapped(modifier) {
536+
let { type = 'any' } = options
537+
let value = coerceValue(type, modifier, options.values)
538+
539+
if (value === undefined) {
540+
return []
541+
}
542+
543+
let includedRules = []
544+
let ruleSets = []
545+
.concat(
546+
rule(value, {
547+
includeRules(rules) {
548+
includedRules.push(...rules)
549+
},
550+
})
551+
)
552+
.map((declaration) => ({
553+
[nameClass(identifier, modifier)]: declaration,
554+
}))
555+
556+
return [...includedRules, ...ruleSets]
557+
}
558+
559+
let withOffsets = [{ sort: offset, layer: 'utilities', options }, wrapped]
536560

537561
if (!context.candidateRuleMap.has(prefixedIdentifier)) {
538562
context.candidateRuleMap.set(prefixedIdentifier, [])
@@ -541,16 +565,6 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
541565
context.candidateRuleMap.get(prefixedIdentifier).push(withOffsets)
542566
}
543567
},
544-
// ---
545-
jit: {
546-
e: escapeClassName,
547-
config: tailwindConfig,
548-
theme: tailwindConfig.theme,
549-
addVariant(variantName, applyVariant, options = {}) {
550-
insertInto(variantList, variantName, options)
551-
variantMap.set(variantName, applyVariant)
552-
},
553-
},
554568
}
555569
}
556570

src/util/pluginUtils.js

+13
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,16 @@ export function asLength(modifier, lookup = {}) {
194194
export function asLookupValue(modifier, lookup = {}) {
195195
return lookup[modifier]
196196
}
197+
198+
let typeMap = {
199+
any: asValue,
200+
list: asList,
201+
color: asColor,
202+
angle: asAngle,
203+
length: asLength,
204+
lookup: asLookupValue,
205+
}
206+
207+
export function coerceValue(type, modifier, values) {
208+
return typeMap[type](modifier, values)
209+
}

tests/plugins/fontSize.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import postcss from 'postcss'
2-
import fs from 'fs'
32
import path from 'path'
43
import tailwind from '../../src/index.js'
54

0 commit comments

Comments
 (0)