tailwindcss 3.4.0 → 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. package/dist/chunk-GNCUPSHB.mjs +5886 -0
  2. package/dist/cli.d.mts +1 -0
  3. package/dist/cli.d.ts +1 -0
  4. package/dist/cli.js +6309 -0
  5. package/dist/cli.mjs +432 -0
  6. package/dist/lib.d.mts +324 -0
  7. package/dist/lib.d.ts +324 -0
  8. package/dist/lib.js +5884 -0
  9. package/dist/lib.mjs +1 -0
  10. package/index.css +5 -0
  11. package/package.json +35 -104
  12. package/preflight.css +355 -0
  13. package/theme.css +465 -0
  14. package/CHANGELOG.md +0 -2552
  15. package/README.md +0 -41
  16. package/base.css +0 -1
  17. package/colors.d.ts +0 -3
  18. package/colors.js +0 -2
  19. package/components.css +0 -1
  20. package/defaultConfig.d.ts +0 -3
  21. package/defaultConfig.js +0 -2
  22. package/defaultTheme.d.ts +0 -4
  23. package/defaultTheme.js +0 -2
  24. package/lib/cli/build/deps.js +0 -62
  25. package/lib/cli/build/index.js +0 -54
  26. package/lib/cli/build/plugin.js +0 -378
  27. package/lib/cli/build/utils.js +0 -88
  28. package/lib/cli/build/watching.js +0 -182
  29. package/lib/cli/help/index.js +0 -73
  30. package/lib/cli/index.js +0 -230
  31. package/lib/cli/init/index.js +0 -63
  32. package/lib/cli-peer-dependencies.js +0 -36
  33. package/lib/cli.js +0 -7
  34. package/lib/corePluginList.js +0 -190
  35. package/lib/corePlugins.js +0 -4244
  36. package/lib/css/LICENSE +0 -25
  37. package/lib/css/preflight.css +0 -385
  38. package/lib/featureFlags.js +0 -83
  39. package/lib/index.js +0 -2
  40. package/lib/lib/cacheInvalidation.js +0 -92
  41. package/lib/lib/collapseAdjacentRules.js +0 -61
  42. package/lib/lib/collapseDuplicateDeclarations.js +0 -85
  43. package/lib/lib/content.js +0 -181
  44. package/lib/lib/defaultExtractor.js +0 -251
  45. package/lib/lib/detectNesting.js +0 -45
  46. package/lib/lib/evaluateTailwindFunctions.js +0 -238
  47. package/lib/lib/expandApplyAtRules.js +0 -540
  48. package/lib/lib/expandTailwindAtRules.js +0 -291
  49. package/lib/lib/findAtConfigPath.js +0 -46
  50. package/lib/lib/generateRules.js +0 -896
  51. package/lib/lib/getModuleDependencies.js +0 -99
  52. package/lib/lib/load-config.js +0 -53
  53. package/lib/lib/normalizeTailwindDirectives.js +0 -89
  54. package/lib/lib/offsets.js +0 -306
  55. package/lib/lib/partitionApplyAtRules.js +0 -58
  56. package/lib/lib/regex.js +0 -74
  57. package/lib/lib/remap-bitfield.js +0 -89
  58. package/lib/lib/resolveDefaultsAtRules.js +0 -165
  59. package/lib/lib/setupContextUtils.js +0 -1278
  60. package/lib/lib/setupTrackingContext.js +0 -166
  61. package/lib/lib/sharedState.js +0 -87
  62. package/lib/lib/substituteScreenAtRules.js +0 -31
  63. package/lib/oxide/cli/build/deps.js +0 -89
  64. package/lib/oxide/cli/build/index.js +0 -53
  65. package/lib/oxide/cli/build/plugin.js +0 -375
  66. package/lib/oxide/cli/build/utils.js +0 -87
  67. package/lib/oxide/cli/build/watching.js +0 -179
  68. package/lib/oxide/cli/help/index.js +0 -72
  69. package/lib/oxide/cli/index.js +0 -214
  70. package/lib/oxide/cli/init/index.js +0 -52
  71. package/lib/oxide/cli.js +0 -5
  72. package/lib/oxide/postcss-plugin.js +0 -2
  73. package/lib/plugin.js +0 -98
  74. package/lib/postcss-plugins/nesting/README.md +0 -42
  75. package/lib/postcss-plugins/nesting/index.js +0 -21
  76. package/lib/postcss-plugins/nesting/plugin.js +0 -89
  77. package/lib/processTailwindFeatures.js +0 -64
  78. package/lib/public/colors.js +0 -355
  79. package/lib/public/create-plugin.js +0 -17
  80. package/lib/public/default-config.js +0 -18
  81. package/lib/public/default-theme.js +0 -18
  82. package/lib/public/load-config.js +0 -12
  83. package/lib/public/resolve-config.js +0 -24
  84. package/lib/util/applyImportantSelector.js +0 -36
  85. package/lib/util/bigSign.js +0 -13
  86. package/lib/util/buildMediaQuery.js +0 -27
  87. package/lib/util/cloneDeep.js +0 -22
  88. package/lib/util/cloneNodes.js +0 -54
  89. package/lib/util/color.js +0 -116
  90. package/lib/util/colorNames.js +0 -752
  91. package/lib/util/configurePlugins.js +0 -23
  92. package/lib/util/createPlugin.js +0 -32
  93. package/lib/util/createUtilityPlugin.js +0 -53
  94. package/lib/util/dataTypes.js +0 -405
  95. package/lib/util/defaults.js +0 -27
  96. package/lib/util/escapeClassName.js +0 -24
  97. package/lib/util/escapeCommas.js +0 -13
  98. package/lib/util/flattenColorPalette.js +0 -18
  99. package/lib/util/formatVariantSelector.js +0 -270
  100. package/lib/util/getAllConfigs.js +0 -50
  101. package/lib/util/hashConfig.js +0 -21
  102. package/lib/util/isKeyframeRule.js +0 -13
  103. package/lib/util/isPlainObject.js +0 -17
  104. package/lib/util/isSyntacticallyValidPropertyValue.js +0 -74
  105. package/lib/util/log.js +0 -61
  106. package/lib/util/nameClass.js +0 -49
  107. package/lib/util/negateValue.js +0 -36
  108. package/lib/util/normalizeConfig.js +0 -282
  109. package/lib/util/normalizeScreens.js +0 -178
  110. package/lib/util/parseAnimationValue.js +0 -93
  111. package/lib/util/parseBoxShadowValue.js +0 -88
  112. package/lib/util/parseDependency.js +0 -47
  113. package/lib/util/parseGlob.js +0 -36
  114. package/lib/util/parseObjectStyles.js +0 -36
  115. package/lib/util/pluginUtils.js +0 -289
  116. package/lib/util/prefixSelector.js +0 -39
  117. package/lib/util/pseudoElements.js +0 -209
  118. package/lib/util/removeAlphaVariables.js +0 -31
  119. package/lib/util/resolveConfig.js +0 -256
  120. package/lib/util/resolveConfigPath.js +0 -70
  121. package/lib/util/responsive.js +0 -24
  122. package/lib/util/splitAtTopLevelOnly.js +0 -51
  123. package/lib/util/tap.js +0 -14
  124. package/lib/util/toColorValue.js +0 -13
  125. package/lib/util/toPath.js +0 -32
  126. package/lib/util/transformThemeValue.js +0 -73
  127. package/lib/util/validateConfig.js +0 -37
  128. package/lib/util/validateFormalSyntax.js +0 -26
  129. package/lib/util/withAlphaVariable.js +0 -79
  130. package/lib/value-parser/LICENSE +0 -22
  131. package/lib/value-parser/README.md +0 -3
  132. package/lib/value-parser/index.d.js +0 -2
  133. package/lib/value-parser/index.js +0 -22
  134. package/lib/value-parser/parse.js +0 -259
  135. package/lib/value-parser/stringify.js +0 -38
  136. package/lib/value-parser/unit.js +0 -86
  137. package/lib/value-parser/walk.js +0 -16
  138. package/loadConfig.d.ts +0 -4
  139. package/loadConfig.js +0 -2
  140. package/nesting/index.d.ts +0 -4
  141. package/nesting/index.js +0 -2
  142. package/peers/index.js +0 -96624
  143. package/plugin.d.ts +0 -11
  144. package/plugin.js +0 -2
  145. package/prettier.config.js +0 -19
  146. package/resolveConfig.d.ts +0 -31
  147. package/resolveConfig.js +0 -2
  148. package/screens.css +0 -1
  149. package/scripts/create-plugin-list.js +0 -10
  150. package/scripts/generate-types.js +0 -104
  151. package/scripts/release-channel.js +0 -18
  152. package/scripts/release-notes.js +0 -21
  153. package/scripts/swap-engines.js +0 -40
  154. package/scripts/type-utils.js +0 -27
  155. package/src/cli/build/deps.js +0 -56
  156. package/src/cli/build/index.js +0 -49
  157. package/src/cli/build/plugin.js +0 -444
  158. package/src/cli/build/utils.js +0 -76
  159. package/src/cli/build/watching.js +0 -229
  160. package/src/cli/help/index.js +0 -70
  161. package/src/cli/index.js +0 -216
  162. package/src/cli/init/index.js +0 -79
  163. package/src/cli-peer-dependencies.js +0 -15
  164. package/src/cli.js +0 -7
  165. package/src/corePluginList.js +0 -1
  166. package/src/corePlugins.js +0 -2904
  167. package/src/css/LICENSE +0 -25
  168. package/src/css/preflight.css +0 -385
  169. package/src/featureFlags.js +0 -66
  170. package/src/index.js +0 -1
  171. package/src/lib/cacheInvalidation.js +0 -52
  172. package/src/lib/collapseAdjacentRules.js +0 -58
  173. package/src/lib/collapseDuplicateDeclarations.js +0 -93
  174. package/src/lib/content.js +0 -208
  175. package/src/lib/defaultExtractor.js +0 -230
  176. package/src/lib/detectNesting.js +0 -47
  177. package/src/lib/evaluateTailwindFunctions.js +0 -272
  178. package/src/lib/expandApplyAtRules.js +0 -620
  179. package/src/lib/expandTailwindAtRules.js +0 -300
  180. package/src/lib/findAtConfigPath.js +0 -48
  181. package/src/lib/generateRules.js +0 -941
  182. package/src/lib/getModuleDependencies.js +0 -79
  183. package/src/lib/load-config.ts +0 -39
  184. package/src/lib/normalizeTailwindDirectives.js +0 -84
  185. package/src/lib/offsets.js +0 -373
  186. package/src/lib/partitionApplyAtRules.js +0 -52
  187. package/src/lib/regex.js +0 -74
  188. package/src/lib/remap-bitfield.js +0 -82
  189. package/src/lib/resolveDefaultsAtRules.js +0 -163
  190. package/src/lib/setupContextUtils.js +0 -1345
  191. package/src/lib/setupTrackingContext.js +0 -169
  192. package/src/lib/sharedState.js +0 -61
  193. package/src/lib/substituteScreenAtRules.js +0 -19
  194. package/src/oxide/cli/build/deps.ts +0 -91
  195. package/src/oxide/cli/build/index.ts +0 -47
  196. package/src/oxide/cli/build/plugin.ts +0 -442
  197. package/src/oxide/cli/build/utils.ts +0 -74
  198. package/src/oxide/cli/build/watching.ts +0 -225
  199. package/src/oxide/cli/help/index.ts +0 -69
  200. package/src/oxide/cli/index.ts +0 -204
  201. package/src/oxide/cli/init/index.ts +0 -59
  202. package/src/oxide/cli.ts +0 -1
  203. package/src/oxide/postcss-plugin.ts +0 -1
  204. package/src/plugin.js +0 -107
  205. package/src/postcss-plugins/nesting/README.md +0 -42
  206. package/src/postcss-plugins/nesting/index.js +0 -13
  207. package/src/postcss-plugins/nesting/plugin.js +0 -80
  208. package/src/processTailwindFeatures.js +0 -59
  209. package/src/public/colors.js +0 -322
  210. package/src/public/create-plugin.js +0 -2
  211. package/src/public/default-config.js +0 -4
  212. package/src/public/default-theme.js +0 -4
  213. package/src/public/load-config.js +0 -2
  214. package/src/public/resolve-config.js +0 -7
  215. package/src/util/applyImportantSelector.js +0 -27
  216. package/src/util/bigSign.js +0 -3
  217. package/src/util/buildMediaQuery.js +0 -22
  218. package/src/util/cloneDeep.js +0 -11
  219. package/src/util/cloneNodes.js +0 -49
  220. package/src/util/color.js +0 -88
  221. package/src/util/colorNames.js +0 -150
  222. package/src/util/configurePlugins.js +0 -23
  223. package/src/util/createPlugin.js +0 -27
  224. package/src/util/createUtilityPlugin.js +0 -37
  225. package/src/util/dataTypes.js +0 -394
  226. package/src/util/defaults.js +0 -17
  227. package/src/util/escapeClassName.js +0 -8
  228. package/src/util/escapeCommas.js +0 -3
  229. package/src/util/flattenColorPalette.js +0 -13
  230. package/src/util/formatVariantSelector.js +0 -324
  231. package/src/util/getAllConfigs.js +0 -38
  232. package/src/util/hashConfig.js +0 -5
  233. package/src/util/isKeyframeRule.js +0 -3
  234. package/src/util/isPlainObject.js +0 -8
  235. package/src/util/isSyntacticallyValidPropertyValue.js +0 -61
  236. package/src/util/log.js +0 -29
  237. package/src/util/nameClass.js +0 -30
  238. package/src/util/negateValue.js +0 -24
  239. package/src/util/normalizeConfig.js +0 -301
  240. package/src/util/normalizeScreens.js +0 -140
  241. package/src/util/parseAnimationValue.js +0 -68
  242. package/src/util/parseBoxShadowValue.js +0 -72
  243. package/src/util/parseDependency.js +0 -44
  244. package/src/util/parseGlob.js +0 -24
  245. package/src/util/parseObjectStyles.js +0 -19
  246. package/src/util/pluginUtils.js +0 -307
  247. package/src/util/prefixSelector.js +0 -33
  248. package/src/util/pseudoElements.js +0 -167
  249. package/src/util/removeAlphaVariables.js +0 -24
  250. package/src/util/resolveConfig.js +0 -277
  251. package/src/util/resolveConfigPath.js +0 -66
  252. package/src/util/responsive.js +0 -10
  253. package/src/util/splitAtTopLevelOnly.js +0 -52
  254. package/src/util/tap.js +0 -4
  255. package/src/util/toColorValue.js +0 -3
  256. package/src/util/toPath.js +0 -26
  257. package/src/util/transformThemeValue.js +0 -62
  258. package/src/util/validateConfig.js +0 -26
  259. package/src/util/validateFormalSyntax.js +0 -34
  260. package/src/util/withAlphaVariable.js +0 -49
  261. package/src/value-parser/LICENSE +0 -22
  262. package/src/value-parser/README.md +0 -3
  263. package/src/value-parser/index.d.ts +0 -177
  264. package/src/value-parser/index.js +0 -28
  265. package/src/value-parser/parse.js +0 -303
  266. package/src/value-parser/stringify.js +0 -41
  267. package/src/value-parser/unit.js +0 -118
  268. package/src/value-parser/walk.js +0 -18
  269. package/stubs/.gitignore +0 -1
  270. package/stubs/.prettierrc.json +0 -6
  271. package/stubs/config.full.js +0 -1062
  272. package/stubs/config.simple.js +0 -7
  273. package/stubs/postcss.config.cjs +0 -6
  274. package/stubs/postcss.config.js +0 -6
  275. package/stubs/tailwind.config.cjs +0 -2
  276. package/stubs/tailwind.config.js +0 -2
  277. package/stubs/tailwind.config.ts +0 -3
  278. package/tailwind.css +0 -5
  279. package/types/config.d.ts +0 -369
  280. package/types/generated/.gitkeep +0 -0
  281. package/types/generated/colors.d.ts +0 -298
  282. package/types/generated/corePluginList.d.ts +0 -1
  283. package/types/generated/default-theme.d.ts +0 -397
  284. package/types/index.d.ts +0 -11
  285. package/variants.css +0 -1
@@ -1,1345 +0,0 @@
1
- import fs from 'fs'
2
- import url from 'url'
3
- import postcss from 'postcss'
4
- import dlv from 'dlv'
5
- import selectorParser from 'postcss-selector-parser'
6
-
7
- import transformThemeValue from '../util/transformThemeValue'
8
- import parseObjectStyles from '../util/parseObjectStyles'
9
- import prefixSelector from '../util/prefixSelector'
10
- import isPlainObject from '../util/isPlainObject'
11
- import escapeClassName from '../util/escapeClassName'
12
- import nameClass, { formatClass } from '../util/nameClass'
13
- import { coerceValue } from '../util/pluginUtils'
14
- import { variantPlugins, corePlugins } from '../corePlugins'
15
- import * as sharedState from './sharedState'
16
- import { env } from './sharedState'
17
- import { toPath } from '../util/toPath'
18
- import log from '../util/log'
19
- import negateValue from '../util/negateValue'
20
- import isSyntacticallyValidPropertyValue from '../util/isSyntacticallyValidPropertyValue'
21
- import { generateRules, getClassNameFromSelector } from './generateRules'
22
- import { hasContentChanged } from './cacheInvalidation.js'
23
- import { Offsets } from './offsets.js'
24
- import { flagEnabled } from '../featureFlags.js'
25
- import { finalizeSelector, formatVariantSelector } from '../util/formatVariantSelector'
26
-
27
- export const INTERNAL_FEATURES = Symbol()
28
-
29
- const VARIANT_TYPES = {
30
- AddVariant: Symbol.for('ADD_VARIANT'),
31
- MatchVariant: Symbol.for('MATCH_VARIANT'),
32
- }
33
-
34
- const VARIANT_INFO = {
35
- Base: 1 << 0,
36
- Dynamic: 1 << 1,
37
- }
38
-
39
- function prefix(context, selector) {
40
- let prefix = context.tailwindConfig.prefix
41
- return typeof prefix === 'function' ? prefix(selector) : prefix + selector
42
- }
43
-
44
- function normalizeOptionTypes({ type = 'any', ...options }) {
45
- let types = [].concat(type)
46
-
47
- return {
48
- ...options,
49
- types: types.map((type) => {
50
- if (Array.isArray(type)) {
51
- return { type: type[0], ...type[1] }
52
- }
53
- return { type, preferOnConflict: false }
54
- }),
55
- }
56
- }
57
-
58
- function parseVariantFormatString(input) {
59
- /** @type {string[]} */
60
- let parts = []
61
-
62
- // When parsing whitespace around special characters are insignificant
63
- // However, _inside_ of a variant they could be
64
- // Because the selector could look like this
65
- // @media { &[data-name="foo bar"] }
66
- // This is why we do not skip whitespace
67
-
68
- let current = ''
69
- let depth = 0
70
-
71
- for (let idx = 0; idx < input.length; idx++) {
72
- let char = input[idx]
73
-
74
- if (char === '\\') {
75
- // Escaped characters are not special
76
- current += '\\' + input[++idx]
77
- } else if (char === '{') {
78
- // Nested rule: start
79
- ++depth
80
- parts.push(current.trim())
81
- current = ''
82
- } else if (char === '}') {
83
- // Nested rule: end
84
- if (--depth < 0) {
85
- throw new Error(`Your { and } are unbalanced.`)
86
- }
87
-
88
- parts.push(current.trim())
89
- current = ''
90
- } else {
91
- // Normal character
92
- current += char
93
- }
94
- }
95
-
96
- if (current.length > 0) {
97
- parts.push(current.trim())
98
- }
99
-
100
- parts = parts.filter((part) => part !== '')
101
-
102
- return parts
103
- }
104
-
105
- function insertInto(list, value, { before = [] } = {}) {
106
- before = [].concat(before)
107
-
108
- if (before.length <= 0) {
109
- list.push(value)
110
- return
111
- }
112
-
113
- let idx = list.length - 1
114
- for (let other of before) {
115
- let iidx = list.indexOf(other)
116
- if (iidx === -1) continue
117
- idx = Math.min(idx, iidx)
118
- }
119
-
120
- list.splice(idx, 0, value)
121
- }
122
-
123
- function parseStyles(styles) {
124
- if (!Array.isArray(styles)) {
125
- return parseStyles([styles])
126
- }
127
-
128
- return styles.flatMap((style) => {
129
- let isNode = !Array.isArray(style) && !isPlainObject(style)
130
- return isNode ? style : parseObjectStyles(style)
131
- })
132
- }
133
-
134
- function getClasses(selector, mutate) {
135
- let parser = selectorParser((selectors) => {
136
- let allClasses = []
137
-
138
- if (mutate) {
139
- mutate(selectors)
140
- }
141
-
142
- selectors.walkClasses((classNode) => {
143
- allClasses.push(classNode.value)
144
- })
145
-
146
- return allClasses
147
- })
148
- return parser.transformSync(selector)
149
- }
150
-
151
- /**
152
- * Ignore everything inside a :not(...). This allows you to write code like
153
- * `div:not(.foo)`. If `.foo` is never found in your code, then we used to
154
- * not generated it. But now we will ignore everything inside a `:not`, so
155
- * that it still gets generated.
156
- *
157
- * @param {selectorParser.Root} selectors
158
- */
159
- function ignoreNot(selectors) {
160
- selectors.walkPseudos((pseudo) => {
161
- if (pseudo.value === ':not') {
162
- pseudo.remove()
163
- }
164
- })
165
- }
166
-
167
- function extractCandidates(node, state = { containsNonOnDemandable: false }, depth = 0) {
168
- let classes = []
169
- let selectors = []
170
-
171
- if (node.type === 'rule') {
172
- // Handle normal rules
173
- selectors.push(...node.selectors)
174
- } else if (node.type === 'atrule') {
175
- // Handle at-rules (which contains nested rules)
176
- node.walkRules((rule) => selectors.push(...rule.selectors))
177
- }
178
-
179
- for (let selector of selectors) {
180
- let classCandidates = getClasses(selector, ignoreNot)
181
-
182
- // At least one of the selectors contains non-"on-demandable" candidates.
183
- if (classCandidates.length === 0) {
184
- state.containsNonOnDemandable = true
185
- }
186
-
187
- for (let classCandidate of classCandidates) {
188
- classes.push(classCandidate)
189
- }
190
- }
191
-
192
- if (depth === 0) {
193
- return [state.containsNonOnDemandable || classes.length === 0, classes]
194
- }
195
-
196
- return classes
197
- }
198
-
199
- function withIdentifiers(styles) {
200
- return parseStyles(styles).flatMap((node) => {
201
- let nodeMap = new Map()
202
- let [containsNonOnDemandableSelectors, candidates] = extractCandidates(node)
203
-
204
- // If this isn't "on-demandable", assign it a universal candidate to always include it.
205
- if (containsNonOnDemandableSelectors) {
206
- candidates.unshift(sharedState.NOT_ON_DEMAND)
207
- }
208
-
209
- // However, it could be that it also contains "on-demandable" candidates.
210
- // E.g.: `span, .foo {}`, in that case it should still be possible to use
211
- // `@apply foo` for example.
212
- return candidates.map((c) => {
213
- if (!nodeMap.has(node)) {
214
- nodeMap.set(node, node)
215
- }
216
- return [c, nodeMap.get(node)]
217
- })
218
- })
219
- }
220
-
221
- export function isValidVariantFormatString(format) {
222
- return format.startsWith('@') || format.includes('&')
223
- }
224
-
225
- export function parseVariant(variant) {
226
- variant = variant
227
- .replace(/\n+/g, '')
228
- .replace(/\s{1,}/g, ' ')
229
- .trim()
230
-
231
- let fns = parseVariantFormatString(variant)
232
- .map((str) => {
233
- if (!str.startsWith('@')) {
234
- return ({ format }) => format(str)
235
- }
236
-
237
- let [, name, params] = /@(\S*)( .+|[({].*)?/g.exec(str)
238
- return ({ wrap }) => wrap(postcss.atRule({ name, params: params?.trim() ?? '' }))
239
- })
240
- .reverse()
241
-
242
- return (api) => {
243
- for (let fn of fns) {
244
- fn(api)
245
- }
246
- }
247
- }
248
-
249
- /**
250
- *
251
- * @param {any} tailwindConfig
252
- * @param {any} context
253
- * @param {object} param2
254
- * @param {Offsets} param2.offsets
255
- */
256
- function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offsets, classList }) {
257
- function getConfigValue(path, defaultValue) {
258
- return path ? dlv(tailwindConfig, path, defaultValue) : tailwindConfig
259
- }
260
-
261
- function applyConfiguredPrefix(selector) {
262
- return prefixSelector(tailwindConfig.prefix, selector)
263
- }
264
-
265
- function prefixIdentifier(identifier, options) {
266
- if (identifier === sharedState.NOT_ON_DEMAND) {
267
- return sharedState.NOT_ON_DEMAND
268
- }
269
-
270
- if (!options.respectPrefix) {
271
- return identifier
272
- }
273
-
274
- return context.tailwindConfig.prefix + identifier
275
- }
276
-
277
- function resolveThemeValue(path, defaultValue, opts = {}) {
278
- let parts = toPath(path)
279
- let value = getConfigValue(['theme', ...parts], defaultValue)
280
- return transformThemeValue(parts[0])(value, opts)
281
- }
282
-
283
- let variantIdentifier = 0
284
- let api = {
285
- postcss,
286
- prefix: applyConfiguredPrefix,
287
- e: escapeClassName,
288
- config: getConfigValue,
289
- theme: resolveThemeValue,
290
- corePlugins: (path) => {
291
- if (Array.isArray(tailwindConfig.corePlugins)) {
292
- return tailwindConfig.corePlugins.includes(path)
293
- }
294
-
295
- return getConfigValue(['corePlugins', path], true)
296
- },
297
- variants: () => {
298
- // Preserved for backwards compatibility but not used in v3.0+
299
- return []
300
- },
301
- addBase(base) {
302
- for (let [identifier, rule] of withIdentifiers(base)) {
303
- let prefixedIdentifier = prefixIdentifier(identifier, {})
304
- let offset = offsets.create('base')
305
-
306
- if (!context.candidateRuleMap.has(prefixedIdentifier)) {
307
- context.candidateRuleMap.set(prefixedIdentifier, [])
308
- }
309
-
310
- context.candidateRuleMap
311
- .get(prefixedIdentifier)
312
- .push([{ sort: offset, layer: 'base' }, rule])
313
- }
314
- },
315
- /**
316
- * @param {string} group
317
- * @param {Record<string, string | string[]>} declarations
318
- */
319
- addDefaults(group, declarations) {
320
- const groups = {
321
- [`@defaults ${group}`]: declarations,
322
- }
323
-
324
- for (let [identifier, rule] of withIdentifiers(groups)) {
325
- let prefixedIdentifier = prefixIdentifier(identifier, {})
326
-
327
- if (!context.candidateRuleMap.has(prefixedIdentifier)) {
328
- context.candidateRuleMap.set(prefixedIdentifier, [])
329
- }
330
-
331
- context.candidateRuleMap
332
- .get(prefixedIdentifier)
333
- .push([{ sort: offsets.create('defaults'), layer: 'defaults' }, rule])
334
- }
335
- },
336
- addComponents(components, options) {
337
- let defaultOptions = {
338
- preserveSource: false,
339
- respectPrefix: true,
340
- respectImportant: false,
341
- }
342
-
343
- options = Object.assign({}, defaultOptions, Array.isArray(options) ? {} : options)
344
-
345
- for (let [identifier, rule] of withIdentifiers(components)) {
346
- let prefixedIdentifier = prefixIdentifier(identifier, options)
347
-
348
- classList.add(prefixedIdentifier)
349
-
350
- if (!context.candidateRuleMap.has(prefixedIdentifier)) {
351
- context.candidateRuleMap.set(prefixedIdentifier, [])
352
- }
353
-
354
- context.candidateRuleMap
355
- .get(prefixedIdentifier)
356
- .push([{ sort: offsets.create('components'), layer: 'components', options }, rule])
357
- }
358
- },
359
- addUtilities(utilities, options) {
360
- let defaultOptions = {
361
- preserveSource: false,
362
- respectPrefix: true,
363
- respectImportant: true,
364
- }
365
-
366
- options = Object.assign({}, defaultOptions, Array.isArray(options) ? {} : options)
367
-
368
- for (let [identifier, rule] of withIdentifiers(utilities)) {
369
- let prefixedIdentifier = prefixIdentifier(identifier, options)
370
-
371
- classList.add(prefixedIdentifier)
372
-
373
- if (!context.candidateRuleMap.has(prefixedIdentifier)) {
374
- context.candidateRuleMap.set(prefixedIdentifier, [])
375
- }
376
-
377
- context.candidateRuleMap
378
- .get(prefixedIdentifier)
379
- .push([{ sort: offsets.create('utilities'), layer: 'utilities', options }, rule])
380
- }
381
- },
382
- matchUtilities: function (utilities, options) {
383
- let defaultOptions = {
384
- respectPrefix: true,
385
- respectImportant: true,
386
- modifiers: false,
387
- }
388
-
389
- options = normalizeOptionTypes({ ...defaultOptions, ...options })
390
-
391
- let offset = offsets.create('utilities')
392
-
393
- for (let identifier in utilities) {
394
- let prefixedIdentifier = prefixIdentifier(identifier, options)
395
- let rule = utilities[identifier]
396
-
397
- classList.add([prefixedIdentifier, options])
398
-
399
- function wrapped(modifier, { isOnlyPlugin }) {
400
- let [value, coercedType, utilityModifier] = coerceValue(
401
- options.types,
402
- modifier,
403
- options,
404
- tailwindConfig
405
- )
406
-
407
- if (value === undefined) {
408
- return []
409
- }
410
-
411
- if (!options.types.some(({ type }) => type === coercedType)) {
412
- if (isOnlyPlugin) {
413
- log.warn([
414
- `Unnecessary typehint \`${coercedType}\` in \`${identifier}-${modifier}\`.`,
415
- `You can safely update it to \`${identifier}-${modifier.replace(
416
- coercedType + ':',
417
- ''
418
- )}\`.`,
419
- ])
420
- } else {
421
- return []
422
- }
423
- }
424
-
425
- if (!isSyntacticallyValidPropertyValue(value)) {
426
- return []
427
- }
428
-
429
- let extras = {
430
- get modifier() {
431
- if (!options.modifiers) {
432
- log.warn(`modifier-used-without-options-for-${identifier}`, [
433
- 'Your plugin must set `modifiers: true` in its options to support modifiers.',
434
- ])
435
- }
436
-
437
- return utilityModifier
438
- },
439
- }
440
-
441
- let modifiersEnabled = flagEnabled(tailwindConfig, 'generalizedModifiers')
442
-
443
- let ruleSets = []
444
- .concat(modifiersEnabled ? rule(value, extras) : rule(value))
445
- .filter(Boolean)
446
- .map((declaration) => ({
447
- [nameClass(identifier, modifier)]: declaration,
448
- }))
449
-
450
- return ruleSets
451
- }
452
-
453
- let withOffsets = [{ sort: offset, layer: 'utilities', options }, wrapped]
454
-
455
- if (!context.candidateRuleMap.has(prefixedIdentifier)) {
456
- context.candidateRuleMap.set(prefixedIdentifier, [])
457
- }
458
-
459
- context.candidateRuleMap.get(prefixedIdentifier).push(withOffsets)
460
- }
461
- },
462
- matchComponents: function (components, options) {
463
- let defaultOptions = {
464
- respectPrefix: true,
465
- respectImportant: false,
466
- modifiers: false,
467
- }
468
-
469
- options = normalizeOptionTypes({ ...defaultOptions, ...options })
470
-
471
- let offset = offsets.create('components')
472
-
473
- for (let identifier in components) {
474
- let prefixedIdentifier = prefixIdentifier(identifier, options)
475
- let rule = components[identifier]
476
-
477
- classList.add([prefixedIdentifier, options])
478
-
479
- function wrapped(modifier, { isOnlyPlugin }) {
480
- let [value, coercedType, utilityModifier] = coerceValue(
481
- options.types,
482
- modifier,
483
- options,
484
- tailwindConfig
485
- )
486
-
487
- if (value === undefined) {
488
- return []
489
- }
490
-
491
- if (!options.types.some(({ type }) => type === coercedType)) {
492
- if (isOnlyPlugin) {
493
- log.warn([
494
- `Unnecessary typehint \`${coercedType}\` in \`${identifier}-${modifier}\`.`,
495
- `You can safely update it to \`${identifier}-${modifier.replace(
496
- coercedType + ':',
497
- ''
498
- )}\`.`,
499
- ])
500
- } else {
501
- return []
502
- }
503
- }
504
-
505
- if (!isSyntacticallyValidPropertyValue(value)) {
506
- return []
507
- }
508
-
509
- let extras = {
510
- get modifier() {
511
- if (!options.modifiers) {
512
- log.warn(`modifier-used-without-options-for-${identifier}`, [
513
- 'Your plugin must set `modifiers: true` in its options to support modifiers.',
514
- ])
515
- }
516
-
517
- return utilityModifier
518
- },
519
- }
520
-
521
- let modifiersEnabled = flagEnabled(tailwindConfig, 'generalizedModifiers')
522
-
523
- let ruleSets = []
524
- .concat(modifiersEnabled ? rule(value, extras) : rule(value))
525
- .filter(Boolean)
526
- .map((declaration) => ({
527
- [nameClass(identifier, modifier)]: declaration,
528
- }))
529
-
530
- return ruleSets
531
- }
532
-
533
- let withOffsets = [{ sort: offset, layer: 'components', options }, wrapped]
534
-
535
- if (!context.candidateRuleMap.has(prefixedIdentifier)) {
536
- context.candidateRuleMap.set(prefixedIdentifier, [])
537
- }
538
-
539
- context.candidateRuleMap.get(prefixedIdentifier).push(withOffsets)
540
- }
541
- },
542
- addVariant(variantName, variantFunctions, options = {}) {
543
- variantFunctions = [].concat(variantFunctions).map((variantFunction) => {
544
- if (typeof variantFunction !== 'string') {
545
- // Safelist public API functions
546
- return (api = {}) => {
547
- let { args, modifySelectors, container, separator, wrap, format } = api
548
- let result = variantFunction(
549
- Object.assign(
550
- { modifySelectors, container, separator },
551
- options.type === VARIANT_TYPES.MatchVariant && { args, wrap, format }
552
- )
553
- )
554
-
555
- if (typeof result === 'string' && !isValidVariantFormatString(result)) {
556
- throw new Error(
557
- `Your custom variant \`${variantName}\` has an invalid format string. Make sure it's an at-rule or contains a \`&\` placeholder.`
558
- )
559
- }
560
-
561
- if (Array.isArray(result)) {
562
- return result
563
- .filter((variant) => typeof variant === 'string')
564
- .map((variant) => parseVariant(variant))
565
- }
566
-
567
- // result may be undefined with legacy variants that use APIs like `modifySelectors`
568
- // result may also be a postcss node if someone was returning the result from `modifySelectors`
569
- return result && typeof result === 'string' && parseVariant(result)(api)
570
- }
571
- }
572
-
573
- if (!isValidVariantFormatString(variantFunction)) {
574
- throw new Error(
575
- `Your custom variant \`${variantName}\` has an invalid format string. Make sure it's an at-rule or contains a \`&\` placeholder.`
576
- )
577
- }
578
-
579
- return parseVariant(variantFunction)
580
- })
581
-
582
- insertInto(variantList, variantName, options)
583
- variantMap.set(variantName, variantFunctions)
584
- context.variantOptions.set(variantName, options)
585
- },
586
- matchVariant(variant, variantFn, options) {
587
- // A unique identifier that "groups" these variants together.
588
- // This is for internal use only which is why it is not present in the types
589
- let id = options?.id ?? ++variantIdentifier
590
- let isSpecial = variant === '@'
591
-
592
- let modifiersEnabled = flagEnabled(tailwindConfig, 'generalizedModifiers')
593
-
594
- for (let [key, value] of Object.entries(options?.values ?? {})) {
595
- if (key === 'DEFAULT') continue
596
-
597
- api.addVariant(
598
- isSpecial ? `${variant}${key}` : `${variant}-${key}`,
599
- ({ args, container }) => {
600
- return variantFn(
601
- value,
602
- modifiersEnabled ? { modifier: args?.modifier, container } : { container }
603
- )
604
- },
605
-
606
- {
607
- ...options,
608
- value,
609
- id,
610
- type: VARIANT_TYPES.MatchVariant,
611
- variantInfo: VARIANT_INFO.Base,
612
- }
613
- )
614
- }
615
-
616
- let hasDefault = 'DEFAULT' in (options?.values ?? {})
617
-
618
- api.addVariant(
619
- variant,
620
- ({ args, container }) => {
621
- if (args?.value === sharedState.NONE && !hasDefault) {
622
- return null
623
- }
624
-
625
- return variantFn(
626
- args?.value === sharedState.NONE
627
- ? options.values.DEFAULT
628
- : // Falling back to args if it is a string, otherwise '' for older intellisense
629
- // (JetBrains) plugins.
630
- args?.value ?? (typeof args === 'string' ? args : ''),
631
- modifiersEnabled ? { modifier: args?.modifier, container } : { container }
632
- )
633
- },
634
- {
635
- ...options,
636
- id,
637
- type: VARIANT_TYPES.MatchVariant,
638
- variantInfo: VARIANT_INFO.Dynamic,
639
- }
640
- )
641
- },
642
- }
643
-
644
- return api
645
- }
646
-
647
- let fileModifiedMapCache = new WeakMap()
648
- export function getFileModifiedMap(context) {
649
- if (!fileModifiedMapCache.has(context)) {
650
- fileModifiedMapCache.set(context, new Map())
651
- }
652
- return fileModifiedMapCache.get(context)
653
- }
654
-
655
- function trackModified(files, fileModifiedMap) {
656
- let changed = false
657
- let mtimesToCommit = new Map()
658
-
659
- for (let file of files) {
660
- if (!file) continue
661
-
662
- let parsed = url.parse(file)
663
- let pathname = parsed.hash ? parsed.href.replace(parsed.hash, '') : parsed.href
664
- pathname = parsed.search ? pathname.replace(parsed.search, '') : pathname
665
- let newModified = fs.statSync(decodeURIComponent(pathname), { throwIfNoEntry: false })?.mtimeMs
666
- if (!newModified) {
667
- // It could happen that a file is passed in that doesn't exist. E.g.:
668
- // postcss-cli will provide you a fake path when reading from stdin. This
669
- // path then looks like /path-to-your-project/stdin In that case we just
670
- // want to ignore it and don't track changes at all.
671
- continue
672
- }
673
-
674
- if (!fileModifiedMap.has(file) || newModified > fileModifiedMap.get(file)) {
675
- changed = true
676
- }
677
-
678
- mtimesToCommit.set(file, newModified)
679
- }
680
-
681
- return [changed, mtimesToCommit]
682
- }
683
-
684
- function extractVariantAtRules(node) {
685
- node.walkAtRules((atRule) => {
686
- if (['responsive', 'variants'].includes(atRule.name)) {
687
- extractVariantAtRules(atRule)
688
- atRule.before(atRule.nodes)
689
- atRule.remove()
690
- }
691
- })
692
- }
693
-
694
- function collectLayerPlugins(root) {
695
- let layerPlugins = []
696
-
697
- root.each((node) => {
698
- if (node.type === 'atrule' && ['responsive', 'variants'].includes(node.name)) {
699
- node.name = 'layer'
700
- node.params = 'utilities'
701
- }
702
- })
703
-
704
- // Walk @layer rules and treat them like plugins
705
- root.walkAtRules('layer', (layerRule) => {
706
- extractVariantAtRules(layerRule)
707
-
708
- if (layerRule.params === 'base') {
709
- for (let node of layerRule.nodes) {
710
- layerPlugins.push(function ({ addBase }) {
711
- addBase(node, { respectPrefix: false })
712
- })
713
- }
714
- layerRule.remove()
715
- } else if (layerRule.params === 'components') {
716
- for (let node of layerRule.nodes) {
717
- layerPlugins.push(function ({ addComponents }) {
718
- addComponents(node, { respectPrefix: false, preserveSource: true })
719
- })
720
- }
721
- layerRule.remove()
722
- } else if (layerRule.params === 'utilities') {
723
- for (let node of layerRule.nodes) {
724
- layerPlugins.push(function ({ addUtilities }) {
725
- addUtilities(node, { respectPrefix: false, preserveSource: true })
726
- })
727
- }
728
- layerRule.remove()
729
- }
730
- })
731
-
732
- return layerPlugins
733
- }
734
-
735
- function resolvePlugins(context, root) {
736
- let corePluginList = Object.entries({ ...variantPlugins, ...corePlugins })
737
- .map(([name, plugin]) => {
738
- if (!context.tailwindConfig.corePlugins.includes(name)) {
739
- return null
740
- }
741
-
742
- return plugin
743
- })
744
- .filter(Boolean)
745
-
746
- let userPlugins = context.tailwindConfig.plugins.map((plugin) => {
747
- if (plugin.__isOptionsFunction) {
748
- plugin = plugin()
749
- }
750
-
751
- return typeof plugin === 'function' ? plugin : plugin.handler
752
- })
753
-
754
- let layerPlugins = collectLayerPlugins(root)
755
-
756
- // TODO: This is a workaround for backwards compatibility, since custom variants
757
- // were historically sorted before screen/stackable variants.
758
- let beforeVariants = [
759
- variantPlugins['childVariant'],
760
- variantPlugins['pseudoElementVariants'],
761
- variantPlugins['pseudoClassVariants'],
762
- variantPlugins['hasVariants'],
763
- variantPlugins['ariaVariants'],
764
- variantPlugins['dataVariants'],
765
- ]
766
- let afterVariants = [
767
- variantPlugins['supportsVariants'],
768
- variantPlugins['reducedMotionVariants'],
769
- variantPlugins['prefersContrastVariants'],
770
- variantPlugins['printVariant'],
771
- variantPlugins['screenVariants'],
772
- variantPlugins['orientationVariants'],
773
- variantPlugins['directionVariants'],
774
- variantPlugins['darkVariants'],
775
- variantPlugins['forcedColorsVariants'],
776
- ]
777
-
778
- return [...corePluginList, ...beforeVariants, ...userPlugins, ...afterVariants, ...layerPlugins]
779
- }
780
-
781
- function registerPlugins(plugins, context) {
782
- let variantList = []
783
- let variantMap = new Map()
784
- context.variantMap = variantMap
785
-
786
- let offsets = new Offsets()
787
- context.offsets = offsets
788
-
789
- let classList = new Set()
790
-
791
- let pluginApi = buildPluginApi(context.tailwindConfig, context, {
792
- variantList,
793
- variantMap,
794
- offsets,
795
- classList,
796
- })
797
-
798
- for (let plugin of plugins) {
799
- if (Array.isArray(plugin)) {
800
- for (let pluginItem of plugin) {
801
- pluginItem(pluginApi)
802
- }
803
- } else {
804
- plugin?.(pluginApi)
805
- }
806
- }
807
-
808
- // Make sure to record bit masks for every variant
809
- offsets.recordVariants(variantList, (variant) => variantMap.get(variant).length)
810
-
811
- // Build variantMap
812
- for (let [variantName, variantFunctions] of variantMap.entries()) {
813
- context.variantMap.set(
814
- variantName,
815
- variantFunctions.map((variantFunction, idx) => [
816
- offsets.forVariant(variantName, idx),
817
- variantFunction,
818
- ])
819
- )
820
- }
821
-
822
- let safelist = (context.tailwindConfig.safelist ?? []).filter(Boolean)
823
- if (safelist.length > 0) {
824
- let checks = []
825
-
826
- for (let value of safelist) {
827
- if (typeof value === 'string') {
828
- context.changedContent.push({ content: value, extension: 'html' })
829
- continue
830
- }
831
-
832
- if (value instanceof RegExp) {
833
- log.warn('root-regex', [
834
- 'Regular expressions in `safelist` work differently in Tailwind CSS v3.0.',
835
- 'Update your `safelist` configuration to eliminate this warning.',
836
- 'https://tailwindcss.com/docs/content-configuration#safelisting-classes',
837
- ])
838
- continue
839
- }
840
-
841
- checks.push(value)
842
- }
843
-
844
- if (checks.length > 0) {
845
- let patternMatchingCount = new Map()
846
- let prefixLength = context.tailwindConfig.prefix.length
847
- let checkImportantUtils = checks.some((check) => check.pattern.source.includes('!'))
848
-
849
- for (let util of classList) {
850
- let utils = Array.isArray(util)
851
- ? (() => {
852
- let [utilName, options] = util
853
- let values = Object.keys(options?.values ?? {})
854
- let classes = values.map((value) => formatClass(utilName, value))
855
-
856
- if (options?.supportsNegativeValues) {
857
- // This is the normal negated version
858
- // e.g. `-inset-1` or `-tw-inset-1`
859
- classes = [...classes, ...classes.map((cls) => '-' + cls)]
860
-
861
- // This is the negated version *after* the prefix
862
- // e.g. `tw--inset-1`
863
- // The prefix is already attached to util name
864
- // So we add the negative after the prefix
865
- classes = [
866
- ...classes,
867
- ...classes.map(
868
- (cls) => cls.slice(0, prefixLength) + '-' + cls.slice(prefixLength)
869
- ),
870
- ]
871
- }
872
-
873
- if (options.types.some(({ type }) => type === 'color')) {
874
- classes = [
875
- ...classes,
876
- ...classes.flatMap((cls) =>
877
- Object.keys(context.tailwindConfig.theme.opacity).map(
878
- (opacity) => `${cls}/${opacity}`
879
- )
880
- ),
881
- ]
882
- }
883
-
884
- if (checkImportantUtils && options?.respectImportant) {
885
- classes = [...classes, ...classes.map((cls) => '!' + cls)]
886
- }
887
-
888
- return classes
889
- })()
890
- : [util]
891
-
892
- for (let util of utils) {
893
- for (let { pattern, variants = [] } of checks) {
894
- // RegExp with the /g flag are stateful, so let's reset the last
895
- // index pointer to reset the state.
896
- pattern.lastIndex = 0
897
-
898
- if (!patternMatchingCount.has(pattern)) {
899
- patternMatchingCount.set(pattern, 0)
900
- }
901
-
902
- if (!pattern.test(util)) continue
903
-
904
- patternMatchingCount.set(pattern, patternMatchingCount.get(pattern) + 1)
905
-
906
- context.changedContent.push({ content: util, extension: 'html' })
907
- for (let variant of variants) {
908
- context.changedContent.push({
909
- content: variant + context.tailwindConfig.separator + util,
910
- extension: 'html',
911
- })
912
- }
913
- }
914
- }
915
- }
916
-
917
- for (let [regex, count] of patternMatchingCount.entries()) {
918
- if (count !== 0) continue
919
-
920
- log.warn([
921
- `The safelist pattern \`${regex}\` doesn't match any Tailwind CSS classes.`,
922
- 'Fix this pattern or remove it from your `safelist` configuration.',
923
- 'https://tailwindcss.com/docs/content-configuration#safelisting-classes',
924
- ])
925
- }
926
- }
927
- }
928
-
929
- let darkClassName = [].concat(context.tailwindConfig.darkMode ?? 'media')[1] ?? 'dark'
930
-
931
- // A list of utilities that are used by certain Tailwind CSS utilities but
932
- // that don't exist on their own. This will result in them "not existing" and
933
- // sorting could be weird since you still require them in order to make the
934
- // host utilities work properly. (Thanks Biology)
935
- let parasiteUtilities = [
936
- prefix(context, darkClassName),
937
- prefix(context, 'group'),
938
- prefix(context, 'peer'),
939
- ]
940
- context.getClassOrder = function getClassOrder(classes) {
941
- // Sort classes so they're ordered in a deterministic manner
942
- let sorted = [...classes].sort((a, z) => {
943
- if (a === z) return 0
944
- if (a < z) return -1
945
- return 1
946
- })
947
-
948
- // Non-util classes won't be generated, so we default them to null
949
- let sortedClassNames = new Map(sorted.map((className) => [className, null]))
950
-
951
- // Sort all classes in order
952
- // Non-tailwind classes won't be generated and will be left as `null`
953
- let rules = generateRules(new Set(sorted), context, true)
954
- rules = context.offsets.sort(rules)
955
-
956
- let idx = BigInt(parasiteUtilities.length)
957
-
958
- for (const [, rule] of rules) {
959
- let candidate = rule.raws.tailwind.candidate
960
-
961
- // When multiple rules match a candidate
962
- // always take the position of the first one
963
- sortedClassNames.set(candidate, sortedClassNames.get(candidate) ?? idx++)
964
- }
965
-
966
- return classes.map((className) => {
967
- let order = sortedClassNames.get(className) ?? null
968
- let parasiteIndex = parasiteUtilities.indexOf(className)
969
-
970
- if (order === null && parasiteIndex !== -1) {
971
- // This will make sure that it is at the very beginning of the
972
- // `components` layer which technically means 'before any
973
- // components'.
974
- order = BigInt(parasiteIndex)
975
- }
976
-
977
- return [className, order]
978
- })
979
- }
980
-
981
- // Generate a list of strings for autocompletion purposes, e.g.
982
- // ['uppercase', 'lowercase', ...]
983
- context.getClassList = function getClassList(options = {}) {
984
- let output = []
985
-
986
- for (let util of classList) {
987
- if (Array.isArray(util)) {
988
- let [utilName, utilOptions] = util
989
- let negativeClasses = []
990
-
991
- let modifiers = Object.keys(utilOptions?.modifiers ?? {})
992
-
993
- if (utilOptions?.types?.some(({ type }) => type === 'color')) {
994
- modifiers.push(...Object.keys(context.tailwindConfig.theme.opacity ?? {}))
995
- }
996
-
997
- let metadata = { modifiers }
998
- let includeMetadata = options.includeMetadata && modifiers.length > 0
999
-
1000
- for (let [key, value] of Object.entries(utilOptions?.values ?? {})) {
1001
- // Ignore undefined and null values
1002
- if (value == null) {
1003
- continue
1004
- }
1005
-
1006
- let cls = formatClass(utilName, key)
1007
- output.push(includeMetadata ? [cls, metadata] : cls)
1008
-
1009
- if (utilOptions?.supportsNegativeValues && negateValue(value)) {
1010
- let cls = formatClass(utilName, `-${key}`)
1011
- negativeClasses.push(includeMetadata ? [cls, metadata] : cls)
1012
- }
1013
- }
1014
-
1015
- output.push(...negativeClasses)
1016
- } else {
1017
- output.push(util)
1018
- }
1019
- }
1020
-
1021
- return output
1022
- }
1023
-
1024
- // Generate a list of available variants with meta information of the type of variant.
1025
- context.getVariants = function getVariants() {
1026
- let result = []
1027
- for (let [name, options] of context.variantOptions.entries()) {
1028
- if (options.variantInfo === VARIANT_INFO.Base) continue
1029
-
1030
- result.push({
1031
- name,
1032
- isArbitrary: options.type === Symbol.for('MATCH_VARIANT'),
1033
- values: Object.keys(options.values ?? {}),
1034
- hasDash: name !== '@',
1035
- selectors({ modifier, value } = {}) {
1036
- let candidate = '__TAILWIND_PLACEHOLDER__'
1037
-
1038
- let rule = postcss.rule({ selector: `.${candidate}` })
1039
- let container = postcss.root({ nodes: [rule.clone()] })
1040
-
1041
- let before = container.toString()
1042
-
1043
- let fns = (context.variantMap.get(name) ?? []).flatMap(([_, fn]) => fn)
1044
- let formatStrings = []
1045
- for (let fn of fns) {
1046
- let localFormatStrings = []
1047
-
1048
- let api = {
1049
- args: { modifier, value: options.values?.[value] ?? value },
1050
- separator: context.tailwindConfig.separator,
1051
- modifySelectors(modifierFunction) {
1052
- // Run the modifierFunction over each rule
1053
- container.each((rule) => {
1054
- if (rule.type !== 'rule') {
1055
- return
1056
- }
1057
-
1058
- rule.selectors = rule.selectors.map((selector) => {
1059
- return modifierFunction({
1060
- get className() {
1061
- return getClassNameFromSelector(selector)
1062
- },
1063
- selector,
1064
- })
1065
- })
1066
- })
1067
-
1068
- return container
1069
- },
1070
- format(str) {
1071
- localFormatStrings.push(str)
1072
- },
1073
- wrap(wrapper) {
1074
- localFormatStrings.push(`@${wrapper.name} ${wrapper.params} { & }`)
1075
- },
1076
- container,
1077
- }
1078
-
1079
- let ruleWithVariant = fn(api)
1080
- if (localFormatStrings.length > 0) {
1081
- formatStrings.push(localFormatStrings)
1082
- }
1083
-
1084
- if (Array.isArray(ruleWithVariant)) {
1085
- for (let variantFunction of ruleWithVariant) {
1086
- localFormatStrings = []
1087
- variantFunction(api)
1088
- formatStrings.push(localFormatStrings)
1089
- }
1090
- }
1091
- }
1092
-
1093
- // Reverse engineer the result of the `container`
1094
- let manualFormatStrings = []
1095
- let after = container.toString()
1096
-
1097
- if (before !== after) {
1098
- // Figure out all selectors
1099
- container.walkRules((rule) => {
1100
- let modified = rule.selector
1101
-
1102
- // Rebuild the base selector, this is what plugin authors would do
1103
- // as well. E.g.: `${variant}${separator}${className}`.
1104
- // However, plugin authors probably also prepend or append certain
1105
- // classes, pseudos, ids, ...
1106
- let rebuiltBase = selectorParser((selectors) => {
1107
- selectors.walkClasses((classNode) => {
1108
- classNode.value = `${name}${context.tailwindConfig.separator}${classNode.value}`
1109
- })
1110
- }).processSync(modified)
1111
-
1112
- // Now that we know the original selector, the new selector, and
1113
- // the rebuild part in between, we can replace the part that plugin
1114
- // authors need to rebuild with `&`, and eventually store it in the
1115
- // collectedFormats. Similar to what `format('...')` would do.
1116
- //
1117
- // E.g.:
1118
- // variant: foo
1119
- // selector: .markdown > p
1120
- // modified (by plugin): .foo .foo\\:markdown > p
1121
- // rebuiltBase (internal): .foo\\:markdown > p
1122
- // format: .foo &
1123
- manualFormatStrings.push(modified.replace(rebuiltBase, '&').replace(candidate, '&'))
1124
- })
1125
-
1126
- // Figure out all atrules
1127
- container.walkAtRules((atrule) => {
1128
- manualFormatStrings.push(`@${atrule.name} (${atrule.params}) { & }`)
1129
- })
1130
- }
1131
-
1132
- let isArbitraryVariant = !(value in (options.values ?? {}))
1133
- let internalFeatures = options[INTERNAL_FEATURES] ?? {}
1134
-
1135
- let respectPrefix = (() => {
1136
- if (isArbitraryVariant) return false
1137
- if (internalFeatures.respectPrefix === false) return false
1138
- return true
1139
- })()
1140
-
1141
- formatStrings = formatStrings.map((format) =>
1142
- format.map((str) => ({
1143
- format: str,
1144
- respectPrefix,
1145
- }))
1146
- )
1147
-
1148
- manualFormatStrings = manualFormatStrings.map((format) => ({
1149
- format,
1150
- respectPrefix,
1151
- }))
1152
-
1153
- let opts = {
1154
- candidate,
1155
- context,
1156
- }
1157
-
1158
- let result = formatStrings.map((formats) =>
1159
- finalizeSelector(`.${candidate}`, formatVariantSelector(formats, opts), opts)
1160
- .replace(`.${candidate}`, '&')
1161
- .replace('{ & }', '')
1162
- .trim()
1163
- )
1164
-
1165
- if (manualFormatStrings.length > 0) {
1166
- result.push(
1167
- formatVariantSelector(manualFormatStrings, opts)
1168
- .toString()
1169
- .replace(`.${candidate}`, '&')
1170
- )
1171
- }
1172
-
1173
- return result
1174
- },
1175
- })
1176
- }
1177
-
1178
- return result
1179
- }
1180
- }
1181
-
1182
- /**
1183
- * Mark as class as retroactively invalid
1184
- *
1185
- *
1186
- * @param {string} candidate
1187
- */
1188
- function markInvalidUtilityCandidate(context, candidate) {
1189
- if (!context.classCache.has(candidate)) {
1190
- return
1191
- }
1192
-
1193
- // Mark this as not being a real utility
1194
- context.notClassCache.add(candidate)
1195
-
1196
- // Remove it from any candidate-specific caches
1197
- context.classCache.delete(candidate)
1198
- context.applyClassCache.delete(candidate)
1199
- context.candidateRuleMap.delete(candidate)
1200
- context.candidateRuleCache.delete(candidate)
1201
-
1202
- // Ensure the stylesheet gets rebuilt
1203
- context.stylesheetCache = null
1204
- }
1205
-
1206
- /**
1207
- * Mark as class as retroactively invalid
1208
- *
1209
- * @param {import('postcss').Node} node
1210
- */
1211
- function markInvalidUtilityNode(context, node) {
1212
- let candidate = node.raws.tailwind.candidate
1213
-
1214
- if (!candidate) {
1215
- return
1216
- }
1217
-
1218
- for (const entry of context.ruleCache) {
1219
- if (entry[1].raws.tailwind.candidate === candidate) {
1220
- context.ruleCache.delete(entry)
1221
- // context.postCssNodeCache.delete(node)
1222
- }
1223
- }
1224
-
1225
- markInvalidUtilityCandidate(context, candidate)
1226
- }
1227
-
1228
- export function createContext(tailwindConfig, changedContent = [], root = postcss.root()) {
1229
- let context = {
1230
- disposables: [],
1231
- ruleCache: new Set(),
1232
- candidateRuleCache: new Map(),
1233
- classCache: new Map(),
1234
- applyClassCache: new Map(),
1235
- // Seed the not class cache with the blocklist (which is only strings)
1236
- notClassCache: new Set(tailwindConfig.blocklist ?? []),
1237
- postCssNodeCache: new Map(),
1238
- candidateRuleMap: new Map(),
1239
- tailwindConfig,
1240
- changedContent: changedContent,
1241
- variantMap: new Map(),
1242
- stylesheetCache: null,
1243
- variantOptions: new Map(),
1244
-
1245
- markInvalidUtilityCandidate: (candidate) => markInvalidUtilityCandidate(context, candidate),
1246
- markInvalidUtilityNode: (node) => markInvalidUtilityNode(context, node),
1247
- }
1248
-
1249
- let resolvedPlugins = resolvePlugins(context, root)
1250
- registerPlugins(resolvedPlugins, context)
1251
-
1252
- return context
1253
- }
1254
-
1255
- let contextMap = sharedState.contextMap
1256
- let configContextMap = sharedState.configContextMap
1257
- let contextSourcesMap = sharedState.contextSourcesMap
1258
-
1259
- export function getContext(
1260
- root,
1261
- result,
1262
- tailwindConfig,
1263
- userConfigPath,
1264
- tailwindConfigHash,
1265
- contextDependencies
1266
- ) {
1267
- let sourcePath = result.opts.from
1268
- let isConfigFile = userConfigPath !== null
1269
-
1270
- env.DEBUG && console.log('Source path:', sourcePath)
1271
-
1272
- let existingContext
1273
-
1274
- if (isConfigFile && contextMap.has(sourcePath)) {
1275
- existingContext = contextMap.get(sourcePath)
1276
- } else if (configContextMap.has(tailwindConfigHash)) {
1277
- let context = configContextMap.get(tailwindConfigHash)
1278
- contextSourcesMap.get(context).add(sourcePath)
1279
- contextMap.set(sourcePath, context)
1280
-
1281
- existingContext = context
1282
- }
1283
-
1284
- let cssDidChange = hasContentChanged(sourcePath, root)
1285
-
1286
- // If there's already a context in the cache and we don't need to
1287
- // reset the context, return the cached context.
1288
- if (existingContext) {
1289
- let [contextDependenciesChanged, mtimesToCommit] = trackModified(
1290
- [...contextDependencies],
1291
- getFileModifiedMap(existingContext)
1292
- )
1293
- if (!contextDependenciesChanged && !cssDidChange) {
1294
- return [existingContext, false, mtimesToCommit]
1295
- }
1296
- }
1297
-
1298
- // If this source is in the context map, get the old context.
1299
- // Remove this source from the context sources for the old context,
1300
- // and clean up that context if no one else is using it. This can be
1301
- // called by many processes in rapid succession, so we check for presence
1302
- // first because the first process to run this code will wipe it out first.
1303
- if (contextMap.has(sourcePath)) {
1304
- let oldContext = contextMap.get(sourcePath)
1305
- if (contextSourcesMap.has(oldContext)) {
1306
- contextSourcesMap.get(oldContext).delete(sourcePath)
1307
- if (contextSourcesMap.get(oldContext).size === 0) {
1308
- contextSourcesMap.delete(oldContext)
1309
- for (let [tailwindConfigHash, context] of configContextMap) {
1310
- if (context === oldContext) {
1311
- configContextMap.delete(tailwindConfigHash)
1312
- }
1313
- }
1314
- for (let disposable of oldContext.disposables.splice(0)) {
1315
- disposable(oldContext)
1316
- }
1317
- }
1318
- }
1319
- }
1320
-
1321
- env.DEBUG && console.log('Setting up new context...')
1322
-
1323
- let context = createContext(tailwindConfig, [], root)
1324
-
1325
- Object.assign(context, {
1326
- userConfigPath,
1327
- })
1328
-
1329
- let [, mtimesToCommit] = trackModified([...contextDependencies], getFileModifiedMap(context))
1330
-
1331
- // ---
1332
-
1333
- // Update all context tracking state
1334
-
1335
- configContextMap.set(tailwindConfigHash, context)
1336
- contextMap.set(sourcePath, context)
1337
-
1338
- if (!contextSourcesMap.has(context)) {
1339
- contextSourcesMap.set(context, new Set())
1340
- }
1341
-
1342
- contextSourcesMap.get(context).add(sourcePath)
1343
-
1344
- return [context, true, mtimesToCommit]
1345
- }