Skip to content

Commit a7afc2a

Browse files
committed
fixing things - working
1 parent d7d7ef1 commit a7afc2a

16 files changed

+262
-292
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ dist-ssr
2121
*.ntvs*
2222
*.njsproj
2323
*.sln
24-
*.sw?
24+
*.sw?
25+
yarn.lock

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@xengine/tailwindcss-class-parser",
2+
"name": "inan-tailwind-parser",
33
"private": false,
44
"version": "1.1.20",
55
"type": "module",

src/classname.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import get from 'lodash/get'
2+
3+
import { PluginNotFoundException } from './exceptions/plugin-not-found-exception'
4+
import { functionalPlugins, namedPlugins } from './plugins'
5+
import { getTailwindTheme } from './theme'
6+
import { buildModifier } from './utils/build-modifier'
7+
import { calculateHexFromString } from './utils/calculate-hex-from-string'
8+
import { findTailwindColorFromHex } from './utils/find-tailwind-color-from-hex'
9+
import { isColor } from './utils/is-color'
10+
import { StringBuilder } from './utils/string-builder'
11+
12+
export const EMPTY_CLASSNAME = ''
13+
14+
export const classname = (ast, config) => {
15+
const theme = getTailwindTheme(config)
16+
const stringBuilder = new StringBuilder()
17+
let negative = ast.negative || false
18+
stringBuilder.appendVariants(...ast.variants || [])
19+
20+
if (ast.important) {
21+
stringBuilder.makeImportant()
22+
}
23+
24+
if (ast.value[0] === '-') {
25+
ast.value = ast.value.slice(1)
26+
negative = true
27+
}
28+
29+
const [namedPluginClassName] = [...namedPlugins.entries()]
30+
.filter(([, plugin]) => plugin.ns === ast.property)
31+
.find(([, plugin]) => plugin.value === ast.value) || []
32+
33+
if (namedPluginClassName) {
34+
return stringBuilder.addRoot(namedPluginClassName).toString()
35+
}
36+
37+
const [root, possiblePlugins = []] = [...functionalPlugins.entries()].find(([, plugins]) => plugins.some(o => o.ns === ast.property)) || []
38+
if (!root) {
39+
throw new PluginNotFoundException(ast.property)
40+
}
41+
42+
stringBuilder.addRoot(root)
43+
if (isColor(ast.value, theme)) {
44+
const matchedPlugin = possiblePlugins.find(plugin => plugin.type === 'color')
45+
if (!matchedPlugin) {
46+
throw new PluginNotFoundException(ast.property)
47+
}
48+
49+
const tailwindThemeColor = get(theme[matchedPlugin.scaleKey || 'colors'], ast.value.split('-').join('.'))
50+
if (tailwindThemeColor && typeof tailwindThemeColor !== 'object') {
51+
return stringBuilder
52+
.appendModifier(buildModifier(ast.modifier, theme.opacity))
53+
.addValue(ast.value)
54+
.toString()
55+
}
56+
57+
const isRgba = ast.value.includes('rgba')
58+
if (isRgba) {
59+
return stringBuilder
60+
.addValue(findTailwindValueByUnit(ast.value))
61+
.toString()
62+
}
63+
64+
const color = calculateHexFromString(ast.value)
65+
if (!color) {
66+
return EMPTY_CLASSNAME
67+
}
68+
69+
return stringBuilder
70+
.appendModifier(buildModifier(color.alpha || ast.modifier, theme.opacity))
71+
.addValue(
72+
findTailwindColorFromHex(color.hex, theme[matchedPlugin.scaleKey || 'colors']) || StringBuilder.makeArbitrary(color.hex),
73+
)
74+
.toString()
75+
}
76+
77+
const matchedPlugin = possiblePlugins.find(plugin => plugin.ns === ast.property)
78+
if (!matchedPlugin) {
79+
throw new PluginNotFoundException(ast.property)
80+
}
81+
82+
const possibleValue = findTailwindValueByUnit(ast.value, matchedPlugin)
83+
if (possibleValue) {
84+
stringBuilder.addValue(possibleValue)
85+
86+
if (matchedPlugin.supportNegative && negative) {
87+
stringBuilder.makeNegative()
88+
}
89+
}
90+
91+
return stringBuilder.toString()
92+
}
93+
94+
const findTailwindValueByUnit = (unit, matchedPlugin = {}) => {
95+
if (!unit) {
96+
return undefined
97+
}
98+
99+
if (matchedPlugin.type === 'image') {
100+
unit = `url(http://github.com/%3Cspan%20class=pl-s1%3E%3Cspan%20class=pl-kos%3E$%7B%3C/span%3E%3Cspan%20class=pl-s1%3Eunit%3C/span%3E%3Cspan%20class=pl-kos%3E%7D%3C/span%3E%3C/span%3E)`
101+
}
102+
103+
return StringBuilder.makeArbitrary(unit)
104+
}

src/classname.ts

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {parse} from "./parse";
2-
import {classname} from "./classname";
1+
import { classname } from './classname';
2+
import { parse } from './parse';
33

4-
export {parse, classname}
4+
export { parse, classname }

src/parse-variant.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {decodeArbitraryValue} from "./utils/decodeArbitraryValue";
2-
import {type Variant, variants} from "./plugins";
3-
import type {ScreensConfig} from "tailwindcss/types/config";
1+
import { decodeArbitraryValue } from "./utils/decodeArbitraryValue";
2+
import { type Variant, variants } from "./plugins";
43

5-
export function parseVariant(variant: string, screens: ScreensConfig): Variant {
4+
export function parseVariant(variant: string, screens: any): Variant {
65
if (variant[0] === '[' && variant[variant.length - 1] === ']') {
76
let arbitraryValue = variant.slice(1, -1)
87

@@ -19,7 +18,7 @@ export function parseVariant(variant: string, screens: ScreensConfig): Variant
1918
}
2019
}
2120

22-
if(Object.keys(screens).includes(variant)) {
21+
if (Object.keys(screens).includes(variant)) {
2322
return {
2423
kind: 'named',
2524
type: 'media',

src/parse.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import {segment} from "./utils/segment";
2-
import {findRoot} from "./find-root";
3-
import {type FunctionalPlugin, functionalPlugins, namedPlugins, type Variant} from "./plugins";
4-
import {parseVariant} from "./parse-variant";
5-
import {inferDataType} from "./utils/infer-data-type";
6-
import {getValue, type Value} from "./utils/value";
7-
import type {Config, ScreensConfig} from "tailwindcss/types/config";
8-
import {getTailwindTheme} from "./theme";
9-
import {CalculateHexFromString} from "./utils/calculate-hex-from-string";
10-
import {findTailwindColorFromHex} from "./utils/find-tailwind-color-from-hex";
11-
import {buildModifier} from "./utils/build-modifier";
12-
import {isColor} from "./utils/is-color";
13-
import {decodeArbitraryValue} from "./utils/decodeArbitraryValue";
1+
import { segment } from "./utils/segment";
2+
import { findRoot } from "./find-root";
3+
import { type FunctionalPlugin, functionalPlugins, namedPlugins, type Variant } from "./plugins";
4+
import { parseVariant } from "./parse-variant";
5+
import { inferDataType } from "./utils/infer-data-type";
6+
import { getValue, type Value } from "./utils/value";
7+
import { getTailwindTheme } from "./theme";
8+
import { CalculateHexFromString } from "./utils/calculate-hex-from-string";
9+
import { findTailwindColorFromHex } from "./utils/find-tailwind-color-from-hex";
10+
import { buildModifier } from "./utils/build-modifier";
11+
import { isColor } from "./utils/is-color";
12+
import { decodeArbitraryValue } from "./utils/decodeArbitraryValue";
1413

1514
export type State = {
1615
important: boolean
@@ -36,8 +35,8 @@ export type Error = {
3635
message: string
3736
}
3837

39-
export const parse = (input: string, config?: Config): AST | Error => {
40-
if(!input) {
38+
export const parse = (input: string, config?: any): AST | Error => {
39+
if (!input) {
4140
return {
4241
root: "",
4342
kind: "error",
@@ -56,7 +55,7 @@ export const parse = (input: string, config?: Config): AST | Error => {
5655
let parsedCandidateVariants: Variant[] = []
5756

5857
for (let i = variants.length - 1; i >= 0; --i) {
59-
let parsedVariant = parseVariant(variants[i], theme.screens as ScreensConfig)
58+
let parsedVariant = parseVariant(variants[i], theme.screens as any)
6059
if (parsedVariant !== null)
6160
parsedCandidateVariants.push(parsedVariant)
6261
}
@@ -112,23 +111,23 @@ export const parse = (input: string, config?: Config): AST | Error => {
112111

113112
if (valueWithoutModifier && valueWithoutModifier[0] === '[' && valueWithoutModifier[valueWithoutModifier.length - 1] === ']') {
114113
let arbitraryValue = valueWithoutModifier.slice(1, -1)
115-
const unitType = inferDataType(arbitraryValue, [...availablePlugins.values()].map(({type}) => type))
114+
const unitType = inferDataType(arbitraryValue, [...availablePlugins.values()].map(({ type }) => type))
116115
let associatedPluginByType = availablePlugins!.find(plugin => plugin.type === unitType)
117116

118117
if (unitType === "color") {
119118
const color = CalculateHexFromString(arbitraryValue)
120-
if(!color){
119+
if (!color) {
121120
return {
122121
root: base,
123122
kind: "error",
124123
message: "Color is not correct",
125124
}
126125
}
127126
valueWithoutModifier = findTailwindColorFromHex(color.hex, theme[associatedPluginByType?.scaleKey || "colors"]) || color.hex
128-
}else{
127+
} else {
129128
//It's not color, but it's still an arbitrary. We are just going to do parse it
130129
//The result might not be correct, but it's better than nothing and even tailwind will parse it anyway
131-
if(availablePlugins.length > 0){
130+
if (availablePlugins.length > 0) {
132131
associatedPluginByType = availablePlugins.find(x => x.type === unitType) || availablePlugins.find(x => x.type !== "color")
133132
}
134133
}
@@ -159,7 +158,7 @@ export const parse = (input: string, config?: Config): AST | Error => {
159158
}
160159

161160
//check value against each scale of available plugins
162-
let matchedPlugin = availablePlugins.find(({scaleKey}) => value.split('-')[0] in theme[scaleKey] || valueWithoutModifier in theme[scaleKey])
161+
let matchedPlugin = availablePlugins.find(({ scaleKey }) => value.split('-')[0] in theme[scaleKey] || valueWithoutModifier in theme[scaleKey])
163162
if (!matchedPlugin) {
164163
return {
165164
root: base,

src/theme.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import type {Config} from "tailwindcss/types/config"
2-
import resolveConfig from 'tailwindcss/resolveConfig';
31
import memoize from 'lodash/memoize'
42

5-
6-
// fuck you TS. I don't want to deal with this, this return a Theme instance but fuck the interface they've put into it.
7-
// for now this ANY will do.
8-
// @ts-ignore IT STUBS ANYWAY
9-
export const getTailwindTheme = memoize((config: Config | undefined = {}) : any => {
10-
const parsedConfig = resolveConfig(config || {})
11-
return parsedConfig.theme
12-
})
3+
export const getTailwindTheme = memoize((config: any | undefined = {}): any => {
4+
const parsedConfig = config || {}
5+
return parsedConfig.theme || {}
6+
})

0 commit comments

Comments
 (0)