|
| 1 | +// @ts-check |
| 2 | +import { expiringMap } from './expiring-map.js' |
| 3 | +import clearModule from 'clear-module' |
| 4 | +import escalade from 'escalade/sync' |
| 5 | +import * as path from 'path' |
| 6 | +import prettier from 'prettier' |
| 7 | +import resolveFrom from 'resolve-from' |
| 8 | +import { generateRules as generateRulesFallback } from 'tailwindcss/lib/lib/generateRules' |
| 9 | +import { createContext as createContextFallback } from 'tailwindcss/lib/lib/setupContextUtils' |
| 10 | +import loadConfigFallback from 'tailwindcss/loadConfig' |
| 11 | +import resolveConfigFallback from 'tailwindcss/resolveConfig' |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef {object} ContextContainer |
| 15 | + * @property {any} context |
| 16 | + * @property {() => any} generateRules |
| 17 | + * @property {any} tailwindConfig |
| 18 | + **/ |
| 19 | + |
| 20 | +/** |
| 21 | + * @typedef {object} PluginOptions |
| 22 | + * @property {string} [tailwindConfig] |
| 23 | + * @property {string} filepath |
| 24 | + **/ |
| 25 | + |
| 26 | +/** |
| 27 | + * @template K |
| 28 | + * @template V |
| 29 | + * @typedef {import('./expiring-map.js').ExpiringMap<K,V>} ExpiringMap |
| 30 | + **/ |
| 31 | + |
| 32 | +/** @type {Map<string, string | null>} */ |
| 33 | +let sourceToPathMap = new Map() |
| 34 | + |
| 35 | +/** @type {ExpiringMap<string | null, ContextContainer>} */ |
| 36 | +let pathToContextMap = expiringMap(10_000) |
| 37 | + |
| 38 | +/** @type {ExpiringMap<string, string | null>} */ |
| 39 | +let prettierConfigCache = expiringMap(10_000) |
| 40 | + |
| 41 | +/** |
| 42 | + * @param {PluginOptions} options |
| 43 | + * @returns {ContextContainer} |
| 44 | + */ |
| 45 | +export function getTailwindConfig(options) { |
| 46 | + let key = `${options.filepath}:${options.tailwindConfig ?? ''}` |
| 47 | + let baseDir = getBaseDir(options) |
| 48 | + |
| 49 | + // Map the source file to it's associated Tailwind config file |
| 50 | + let configPath = sourceToPathMap.get(key) |
| 51 | + if (configPath === undefined) { |
| 52 | + configPath = getConfigPath(options, baseDir) |
| 53 | + sourceToPathMap.set(key, configPath) |
| 54 | + } |
| 55 | + |
| 56 | + // Now see if we've loaded the Tailwind config file before (and it's still valid) |
| 57 | + let existing = pathToContextMap.get(configPath) |
| 58 | + if (existing) { |
| 59 | + return existing |
| 60 | + } |
| 61 | + |
| 62 | + // By this point we know we need to load the Tailwind config file |
| 63 | + let result = loadTailwindConfig(baseDir, configPath) |
| 64 | + |
| 65 | + pathToContextMap.set(configPath, result) |
| 66 | + |
| 67 | + return result |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * |
| 72 | + * @param {PluginOptions} options |
| 73 | + * @returns {string | null} |
| 74 | + */ |
| 75 | +function getPrettierConfigPath(options) { |
| 76 | + // Locating the config file can be mildly expensive so we cache it temporarily |
| 77 | + let existingPath = prettierConfigCache.get(options.filepath) |
| 78 | + if (existingPath !== undefined) { |
| 79 | + return existingPath |
| 80 | + } |
| 81 | + |
| 82 | + let path = prettier.resolveConfigFile.sync(options.filepath) |
| 83 | + prettierConfigCache.set(options.filepath, path) |
| 84 | + |
| 85 | + return path |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param {PluginOptions} options |
| 90 | + * @returns {string} |
| 91 | + */ |
| 92 | +function getBaseDir(options) { |
| 93 | + let prettierConfigPath = getPrettierConfigPath(options) |
| 94 | + |
| 95 | + if (options.tailwindConfig) { |
| 96 | + return prettierConfigPath ? path.dirname(prettierConfigPath) : process.cwd() |
| 97 | + } |
| 98 | + |
| 99 | + return prettierConfigPath |
| 100 | + ? path.dirname(prettierConfigPath) |
| 101 | + : options.filepath |
| 102 | + ? path.dirname(options.filepath) |
| 103 | + : process.cwd() |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * |
| 108 | + * @param {string} baseDir |
| 109 | + * @param {string | null} tailwindConfigPath |
| 110 | + * @returns {ContextContainer} |
| 111 | + */ |
| 112 | +function loadTailwindConfig(baseDir, tailwindConfigPath) { |
| 113 | + let createContext = createContextFallback |
| 114 | + let generateRules = generateRulesFallback |
| 115 | + let resolveConfig = resolveConfigFallback |
| 116 | + let loadConfig = loadConfigFallback |
| 117 | + let tailwindConfig = {} |
| 118 | + |
| 119 | + try { |
| 120 | + let pkgDir = path.dirname(resolveFrom(baseDir, 'tailwindcss/package.json')) |
| 121 | + |
| 122 | + resolveConfig = require(path.join(pkgDir, 'resolveConfig')) |
| 123 | + createContext = require(path.join( |
| 124 | + pkgDir, |
| 125 | + 'lib/lib/setupContextUtils', |
| 126 | + )).createContext |
| 127 | + generateRules = require(path.join( |
| 128 | + pkgDir, |
| 129 | + 'lib/lib/generateRules', |
| 130 | + )).generateRules |
| 131 | + |
| 132 | + // Prior to `[email protected]` this won't exist so we load it last |
| 133 | + loadConfig = require(path.join(pkgDir, 'loadConfig')) |
| 134 | + } catch {} |
| 135 | + |
| 136 | + if (tailwindConfigPath) { |
| 137 | + clearModule(tailwindConfigPath) |
| 138 | + const loadedConfig = loadConfig(tailwindConfigPath) |
| 139 | + tailwindConfig = loadedConfig.default ?? loadedConfig |
| 140 | + } |
| 141 | + |
| 142 | + // suppress "empty content" warning |
| 143 | + tailwindConfig.content = ['no-op'] |
| 144 | + |
| 145 | + // Create the context |
| 146 | + let context = createContext(resolveConfig(tailwindConfig)) |
| 147 | + |
| 148 | + return { |
| 149 | + context, |
| 150 | + tailwindConfig, |
| 151 | + generateRules, |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * @param {PluginOptions} options |
| 157 | + * @param {string} baseDir |
| 158 | + * @returns {string | null} |
| 159 | + */ |
| 160 | +function getConfigPath(options, baseDir) { |
| 161 | + if (options.tailwindConfig) { |
| 162 | + return path.resolve(baseDir, options.tailwindConfig) |
| 163 | + } |
| 164 | + |
| 165 | + let configPath |
| 166 | + try { |
| 167 | + configPath = escalade(baseDir, (_dir, names) => { |
| 168 | + if (names.includes('tailwind.config.js')) { |
| 169 | + return 'tailwind.config.js' |
| 170 | + } |
| 171 | + if (names.includes('tailwind.config.cjs')) { |
| 172 | + return 'tailwind.config.cjs' |
| 173 | + } |
| 174 | + if (names.includes('tailwind.config.mjs')) { |
| 175 | + return 'tailwind.config.mjs' |
| 176 | + } |
| 177 | + if (names.includes('tailwind.config.ts')) { |
| 178 | + return 'tailwind.config.ts' |
| 179 | + } |
| 180 | + }) |
| 181 | + } catch {} |
| 182 | + |
| 183 | + if (configPath) { |
| 184 | + return configPath |
| 185 | + } |
| 186 | + |
| 187 | + return null |
| 188 | +} |
0 commit comments