MCPcopy
hub / github.com/tw-in-js/twind / twind

Function twind

packages/core/src/twind.ts:42–164  ·  view source on GitHub ↗
(userConfig: TwindConfig<any> | TwindUserConfig<any>, sheet: Sheet)

Source from the content-addressed store, hash-verified

40): Twind<BaseTheme & ExtractThemes<Theme, Presets>, Target>
41
42export function twind(userConfig: TwindConfig<any> | TwindUserConfig<any>, sheet: Sheet): Twind {
43 const config = defineConfig(userConfig as TwindUserConfig<any>)
44
45 const context = createContext(config)
46
47 // Map of tokens to generated className
48 let cache = new Map<string, string>()
49
50 // An array of precedence by index within the sheet
51 // always sorted
52 let sortedPrecedences: SortableRule[] = []
53
54 // Cache for already inserted css rules
55 // to prevent double insertions
56 let insertedRules = new Set<string>()
57
58 sheet.resume(
59 (className) => cache.set(className, className),
60 (cssText, rule) => {
61 sheet.insert(cssText, sortedPrecedences.length, rule)
62 sortedPrecedences.push(rule)
63 insertedRules.add(cssText)
64 },
65 )
66
67 function insert(rule: TwindRule): string | undefined {
68 const finalRule = context.f(rule)
69
70 const cssText = stringify(finalRule)
71
72 // If not already inserted
73 if (cssText && !insertedRules.has(cssText)) {
74 // Mark rule as inserted
75 insertedRules.add(cssText)
76
77 // Find the correct position
78 const index = sortedInsertionIndex(sortedPrecedences, rule)
79
80 // Insert
81 sheet.insert(cssText, index, rule)
82
83 // Update sorted index
84 sortedPrecedences.splice(index, 0, rule)
85 }
86
87 return finalRule.n
88 }
89
90 return Object.defineProperties(
91 function tw(tokens) {
92 if (!cache.size) {
93 for (let preflight of asArray(config.preflight)) {
94 if (typeof preflight == 'function') {
95 preflight = preflight(context)
96 }
97
98 if (preflight) {
99 ;(typeof preflight == 'string'

Callers 15

+server.tsFile · 0.90
+server.tsFile · 0.90
+server.tsFile · 0.90
+server.tsFile · 0.90
rules.test.tsFile · 0.90
preflight.test.tsFile · 0.90
typography.test.tsFile · 0.90
installFunction · 0.90
preset-ext.test.tsFile · 0.90
setupFunction · 0.90

Calls 8

defineConfigFunction · 0.90
createContextFunction · 0.90
asArrayFunction · 0.90
translateWithFunction · 0.90
parseFunction · 0.90
serializeFunction · 0.90
translateFunction · 0.90
insertFunction · 0.70

Tested by

no test coverage detected