tailwindcss 0.0.0-insiders.fda68f7 → 0.0.0-oxide.6bf5e56

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 (195) hide show
  1. package/CHANGELOG.md +603 -2
  2. package/LICENSE +1 -2
  3. package/README.md +14 -6
  4. package/colors.d.ts +3 -0
  5. package/colors.js +2 -304
  6. package/defaultConfig.d.ts +3 -0
  7. package/defaultConfig.js +2 -4
  8. package/defaultTheme.d.ts +4 -0
  9. package/defaultTheme.js +2 -4
  10. package/lib/cli/build/deps.js +54 -0
  11. package/lib/cli/build/index.js +48 -0
  12. package/lib/cli/build/plugin.js +367 -0
  13. package/lib/cli/build/utils.js +78 -0
  14. package/lib/cli/build/watching.js +178 -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 +13 -0
  19. package/lib/cli-peer-dependencies.js +22 -14
  20. package/lib/cli.js +217 -743
  21. package/lib/constants.js +41 -34
  22. package/lib/corePluginList.js +178 -5
  23. package/lib/corePlugins.js +3879 -2941
  24. package/lib/css/preflight.css +22 -9
  25. package/lib/featureFlags.js +61 -50
  26. package/lib/index.js +45 -28
  27. package/lib/lib/cacheInvalidation.js +90 -0
  28. package/lib/lib/collapseAdjacentRules.js +52 -36
  29. package/lib/lib/collapseDuplicateDeclarations.js +83 -0
  30. package/lib/lib/content.js +176 -0
  31. package/lib/lib/defaultExtractor.js +236 -0
  32. package/lib/lib/detectNesting.js +37 -0
  33. package/lib/lib/evaluateTailwindFunctions.js +203 -161
  34. package/lib/lib/expandApplyAtRules.js +502 -221
  35. package/lib/lib/expandTailwindAtRules.js +258 -243
  36. package/lib/lib/findAtConfigPath.js +44 -0
  37. package/lib/lib/generateRules.js +775 -320
  38. package/lib/lib/getModuleDependencies.js +44 -46
  39. package/lib/lib/normalizeTailwindDirectives.js +79 -60
  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 +150 -94
  44. package/lib/lib/setupContextUtils.js +1146 -599
  45. package/lib/lib/setupTrackingContext.js +129 -177
  46. package/lib/lib/sharedState.js +53 -21
  47. package/lib/lib/substituteScreenAtRules.js +26 -28
  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 +58 -53
  52. package/lib/public/colors.js +331 -0
  53. package/lib/public/create-plugin.js +15 -0
  54. package/lib/public/default-config.js +16 -0
  55. package/lib/public/default-theme.js +16 -0
  56. package/lib/public/resolve-config.js +22 -0
  57. package/lib/util/bigSign.js +7 -6
  58. package/lib/util/buildMediaQuery.js +21 -32
  59. package/lib/util/cloneDeep.js +16 -14
  60. package/lib/util/cloneNodes.js +29 -15
  61. package/lib/util/color.js +90 -66
  62. package/lib/util/configurePlugins.js +17 -15
  63. package/lib/util/createPlugin.js +23 -26
  64. package/lib/util/createUtilityPlugin.js +46 -46
  65. package/lib/util/dataTypes.js +242 -0
  66. package/lib/util/defaults.js +20 -15
  67. package/lib/util/escapeClassName.js +18 -17
  68. package/lib/util/escapeCommas.js +7 -6
  69. package/lib/util/flattenColorPalette.js +13 -12
  70. package/lib/util/formatVariantSelector.js +285 -0
  71. package/lib/util/getAllConfigs.js +44 -18
  72. package/lib/util/hashConfig.js +15 -12
  73. package/lib/util/isKeyframeRule.js +7 -6
  74. package/lib/util/isPlainObject.js +11 -11
  75. package/lib/util/isSyntacticallyValidPropertyValue.js +72 -0
  76. package/lib/util/log.js +52 -33
  77. package/lib/util/nameClass.js +37 -26
  78. package/lib/util/negateValue.js +31 -17
  79. package/lib/util/normalizeConfig.js +281 -0
  80. package/lib/util/normalizeScreens.js +170 -0
  81. package/lib/util/parseAnimationValue.js +85 -54
  82. package/lib/util/parseBoxShadowValue.js +84 -0
  83. package/lib/util/parseDependency.js +41 -70
  84. package/lib/util/parseGlob.js +34 -0
  85. package/lib/util/parseObjectStyles.js +30 -24
  86. package/lib/util/pluginUtils.js +252 -287
  87. package/lib/util/prefixSelector.js +20 -20
  88. package/lib/util/removeAlphaVariables.js +29 -0
  89. package/lib/util/resolveConfig.js +221 -256
  90. package/lib/util/resolveConfigPath.js +43 -48
  91. package/lib/util/responsive.js +18 -14
  92. package/lib/util/splitAtTopLevelOnly.js +43 -0
  93. package/lib/util/tap.js +8 -7
  94. package/lib/util/toColorValue.js +7 -6
  95. package/lib/util/toPath.js +27 -8
  96. package/lib/util/transformThemeValue.js +67 -28
  97. package/lib/util/validateConfig.js +24 -0
  98. package/lib/util/validateFormalSyntax.js +24 -0
  99. package/lib/util/withAlphaVariable.js +67 -57
  100. package/nesting/index.js +2 -12
  101. package/package.json +60 -65
  102. package/peers/index.js +76445 -84221
  103. package/plugin.d.ts +11 -0
  104. package/plugin.js +1 -2
  105. package/resolveConfig.d.ts +12 -0
  106. package/resolveConfig.js +2 -7
  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 +49 -0
  114. package/src/cli/build/plugin.js +439 -0
  115. package/src/cli/build/utils.js +76 -0
  116. package/src/cli/build/watching.js +227 -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 +6 -0
  121. package/src/cli-peer-dependencies.js +7 -1
  122. package/src/cli.js +50 -575
  123. package/src/corePluginList.js +1 -1
  124. package/src/corePlugins.js +2405 -1948
  125. package/src/css/preflight.css +22 -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 +212 -0
  132. package/src/lib/defaultExtractor.js +211 -0
  133. package/src/lib/detectNesting.js +39 -0
  134. package/src/lib/evaluateTailwindFunctions.js +84 -10
  135. package/src/lib/expandApplyAtRules.js +508 -153
  136. package/src/lib/expandTailwindAtRules.js +130 -104
  137. package/src/lib/findAtConfigPath.js +48 -0
  138. package/src/lib/generateRules.js +596 -70
  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 +105 -47
  144. package/src/lib/setupContextUtils.js +828 -196
  145. package/src/lib/setupTrackingContext.js +19 -54
  146. package/src/lib/sharedState.js +45 -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 +19 -2
  152. package/src/public/colors.js +300 -0
  153. package/src/public/create-plugin.js +2 -0
  154. package/src/public/default-config.js +4 -0
  155. package/src/public/default-theme.js +4 -0
  156. package/src/public/resolve-config.js +7 -0
  157. package/src/util/buildMediaQuery.js +14 -16
  158. package/src/util/cloneNodes.js +19 -2
  159. package/src/util/color.js +31 -14
  160. package/src/util/createUtilityPlugin.js +2 -11
  161. package/src/util/dataTypes.js +256 -0
  162. package/src/util/defaults.js +6 -0
  163. package/src/util/formatVariantSelector.js +319 -0
  164. package/src/util/getAllConfigs.js +19 -0
  165. package/src/util/isSyntacticallyValidPropertyValue.js +61 -0
  166. package/src/util/log.js +23 -22
  167. package/src/util/nameClass.js +14 -6
  168. package/src/util/negateValue.js +15 -5
  169. package/src/util/normalizeConfig.js +300 -0
  170. package/src/util/normalizeScreens.js +140 -0
  171. package/src/util/parseAnimationValue.js +7 -1
  172. package/src/util/parseBoxShadowValue.js +72 -0
  173. package/src/util/parseDependency.js +37 -38
  174. package/src/util/parseGlob.js +24 -0
  175. package/src/util/pluginUtils.js +216 -197
  176. package/src/util/prefixSelector.js +7 -8
  177. package/src/util/removeAlphaVariables.js +24 -0
  178. package/src/util/resolveConfig.js +86 -91
  179. package/src/util/splitAtTopLevelOnly.js +45 -0
  180. package/src/util/toPath.js +23 -1
  181. package/src/util/transformThemeValue.js +33 -8
  182. package/src/util/validateConfig.js +13 -0
  183. package/src/util/validateFormalSyntax.js +34 -0
  184. package/src/util/withAlphaVariable.js +14 -9
  185. package/stubs/defaultConfig.stub.js +186 -117
  186. package/stubs/simpleConfig.stub.js +1 -1
  187. package/types/config.d.ts +362 -0
  188. package/types/generated/.gitkeep +0 -0
  189. package/types/generated/colors.d.ts +276 -0
  190. package/types/generated/corePluginList.d.ts +1 -0
  191. package/types/generated/default-theme.d.ts +342 -0
  192. package/types/index.d.ts +7 -0
  193. package/lib/lib/setupWatchingContext.js +0 -331
  194. package/nesting/plugin.js +0 -41
  195. 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,42 +2,58 @@ import postcss from 'postcss'
2
2
  import selectorParser from 'postcss-selector-parser'
3
3
  import { flagEnabled } from '../featureFlags'
4
4
 
5
+ let getNode = {
6
+ id(node) {
7
+ return selectorParser.attribute({
8
+ attribute: 'id',
9
+ operator: '=',
10
+ value: node.value,
11
+ quoteMark: '"',
12
+ })
13
+ },
14
+ }
15
+
5
16
  function minimumImpactSelector(nodes) {
6
- let pseudos = nodes.filter((n) => n.type === 'pseudo')
7
- let [bestNode] = nodes
8
-
9
- for (let [type, getNode = (n) => n] of [
10
- ['class'],
11
- [
12
- 'id',
13
- (n) =>
14
- selectorParser.attribute({
15
- attribute: 'id',
16
- operator: '=',
17
- value: n.value,
18
- quoteMark: '"',
19
- }),
20
- ],
21
- ['attribute'],
22
- ]) {
23
- let match = nodes.find((n) => n.type === type)
24
-
25
- if (match) {
26
- bestNode = getNode(match)
27
- break
28
- }
17
+ let rest = nodes
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()
34
+
35
+ let searchFor = new Set(['tag', 'class', 'id', 'attribute'])
36
+
37
+ let splitPointIdx = rest.findIndex((n) => searchFor.has(n.type))
38
+ if (splitPointIdx === -1) return rest.reverse().join('').trim()
39
+
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())
29
49
  }
30
50
 
31
- return [bestNode, ...pseudos].join('').trim()
51
+ return [bestNode, ...rest.reverse()].join('').trim()
32
52
  }
33
53
 
34
- let elementSelectorParser = selectorParser((selectors) => {
54
+ export let elementSelectorParser = selectorParser((selectors) => {
35
55
  return selectors.map((s) => {
36
- let nodes = s
37
- .split((n) => n.type === 'combinator')
38
- .pop()
39
- .filter((n) => n.type !== 'pseudo' || n.value.startsWith('::'))
40
-
56
+ let nodes = s.split((n) => n.type === 'combinator' && n.value === ' ').pop()
41
57
  return minimumImpactSelector(nodes)
42
58
  })
43
59
  })
@@ -55,6 +71,8 @@ function extractElementSelector(selector) {
55
71
  export default function resolveDefaultsAtRules({ tailwindConfig }) {
56
72
  return (root) => {
57
73
  let variableNodeMap = new Map()
74
+
75
+ /** @type {Set<import('postcss').AtRule>} */
58
76
  let universals = new Set()
59
77
 
60
78
  root.walkAtRules('defaults', (rule) => {
@@ -73,33 +91,73 @@ export default function resolveDefaultsAtRules({ tailwindConfig }) {
73
91
  rule.remove()
74
92
  })
75
93
 
76
- for (let universal of universals) {
77
- 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
+
99
+ let rules = variableNodeMap.get(universal.params) ?? []
78
100
 
79
- let rules = variableNodeMap.get(universal.params) ?? []
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__'
80
109
 
81
- for (let rule of rules) {
82
- for (let selector of extractElementSelector(rule.selector)) {
83
- selectors.add(selector)
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
+ }
84
133
  }
85
- }
86
134
 
87
- if (selectors.size === 0) {
88
135
  universal.remove()
89
- continue
90
136
  }
137
+ } else if (universals.size) {
138
+ let universalRule = postcss.rule({
139
+ selectors: ['*', '::before', '::after'],
140
+ })
91
141
 
92
- let universalRule = postcss.rule()
142
+ for (let universal of universals) {
143
+ universalRule.append(universal.nodes)
93
144
 
94
- if (flagEnabled(tailwindConfig, 'optimizeUniversalDefaults')) {
95
- universalRule.selectors = [...selectors]
96
- } else {
97
- 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()
98
154
  }
99
155
 
100
- universalRule.append(universal.nodes)
101
- universal.before(universalRule)
102
- universal.remove()
156
+ let backdropRule = universalRule.clone({
157
+ selectors: ['::backdrop'],
158
+ })
159
+
160
+ universalRule.after(backdropRule)
103
161
  }
104
162
  }
105
163
  }