tailwindcss 0.0.0-insiders.ca1dfd6 → 0.0.0-insiders.cb6e45a

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 (190) hide show
  1. package/CHANGELOG.md +526 -2
  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/build/deps.js +54 -0
  11. package/lib/cli/build/index.js +44 -0
  12. package/lib/cli/build/plugin.js +335 -0
  13. package/lib/cli/build/utils.js +78 -0
  14. package/lib/cli/build/watching.js +113 -0
  15. package/lib/cli/help/index.js +71 -0
  16. package/lib/cli/index.js +18 -0
  17. package/lib/cli/init/index.js +46 -0
  18. package/lib/cli/shared.js +12 -0
  19. package/lib/cli-peer-dependencies.js +20 -7
  20. package/lib/cli.js +91 -569
  21. package/lib/constants.js +27 -20
  22. package/lib/corePluginList.js +16 -4
  23. package/lib/corePlugins.js +2247 -1873
  24. package/lib/css/preflight.css +20 -9
  25. package/lib/featureFlags.js +37 -23
  26. package/lib/index.js +28 -12
  27. package/lib/lib/cacheInvalidation.js +90 -0
  28. package/lib/lib/collapseAdjacentRules.js +27 -9
  29. package/lib/lib/collapseDuplicateDeclarations.js +83 -0
  30. package/lib/lib/content.js +167 -0
  31. package/lib/lib/defaultExtractor.js +236 -0
  32. package/lib/lib/detectNesting.js +21 -3
  33. package/lib/lib/evaluateTailwindFunctions.js +117 -50
  34. package/lib/lib/expandApplyAtRules.js +439 -160
  35. package/lib/lib/expandTailwindAtRules.js +141 -151
  36. package/lib/lib/findAtConfigPath.js +44 -0
  37. package/lib/lib/generateRules.js +572 -105
  38. package/lib/lib/getModuleDependencies.js +11 -8
  39. package/lib/lib/normalizeTailwindDirectives.js +45 -33
  40. package/lib/lib/offsets.js +217 -0
  41. package/lib/lib/partitionApplyAtRules.js +56 -0
  42. package/lib/lib/regex.js +60 -0
  43. package/lib/lib/resolveDefaultsAtRules.js +114 -81
  44. package/lib/lib/setupContextUtils.js +777 -385
  45. package/lib/lib/setupTrackingContext.js +43 -71
  46. package/lib/lib/sharedState.js +49 -18
  47. package/lib/lib/substituteScreenAtRules.js +13 -8
  48. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  49. package/lib/postcss-plugins/nesting/index.js +19 -0
  50. package/lib/postcss-plugins/nesting/plugin.js +87 -0
  51. package/lib/processTailwindFeatures.js +36 -21
  52. package/lib/public/colors.js +250 -252
  53. package/lib/public/create-plugin.js +6 -4
  54. package/lib/public/default-config.js +7 -5
  55. package/lib/public/default-theme.js +7 -5
  56. package/lib/public/resolve-config.js +8 -5
  57. package/lib/util/bigSign.js +4 -1
  58. package/lib/util/buildMediaQuery.js +18 -24
  59. package/lib/util/cloneDeep.js +7 -6
  60. package/lib/util/cloneNodes.js +21 -3
  61. package/lib/util/color.js +54 -40
  62. package/lib/util/configurePlugins.js +5 -2
  63. package/lib/util/createPlugin.js +6 -6
  64. package/lib/util/createUtilityPlugin.js +11 -13
  65. package/lib/util/dataTypes.js +137 -100
  66. package/lib/util/defaults.js +10 -1
  67. package/lib/util/escapeClassName.js +7 -4
  68. package/lib/util/escapeCommas.js +5 -2
  69. package/lib/util/flattenColorPalette.js +9 -12
  70. package/lib/util/formatVariantSelector.js +285 -0
  71. package/lib/util/getAllConfigs.js +27 -8
  72. package/lib/util/hashConfig.js +6 -3
  73. package/lib/util/isKeyframeRule.js +5 -2
  74. package/lib/util/isPlainObject.js +5 -2
  75. package/lib/util/isValidArbitraryValue.js +72 -0
  76. package/lib/util/log.js +44 -22
  77. package/lib/util/nameClass.js +20 -8
  78. package/lib/util/negateValue.js +23 -6
  79. package/lib/util/normalizeConfig.js +267 -0
  80. package/lib/util/normalizeScreens.js +170 -0
  81. package/lib/util/parseAnimationValue.js +42 -40
  82. package/lib/util/parseBoxShadowValue.js +84 -0
  83. package/lib/util/parseDependency.js +38 -56
  84. package/lib/util/parseGlob.js +34 -0
  85. package/lib/util/parseObjectStyles.js +11 -8
  86. package/lib/util/pluginUtils.js +171 -189
  87. package/lib/util/prefixSelector.js +10 -10
  88. package/lib/util/removeAlphaVariables.js +29 -0
  89. package/lib/util/resolveConfig.js +92 -126
  90. package/lib/util/resolveConfigPath.js +11 -9
  91. package/lib/util/responsive.js +8 -5
  92. package/lib/util/splitAtTopLevelOnly.js +43 -0
  93. package/lib/util/tap.js +4 -1
  94. package/lib/util/toColorValue.js +5 -3
  95. package/lib/util/toPath.js +24 -3
  96. package/lib/util/transformThemeValue.js +49 -31
  97. package/lib/util/validateConfig.js +24 -0
  98. package/lib/util/validateFormalSyntax.js +24 -0
  99. package/lib/util/withAlphaVariable.js +23 -15
  100. package/nesting/index.js +2 -12
  101. package/package.json +48 -46
  102. package/peers/index.js +14817 -12471
  103. package/plugin.d.ts +11 -0
  104. package/plugin.js +2 -1
  105. package/resolveConfig.d.ts +3 -0
  106. package/resolveConfig.js +2 -1
  107. package/scripts/create-plugin-list.js +2 -2
  108. package/scripts/generate-types.js +105 -0
  109. package/scripts/release-channel.js +18 -0
  110. package/scripts/release-notes.js +21 -0
  111. package/scripts/type-utils.js +27 -0
  112. package/src/cli/build/deps.js +56 -0
  113. package/src/cli/build/index.js +45 -0
  114. package/src/cli/build/plugin.js +397 -0
  115. package/src/cli/build/utils.js +76 -0
  116. package/src/cli/build/watching.js +134 -0
  117. package/src/cli/help/index.js +70 -0
  118. package/src/cli/index.js +3 -0
  119. package/src/cli/init/index.js +50 -0
  120. package/src/cli/shared.js +5 -0
  121. package/src/cli-peer-dependencies.js +7 -1
  122. package/src/cli.js +26 -565
  123. package/src/corePluginList.js +1 -1
  124. package/src/corePlugins.js +905 -615
  125. package/src/css/preflight.css +20 -9
  126. package/src/featureFlags.js +26 -10
  127. package/src/index.js +19 -6
  128. package/src/lib/cacheInvalidation.js +52 -0
  129. package/src/lib/collapseAdjacentRules.js +21 -2
  130. package/src/lib/collapseDuplicateDeclarations.js +93 -0
  131. package/src/lib/content.js +205 -0
  132. package/src/lib/defaultExtractor.js +211 -0
  133. package/src/lib/detectNesting.js +22 -3
  134. package/src/lib/evaluateTailwindFunctions.js +84 -10
  135. package/src/lib/expandApplyAtRules.js +488 -153
  136. package/src/lib/expandTailwindAtRules.js +94 -106
  137. package/src/lib/findAtConfigPath.js +50 -0
  138. package/src/lib/generateRules.js +589 -67
  139. package/src/lib/normalizeTailwindDirectives.js +10 -3
  140. package/src/lib/offsets.js +270 -0
  141. package/src/lib/partitionApplyAtRules.js +52 -0
  142. package/src/lib/regex.js +74 -0
  143. package/src/lib/resolveDefaultsAtRules.js +98 -60
  144. package/src/lib/setupContextUtils.js +737 -246
  145. package/src/lib/setupTrackingContext.js +16 -54
  146. package/src/lib/sharedState.js +44 -7
  147. package/src/lib/substituteScreenAtRules.js +6 -3
  148. package/src/postcss-plugins/nesting/README.md +42 -0
  149. package/src/postcss-plugins/nesting/index.js +13 -0
  150. package/src/postcss-plugins/nesting/plugin.js +80 -0
  151. package/src/processTailwindFeatures.js +17 -2
  152. package/src/public/colors.js +4 -9
  153. package/src/util/buildMediaQuery.js +14 -16
  154. package/src/util/cloneNodes.js +19 -2
  155. package/src/util/color.js +31 -14
  156. package/src/util/createUtilityPlugin.js +2 -2
  157. package/src/util/dataTypes.js +57 -21
  158. package/src/util/defaults.js +6 -0
  159. package/src/util/formatVariantSelector.js +319 -0
  160. package/src/util/getAllConfigs.js +19 -0
  161. package/src/util/isValidArbitraryValue.js +61 -0
  162. package/src/util/log.js +23 -22
  163. package/src/util/nameClass.js +6 -2
  164. package/src/util/negateValue.js +14 -4
  165. package/src/util/normalizeConfig.js +282 -0
  166. package/src/util/normalizeScreens.js +140 -0
  167. package/src/util/parseBoxShadowValue.js +72 -0
  168. package/src/util/parseDependency.js +37 -42
  169. package/src/util/parseGlob.js +24 -0
  170. package/src/util/pluginUtils.js +144 -152
  171. package/src/util/prefixSelector.js +7 -8
  172. package/src/util/removeAlphaVariables.js +24 -0
  173. package/src/util/resolveConfig.js +82 -81
  174. package/src/util/splitAtTopLevelOnly.js +45 -0
  175. package/src/util/toPath.js +23 -1
  176. package/src/util/transformThemeValue.js +33 -8
  177. package/src/util/validateConfig.js +13 -0
  178. package/src/util/validateFormalSyntax.js +34 -0
  179. package/src/util/withAlphaVariable.js +1 -1
  180. package/stubs/defaultConfig.stub.js +173 -93
  181. package/stubs/simpleConfig.stub.js +1 -1
  182. package/types/config.d.ts +361 -0
  183. package/types/generated/.gitkeep +0 -0
  184. package/types/generated/colors.d.ts +276 -0
  185. package/types/generated/corePluginList.d.ts +1 -0
  186. package/types/generated/default-theme.d.ts +342 -0
  187. package/types/index.d.ts +7 -0
  188. package/lib/lib/setupWatchingContext.js +0 -284
  189. package/nesting/plugin.js +0 -41
  190. package/src/lib/setupWatchingContext.js +0 -306
@@ -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'
@@ -41,8 +46,10 @@ export default function normalizeTailwindDirectives(root) {
41
46
 
42
47
  if (['layer', 'responsive', 'variants'].includes(atRule.name)) {
43
48
  if (['responsive', 'variants'].includes(atRule.name)) {
44
- log.warn([
45
- `'@${atRule.name}' is deprecated, use '@layer utilities' or '@layer components' instead.`,
49
+ log.warn(`${atRule.name}-at-rule-deprecated`, [
50
+ `The \`@${atRule.name}\` directive has been deprecated in Tailwind CSS v3.0.`,
51
+ `Use \`@layer utilities\` or \`@layer components\` instead.`,
52
+ 'https://tailwindcss.com/docs/upgrade-guide#replace-variants-with-layer',
46
53
  ])
47
54
  }
48
55
  layerDirectives.add(atRule)
@@ -73,5 +80,5 @@ export default function normalizeTailwindDirectives(root) {
73
80
  }
74
81
  }
75
82
 
76
- return tailwindDirectives
83
+ return { tailwindDirectives, applyDirectives }
77
84
  }
@@ -0,0 +1,270 @@
1
+ // @ts-check
2
+
3
+ import bigSign from '../util/bigSign'
4
+
5
+ /**
6
+ * @typedef {'base' | 'defaults' | 'components' | 'utilities' | 'variants' | 'user'} Layer
7
+ */
8
+
9
+ /**
10
+ * @typedef {object} VariantOption
11
+ * @property {number} id An unique identifier to identify `matchVariant`
12
+ * @property {function | undefined} sort The sort function
13
+ * @property {string|null} value The value we want to compare
14
+ * @property {string|null} modifier The modifier that was used (if any)
15
+ */
16
+
17
+ /**
18
+ * @typedef {object} RuleOffset
19
+ * @property {Layer} layer The layer that this rule belongs to
20
+ * @property {Layer} parentLayer The layer that this rule originally belonged to. Only different from layer if this is a variant.
21
+ * @property {bigint} arbitrary 0n if false, 1n if true
22
+ * @property {bigint} variants Dynamic size. 1 bit per registered variant. 0n means no variants
23
+ * @property {bigint} parallelIndex Rule index for the parallel variant. 0 if not applicable.
24
+ * @property {bigint} index Index of the rule / utility in it's given *parent* layer. Monotonically increasing.
25
+ * @property {VariantOption[]} options Some information on how we can sort arbitrary variants
26
+ */
27
+
28
+ export class Offsets {
29
+ constructor() {
30
+ /**
31
+ * Offsets for the next rule in a given layer
32
+ *
33
+ * @type {Record<Layer, bigint>}
34
+ */
35
+ this.offsets = {
36
+ defaults: 0n,
37
+ base: 0n,
38
+ components: 0n,
39
+ utilities: 0n,
40
+ variants: 0n,
41
+ user: 0n,
42
+ }
43
+
44
+ /**
45
+ * Positions for a given layer
46
+ *
47
+ * @type {Record<Layer, bigint>}
48
+ */
49
+ this.layerPositions = {
50
+ defaults: 0n,
51
+ base: 1n,
52
+ components: 2n,
53
+ utilities: 3n,
54
+
55
+ // There isn't technically a "user" layer, but we need to give it a position
56
+ // Because it's used for ordering user-css from @apply
57
+ user: 4n,
58
+
59
+ variants: 5n,
60
+ }
61
+
62
+ /**
63
+ * The total number of functions currently registered across all variants (including arbitrary variants)
64
+ *
65
+ * @type {bigint}
66
+ */
67
+ this.reservedVariantBits = 0n
68
+
69
+ /**
70
+ * Positions for a given variant
71
+ *
72
+ * @type {Map<string, bigint>}
73
+ */
74
+ this.variantOffsets = new Map()
75
+ }
76
+
77
+ /**
78
+ * @param {Layer} layer
79
+ * @returns {RuleOffset}
80
+ */
81
+ create(layer) {
82
+ return {
83
+ layer,
84
+ parentLayer: layer,
85
+ arbitrary: 0n,
86
+ variants: 0n,
87
+ parallelIndex: 0n,
88
+ index: this.offsets[layer]++,
89
+ options: [],
90
+ }
91
+ }
92
+
93
+ /**
94
+ * @returns {RuleOffset}
95
+ */
96
+ arbitraryProperty() {
97
+ return {
98
+ ...this.create('utilities'),
99
+ arbitrary: 1n,
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Get the offset for a variant
105
+ *
106
+ * @param {string} variant
107
+ * @param {number} index
108
+ * @returns {RuleOffset}
109
+ */
110
+ forVariant(variant, index = 0) {
111
+ let offset = this.variantOffsets.get(variant)
112
+ if (offset === undefined) {
113
+ throw new Error(`Cannot find offset for unknown variant ${variant}`)
114
+ }
115
+
116
+ return {
117
+ ...this.create('variants'),
118
+ variants: offset << BigInt(index),
119
+ }
120
+ }
121
+
122
+ /**
123
+ * @param {RuleOffset} rule
124
+ * @param {RuleOffset} variant
125
+ * @param {VariantOption} options
126
+ * @returns {RuleOffset}
127
+ */
128
+ applyVariantOffset(rule, variant, options) {
129
+ return {
130
+ ...rule,
131
+ layer: 'variants',
132
+ parentLayer: rule.layer === 'variants' ? rule.parentLayer : rule.layer,
133
+ variants: rule.variants | variant.variants,
134
+ options: options.sort ? [].concat(options, rule.options) : rule.options,
135
+
136
+ // TODO: Technically this is wrong. We should be handling parallel index on a per variant basis.
137
+ // We'll take the max of all the parallel indexes for now.
138
+ // @ts-ignore
139
+ parallelIndex: max([rule.parallelIndex, variant.parallelIndex]),
140
+ }
141
+ }
142
+
143
+ /**
144
+ * @param {RuleOffset} offset
145
+ * @param {number} parallelIndex
146
+ * @returns {RuleOffset}
147
+ */
148
+ applyParallelOffset(offset, parallelIndex) {
149
+ return {
150
+ ...offset,
151
+ parallelIndex: BigInt(parallelIndex),
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Each variant gets 1 bit per function / rule registered.
157
+ * This is because multiple variants can be applied to a single rule and we need to know which ones are present and which ones are not.
158
+ * Additionally, every unique group of variants is grouped together in the stylesheet.
159
+ *
160
+ * This grouping is order-independent. For instance, we do not differentiate between `hover:focus` and `focus:hover`.
161
+ *
162
+ * @param {string[]} variants
163
+ * @param {(name: string) => number} getLength
164
+ */
165
+ recordVariants(variants, getLength) {
166
+ for (let variant of variants) {
167
+ this.recordVariant(variant, getLength(variant))
168
+ }
169
+ }
170
+
171
+ /**
172
+ * The same as `recordVariants` but for a single arbitrary variant at runtime.
173
+ * @param {string} variant
174
+ * @param {number} fnCount
175
+ *
176
+ * @returns {RuleOffset} The highest offset for this variant
177
+ */
178
+ recordVariant(variant, fnCount = 1) {
179
+ this.variantOffsets.set(variant, 1n << this.reservedVariantBits)
180
+
181
+ // Ensure space is reserved for each "function" in the parallel variant
182
+ // by offsetting the next variant by the number of parallel variants
183
+ // in the one we just added.
184
+
185
+ // Single functions that return parallel variants are NOT handled separately here
186
+ // They're offset by 1 (or the number of functions) as usual
187
+ // And each rule returned is tracked separately since the functions are evaluated lazily.
188
+ // @see `RuleOffset.parallelIndex`
189
+ this.reservedVariantBits += BigInt(fnCount)
190
+
191
+ return {
192
+ ...this.create('variants'),
193
+ variants: 1n << this.reservedVariantBits,
194
+ }
195
+ }
196
+
197
+ /**
198
+ * @param {RuleOffset} a
199
+ * @param {RuleOffset} b
200
+ * @returns {bigint}
201
+ */
202
+ compare(a, b) {
203
+ // Sort layers together
204
+ if (a.layer !== b.layer) {
205
+ return this.layerPositions[a.layer] - this.layerPositions[b.layer]
206
+ }
207
+
208
+ // Sort based on the sorting function
209
+ for (let aOptions of a.options) {
210
+ for (let bOptions of b.options) {
211
+ if (aOptions.id !== bOptions.id) continue
212
+ if (!aOptions.sort || !bOptions.sort) continue
213
+ let result = aOptions.sort(
214
+ {
215
+ value: aOptions.value,
216
+ modifier: aOptions.modifier,
217
+ },
218
+ {
219
+ value: bOptions.value,
220
+ modifier: bOptions.modifier,
221
+ }
222
+ )
223
+ if (result !== 0) return result
224
+ }
225
+ }
226
+
227
+ // Sort variants in the order they were registered
228
+ if (a.variants !== b.variants) {
229
+ return a.variants - b.variants
230
+ }
231
+
232
+ // Make sure each rule returned by a parallel variant is sorted in ascending order
233
+ if (a.parallelIndex !== b.parallelIndex) {
234
+ return a.parallelIndex - b.parallelIndex
235
+ }
236
+
237
+ // Always sort arbitrary properties after other utilities
238
+ if (a.arbitrary !== b.arbitrary) {
239
+ return a.arbitrary - b.arbitrary
240
+ }
241
+
242
+ // Sort utilities, components, etc… in the order they were registered
243
+ return a.index - b.index
244
+ }
245
+
246
+ /**
247
+ * @template T
248
+ * @param {[RuleOffset, T][]} list
249
+ * @returns {[RuleOffset, T][]}
250
+ */
251
+ sort(list) {
252
+ return list.sort(([a], [b]) => bigSign(this.compare(a, b)))
253
+ }
254
+ }
255
+
256
+ /**
257
+ *
258
+ * @param {bigint[]} nums
259
+ * @returns {bigint|null}
260
+ */
261
+ function max(nums) {
262
+ let max = null
263
+
264
+ for (const num of nums) {
265
+ max = max ?? num
266
+ max = max > num ? max : num
267
+ }
268
+
269
+ return max
270
+ }
@@ -0,0 +1,52 @@
1
+ function partitionRules(root) {
2
+ if (!root.walkAtRules) return
3
+
4
+ let applyParents = new Set()
5
+
6
+ root.walkAtRules('apply', (rule) => {
7
+ applyParents.add(rule.parent)
8
+ })
9
+
10
+ if (applyParents.size === 0) {
11
+ return
12
+ }
13
+
14
+ for (let rule of applyParents) {
15
+ let nodeGroups = []
16
+ let lastGroup = []
17
+
18
+ for (let node of rule.nodes) {
19
+ if (node.type === 'atrule' && node.name === 'apply') {
20
+ if (lastGroup.length > 0) {
21
+ nodeGroups.push(lastGroup)
22
+ lastGroup = []
23
+ }
24
+ nodeGroups.push([node])
25
+ } else {
26
+ lastGroup.push(node)
27
+ }
28
+ }
29
+
30
+ if (lastGroup.length > 0) {
31
+ nodeGroups.push(lastGroup)
32
+ }
33
+
34
+ if (nodeGroups.length === 1) {
35
+ continue
36
+ }
37
+
38
+ for (let group of [...nodeGroups].reverse()) {
39
+ let clone = rule.clone({ nodes: [] })
40
+ clone.append(group)
41
+ rule.after(clone)
42
+ }
43
+
44
+ rule.remove()
45
+ }
46
+ }
47
+
48
+ export default function expandApplyAtRules() {
49
+ return (root) => {
50
+ partitionRules(root)
51
+ }
52
+ }
@@ -0,0 +1,74 @@
1
+ const REGEX_SPECIAL = /[\\^$.*+?()[\]{}|]/g
2
+ const REGEX_HAS_SPECIAL = RegExp(REGEX_SPECIAL.source)
3
+
4
+ /**
5
+ * @param {string|RegExp|Array<string|RegExp>} source
6
+ */
7
+ function toSource(source) {
8
+ source = Array.isArray(source) ? source : [source]
9
+
10
+ source = source.map((item) => (item instanceof RegExp ? item.source : item))
11
+
12
+ return source.join('')
13
+ }
14
+
15
+ /**
16
+ * @param {string|RegExp|Array<string|RegExp>} source
17
+ */
18
+ export function pattern(source) {
19
+ return new RegExp(toSource(source), 'g')
20
+ }
21
+
22
+ /**
23
+ * @param {string|RegExp|Array<string|RegExp>} source
24
+ */
25
+ export function withoutCapturing(source) {
26
+ return new RegExp(`(?:${toSource(source)})`, 'g')
27
+ }
28
+
29
+ /**
30
+ * @param {Array<string|RegExp>} sources
31
+ */
32
+ export function any(sources) {
33
+ return `(?:${sources.map(toSource).join('|')})`
34
+ }
35
+
36
+ /**
37
+ * @param {string|RegExp} source
38
+ */
39
+ export function optional(source) {
40
+ return `(?:${toSource(source)})?`
41
+ }
42
+
43
+ /**
44
+ * @param {string|RegExp|Array<string|RegExp>} source
45
+ */
46
+ export function zeroOrMore(source) {
47
+ return `(?:${toSource(source)})*`
48
+ }
49
+
50
+ /**
51
+ * Generate a RegExp that matches balanced brackets for a given depth
52
+ * We have to specify a depth because JS doesn't support recursive groups using ?R
53
+ *
54
+ * Based on https://stackoverflow.com/questions/17759004/how-to-match-string-within-parentheses-nested-in-java/17759264#17759264
55
+ *
56
+ * @param {string|RegExp|Array<string|RegExp>} source
57
+ */
58
+ export function nestedBrackets(open, close, depth = 1) {
59
+ return withoutCapturing([
60
+ escape(open),
61
+ /[^\s]*/,
62
+ depth === 1
63
+ ? `[^${escape(open)}${escape(close)}\s]*`
64
+ : any([`[^${escape(open)}${escape(close)}\s]*`, nestedBrackets(open, close, depth - 1)]),
65
+ /[^\s]*/,
66
+ escape(close),
67
+ ])
68
+ }
69
+
70
+ export function escape(string) {
71
+ return string && REGEX_HAS_SPECIAL.test(string)
72
+ ? string.replace(REGEX_SPECIAL, '\\$&')
73
+ : string || ''
74
+ }
@@ -2,60 +2,56 @@ import postcss from 'postcss'
2
2
  import selectorParser from 'postcss-selector-parser'
3
3
  import { flagEnabled } from '../featureFlags'
4
4
 
5
- function isPseudoElement(n) {
6
- if (n.type !== 'pseudo') {
7
- return false
8
- }
9
-
10
- return (
11
- n.value.startsWith('::') ||
12
- [':before', ':after', ':first-line', ':first-letter'].includes(n.value)
13
- )
5
+ let getNode = {
6
+ id(node) {
7
+ return selectorParser.attribute({
8
+ attribute: 'id',
9
+ operator: '=',
10
+ value: node.value,
11
+ quoteMark: '"',
12
+ })
13
+ },
14
14
  }
15
15
 
16
16
  function minimumImpactSelector(nodes) {
17
17
  let rest = nodes
18
- // Keep all pseudo & combinator types (:not([hidden]) ~ :not([hidden]))
19
- .filter((n) => n.type === 'pseudo' || n.type === 'combinator')
20
- // Remove leading pseudo's (:hover, :focus, ...)
21
- .filter((n, idx, all) => {
22
- // Keep pseudo elements
23
- if (isPseudoElement(n)) return true
18
+ .filter((node) => {
19
+ // Keep non-pseudo nodes
20
+ if (node.type !== 'pseudo') return true
21
+
22
+ // Keep pseudo nodes that have subnodes
23
+ // E.g.: `:not()` contains subnodes inside the parentheses
24
+ if (node.nodes.length > 0) return true
25
+
26
+ // Keep pseudo `elements`
27
+ // This implicitly means that we ignore pseudo `classes`
28
+ return (
29
+ node.value.startsWith('::') ||
30
+ [':before', ':after', ':first-line', ':first-letter'].includes(node.value)
31
+ )
32
+ })
33
+ .reverse()
24
34
 
25
- if (idx === 0 && n.type === 'pseudo') return false
26
- if (idx > 0 && n.type === 'pseudo' && all[idx - 1].type === 'pseudo') return false
35
+ let searchFor = new Set(['tag', 'class', 'id', 'attribute'])
27
36
 
28
- return true
29
- })
37
+ let splitPointIdx = rest.findIndex((n) => searchFor.has(n.type))
38
+ if (splitPointIdx === -1) return rest.reverse().join('').trim()
30
39
 
31
- let [bestNode] = nodes
32
-
33
- for (let [type, getNode = (n) => n] of [
34
- ['class'],
35
- [
36
- 'id',
37
- (n) =>
38
- selectorParser.attribute({
39
- attribute: 'id',
40
- operator: '=',
41
- value: n.value,
42
- quoteMark: '"',
43
- }),
44
- ],
45
- ['attribute'],
46
- ]) {
47
- let match = nodes.find((n) => n.type === type)
48
-
49
- if (match) {
50
- bestNode = getNode(match)
51
- break
52
- }
40
+ let node = rest[splitPointIdx]
41
+ let bestNode = getNode[node.type] ? getNode[node.type](node) : node
42
+
43
+ rest = rest.slice(0, splitPointIdx)
44
+
45
+ let combinatorIdx = rest.findIndex((n) => n.type === 'combinator' && n.value === '>')
46
+ if (combinatorIdx !== -1) {
47
+ rest.splice(0, combinatorIdx)
48
+ rest.unshift(selectorParser.universal())
53
49
  }
54
50
 
55
- return [bestNode, ...rest].join('').trim()
51
+ return [bestNode, ...rest.reverse()].join('').trim()
56
52
  }
57
53
 
58
- let elementSelectorParser = selectorParser((selectors) => {
54
+ export let elementSelectorParser = selectorParser((selectors) => {
59
55
  return selectors.map((s) => {
60
56
  let nodes = s.split((n) => n.type === 'combinator' && n.value === ' ').pop()
61
57
  return minimumImpactSelector(nodes)
@@ -75,6 +71,8 @@ function extractElementSelector(selector) {
75
71
  export default function resolveDefaultsAtRules({ tailwindConfig }) {
76
72
  return (root) => {
77
73
  let variableNodeMap = new Map()
74
+
75
+ /** @type {Set<import('postcss').AtRule>} */
78
76
  let universals = new Set()
79
77
 
80
78
  root.walkAtRules('defaults', (rule) => {
@@ -93,33 +91,73 @@ export default function resolveDefaultsAtRules({ tailwindConfig }) {
93
91
  rule.remove()
94
92
  })
95
93
 
96
- for (let universal of universals) {
97
- let selectors = new Set()
94
+ if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
95
+ for (let universal of universals) {
96
+ /** @type {Map<string, Set<string>>} */
97
+ let selectorGroups = new Map()
98
98
 
99
- let rules = variableNodeMap.get(universal.params) ?? []
99
+ let rules = variableNodeMap.get(universal.params) ?? []
100
100
 
101
- for (let rule of rules) {
102
- for (let selector of extractElementSelector(rule.selector)) {
103
- selectors.add(selector)
101
+ for (let rule of rules) {
102
+ for (let selector of extractElementSelector(rule.selector)) {
103
+ // If selector contains a vendor prefix after a pseudo element or class,
104
+ // we consider them separately because merging the declarations into
105
+ // a single rule will cause browsers that do not understand the
106
+ // vendor prefix to throw out the whole rule
107
+ let selectorGroupName =
108
+ selector.includes(':-') || selector.includes('::-') ? selector : '__DEFAULT__'
109
+
110
+ let selectors = selectorGroups.get(selectorGroupName) ?? new Set()
111
+ selectorGroups.set(selectorGroupName, selectors)
112
+
113
+ selectors.add(selector)
114
+ }
115
+ }
116
+
117
+ if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
118
+ if (selectorGroups.size === 0) {
119
+ universal.remove()
120
+ continue
121
+ }
122
+
123
+ for (let [, selectors] of selectorGroups) {
124
+ let universalRule = postcss.rule({
125
+ source: universal.source,
126
+ })
127
+
128
+ universalRule.selectors = [...selectors]
129
+
130
+ universalRule.append(universal.nodes.map((node) => node.clone()))
131
+ universal.before(universalRule)
132
+ }
104
133
  }
105
- }
106
134
 
107
- if (selectors.size === 0) {
108
135
  universal.remove()
109
- continue
110
136
  }
137
+ } else if (universals.size) {
138
+ let universalRule = postcss.rule({
139
+ selectors: ['*', '::before', '::after'],
140
+ })
111
141
 
112
- let universalRule = postcss.rule()
142
+ for (let universal of universals) {
143
+ universalRule.append(universal.nodes)
113
144
 
114
- if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
115
- universalRule.selectors = [...selectors]
116
- } else {
117
- universalRule.selectors = ['*', '::before', '::after']
145
+ if (!universalRule.parent) {
146
+ universal.before(universalRule)
147
+ }
148
+
149
+ if (!universalRule.source) {
150
+ universalRule.source = universal.source
151
+ }
152
+
153
+ universal.remove()
118
154
  }
119
155
 
120
- universalRule.append(universal.nodes)
121
- universal.before(universalRule)
122
- universal.remove()
156
+ let backdropRule = universalRule.clone({
157
+ selectors: ['::backdrop'],
158
+ })
159
+
160
+ universalRule.after(backdropRule)
123
161
  }
124
162
  }
125
163
  }