(userConfig: TwindConfig<any> | TwindUserConfig<any>, sheet: Sheet)
| 40 | ): Twind<BaseTheme & ExtractThemes<Theme, Presets>, Target> |
| 41 | |
| 42 | export 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' |
no test coverage detected