tailwindcss 0.0.0-insiders.ee945bb → 0.0.0-insiders.ef2ebb2

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 (215) hide show
  1. package/README.md +13 -9
  2. package/colors.d.ts +3 -0
  3. package/defaultConfig.d.ts +3 -0
  4. package/defaultTheme.d.ts +4 -0
  5. package/index.css +5 -0
  6. package/lib/cli/build/index.js +57 -0
  7. package/lib/cli/build/plugin.js +433 -0
  8. package/lib/cli/build/utils.js +88 -0
  9. package/lib/cli/build/watching.js +182 -0
  10. package/lib/cli/help/index.js +73 -0
  11. package/lib/cli/index.js +231 -0
  12. package/lib/cli/init/index.js +63 -0
  13. package/lib/cli.js +1 -751
  14. package/lib/corePluginList.js +13 -3
  15. package/lib/corePlugins.js +2323 -1720
  16. package/lib/css/preflight.css +13 -5
  17. package/lib/featureFlags.js +50 -25
  18. package/lib/index.js +1 -40
  19. package/lib/lib/cacheInvalidation.js +48 -25
  20. package/lib/lib/collapseAdjacentRules.js +17 -10
  21. package/lib/lib/collapseDuplicateDeclarations.js +13 -8
  22. package/lib/lib/content.js +207 -0
  23. package/lib/lib/defaultExtractor.js +232 -33
  24. package/lib/lib/detectNesting.js +21 -10
  25. package/lib/lib/evaluateTailwindFunctions.js +117 -50
  26. package/lib/lib/expandApplyAtRules.js +316 -215
  27. package/lib/lib/expandTailwindAtRules.js +167 -115
  28. package/lib/lib/findAtConfigPath.js +46 -0
  29. package/lib/lib/generateRules.js +454 -206
  30. package/lib/lib/getModuleDependencies.js +88 -37
  31. package/lib/lib/handleImportAtRules.js +50 -0
  32. package/lib/lib/load-config.js +42 -0
  33. package/lib/lib/normalizeTailwindDirectives.js +29 -39
  34. package/lib/lib/offsets.js +306 -0
  35. package/lib/lib/partitionApplyAtRules.js +13 -8
  36. package/lib/lib/regex.js +74 -0
  37. package/lib/lib/remap-bitfield.js +89 -0
  38. package/lib/lib/resolveDefaultsAtRules.js +96 -74
  39. package/lib/lib/setupContextUtils.js +687 -301
  40. package/lib/lib/setupTrackingContext.js +63 -68
  41. package/lib/lib/sharedState.js +44 -17
  42. package/lib/lib/substituteScreenAtRules.js +14 -10
  43. package/lib/plugin.js +166 -0
  44. package/lib/postcss-plugins/nesting/README.md +2 -2
  45. package/lib/postcss-plugins/nesting/index.js +10 -6
  46. package/lib/postcss-plugins/nesting/plugin.js +24 -20
  47. package/lib/processTailwindFeatures.js +34 -29
  48. package/lib/public/colors.js +272 -246
  49. package/lib/public/create-plugin.js +9 -5
  50. package/lib/public/default-config.js +10 -6
  51. package/lib/public/default-theme.js +10 -6
  52. package/lib/public/load-config.js +12 -0
  53. package/lib/public/resolve-config.js +11 -6
  54. package/lib/util/applyImportantSelector.js +36 -0
  55. package/lib/util/bigSign.js +6 -1
  56. package/lib/util/buildMediaQuery.js +13 -6
  57. package/lib/util/cloneDeep.js +9 -6
  58. package/lib/util/cloneNodes.js +12 -3
  59. package/lib/util/color.js +70 -53
  60. package/lib/util/colorNames.js +752 -0
  61. package/lib/util/configurePlugins.js +7 -2
  62. package/lib/util/createPlugin.js +8 -5
  63. package/lib/util/createUtilityPlugin.js +13 -9
  64. package/lib/util/dataTypes.js +182 -111
  65. package/lib/util/defaults.js +12 -7
  66. package/lib/util/escapeClassName.js +13 -8
  67. package/lib/util/escapeCommas.js +7 -2
  68. package/lib/util/flattenColorPalette.js +11 -10
  69. package/lib/util/formatVariantSelector.js +228 -151
  70. package/lib/util/getAllConfigs.js +33 -12
  71. package/lib/util/hashConfig.js +9 -4
  72. package/lib/util/isKeyframeRule.js +7 -2
  73. package/lib/util/isPlainObject.js +7 -2
  74. package/lib/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +25 -15
  75. package/lib/util/log.js +40 -13
  76. package/lib/util/nameClass.js +27 -10
  77. package/lib/util/negateValue.js +25 -8
  78. package/lib/util/normalizeConfig.js +155 -87
  79. package/lib/util/normalizeScreens.js +127 -8
  80. package/lib/util/parseAnimationValue.js +44 -40
  81. package/lib/util/parseBoxShadowValue.js +33 -62
  82. package/lib/util/parseDependency.js +39 -55
  83. package/lib/util/parseGlob.js +36 -0
  84. package/lib/util/parseObjectStyles.js +15 -10
  85. package/lib/util/pluginUtils.js +154 -59
  86. package/lib/util/prefixSelector.js +29 -10
  87. package/lib/util/pseudoElements.js +209 -0
  88. package/lib/util/removeAlphaVariables.js +31 -0
  89. package/lib/util/resolveConfig.js +96 -95
  90. package/lib/util/resolveConfigPath.js +29 -10
  91. package/lib/util/responsive.js +11 -6
  92. package/lib/util/splitAtTopLevelOnly.js +51 -0
  93. package/lib/util/tap.js +6 -1
  94. package/lib/util/toColorValue.js +7 -2
  95. package/lib/util/toPath.js +22 -4
  96. package/lib/util/transformThemeValue.js +40 -26
  97. package/lib/util/validateConfig.js +48 -0
  98. package/lib/util/validateFormalSyntax.js +26 -0
  99. package/lib/util/withAlphaVariable.js +27 -15
  100. package/lib/value-parser/LICENSE +22 -0
  101. package/lib/value-parser/README.md +3 -0
  102. package/lib/value-parser/index.d.js +2 -0
  103. package/lib/value-parser/index.js +22 -0
  104. package/lib/value-parser/parse.js +259 -0
  105. package/lib/value-parser/stringify.js +38 -0
  106. package/lib/value-parser/unit.js +86 -0
  107. package/lib/value-parser/walk.js +16 -0
  108. package/loadConfig.d.ts +4 -0
  109. package/loadConfig.js +2 -0
  110. package/package.json +59 -53
  111. package/plugin.d.ts +11 -0
  112. package/resolveConfig.d.ts +12 -0
  113. package/scripts/generate-types.js +105 -0
  114. package/scripts/release-channel.js +18 -0
  115. package/scripts/release-notes.js +21 -0
  116. package/scripts/type-utils.js +27 -0
  117. package/src/cli/build/index.js +53 -0
  118. package/src/cli/build/plugin.js +476 -0
  119. package/src/cli/build/utils.js +76 -0
  120. package/src/cli/build/watching.js +229 -0
  121. package/src/cli/help/index.js +70 -0
  122. package/src/cli/index.js +217 -0
  123. package/src/cli/init/index.js +79 -0
  124. package/src/cli.js +1 -825
  125. package/src/corePluginList.js +1 -1
  126. package/src/corePlugins.js +614 -106
  127. package/src/css/preflight.css +13 -5
  128. package/src/featureFlags.js +17 -3
  129. package/src/index.js +1 -42
  130. package/src/lib/collapseAdjacentRules.js +5 -1
  131. package/src/lib/content.js +240 -0
  132. package/src/lib/defaultExtractor.js +202 -35
  133. package/src/lib/detectNesting.js +9 -1
  134. package/src/lib/evaluateTailwindFunctions.js +82 -8
  135. package/src/lib/expandApplyAtRules.js +322 -189
  136. package/src/lib/expandTailwindAtRules.js +82 -59
  137. package/src/lib/findAtConfigPath.js +48 -0
  138. package/src/lib/generateRules.js +427 -127
  139. package/src/lib/getModuleDependencies.js +70 -30
  140. package/src/lib/handleImportAtRules.js +34 -0
  141. package/src/lib/load-config.ts +31 -0
  142. package/src/lib/normalizeTailwindDirectives.js +0 -27
  143. package/src/lib/offsets.js +373 -0
  144. package/src/lib/regex.js +74 -0
  145. package/src/lib/remap-bitfield.js +82 -0
  146. package/src/lib/resolveDefaultsAtRules.js +53 -36
  147. package/src/lib/setupContextUtils.js +591 -156
  148. package/src/lib/setupTrackingContext.js +44 -58
  149. package/src/lib/sharedState.js +13 -4
  150. package/src/plugin.js +141 -0
  151. package/src/postcss-plugins/nesting/README.md +2 -2
  152. package/src/public/colors.js +22 -0
  153. package/src/public/default-config.js +1 -1
  154. package/src/public/default-theme.js +2 -2
  155. package/src/public/load-config.js +2 -0
  156. package/src/util/applyImportantSelector.js +27 -0
  157. package/src/util/buildMediaQuery.js +5 -3
  158. package/src/util/cloneNodes.js +5 -1
  159. package/src/util/color.js +39 -20
  160. package/src/util/colorNames.js +150 -0
  161. package/src/util/dataTypes.js +67 -23
  162. package/src/util/formatVariantSelector.js +264 -144
  163. package/src/util/getAllConfigs.js +21 -2
  164. package/src/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +1 -1
  165. package/src/util/log.js +25 -1
  166. package/src/util/nameClass.js +4 -0
  167. package/src/util/negateValue.js +11 -3
  168. package/src/util/normalizeConfig.js +78 -20
  169. package/src/util/normalizeScreens.js +99 -4
  170. package/src/util/parseBoxShadowValue.js +3 -50
  171. package/src/util/parseDependency.js +37 -42
  172. package/src/util/parseGlob.js +24 -0
  173. package/src/util/pluginUtils.js +118 -23
  174. package/src/util/prefixSelector.js +28 -10
  175. package/src/util/pseudoElements.js +167 -0
  176. package/src/util/removeAlphaVariables.js +24 -0
  177. package/src/util/resolveConfig.js +70 -62
  178. package/src/util/resolveConfigPath.js +12 -1
  179. package/src/util/splitAtTopLevelOnly.js +52 -0
  180. package/src/util/toPath.js +1 -1
  181. package/src/util/transformThemeValue.js +13 -3
  182. package/src/util/validateConfig.js +36 -0
  183. package/src/util/validateFormalSyntax.js +34 -0
  184. package/src/util/withAlphaVariable.js +1 -1
  185. package/src/value-parser/LICENSE +22 -0
  186. package/src/value-parser/README.md +3 -0
  187. package/src/value-parser/index.d.ts +177 -0
  188. package/src/value-parser/index.js +28 -0
  189. package/src/value-parser/parse.js +303 -0
  190. package/src/value-parser/stringify.js +41 -0
  191. package/src/value-parser/unit.js +118 -0
  192. package/src/value-parser/walk.js +18 -0
  193. package/stubs/.gitignore +1 -0
  194. package/stubs/.prettierrc.json +6 -0
  195. package/stubs/{defaultConfig.stub.js → config.full.js} +218 -165
  196. package/stubs/{simpleConfig.stub.js → config.simple.js} +1 -1
  197. package/stubs/{defaultPostCssConfig.stub.js → postcss.config.cjs} +0 -1
  198. package/stubs/postcss.config.js +5 -0
  199. package/stubs/tailwind.config.cjs +2 -0
  200. package/stubs/tailwind.config.js +2 -0
  201. package/stubs/tailwind.config.ts +3 -0
  202. package/types/config.d.ts +369 -0
  203. package/types/generated/.gitkeep +0 -0
  204. package/types/generated/colors.d.ts +298 -0
  205. package/types/generated/corePluginList.d.ts +1 -0
  206. package/types/generated/default-theme.d.ts +372 -0
  207. package/types/index.d.ts +7 -0
  208. package/CHANGELOG.md +0 -2090
  209. package/lib/cli-peer-dependencies.js +0 -15
  210. package/lib/constants.js +0 -37
  211. package/peers/index.js +0 -75077
  212. package/scripts/install-integrations.js +0 -27
  213. package/scripts/rebuildFixtures.js +0 -68
  214. package/src/cli-peer-dependencies.js +0 -9
  215. package/src/constants.js +0 -17
@@ -3,19 +3,27 @@ import selectorParser from 'postcss-selector-parser'
3
3
  import parseObjectStyles from '../util/parseObjectStyles'
4
4
  import isPlainObject from '../util/isPlainObject'
5
5
  import prefixSelector from '../util/prefixSelector'
6
- import { updateAllClasses } from '../util/pluginUtils'
6
+ import { updateAllClasses, getMatchingTypes } from '../util/pluginUtils'
7
7
  import log from '../util/log'
8
8
  import * as sharedState from './sharedState'
9
- import { formatVariantSelector, finalizeSelector } from '../util/formatVariantSelector'
9
+ import {
10
+ formatVariantSelector,
11
+ finalizeSelector,
12
+ eliminateIrrelevantSelectors,
13
+ } from '../util/formatVariantSelector'
10
14
  import { asClass } from '../util/nameClass'
11
15
  import { normalize } from '../util/dataTypes'
12
- import isValidArbitraryValue from '../util/isValidArbitraryValue'
16
+ import { isValidVariantFormatString, parseVariant } from './setupContextUtils'
17
+ import isValidArbitraryValue from '../util/isSyntacticallyValidPropertyValue'
18
+ import { splitAtTopLevelOnly } from '../util/splitAtTopLevelOnly.js'
19
+ import { flagEnabled } from '../featureFlags'
20
+ import { applyImportantSelector } from '../util/applyImportantSelector'
13
21
 
14
22
  let classNameParser = selectorParser((selectors) => {
15
23
  return selectors.first.filter(({ type }) => type === 'class').pop().value
16
24
  })
17
25
 
18
- function getClassNameFromSelector(selector) {
26
+ export function getClassNameFromSelector(selector) {
19
27
  return classNameParser.transformSync(selector)
20
28
  }
21
29
 
@@ -31,13 +39,24 @@ function* candidatePermutations(candidate) {
31
39
 
32
40
  while (lastIndex >= 0) {
33
41
  let dashIdx
42
+ let wasSlash = false
34
43
 
35
44
  if (lastIndex === Infinity && candidate.endsWith(']')) {
36
45
  let bracketIdx = candidate.indexOf('[')
37
46
 
38
47
  // If character before `[` isn't a dash or a slash, this isn't a dynamic class
39
48
  // eg. string[]
40
- dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
49
+ if (candidate[bracketIdx - 1] === '-') {
50
+ dashIdx = bracketIdx - 1
51
+ } else if (candidate[bracketIdx - 1] === '/') {
52
+ dashIdx = bracketIdx - 1
53
+ wasSlash = true
54
+ } else {
55
+ dashIdx = -1
56
+ }
57
+ } else if (lastIndex === Infinity && candidate.includes('/')) {
58
+ dashIdx = candidate.lastIndexOf('/')
59
+ wasSlash = true
41
60
  } else {
42
61
  dashIdx = candidate.lastIndexOf('-', lastIndex)
43
62
  }
@@ -47,11 +66,16 @@ function* candidatePermutations(candidate) {
47
66
  }
48
67
 
49
68
  let prefix = candidate.slice(0, dashIdx)
50
- let modifier = candidate.slice(dashIdx + 1)
51
-
52
- yield [prefix, modifier]
69
+ let modifier = candidate.slice(wasSlash ? dashIdx : dashIdx + 1)
53
70
 
54
71
  lastIndex = dashIdx - 1
72
+
73
+ // TODO: This feels a bit hacky
74
+ if (prefix === '' || modifier === '/') {
75
+ continue
76
+ }
77
+
78
+ yield [prefix, modifier]
55
79
  }
56
80
  }
57
81
 
@@ -92,19 +116,28 @@ function applyImportant(matches, classCandidate) {
92
116
  if (matches.length === 0) {
93
117
  return matches
94
118
  }
119
+
95
120
  let result = []
96
121
 
97
122
  for (let [meta, rule] of matches) {
98
123
  let container = postcss.root({ nodes: [rule.clone()] })
124
+
99
125
  container.walkRules((r) => {
100
- r.selector = updateAllClasses(r.selector, (className) => {
101
- if (className === classCandidate) {
102
- return `!${className}`
103
- }
104
- return className
105
- })
126
+ let ast = selectorParser().astSync(r.selector)
127
+
128
+ // Remove extraneous selectors that do not include the base candidate
129
+ ast.each((sel) => eliminateIrrelevantSelectors(sel, classCandidate))
130
+
131
+ // Update all instances of the base candidate to include the important marker
132
+ updateAllClasses(ast, (className) =>
133
+ className === classCandidate ? `!${className}` : className
134
+ )
135
+
136
+ r.selector = ast.toString()
137
+
106
138
  r.walkDecls((d) => (d.important = true))
107
139
  })
140
+
108
141
  result.push([{ ...meta, important: true }, container.nodes[0]])
109
142
  }
110
143
 
@@ -125,8 +158,75 @@ function applyVariant(variant, matches, context) {
125
158
  return matches
126
159
  }
127
160
 
161
+ /** @type {{modifier: string | null, value: string | null}} */
162
+ let args = { modifier: null, value: sharedState.NONE }
163
+
164
+ // Retrieve "modifier"
165
+ {
166
+ let [baseVariant, ...modifiers] = splitAtTopLevelOnly(variant, '/')
167
+
168
+ // This is a hack to support variants with `/` in them, like `ar-1/10/20:text-red-500`
169
+ // In this case 1/10 is a value but /20 is a modifier
170
+ if (modifiers.length > 1) {
171
+ baseVariant = baseVariant + '/' + modifiers.slice(0, -1).join('/')
172
+ modifiers = modifiers.slice(-1)
173
+ }
174
+
175
+ if (modifiers.length && !context.variantMap.has(variant)) {
176
+ variant = baseVariant
177
+ args.modifier = modifiers[0]
178
+ }
179
+ }
180
+
181
+ // Retrieve "arbitrary value"
182
+ if (variant.endsWith(']') && !variant.startsWith('[')) {
183
+ // We either have:
184
+ // @[200px]
185
+ // group-[:hover]
186
+ //
187
+ // But we don't want:
188
+ // @-[200px] (`-` is incorrect)
189
+ // group[:hover] (`-` is missing)
190
+ let match = /(.)(-?)\[(.*)\]/g.exec(variant)
191
+ if (match) {
192
+ let [, char, separator, value] = match
193
+ // @-[200px] case
194
+ if (char === '@' && separator === '-') return []
195
+ // group[:hover] case
196
+ if (char !== '@' && separator === '') return []
197
+
198
+ variant = variant.replace(`${separator}[${value}]`, '')
199
+ args.value = value
200
+ }
201
+ }
202
+
203
+ // Register arbitrary variants
204
+ if (isArbitraryValue(variant) && !context.variantMap.has(variant)) {
205
+ let sort = context.offsets.recordVariant(variant)
206
+
207
+ let selector = normalize(variant.slice(1, -1))
208
+ let selectors = splitAtTopLevelOnly(selector, ',')
209
+
210
+ // We do not support multiple selectors for arbitrary variants
211
+ if (selectors.length > 1) {
212
+ return []
213
+ }
214
+
215
+ if (!selectors.every(isValidVariantFormatString)) {
216
+ return []
217
+ }
218
+
219
+ let records = selectors.map((sel, idx) => [
220
+ context.offsets.applyParallelOffset(sort, idx),
221
+ parseVariant(sel.trim()),
222
+ ])
223
+
224
+ context.variantMap.set(variant, records)
225
+ }
226
+
128
227
  if (context.variantMap.has(variant)) {
129
- let variantFunctionTuples = context.variantMap.get(variant)
228
+ let isArbitraryVariant = isArbitraryValue(variant)
229
+ let variantFunctionTuples = context.variantMap.get(variant).slice()
130
230
  let result = []
131
231
 
132
232
  for (let [meta, rule] of matches) {
@@ -137,15 +237,17 @@ function applyVariant(variant, matches, context) {
137
237
 
138
238
  let container = postcss.root({ nodes: [rule.clone()] })
139
239
 
140
- for (let [variantSort, variantFunction] of variantFunctionTuples) {
141
- let clone = container.clone()
240
+ for (let [variantSort, variantFunction, containerFromArray] of variantFunctionTuples) {
241
+ let clone = (containerFromArray ?? container).clone()
142
242
  let collectedFormats = []
143
243
 
144
- let originals = new Map()
145
-
146
244
  function prepareBackup() {
147
- if (originals.size > 0) return // Already prepared, chicken out
148
- clone.walkRules((rule) => originals.set(rule, rule.selector))
245
+ // Already prepared, chicken out
246
+ if (clone.raws.neededBackup) {
247
+ return
248
+ }
249
+ clone.raws.neededBackup = true
250
+ clone.walkRules((rule) => (rule.raws.originalSelector = rule.selector))
149
251
  }
150
252
 
151
253
  function modifySelectors(modifierFunction) {
@@ -185,25 +287,54 @@ function applyVariant(variant, matches, context) {
185
287
  clone.append(wrapper)
186
288
  },
187
289
  format(selectorFormat) {
188
- collectedFormats.push(selectorFormat)
290
+ collectedFormats.push({
291
+ format: selectorFormat,
292
+ isArbitraryVariant,
293
+ })
189
294
  },
295
+ args,
190
296
  })
191
297
 
298
+ // It can happen that a list of format strings is returned from within the function. In that
299
+ // case, we have to process them as well. We can use the existing `variantSort`.
300
+ if (Array.isArray(ruleWithVariant)) {
301
+ for (let [idx, variantFunction] of ruleWithVariant.entries()) {
302
+ // This is a little bit scary since we are pushing to an array of items that we are
303
+ // currently looping over. However, you can also think of it like a processing queue
304
+ // where you keep handling jobs until everything is done and each job can queue more
305
+ // jobs if needed.
306
+ variantFunctionTuples.push([
307
+ context.offsets.applyParallelOffset(variantSort, idx),
308
+ variantFunction,
309
+
310
+ // If the clone has been modified we have to pass that back
311
+ // though so each rule can use the modified container
312
+ clone.clone(),
313
+ ])
314
+ }
315
+ continue
316
+ }
317
+
192
318
  if (typeof ruleWithVariant === 'string') {
193
- collectedFormats.push(ruleWithVariant)
319
+ collectedFormats.push({
320
+ format: ruleWithVariant,
321
+ isArbitraryVariant,
322
+ })
194
323
  }
195
324
 
196
325
  if (ruleWithVariant === null) {
197
326
  continue
198
327
  }
199
328
 
200
- // We filled the `originals`, therefore we assume that somebody touched
329
+ // We had to backup selectors, therefore we assume that somebody touched
201
330
  // `container` or `modifySelectors`. Let's see if they did, so that we
202
331
  // can restore the selectors, and collect the format strings.
203
- if (originals.size > 0) {
332
+ if (clone.raws.neededBackup) {
333
+ delete clone.raws.neededBackup
204
334
  clone.walkRules((rule) => {
205
- if (!originals.has(rule)) return
206
- let before = originals.get(rule)
335
+ let before = rule.raws.originalSelector
336
+ if (!before) return
337
+ delete rule.raws.originalSelector
207
338
  if (before === rule.selector) return // No mutation happened
208
339
 
209
340
  let modified = rule.selector
@@ -229,7 +360,10 @@ function applyVariant(variant, matches, context) {
229
360
  // modified (by plugin): .foo .foo\\:markdown > p
230
361
  // rebuiltBase (internal): .foo\\:markdown > p
231
362
  // format: .foo &
232
- collectedFormats.push(modified.replace(rebuiltBase, '&'))
363
+ collectedFormats.push({
364
+ format: modified.replace(rebuiltBase, '&'),
365
+ isArbitraryVariant,
366
+ })
233
367
  rule.selector = before
234
368
  })
235
369
  }
@@ -243,7 +377,11 @@ function applyVariant(variant, matches, context) {
243
377
  let withOffset = [
244
378
  {
245
379
  ...meta,
246
- sort: variantSort | meta.sort,
380
+ sort: context.offsets.applyVariantOffset(
381
+ meta.sort,
382
+ variantSort,
383
+ Object.assign(args, context.variantOptions.get(variant))
384
+ ),
247
385
  collectedFormats: (meta.collectedFormats ?? []).concat(collectedFormats),
248
386
  },
249
387
  clone.nodes[0],
@@ -303,6 +441,19 @@ function looksLikeUri(declaration) {
303
441
  }
304
442
  }
305
443
 
444
+ function isParsableNode(node) {
445
+ let isParsable = true
446
+
447
+ node.walkDecls((decl) => {
448
+ if (!isParsableCssValue(decl.prop, decl.value)) {
449
+ isParsable = false
450
+ return false
451
+ }
452
+ })
453
+
454
+ return isParsable
455
+ }
456
+
306
457
  function isParsableCssValue(property, value) {
307
458
  // We don't want to to treat [https://example.com] as a custom property
308
459
  // Even though, according to the CSS grammar, it's a totally valid CSS declaration
@@ -340,9 +491,11 @@ function extractArbitraryProperty(classCandidate, context) {
340
491
  return null
341
492
  }
342
493
 
494
+ let sort = context.offsets.arbitraryProperty()
495
+
343
496
  return [
344
497
  [
345
- { sort: context.arbitraryPropertiesSort, layer: 'utilities' },
498
+ { sort, layer: 'utilities' },
346
499
  () => ({
347
500
  [asClass(classCandidate)]: {
348
501
  [property]: normalized,
@@ -369,7 +522,11 @@ function* resolveMatchedPlugins(classCandidate, context) {
369
522
  const twConfigPrefix = context.tailwindConfig.prefix
370
523
 
371
524
  const twConfigPrefixLen = twConfigPrefix.length
372
- if (candidatePrefix[twConfigPrefixLen] === '-') {
525
+
526
+ const hasMatchingPrefix =
527
+ candidatePrefix.startsWith(twConfigPrefix) || candidatePrefix.startsWith(`-${twConfigPrefix}`)
528
+
529
+ if (candidatePrefix[twConfigPrefixLen] === '-' && hasMatchingPrefix) {
373
530
  negative = true
374
531
  candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1)
375
532
  }
@@ -390,18 +547,22 @@ function splitWithSeparator(input, separator) {
390
547
  return [sharedState.NOT_ON_DEMAND]
391
548
  }
392
549
 
393
- return input.split(new RegExp(`\\${separator}(?![^[]*\\])`, 'g'))
550
+ return splitAtTopLevelOnly(input, separator)
394
551
  }
395
552
 
396
553
  function* recordCandidates(matches, classCandidate) {
397
554
  for (const match of matches) {
398
- match[1].raws.tailwind = { ...match[1].raws.tailwind, classCandidate }
555
+ match[1].raws.tailwind = {
556
+ ...match[1].raws.tailwind,
557
+ classCandidate,
558
+ preserveSource: match[0].options?.preserveSource ?? false,
559
+ }
399
560
 
400
561
  yield match
401
562
  }
402
563
  }
403
564
 
404
- function* resolveMatches(candidate, context) {
565
+ function* resolveMatches(candidate, context, original = candidate) {
405
566
  let separator = context.tailwindConfig.separator
406
567
  let [classCandidate, ...variants] = splitWithSeparator(candidate, separator).reverse()
407
568
  let important = false
@@ -411,6 +572,15 @@ function* resolveMatches(candidate, context) {
411
572
  classCandidate = classCandidate.slice(1)
412
573
  }
413
574
 
575
+ if (flagEnabled(context.tailwindConfig, 'variantGrouping')) {
576
+ if (classCandidate.startsWith('(') && classCandidate.endsWith(')')) {
577
+ let base = variants.slice().reverse().join(separator)
578
+ for (let part of splitAtTopLevelOnly(classCandidate.slice(1, -1), ',')) {
579
+ yield* resolveMatches(base + separator + part, context, original)
580
+ }
581
+ }
582
+ }
583
+
414
584
  // TODO: Reintroduce this in ways that doesn't break on false positives
415
585
  // function sortAgainst(toSort, against) {
416
586
  // return toSort.slice().sort((a, z) => {
@@ -451,65 +621,136 @@ function* resolveMatches(candidate, context) {
451
621
  }
452
622
 
453
623
  if (matchesPerPlugin.length > 0) {
454
- typesByMatches.set(matchesPerPlugin, sort.options?.type)
624
+ let matchingTypes = Array.from(
625
+ getMatchingTypes(
626
+ sort.options?.types ?? [],
627
+ modifier,
628
+ sort.options ?? {},
629
+ context.tailwindConfig
630
+ )
631
+ ).map(([_, type]) => type)
632
+
633
+ if (matchingTypes.length > 0) {
634
+ typesByMatches.set(matchesPerPlugin, matchingTypes)
635
+ }
636
+
455
637
  matches.push(matchesPerPlugin)
456
638
  }
457
639
  }
458
640
 
459
- // Only keep the result of the very first plugin if we are dealing with
460
- // arbitrary values, to protect against ambiguity.
461
- if (isArbitraryValue(modifier) && matches.length > 1) {
462
- let typesPerPlugin = matches.map((match) => new Set([...(typesByMatches.get(match) ?? [])]))
463
-
464
- // Remove duplicates, so that we can detect proper unique types for each plugin.
465
- for (let pluginTypes of typesPerPlugin) {
466
- for (let type of pluginTypes) {
467
- let removeFromOwnGroup = false
468
-
469
- for (let otherGroup of typesPerPlugin) {
470
- if (pluginTypes === otherGroup) continue
641
+ if (isArbitraryValue(modifier)) {
642
+ if (matches.length > 1) {
643
+ // Partition plugins in 2 categories so that we can start searching in the plugins that
644
+ // don't have `any` as a type first.
645
+ let [withAny, withoutAny] = matches.reduce(
646
+ (group, plugin) => {
647
+ let hasAnyType = plugin.some(([{ options }]) =>
648
+ options.types.some(({ type }) => type === 'any')
649
+ )
471
650
 
472
- if (otherGroup.has(type)) {
473
- otherGroup.delete(type)
474
- removeFromOwnGroup = true
651
+ if (hasAnyType) {
652
+ group[0].push(plugin)
653
+ } else {
654
+ group[1].push(plugin)
475
655
  }
656
+ return group
657
+ },
658
+ [[], []]
659
+ )
660
+
661
+ function findFallback(matches) {
662
+ // If only a single plugin matches, let's take that one
663
+ if (matches.length === 1) {
664
+ return matches[0]
476
665
  }
477
666
 
478
- if (removeFromOwnGroup) pluginTypes.delete(type)
667
+ // Otherwise, find the plugin that creates a valid rule given the arbitrary value, and
668
+ // also has the correct type which preferOnConflicts the plugin in case of clashes.
669
+ return matches.find((rules) => {
670
+ let matchingTypes = typesByMatches.get(rules)
671
+ return rules.some(([{ options }, rule]) => {
672
+ if (!isParsableNode(rule)) {
673
+ return false
674
+ }
675
+
676
+ return options.types.some(
677
+ ({ type, preferOnConflict }) => matchingTypes.includes(type) && preferOnConflict
678
+ )
679
+ })
680
+ })
479
681
  }
480
- }
481
682
 
482
- let messages = []
483
-
484
- for (let [idx, group] of typesPerPlugin.entries()) {
485
- for (let type of group) {
486
- let rules = matches[idx]
487
- .map(([, rule]) => rule)
488
- .flat()
489
- .map((rule) =>
490
- rule
491
- .toString()
492
- .split('\n')
493
- .slice(1, -1) // Remove selector and closing '}'
494
- .map((line) => line.trim())
495
- .map((x) => ` ${x}`) // Re-indent
496
- .join('\n')
497
- )
498
- .join('\n\n')
683
+ // Try to find a fallback plugin, because we already know that multiple plugins matched for
684
+ // the given arbitrary value.
685
+ let fallback = findFallback(withoutAny) ?? findFallback(withAny)
686
+ if (fallback) {
687
+ matches = [fallback]
688
+ }
499
689
 
500
- messages.push(` Use \`${candidate.replace('[', `[${type}:`)}\` for \`${rules.trim()}\``)
501
- break
690
+ // We couldn't find a fallback plugin which means that there are now multiple plugins that
691
+ // generated css for the current candidate. This means that the result is ambiguous and this
692
+ // should not happen. We won't generate anything right now, so let's report this to the user
693
+ // by logging some options about what they can do.
694
+ else {
695
+ let typesPerPlugin = matches.map(
696
+ (match) => new Set([...(typesByMatches.get(match) ?? [])])
697
+ )
698
+
699
+ // Remove duplicates, so that we can detect proper unique types for each plugin.
700
+ for (let pluginTypes of typesPerPlugin) {
701
+ for (let type of pluginTypes) {
702
+ let removeFromOwnGroup = false
703
+
704
+ for (let otherGroup of typesPerPlugin) {
705
+ if (pluginTypes === otherGroup) continue
706
+
707
+ if (otherGroup.has(type)) {
708
+ otherGroup.delete(type)
709
+ removeFromOwnGroup = true
710
+ }
711
+ }
712
+
713
+ if (removeFromOwnGroup) pluginTypes.delete(type)
714
+ }
715
+ }
716
+
717
+ let messages = []
718
+
719
+ for (let [idx, group] of typesPerPlugin.entries()) {
720
+ for (let type of group) {
721
+ let rules = matches[idx]
722
+ .map(([, rule]) => rule)
723
+ .flat()
724
+ .map((rule) =>
725
+ rule
726
+ .toString()
727
+ .split('\n')
728
+ .slice(1, -1) // Remove selector and closing '}'
729
+ .map((line) => line.trim())
730
+ .map((x) => ` ${x}`) // Re-indent
731
+ .join('\n')
732
+ )
733
+ .join('\n\n')
734
+
735
+ messages.push(
736
+ ` Use \`${candidate.replace('[', `[${type}:`)}\` for \`${rules.trim()}\``
737
+ )
738
+ break
739
+ }
740
+ }
741
+
742
+ log.warn([
743
+ `The class \`${candidate}\` is ambiguous and matches multiple utilities.`,
744
+ ...messages,
745
+ `If this is content and not a class, replace it with \`${candidate
746
+ .replace('[', '[')
747
+ .replace(']', ']')}\` to silence this warning.`,
748
+ ])
749
+ continue
502
750
  }
503
751
  }
504
752
 
505
- log.warn([
506
- `The class \`${candidate}\` is ambiguous and matches multiple utilities.`,
507
- ...messages,
508
- `If this is content and not a class, replace it with \`${candidate
509
- .replace('[', '[')
510
- .replace(']', ']')}\` to silence this warning.`,
511
- ])
512
- continue
753
+ matches = matches.map((list) => list.filter((match) => isParsableNode(match[1])))
513
754
  }
514
755
 
515
756
  matches = matches.flat()
@@ -528,19 +769,13 @@ function* resolveMatches(candidate, context) {
528
769
  match[1].raws.tailwind = { ...match[1].raws.tailwind, candidate }
529
770
 
530
771
  // Apply final format selector
531
- if (match[0].collectedFormats) {
532
- let finalFormat = formatVariantSelector('&', ...match[0].collectedFormats)
533
- let container = postcss.root({ nodes: [match[1].clone()] })
534
- container.walkRules((rule) => {
535
- if (inKeyframes(rule)) return
536
-
537
- rule.selector = finalizeSelector(finalFormat, {
538
- selector: rule.selector,
539
- candidate,
540
- context,
541
- })
542
- })
543
- match[1] = container.nodes[0]
772
+ match = applyFinalFormat(match, { context, candidate, original })
773
+
774
+ // Skip rules with invalid selectors
775
+ // This will cause the candidate to be added to the "not class"
776
+ // cache skipping it entirely for future builds
777
+ if (match === null) {
778
+ continue
544
779
  }
545
780
 
546
781
  yield match
@@ -548,20 +783,105 @@ function* resolveMatches(candidate, context) {
548
783
  }
549
784
  }
550
785
 
786
+ function applyFinalFormat(match, { context, candidate, original }) {
787
+ if (!match[0].collectedFormats) {
788
+ return match
789
+ }
790
+
791
+ let isValid = true
792
+ let finalFormat
793
+
794
+ try {
795
+ finalFormat = formatVariantSelector(match[0].collectedFormats, {
796
+ context,
797
+ candidate,
798
+ })
799
+ } catch {
800
+ // The format selector we produced is invalid
801
+ // This could be because:
802
+ // - A bug exists
803
+ // - A plugin introduced an invalid variant selector (ex: `addVariant('foo', '&;foo')`)
804
+ // - The user used an invalid arbitrary variant (ex: `[&;foo]:underline`)
805
+ // Either way the build will fail because of this
806
+ // We would rather that the build pass "silently" given that this could
807
+ // happen because of picking up invalid things when scanning content
808
+ // So we'll throw out the candidate instead
809
+
810
+ return null
811
+ }
812
+
813
+ let container = postcss.root({ nodes: [match[1].clone()] })
814
+
815
+ container.walkRules((rule) => {
816
+ if (inKeyframes(rule)) {
817
+ return
818
+ }
819
+
820
+ try {
821
+ rule.selector = finalizeSelector(rule.selector, finalFormat, {
822
+ candidate: original,
823
+ context,
824
+ })
825
+ } catch {
826
+ // If this selector is invalid we also want to skip it
827
+ // But it's likely that being invalid here means there's a bug in a plugin rather than too loosely matching content
828
+ isValid = false
829
+ return false
830
+ }
831
+ })
832
+
833
+ if (!isValid) {
834
+ return null
835
+ }
836
+
837
+ match[1] = container.nodes[0]
838
+
839
+ return match
840
+ }
841
+
551
842
  function inKeyframes(rule) {
552
843
  return rule.parent && rule.parent.type === 'atrule' && rule.parent.name === 'keyframes'
553
844
  }
554
845
 
846
+ function getImportantStrategy(important) {
847
+ if (important === true) {
848
+ return (rule) => {
849
+ if (inKeyframes(rule)) {
850
+ return
851
+ }
852
+
853
+ rule.walkDecls((d) => {
854
+ if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
855
+ d.important = true
856
+ }
857
+ })
858
+ }
859
+ }
860
+
861
+ if (typeof important === 'string') {
862
+ return (rule) => {
863
+ if (inKeyframes(rule)) {
864
+ return
865
+ }
866
+
867
+ rule.selectors = rule.selectors.map((selector) => {
868
+ return applyImportantSelector(selector, important)
869
+ })
870
+ }
871
+ }
872
+ }
873
+
555
874
  function generateRules(candidates, context) {
556
875
  let allRules = []
876
+ let strategy = getImportantStrategy(context.tailwindConfig.important)
557
877
 
558
878
  for (let candidate of candidates) {
559
879
  if (context.notClassCache.has(candidate)) {
560
880
  continue
561
881
  }
562
882
 
563
- if (context.classCache.has(candidate)) {
564
- allRules.push(context.classCache.get(candidate))
883
+ if (context.candidateRuleCache.has(candidate)) {
884
+ allRules = allRules.concat(Array.from(context.candidateRuleCache.get(candidate)))
565
885
  continue
566
886
  }
567
887
 
@@ -573,47 +893,27 @@ function generateRules(candidates, context) {
573
893
  }
574
894
 
575
895
  context.classCache.set(candidate, matches)
576
- allRules.push(matches)
577
- }
578
896
 
579
- // Strategy based on `tailwindConfig.important`
580
- let strategy = ((important) => {
581
- if (important === true) {
582
- return (rule) => {
583
- rule.walkDecls((d) => {
584
- if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
585
- d.important = true
586
- }
587
- })
588
- }
589
- }
897
+ let rules = context.candidateRuleCache.get(candidate) ?? new Set()
898
+ context.candidateRuleCache.set(candidate, rules)
590
899
 
591
- if (typeof important === 'string') {
592
- return (rule) => {
593
- rule.selectors = rule.selectors.map((selector) => {
594
- return `${important} ${selector}`
595
- })
596
- }
597
- }
598
- })(context.tailwindConfig.important)
900
+ for (const match of matches) {
901
+ let [{ sort, options }, rule] = match
599
902
 
600
- return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
601
- if (options.respectImportant) {
602
- if (strategy) {
903
+ if (options.respectImportant && strategy) {
603
904
  let container = postcss.root({ nodes: [rule.clone()] })
604
- container.walkRules((r) => {
605
- if (inKeyframes(r)) {
606
- return
607
- }
608
-
609
- strategy(r)
610
- })
905
+ container.walkRules(strategy)
611
906
  rule = container.nodes[0]
612
907
  }
908
+
909
+ let newEntry = [sort, rule]
910
+ rules.add(newEntry)
911
+ context.ruleCache.add(newEntry)
912
+ allRules.push(newEntry)
613
913
  }
914
+ }
614
915
 
615
- return [sort | context.layerOrder[layer], rule]
616
- })
916
+ return allRules
617
917
  }
618
918
 
619
919
  function isArbitraryValue(input) {