From d256d5388ec7d26218d2c6e5f871b784e067ebba Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 5 Nov 2021 16:13:22 +0000 Subject: [PATCH] Update variant API for compatibility with `v3-alpha.2` --- .../tailwindcss-language-server/src/server.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss-language-server/src/server.ts b/packages/tailwindcss-language-server/src/server.ts index 9fbdd0b..426a1c6 100644 --- a/packages/tailwindcss-language-server/src/server.ts +++ b/packages/tailwindcss-language-server/src/server.ts @@ -39,7 +39,7 @@ import chokidar, { FSWatcher } from 'chokidar' import findUp from 'find-up' import minimatch from 'minimatch' import resolveFrom, { setPnpApi } from './util/resolveFrom' -import { /*postcssFallback,*/ Result } from 'postcss' +import { /*postcssFallback,*/ AtRule, Container, Node, Result } from 'postcss' // import tailwindcssFallback from 'tailwindcss' // import resolveConfigFallback from 'tailwindcss/resolveConfig' import Module from 'module' @@ -1096,6 +1096,10 @@ function runPlugin( } catch (_) {} } +function isAtRule(node: Node): node is AtRule { + return node.type === 'atrule' +} + function getVariants(state: State): Record { if (state.jit) { function escape(className: string): string { @@ -1153,18 +1157,36 @@ function getVariants(state: State): Record { let definitions = [] for (let fn of fns) { + let definition: string let container = root.clone() - fn({ + let returnValue = fn({ container, separator: state.separator, modifySelectors, + format: (def: string) => { + definition = def.replace(/:merge\(([^)]+)\)/g, '$1') + }, + wrap: (rule: Container) => { + if (isAtRule(rule)) { + definition = `@${rule.name} ${rule.params}` + } + }, }) + if (!definition) { + definition = returnValue + } + + if (definition) { + definitions.push(definition) + continue + } + container.walkDecls((decl) => { decl.remove() }) - let definition = container + definition = container .toString() .replace(`.${escape(`${variantName}:${placeholder}`)}`, '&') .replace(/(?