tailwindcss 0.0.0-insiders.4e21639 → 0.0.0-insiders.4e62334

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 (157) hide show
  1. package/CHANGELOG.md +435 -3
  2. package/LICENSE +1 -2
  3. package/README.md +13 -6
  4. package/colors.d.ts +3 -0
  5. package/colors.js +2 -1
  6. package/defaultConfig.d.ts +3 -0
  7. package/defaultConfig.js +2 -1
  8. package/defaultTheme.d.ts +4 -0
  9. package/defaultTheme.js +2 -1
  10. package/lib/cli-peer-dependencies.js +20 -7
  11. package/lib/cli.js +460 -259
  12. package/lib/constants.js +27 -20
  13. package/lib/corePluginList.js +9 -3
  14. package/lib/corePlugins.js +1840 -1753
  15. package/lib/css/preflight.css +8 -9
  16. package/lib/featureFlags.js +29 -20
  17. package/lib/index.js +22 -11
  18. package/lib/lib/cacheInvalidation.js +90 -0
  19. package/lib/lib/collapseAdjacentRules.js +27 -9
  20. package/lib/lib/collapseDuplicateDeclarations.js +58 -4
  21. package/lib/lib/defaultExtractor.js +231 -0
  22. package/lib/lib/detectNesting.js +13 -10
  23. package/lib/lib/evaluateTailwindFunctions.js +119 -52
  24. package/lib/lib/expandApplyAtRules.js +440 -161
  25. package/lib/lib/expandTailwindAtRules.js +130 -128
  26. package/lib/lib/generateRules.js +410 -171
  27. package/lib/lib/getModuleDependencies.js +11 -8
  28. package/lib/lib/normalizeTailwindDirectives.js +43 -32
  29. package/lib/lib/offsets.js +199 -0
  30. package/lib/lib/partitionApplyAtRules.js +56 -0
  31. package/lib/lib/regex.js +60 -0
  32. package/lib/lib/resolveDefaultsAtRules.js +91 -55
  33. package/lib/lib/setupContextUtils.js +453 -347
  34. package/lib/lib/setupTrackingContext.js +52 -44
  35. package/lib/lib/sharedState.js +48 -9
  36. package/lib/lib/substituteScreenAtRules.js +13 -8
  37. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  38. package/lib/postcss-plugins/nesting/index.js +19 -0
  39. package/lib/postcss-plugins/nesting/plugin.js +87 -0
  40. package/lib/processTailwindFeatures.js +36 -25
  41. package/lib/public/colors.js +247 -245
  42. package/lib/public/create-plugin.js +6 -4
  43. package/lib/public/default-config.js +7 -5
  44. package/lib/public/default-theme.js +7 -5
  45. package/lib/public/resolve-config.js +8 -5
  46. package/lib/util/bigSign.js +4 -1
  47. package/lib/util/buildMediaQuery.js +16 -25
  48. package/lib/util/cloneDeep.js +7 -6
  49. package/lib/util/cloneNodes.js +21 -3
  50. package/lib/util/color.js +54 -40
  51. package/lib/util/configurePlugins.js +4 -1
  52. package/lib/util/createPlugin.js +6 -6
  53. package/lib/util/createUtilityPlugin.js +12 -14
  54. package/lib/util/dataTypes.js +119 -102
  55. package/lib/util/defaults.js +10 -1
  56. package/lib/util/escapeClassName.js +7 -4
  57. package/lib/util/escapeCommas.js +5 -2
  58. package/lib/util/flattenColorPalette.js +9 -12
  59. package/lib/util/formatVariantSelector.js +223 -40
  60. package/lib/util/getAllConfigs.js +27 -8
  61. package/lib/util/hashConfig.js +6 -3
  62. package/lib/util/isKeyframeRule.js +5 -2
  63. package/lib/util/isPlainObject.js +5 -2
  64. package/lib/util/isValidArbitraryValue.js +72 -0
  65. package/lib/util/log.js +22 -12
  66. package/lib/util/nameClass.js +17 -8
  67. package/lib/util/negateValue.js +22 -7
  68. package/lib/util/normalizeConfig.js +99 -54
  69. package/lib/util/normalizeScreens.js +73 -0
  70. package/lib/util/parseAnimationValue.js +42 -40
  71. package/lib/util/parseBoxShadowValue.js +30 -23
  72. package/lib/util/parseDependency.js +18 -15
  73. package/lib/util/parseObjectStyles.js +10 -7
  74. package/lib/util/pluginUtils.js +51 -42
  75. package/lib/util/prefixSelector.js +10 -8
  76. package/lib/util/removeAlphaVariables.js +29 -0
  77. package/lib/util/resolveConfig.js +91 -66
  78. package/lib/util/resolveConfigPath.js +11 -9
  79. package/lib/util/responsive.js +8 -5
  80. package/lib/util/splitAtTopLevelOnly.js +43 -0
  81. package/lib/util/tap.js +4 -1
  82. package/lib/util/toColorValue.js +5 -3
  83. package/lib/util/toPath.js +24 -3
  84. package/lib/util/transformThemeValue.js +37 -29
  85. package/lib/util/validateConfig.js +24 -0
  86. package/lib/util/withAlphaVariable.js +23 -15
  87. package/nesting/index.js +2 -12
  88. package/package.json +43 -43
  89. package/peers/index.js +11543 -8114
  90. package/plugin.d.ts +11 -0
  91. package/plugin.js +2 -1
  92. package/resolveConfig.d.ts +3 -0
  93. package/resolveConfig.js +2 -1
  94. package/scripts/generate-types.js +105 -0
  95. package/scripts/type-utils.js +27 -0
  96. package/src/cli-peer-dependencies.js +7 -1
  97. package/src/cli.js +293 -36
  98. package/src/corePluginList.js +1 -1
  99. package/src/corePlugins.js +428 -285
  100. package/src/css/preflight.css +8 -9
  101. package/src/featureFlags.js +9 -5
  102. package/src/index.js +14 -6
  103. package/src/lib/cacheInvalidation.js +52 -0
  104. package/src/lib/collapseAdjacentRules.js +21 -2
  105. package/src/lib/collapseDuplicateDeclarations.js +66 -1
  106. package/src/lib/defaultExtractor.js +208 -0
  107. package/src/lib/evaluateTailwindFunctions.js +84 -10
  108. package/src/lib/expandApplyAtRules.js +488 -153
  109. package/src/lib/expandTailwindAtRules.js +82 -80
  110. package/src/lib/generateRules.js +352 -120
  111. package/src/lib/normalizeTailwindDirectives.js +7 -1
  112. package/src/lib/offsets.js +239 -0
  113. package/src/lib/partitionApplyAtRules.js +52 -0
  114. package/src/lib/regex.js +74 -0
  115. package/src/lib/resolveDefaultsAtRules.js +59 -17
  116. package/src/lib/setupContextUtils.js +337 -174
  117. package/src/lib/setupTrackingContext.js +13 -8
  118. package/src/lib/sharedState.js +42 -4
  119. package/src/lib/substituteScreenAtRules.js +6 -3
  120. package/src/postcss-plugins/nesting/README.md +42 -0
  121. package/src/postcss-plugins/nesting/index.js +13 -0
  122. package/src/postcss-plugins/nesting/plugin.js +80 -0
  123. package/src/processTailwindFeatures.js +12 -2
  124. package/src/util/buildMediaQuery.js +14 -18
  125. package/src/util/cloneNodes.js +19 -2
  126. package/src/util/color.js +31 -14
  127. package/src/util/dataTypes.js +28 -19
  128. package/src/util/defaults.js +6 -0
  129. package/src/util/formatVariantSelector.js +237 -23
  130. package/src/util/getAllConfigs.js +19 -0
  131. package/src/util/isValidArbitraryValue.js +61 -0
  132. package/src/util/log.js +11 -7
  133. package/src/util/nameClass.js +1 -1
  134. package/src/util/negateValue.js +10 -2
  135. package/src/util/normalizeConfig.js +16 -3
  136. package/src/util/normalizeScreens.js +45 -0
  137. package/src/util/parseBoxShadowValue.js +4 -3
  138. package/src/util/pluginUtils.js +14 -2
  139. package/src/util/prefixSelector.js +7 -5
  140. package/src/util/removeAlphaVariables.js +24 -0
  141. package/src/util/resolveConfig.js +71 -17
  142. package/src/util/splitAtTopLevelOnly.js +45 -0
  143. package/src/util/toPath.js +23 -1
  144. package/src/util/transformThemeValue.js +13 -3
  145. package/src/util/validateConfig.js +13 -0
  146. package/src/util/withAlphaVariable.js +1 -1
  147. package/stubs/defaultConfig.stub.js +24 -3
  148. package/stubs/simpleConfig.stub.js +1 -0
  149. package/types/config.d.ts +334 -0
  150. package/types/generated/.gitkeep +0 -0
  151. package/types/generated/colors.d.ts +276 -0
  152. package/types/generated/corePluginList.d.ts +1 -0
  153. package/types/generated/default-theme.d.ts +331 -0
  154. package/types/index.d.ts +7 -0
  155. package/lib/lib/setupWatchingContext.js +0 -288
  156. package/nesting/plugin.js +0 -41
  157. package/src/lib/setupWatchingContext.js +0 -311
@@ -5,7 +5,14 @@ import isPlainObject from '../util/isPlainObject'
5
5
  import prefixSelector from '../util/prefixSelector'
6
6
  import { updateAllClasses } from '../util/pluginUtils'
7
7
  import log from '../util/log'
8
+ import * as sharedState from './sharedState'
8
9
  import { formatVariantSelector, finalizeSelector } from '../util/formatVariantSelector'
10
+ import { asClass } from '../util/nameClass'
11
+ import { normalize } from '../util/dataTypes'
12
+ import { isValidVariantFormatString, parseVariant } from './setupContextUtils'
13
+ import isValidArbitraryValue from '../util/isValidArbitraryValue'
14
+ import { splitAtTopLevelOnly } from '../util/splitAtTopLevelOnly.js'
15
+ import { flagEnabled } from '../featureFlags'
9
16
 
10
17
  let classNameParser = selectorParser((selectors) => {
11
18
  return selectors.first.filter(({ type }) => type === 'class').pop().value
@@ -22,33 +29,33 @@ function getClassNameFromSelector(selector) {
22
29
  // Example with dynamic classes:
23
30
  // ['grid-cols', '[[linename],1fr,auto]']
24
31
  // ['grid', 'cols-[[linename],1fr,auto]']
25
- function* candidatePermutations(candidate, lastIndex = Infinity) {
26
- if (lastIndex < 0) {
27
- return
28
- }
32
+ function* candidatePermutations(candidate) {
33
+ let lastIndex = Infinity
29
34
 
30
- let dashIdx
35
+ while (lastIndex >= 0) {
36
+ let dashIdx
31
37
 
32
- if (lastIndex === Infinity && candidate.endsWith(']')) {
33
- let bracketIdx = candidate.indexOf('[')
38
+ if (lastIndex === Infinity && candidate.endsWith(']')) {
39
+ let bracketIdx = candidate.indexOf('[')
34
40
 
35
- // If character before `[` isn't a dash or a slash, this isn't a dynamic class
36
- // eg. string[]
37
- dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
38
- } else {
39
- dashIdx = candidate.lastIndexOf('-', lastIndex)
40
- }
41
+ // If character before `[` isn't a dash or a slash, this isn't a dynamic class
42
+ // eg. string[]
43
+ dashIdx = ['-', '/'].includes(candidate[bracketIdx - 1]) ? bracketIdx - 1 : -1
44
+ } else {
45
+ dashIdx = candidate.lastIndexOf('-', lastIndex)
46
+ }
41
47
 
42
- if (dashIdx < 0) {
43
- return
44
- }
48
+ if (dashIdx < 0) {
49
+ break
50
+ }
45
51
 
46
- let prefix = candidate.slice(0, dashIdx)
47
- let modifier = candidate.slice(dashIdx + 1)
52
+ let prefix = candidate.slice(0, dashIdx)
53
+ let modifier = candidate.slice(dashIdx + 1)
48
54
 
49
- yield [prefix, modifier]
55
+ yield [prefix, modifier]
50
56
 
51
- yield* candidatePermutations(candidate, dashIdx - 1)
57
+ lastIndex = dashIdx - 1
58
+ }
52
59
  }
53
60
 
54
61
  function applyPrefix(matches, context) {
@@ -60,9 +67,23 @@ function applyPrefix(matches, context) {
60
67
  let [meta] = match
61
68
  if (meta.options.respectPrefix) {
62
69
  let container = postcss.root({ nodes: [match[1].clone()] })
70
+ let classCandidate = match[1].raws.tailwind.classCandidate
71
+
63
72
  container.walkRules((r) => {
64
- r.selector = prefixSelector(context.tailwindConfig.prefix, r.selector)
73
+ // If this is a negative utility with a dash *before* the prefix we
74
+ // have to ensure that the generated selector matches the candidate
75
+
76
+ // Not doing this will cause `-tw-top-1` to generate the class `.tw--top-1`
77
+ // The disconnect between candidate <-> class can cause @apply to hard crash.
78
+ let shouldPrependNegative = classCandidate.startsWith('-')
79
+
80
+ r.selector = prefixSelector(
81
+ context.tailwindConfig.prefix,
82
+ r.selector,
83
+ shouldPrependNegative
84
+ )
65
85
  })
86
+
66
87
  match[1] = container.nodes[0]
67
88
  }
68
89
  }
@@ -70,7 +91,7 @@ function applyPrefix(matches, context) {
70
91
  return matches
71
92
  }
72
93
 
73
- function applyImportant(matches) {
94
+ function applyImportant(matches, classCandidate) {
74
95
  if (matches.length === 0) {
75
96
  return matches
76
97
  }
@@ -80,7 +101,10 @@ function applyImportant(matches) {
80
101
  let container = postcss.root({ nodes: [rule.clone()] })
81
102
  container.walkRules((r) => {
82
103
  r.selector = updateAllClasses(r.selector, (className) => {
83
- return `!${className}`
104
+ if (className === classCandidate) {
105
+ return `!${className}`
106
+ }
107
+ return className
84
108
  })
85
109
  r.walkDecls((d) => (d.important = true))
86
110
  })
@@ -104,22 +128,52 @@ function applyVariant(variant, matches, context) {
104
128
  return matches
105
129
  }
106
130
 
131
+ let args
132
+
133
+ // Find partial arbitrary variants
134
+ if (variant.endsWith(']') && !variant.startsWith('[')) {
135
+ args = variant.slice(variant.lastIndexOf('[') + 1, -1)
136
+ variant = variant.slice(0, variant.indexOf(args) - 1 /* - */ - 1 /* [ */)
137
+ }
138
+
139
+ // Register arbitrary variants
140
+ if (isArbitraryValue(variant) && !context.variantMap.has(variant)) {
141
+ let selector = normalize(variant.slice(1, -1))
142
+
143
+ if (!isValidVariantFormatString(selector)) {
144
+ return []
145
+ }
146
+
147
+ let fn = parseVariant(selector)
148
+
149
+ let sort = context.offsets.recordVariant(variant)
150
+
151
+ context.variantMap.set(variant, [[sort, fn]])
152
+ }
153
+
107
154
  if (context.variantMap.has(variant)) {
108
- let variantFunctionTuples = context.variantMap.get(variant)
155
+ let variantFunctionTuples = context.variantMap.get(variant).slice()
109
156
  let result = []
110
157
 
111
158
  for (let [meta, rule] of matches) {
159
+ // Don't generate variants for user css
160
+ if (meta.layer === 'user') {
161
+ continue
162
+ }
163
+
112
164
  let container = postcss.root({ nodes: [rule.clone()] })
113
165
 
114
- for (let [variantSort, variantFunction] of variantFunctionTuples) {
115
- let clone = container.clone()
166
+ for (let [variantSort, variantFunction, containerFromArray] of variantFunctionTuples) {
167
+ let clone = containerFromArray ?? container.clone()
116
168
  let collectedFormats = []
117
169
 
118
- let originals = new Map()
119
-
120
170
  function prepareBackup() {
121
- if (originals.size > 0) return // Already prepared, chicken out
122
- clone.walkRules((rule) => originals.set(rule, rule.selector))
171
+ // Already prepared, chicken out
172
+ if (clone.raws.neededBackup) {
173
+ return
174
+ }
175
+ clone.raws.neededBackup = true
176
+ clone.walkRules((rule) => (rule.raws.originalSelector = rule.selector))
123
177
  }
124
178
 
125
179
  function modifySelectors(modifierFunction) {
@@ -161,8 +215,29 @@ function applyVariant(variant, matches, context) {
161
215
  format(selectorFormat) {
162
216
  collectedFormats.push(selectorFormat)
163
217
  },
218
+ args,
164
219
  })
165
220
 
221
+ // It can happen that a list of format strings is returned from within the function. In that
222
+ // case, we have to process them as well. We can use the existing `variantSort`.
223
+ if (Array.isArray(ruleWithVariant)) {
224
+ for (let [idx, variantFunction] of ruleWithVariant.entries()) {
225
+ // This is a little bit scary since we are pushing to an array of items that we are
226
+ // currently looping over. However, you can also think of it like a processing queue
227
+ // where you keep handling jobs until everything is done and each job can queue more
228
+ // jobs if needed.
229
+ variantFunctionTuples.push([
230
+ context.offsets.applyParallelOffset(variantSort, idx),
231
+ variantFunction,
232
+
233
+ // If the clone has been modified we have to pass that back
234
+ // though so each rule can use the modified container
235
+ clone.clone(),
236
+ ])
237
+ }
238
+ continue
239
+ }
240
+
166
241
  if (typeof ruleWithVariant === 'string') {
167
242
  collectedFormats.push(ruleWithVariant)
168
243
  }
@@ -171,13 +246,15 @@ function applyVariant(variant, matches, context) {
171
246
  continue
172
247
  }
173
248
 
174
- // We filled the `originals`, therefore we assume that somebody touched
249
+ // We had to backup selectors, therefore we assume that somebody touched
175
250
  // `container` or `modifySelectors`. Let's see if they did, so that we
176
251
  // can restore the selectors, and collect the format strings.
177
- if (originals.size > 0) {
252
+ if (clone.raws.neededBackup) {
253
+ delete clone.raws.neededBackup
178
254
  clone.walkRules((rule) => {
179
- if (!originals.has(rule)) return
180
- let before = originals.get(rule)
255
+ let before = rule.raws.originalSelector
256
+ if (!before) return
257
+ delete rule.raws.originalSelector
181
258
  if (before === rule.selector) return // No mutation happened
182
259
 
183
260
  let modified = rule.selector
@@ -208,11 +285,18 @@ function applyVariant(variant, matches, context) {
208
285
  })
209
286
  }
210
287
 
288
+ // This tracks the originating layer for the variant
289
+ // For example:
290
+ // .sm:underline {} is a variant of something in the utilities layer
291
+ // .sm:container {} is a variant of the container component
292
+ clone.nodes[0].raws.tailwind = { ...clone.nodes[0].raws.tailwind, parentLayer: meta.layer }
293
+
211
294
  let withOffset = [
212
295
  {
213
296
  ...meta,
214
- sort: variantSort | meta.sort,
297
+ sort: context.offsets.applyVariantOffset(meta.sort, variantSort),
215
298
  collectedFormats: (meta.collectedFormats ?? []).concat(collectedFormats),
299
+ isArbitraryVariant: isArbitraryValue(variant),
216
300
  },
217
301
  clone.nodes[0],
218
302
  ]
@@ -245,18 +329,118 @@ function parseRules(rule, cache, options = {}) {
245
329
  return [cache.get(rule), options]
246
330
  }
247
331
 
332
+ const IS_VALID_PROPERTY_NAME = /^[a-z_-]/
333
+
334
+ function isValidPropName(name) {
335
+ return IS_VALID_PROPERTY_NAME.test(name)
336
+ }
337
+
338
+ /**
339
+ * @param {string} declaration
340
+ * @returns {boolean}
341
+ */
342
+ function looksLikeUri(declaration) {
343
+ // Quick bailout for obvious non-urls
344
+ // This doesn't support schemes that don't use a leading // but that's unlikely to be a problem
345
+ if (!declaration.includes('://')) {
346
+ return false
347
+ }
348
+
349
+ try {
350
+ const url = new URL(declaration)
351
+ return url.scheme !== '' && url.host !== ''
352
+ } catch (err) {
353
+ // Definitely not a valid url
354
+ return false
355
+ }
356
+ }
357
+
358
+ function isParsableNode(node) {
359
+ let isParsable = true
360
+
361
+ node.walkDecls((decl) => {
362
+ if (!isParsableCssValue(decl.name, decl.value)) {
363
+ isParsable = false
364
+ return false
365
+ }
366
+ })
367
+
368
+ return isParsable
369
+ }
370
+
371
+ function isParsableCssValue(property, value) {
372
+ // We don't want to to treat [https://example.com] as a custom property
373
+ // Even though, according to the CSS grammar, it's a totally valid CSS declaration
374
+ // So we short-circuit here by checking if the custom property looks like a url
375
+ if (looksLikeUri(`${property}:${value}`)) {
376
+ return false
377
+ }
378
+
379
+ try {
380
+ postcss.parse(`a{${property}:${value}}`).toResult()
381
+ return true
382
+ } catch (err) {
383
+ return false
384
+ }
385
+ }
386
+
387
+ function extractArbitraryProperty(classCandidate, context) {
388
+ let [, property, value] = classCandidate.match(/^\[([a-zA-Z0-9-_]+):(\S+)\]$/) ?? []
389
+
390
+ if (value === undefined) {
391
+ return null
392
+ }
393
+
394
+ if (!isValidPropName(property)) {
395
+ return null
396
+ }
397
+
398
+ if (!isValidArbitraryValue(value)) {
399
+ return null
400
+ }
401
+
402
+ let normalized = normalize(value)
403
+
404
+ if (!isParsableCssValue(property, normalized)) {
405
+ return null
406
+ }
407
+
408
+ let sort = context.offsets.arbitraryProperty()
409
+
410
+ return [
411
+ [
412
+ { sort, layer: 'utilities' },
413
+ () => ({
414
+ [asClass(classCandidate)]: {
415
+ [property]: normalized,
416
+ },
417
+ }),
418
+ ],
419
+ ]
420
+ }
421
+
248
422
  function* resolveMatchedPlugins(classCandidate, context) {
249
423
  if (context.candidateRuleMap.has(classCandidate)) {
250
424
  yield [context.candidateRuleMap.get(classCandidate), 'DEFAULT']
251
425
  }
252
426
 
427
+ yield* (function* (arbitraryPropertyRule) {
428
+ if (arbitraryPropertyRule !== null) {
429
+ yield [arbitraryPropertyRule, 'DEFAULT']
430
+ }
431
+ })(extractArbitraryProperty(classCandidate, context))
432
+
253
433
  let candidatePrefix = classCandidate
254
434
  let negative = false
255
435
 
256
436
  const twConfigPrefix = context.tailwindConfig.prefix
257
437
 
258
438
  const twConfigPrefixLen = twConfigPrefix.length
259
- if (candidatePrefix[twConfigPrefixLen] === '-') {
439
+
440
+ const hasMatchingPrefix =
441
+ candidatePrefix.startsWith(twConfigPrefix) || candidatePrefix.startsWith(`-${twConfigPrefix}`)
442
+
443
+ if (candidatePrefix[twConfigPrefixLen] === '-' && hasMatchingPrefix) {
260
444
  negative = true
261
445
  candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1)
262
446
  }
@@ -268,16 +452,31 @@ function* resolveMatchedPlugins(classCandidate, context) {
268
452
  for (let [prefix, modifier] of candidatePermutations(candidatePrefix)) {
269
453
  if (context.candidateRuleMap.has(prefix)) {
270
454
  yield [context.candidateRuleMap.get(prefix), negative ? `-${modifier}` : modifier]
271
- return
272
455
  }
273
456
  }
274
457
  }
275
458
 
276
459
  function splitWithSeparator(input, separator) {
277
- return input.split(new RegExp(`\\${separator}(?![^[]*\\])`, 'g'))
460
+ if (input === sharedState.NOT_ON_DEMAND) {
461
+ return [sharedState.NOT_ON_DEMAND]
462
+ }
463
+
464
+ return splitAtTopLevelOnly(input, separator)
465
+ }
466
+
467
+ function* recordCandidates(matches, classCandidate) {
468
+ for (const match of matches) {
469
+ match[1].raws.tailwind = {
470
+ ...match[1].raws.tailwind,
471
+ classCandidate,
472
+ preserveSource: match[0].options?.preserveSource ?? false,
473
+ }
474
+
475
+ yield match
476
+ }
278
477
  }
279
478
 
280
- function* resolveMatches(candidate, context) {
479
+ function* resolveMatches(candidate, context, original = candidate) {
281
480
  let separator = context.tailwindConfig.separator
282
481
  let [classCandidate, ...variants] = splitWithSeparator(candidate, separator).reverse()
283
482
  let important = false
@@ -287,6 +486,15 @@ function* resolveMatches(candidate, context) {
287
486
  classCandidate = classCandidate.slice(1)
288
487
  }
289
488
 
489
+ if (flagEnabled(context.tailwindConfig, 'variantGrouping')) {
490
+ if (classCandidate.startsWith('(') && classCandidate.endsWith(')')) {
491
+ let base = variants.slice().reverse().join(separator)
492
+ for (let part of splitAtTopLevelOnly(classCandidate.slice(1, -1), ',')) {
493
+ yield* resolveMatches(base + separator + part, context, original)
494
+ }
495
+ }
496
+ }
497
+
290
498
  // TODO: Reintroduce this in ways that doesn't break on false positives
291
499
  // function sortAgainst(toSort, against) {
292
500
  // return toSort.slice().sort((a, z) => {
@@ -332,66 +540,74 @@ function* resolveMatches(candidate, context) {
332
540
  }
333
541
  }
334
542
 
335
- // Only keep the result of the very first plugin if we are dealing with
336
- // arbitrary values, to protect against ambiguity.
337
- if (isArbitraryValue(modifier) && matches.length > 1) {
338
- let typesPerPlugin = matches.map((match) => new Set([...(typesByMatches.get(match) ?? [])]))
543
+ if (isArbitraryValue(modifier)) {
544
+ // When generated arbitrary values are ambiguous, we can't know
545
+ // which to pick so don't generate any utilities for them
546
+ if (matches.length > 1) {
547
+ let typesPerPlugin = matches.map((match) => new Set([...(typesByMatches.get(match) ?? [])]))
339
548
 
340
- // Remove duplicates, so that we can detect proper unique types for each plugin.
341
- for (let pluginTypes of typesPerPlugin) {
342
- for (let type of pluginTypes) {
343
- let removeFromOwnGroup = false
549
+ // Remove duplicates, so that we can detect proper unique types for each plugin.
550
+ for (let pluginTypes of typesPerPlugin) {
551
+ for (let type of pluginTypes) {
552
+ let removeFromOwnGroup = false
344
553
 
345
- for (let otherGroup of typesPerPlugin) {
346
- if (pluginTypes === otherGroup) continue
554
+ for (let otherGroup of typesPerPlugin) {
555
+ if (pluginTypes === otherGroup) continue
347
556
 
348
- if (otherGroup.has(type)) {
349
- otherGroup.delete(type)
350
- removeFromOwnGroup = true
557
+ if (otherGroup.has(type)) {
558
+ otherGroup.delete(type)
559
+ removeFromOwnGroup = true
560
+ }
351
561
  }
352
- }
353
562
 
354
- if (removeFromOwnGroup) pluginTypes.delete(type)
563
+ if (removeFromOwnGroup) pluginTypes.delete(type)
564
+ }
355
565
  }
356
- }
357
566
 
358
- let messages = []
359
-
360
- for (let [idx, group] of typesPerPlugin.entries()) {
361
- for (let type of group) {
362
- let rules = matches[idx]
363
- .map(([, rule]) => rule)
364
- .flat()
365
- .map((rule) =>
366
- rule
367
- .toString()
368
- .split('\n')
369
- .slice(1, -1) // Remove selector and closing '}'
370
- .map((line) => line.trim())
371
- .map((x) => ` ${x}`) // Re-indent
372
- .join('\n')
567
+ let messages = []
568
+
569
+ for (let [idx, group] of typesPerPlugin.entries()) {
570
+ for (let type of group) {
571
+ let rules = matches[idx]
572
+ .map(([, rule]) => rule)
573
+ .flat()
574
+ .map((rule) =>
575
+ rule
576
+ .toString()
577
+ .split('\n')
578
+ .slice(1, -1) // Remove selector and closing '}'
579
+ .map((line) => line.trim())
580
+ .map((x) => ` ${x}`) // Re-indent
581
+ .join('\n')
582
+ )
583
+ .join('\n\n')
584
+
585
+ messages.push(
586
+ ` Use \`${candidate.replace('[', `[${type}:`)}\` for \`${rules.trim()}\``
373
587
  )
374
- .join('\n\n')
375
-
376
- messages.push(` Use \`${candidate.replace('[', `[${type}:`)}\` for \`${rules.trim()}\``)
377
- break
588
+ break
589
+ }
378
590
  }
591
+
592
+ log.warn([
593
+ `The class \`${candidate}\` is ambiguous and matches multiple utilities.`,
594
+ ...messages,
595
+ `If this is content and not a class, replace it with \`${candidate
596
+ .replace('[', '&lsqb;')
597
+ .replace(']', '&rsqb;')}\` to silence this warning.`,
598
+ ])
599
+ continue
379
600
  }
380
601
 
381
- log.warn([
382
- `The class \`${candidate}\` is ambiguous and matches multiple utilities.`,
383
- ...messages,
384
- `If this is content and not a class, replace it with \`${candidate
385
- .replace('[', '&lsqb;')
386
- .replace(']', '&rsqb;')}\` to silence this warning.`,
387
- ])
388
- continue
602
+ matches = matches.map((list) => list.filter((match) => isParsableNode(match[1])))
389
603
  }
390
604
 
391
- matches = applyPrefix(matches.flat(), context)
605
+ matches = matches.flat()
606
+ matches = Array.from(recordCandidates(matches, classCandidate))
607
+ matches = applyPrefix(matches, context)
392
608
 
393
609
  if (important) {
394
- matches = applyImportant(matches, context)
610
+ matches = applyImportant(matches, classCandidate)
395
611
  }
396
612
 
397
613
  for (let variant of variants) {
@@ -399,6 +615,8 @@ function* resolveMatches(candidate, context) {
399
615
  }
400
616
 
401
617
  for (let match of matches) {
618
+ match[1].raws.tailwind = { ...match[1].raws.tailwind, candidate }
619
+
402
620
  // Apply final format selector
403
621
  if (match[0].collectedFormats) {
404
622
  let finalFormat = formatVariantSelector('&', ...match[0].collectedFormats)
@@ -408,7 +626,12 @@ function* resolveMatches(candidate, context) {
408
626
 
409
627
  rule.selector = finalizeSelector(finalFormat, {
410
628
  selector: rule.selector,
411
- candidate,
629
+ candidate: original,
630
+ base: candidate
631
+ .split(new RegExp(`\\${context?.tailwindConfig?.separator ?? ':'}(?![^[]*\\])`))
632
+ .pop(),
633
+ isArbitraryVariant: match[0].isArbitraryVariant,
634
+
412
635
  context,
413
636
  })
414
637
  })
@@ -424,16 +647,45 @@ function inKeyframes(rule) {
424
647
  return rule.parent && rule.parent.type === 'atrule' && rule.parent.name === 'keyframes'
425
648
  }
426
649
 
650
+ function getImportantStrategy(important) {
651
+ if (important === true) {
652
+ return (rule) => {
653
+ if (inKeyframes(rule)) {
654
+ return
655
+ }
656
+
657
+ rule.walkDecls((d) => {
658
+ if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
659
+ d.important = true
660
+ }
661
+ })
662
+ }
663
+ }
664
+
665
+ if (typeof important === 'string') {
666
+ return (rule) => {
667
+ if (inKeyframes(rule)) {
668
+ return
669
+ }
670
+
671
+ rule.selectors = rule.selectors.map((selector) => {
672
+ return `${important} ${selector}`
673
+ })
674
+ }
675
+ }
676
+ }
677
+
427
678
  function generateRules(candidates, context) {
428
679
  let allRules = []
680
+ let strategy = getImportantStrategy(context.tailwindConfig.important)
429
681
 
430
682
  for (let candidate of candidates) {
431
683
  if (context.notClassCache.has(candidate)) {
432
684
  continue
433
685
  }
434
686
 
435
- if (context.classCache.has(candidate)) {
436
- allRules.push(context.classCache.get(candidate))
687
+ if (context.candidateRuleCache.has(candidate)) {
688
+ allRules = allRules.concat(Array.from(context.candidateRuleCache.get(candidate)))
437
689
  continue
438
690
  }
439
691
 
@@ -445,47 +697,27 @@ function generateRules(candidates, context) {
445
697
  }
446
698
 
447
699
  context.classCache.set(candidate, matches)
448
- allRules.push(matches)
449
- }
450
700
 
451
- // Strategy based on `tailwindConfig.important`
452
- let strategy = ((important) => {
453
- if (important === true) {
454
- return (rule) => {
455
- rule.walkDecls((d) => {
456
- if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
457
- d.important = true
458
- }
459
- })
460
- }
461
- }
701
+ let rules = context.candidateRuleCache.get(candidate) ?? new Set()
702
+ context.candidateRuleCache.set(candidate, rules)
462
703
 
463
- if (typeof important === 'string') {
464
- return (rule) => {
465
- rule.selectors = rule.selectors.map((selector) => {
466
- return `${important} ${selector}`
467
- })
468
- }
469
- }
470
- })(context.tailwindConfig.important)
704
+ for (const match of matches) {
705
+ let [{ sort, options }, rule] = match
471
706
 
472
- return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
473
- if (options.respectImportant) {
474
- if (strategy) {
707
+ if (options.respectImportant && strategy) {
475
708
  let container = postcss.root({ nodes: [rule.clone()] })
476
- container.walkRules((r) => {
477
- if (inKeyframes(r)) {
478
- return
479
- }
480
-
481
- strategy(r)
482
- })
709
+ container.walkRules(strategy)
483
710
  rule = container.nodes[0]
484
711
  }
712
+
713
+ let newEntry = [sort, rule]
714
+ rules.add(newEntry)
715
+ context.ruleCache.add(newEntry)
716
+ allRules.push(newEntry)
485
717
  }
718
+ }
486
719
 
487
- return [sort | context.layerOrder[layer], rule]
488
- })
720
+ return allRules
489
721
  }
490
722
 
491
723
  function isArbitraryValue(input) {
@@ -3,8 +3,13 @@ import log from '../util/log'
3
3
  export default function normalizeTailwindDirectives(root) {
4
4
  let tailwindDirectives = new Set()
5
5
  let layerDirectives = new Set()
6
+ let applyDirectives = new Set()
6
7
 
7
8
  root.walkAtRules((atRule) => {
9
+ if (atRule.name === 'apply') {
10
+ applyDirectives.add(atRule)
11
+ }
12
+
8
13
  if (atRule.name === 'import') {
9
14
  if (atRule.params === '"tailwindcss/base"' || atRule.params === "'tailwindcss/base'") {
10
15
  atRule.name = 'tailwind'
@@ -44,6 +49,7 @@ export default function normalizeTailwindDirectives(root) {
44
49
  log.warn(`${atRule.name}-at-rule-deprecated`, [
45
50
  `The \`@${atRule.name}\` directive has been deprecated in Tailwind CSS v3.0.`,
46
51
  `Use \`@layer utilities\` or \`@layer components\` instead.`,
52
+ 'https://tailwindcss.com/docs/upgrade-guide#replace-variants-with-layer',
47
53
  ])
48
54
  }
49
55
  layerDirectives.add(atRule)
@@ -74,5 +80,5 @@ export default function normalizeTailwindDirectives(root) {
74
80
  }
75
81
  }
76
82
 
77
- return tailwindDirectives
83
+ return { tailwindDirectives, applyDirectives }
78
84
  }