|
| 1 | +// @ts-check |
| 2 | +import * as path from 'path' |
| 3 | +import clearModule from 'clear-module' |
| 4 | +// @ts-ignore |
| 5 | +import { generateRules as generateRulesFallback } from 'tailwindcss/lib/lib/generateRules' |
| 6 | +// @ts-ignore |
| 7 | +import { createContext as createContextFallback } from 'tailwindcss/lib/lib/setupContextUtils' |
| 8 | +import loadConfigFallback from 'tailwindcss/loadConfig' |
| 9 | +import resolveConfigFallback from 'tailwindcss/resolveConfig' |
| 10 | +import type { RequiredConfig } from 'tailwindcss/types/config.js' |
| 11 | +import type { UnifiedApi } from '../types' |
| 12 | +import { bigSign } from '../utils' |
| 13 | + |
| 14 | +interface LegacyTailwindContext { |
| 15 | + tailwindConfig: { |
| 16 | + prefix: string | ((selector: string) => string) |
| 17 | + } |
| 18 | + |
| 19 | + getClassOrder?: (classList: string[]) => [string, bigint | null][] |
| 20 | + |
| 21 | + layerOrder: { |
| 22 | + components: bigint |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +interface GenerateRules { |
| 27 | + (classes: Iterable<string>, context: LegacyTailwindContext): [bigint][] |
| 28 | +} |
| 29 | + |
| 30 | +function prefixCandidate(context: LegacyTailwindContext, selector: string): string { |
| 31 | + let prefix = context.tailwindConfig.prefix |
| 32 | + return typeof prefix === 'function' ? prefix(selector) : prefix + selector |
| 33 | +} |
| 34 | + |
| 35 | +export async function loadV3(pkgDir: string | null, jsConfig: string | null): Promise<UnifiedApi> { |
| 36 | + let createContext = createContextFallback |
| 37 | + let generateRules: GenerateRules = generateRulesFallback |
| 38 | + let resolveConfig = resolveConfigFallback |
| 39 | + let loadConfig = loadConfigFallback |
| 40 | + let tailwindConfig: RequiredConfig = { content: [] } |
| 41 | + |
| 42 | + try { |
| 43 | + if (pkgDir) { |
| 44 | + resolveConfig = require(path.join(pkgDir, 'resolveConfig')) |
| 45 | + createContext = require(path.join(pkgDir, 'lib/lib/setupContextUtils')).createContext |
| 46 | + generateRules = require(path.join(pkgDir, 'lib/lib/generateRules')).generateRules |
| 47 | + // Prior to `[email protected]` this won't exist so we load it last |
| 48 | + loadConfig = require(path.join(pkgDir, 'loadConfig')) |
| 49 | + } |
| 50 | + } catch {} |
| 51 | + |
| 52 | + try { |
| 53 | + if (jsConfig) { |
| 54 | + clearModule(jsConfig) |
| 55 | + let loadedConfig = loadConfig(jsConfig) |
| 56 | + tailwindConfig = loadedConfig.default ?? loadedConfig |
| 57 | + } |
| 58 | + } catch (err) { |
| 59 | + console.error(`Unable to load your Tailwind CSS v3 config: ${jsConfig}`) |
| 60 | + throw err |
| 61 | + } |
| 62 | + |
| 63 | + // suppress "empty content" warning |
| 64 | + tailwindConfig.content = ['no-op'] |
| 65 | + |
| 66 | + // Create the context |
| 67 | + let context: LegacyTailwindContext = createContext(resolveConfig(tailwindConfig)) |
| 68 | + |
| 69 | + // Polyfill for older Tailwind CSS versions |
| 70 | + function getClassOrderPolyfill(classes: string[]): [string, bigint | null][] { |
| 71 | + // A list of utilities that are used by certain Tailwind CSS utilities but |
| 72 | + // that don't exist on their own. This will result in them "not existing" and |
| 73 | + // sorting could be weird since you still require them in order to make the |
| 74 | + // host utitlies work properly. (Thanks Biology) |
| 75 | + let parasiteUtilities = new Set([prefixCandidate(context, 'group'), prefixCandidate(context, 'peer')]) |
| 76 | + |
| 77 | + let classNamesWithOrder: [string, bigint | null][] = [] |
| 78 | + |
| 79 | + for (let className of classes) { |
| 80 | + let order: bigint | null = |
| 81 | + generateRules(new Set([className]), context).sort(([a], [z]) => bigSign(z - a))[0]?.[0] ?? null |
| 82 | + |
| 83 | + if (order === null && parasiteUtilities.has(className)) { |
| 84 | + // This will make sure that it is at the very beginning of the |
| 85 | + // `components` layer which technically means 'before any |
| 86 | + // components'. |
| 87 | + order = context.layerOrder.components |
| 88 | + } |
| 89 | + |
| 90 | + classNamesWithOrder.push([className, order]) |
| 91 | + } |
| 92 | + |
| 93 | + return classNamesWithOrder |
| 94 | + } |
| 95 | + |
| 96 | + context.getClassOrder ??= getClassOrderPolyfill |
| 97 | + |
| 98 | + return { |
| 99 | + getClassOrder: (classList: string[]) => { |
| 100 | + return context.getClassOrder ? context.getClassOrder(classList) : getClassOrderPolyfill(classList) |
| 101 | + }, |
| 102 | + } |
| 103 | +} |
0 commit comments