From 56c320d00491bc0409fd16170a495afa0a201dd7 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Sat, 7 May 2022 22:48:36 -0400 Subject: [PATCH] Refactor --- src/lib/setupContextUtils.js | 90 ++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/src/lib/setupContextUtils.js b/src/lib/setupContextUtils.js index f2dc6a516b2e..b915d21a830e 100644 --- a/src/lib/setupContextUtils.js +++ b/src/lib/setupContextUtils.js @@ -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 @@ -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) @@ -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) {