Skip to content

Commit 56c320d

Browse files
committed
Refactor
1 parent 623718e commit 56c320d

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

src/lib/setupContextUtils.js

+46-44
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ function withIdentifiers(styles) {
170170
})
171171
}
172172

173+
export function parseVariant(variant) {
174+
variant = variant
175+
.replace(/\n+/g, '')
176+
.replace(/\s{1,}/g, ' ')
177+
.trim()
178+
179+
let fns = parseVariantFormatString(variant).map((str) => {
180+
if (!str.startsWith('@')) {
181+
return ({ format }) => format(str)
182+
}
183+
184+
let [, name, params] = /@(.*?) (.*)/g.exec(str)
185+
return ({ wrap }) => wrap(postcss.atRule({ name, params }))
186+
}).reverse()
187+
188+
return (api) => {
189+
for (let fn of fns) {
190+
fn(api)
191+
}
192+
}
193+
}
194+
173195
function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offsets, classList }) {
174196
function getConfigValue(path, defaultValue) {
175197
return path ? dlv(tailwindConfig, path, defaultValue) : tailwindConfig
@@ -201,27 +223,7 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
201223
}
202224
}
203225

204-
variantFunction = variantFunction
205-
.replace(/\n+/g, '')
206-
.replace(/\s{1,}/g, ' ')
207-
.trim()
208-
209-
let fns = parseVariantFormatString(variantFunction)
210-
.map((str) => {
211-
if (!str.startsWith('@')) {
212-
return ({ format }) => format(str)
213-
}
214-
215-
let [, name, params] = /@(.*?) (.*)/g.exec(str)
216-
return ({ wrap }) => wrap(postcss.atRule({ name, params }))
217-
})
218-
.reverse()
219-
220-
return (api) => {
221-
for (let fn of fns) {
222-
fn(api)
223-
}
224-
}
226+
return parseVariant(variantFunction)
225227
})
226228

227229
insertInto(variantList, variantName, options)
@@ -674,29 +676,29 @@ function registerPlugins(plugins, context) {
674676
for (let util of classList) {
675677
let utils = Array.isArray(util)
676678
? (() => {
677-
let [utilName, options] = util
678-
let values = Object.keys(options?.values ?? {})
679-
let classes = values.map((value) => formatClass(utilName, value))
680-
681-
if (options?.supportsNegativeValues) {
682-
// This is the normal negated version
683-
// e.g. `-inset-1` or `-tw-inset-1`
684-
classes = [...classes, ...classes.map((cls) => '-' + cls)]
685-
686-
// This is the negated version *after* the prefix
687-
// e.g. `tw--inset-1`
688-
// The prefix is already attached to util name
689-
// So we add the negative after the prefix
690-
classes = [
691-
...classes,
692-
...classes.map(
693-
(cls) => cls.slice(0, prefixLength) + '-' + cls.slice(prefixLength)
694-
),
695-
]
696-
}
697-
698-
return classes
699-
})()
679+
let [utilName, options] = util
680+
let values = Object.keys(options?.values ?? {})
681+
let classes = values.map((value) => formatClass(utilName, value))
682+
683+
if (options?.supportsNegativeValues) {
684+
// This is the normal negated version
685+
// e.g. `-inset-1` or `-tw-inset-1`
686+
classes = [...classes, ...classes.map((cls) => '-' + cls)]
687+
688+
// This is the negated version *after* the prefix
689+
// e.g. `tw--inset-1`
690+
// The prefix is already attached to util name
691+
// So we add the negative after the prefix
692+
classes = [
693+
...classes,
694+
...classes.map(
695+
(cls) => cls.slice(0, prefixLength) + '-' + cls.slice(prefixLength)
696+
),
697+
]
698+
}
699+
700+
return classes
701+
})()
700702
: [util]
701703

702704
for (let util of utils) {

0 commit comments

Comments
 (0)