Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed May 8, 2022
1 parent 623718e commit 56c320d
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions src/lib/setupContextUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ function withIdentifiers(styles) {
})
}

export function parseVariant(variant) {
variant = variant
.replace(/\n+/g, '')
.replace(/\s{1,}/g, ' ')
.trim()

let fns = parseVariantFormatString(variant).map((str) => {
if (!str.startsWith('@')) {
return ({ format }) => format(str)
}

let [, name, params] = /@(.*?) (.*)/g.exec(str)
return ({ wrap }) => wrap(postcss.atRule({ name, params }))
}).reverse()

return (api) => {
for (let fn of fns) {
fn(api)
}
}
}

function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offsets, classList }) {
function getConfigValue(path, defaultValue) {
return path ? dlv(tailwindConfig, path, defaultValue) : tailwindConfig
Expand Down Expand Up @@ -201,27 +223,7 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
}
}

variantFunction = variantFunction
.replace(/\n+/g, '')
.replace(/\s{1,}/g, ' ')
.trim()

let fns = parseVariantFormatString(variantFunction)
.map((str) => {
if (!str.startsWith('@')) {
return ({ format }) => format(str)
}

let [, name, params] = /@(.*?) (.*)/g.exec(str)
return ({ wrap }) => wrap(postcss.atRule({ name, params }))
})
.reverse()

return (api) => {
for (let fn of fns) {
fn(api)
}
}
return parseVariant(variantFunction)
})

insertInto(variantList, variantName, options)
Expand Down Expand Up @@ -674,29 +676,29 @@ function registerPlugins(plugins, context) {
for (let util of classList) {
let utils = Array.isArray(util)
? (() => {
let [utilName, options] = util
let values = Object.keys(options?.values ?? {})
let classes = values.map((value) => formatClass(utilName, value))

if (options?.supportsNegativeValues) {
// This is the normal negated version
// e.g. `-inset-1` or `-tw-inset-1`
classes = [...classes, ...classes.map((cls) => '-' + cls)]

// This is the negated version *after* the prefix
// e.g. `tw--inset-1`
// The prefix is already attached to util name
// So we add the negative after the prefix
classes = [
...classes,
...classes.map(
(cls) => cls.slice(0, prefixLength) + '-' + cls.slice(prefixLength)
),
]
}

return classes
})()
let [utilName, options] = util
let values = Object.keys(options?.values ?? {})
let classes = values.map((value) => formatClass(utilName, value))

if (options?.supportsNegativeValues) {
// This is the normal negated version
// e.g. `-inset-1` or `-tw-inset-1`
classes = [...classes, ...classes.map((cls) => '-' + cls)]

// This is the negated version *after* the prefix
// e.g. `tw--inset-1`
// The prefix is already attached to util name
// So we add the negative after the prefix
classes = [
...classes,
...classes.map(
(cls) => cls.slice(0, prefixLength) + '-' + cls.slice(prefixLength)
),
]
}

return classes
})()
: [util]

for (let util of utils) {
Expand Down

0 comments on commit 56c320d

Please sign in to comment.