tailwindcss 0.0.0-insiders.fe08e91 → 0.0.0-oxide.956419c

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 (186) hide show
  1. package/CHANGELOG.md +384 -3
  2. package/LICENSE +1 -2
  3. package/README.md +12 -8
  4. package/colors.d.ts +3 -0
  5. package/defaultConfig.d.ts +3 -0
  6. package/defaultTheme.d.ts +4 -0
  7. package/lib/cli/build/deps.js +54 -0
  8. package/lib/cli/build/index.js +48 -0
  9. package/lib/cli/build/plugin.js +367 -0
  10. package/lib/cli/build/utils.js +78 -0
  11. package/lib/cli/build/watching.js +178 -0
  12. package/lib/cli/help/index.js +71 -0
  13. package/lib/cli/index.js +239 -0
  14. package/lib/cli/init/index.js +46 -0
  15. package/lib/cli/shared.js +13 -0
  16. package/lib/cli-peer-dependencies.js +20 -7
  17. package/lib/cli.js +4 -740
  18. package/lib/constants.js +27 -20
  19. package/lib/corePluginList.js +6 -3
  20. package/lib/corePlugins.js +2064 -1811
  21. package/lib/css/preflight.css +5 -5
  22. package/lib/featureFlags.js +31 -22
  23. package/lib/index.js +4 -28
  24. package/lib/lib/cacheInvalidation.js +90 -0
  25. package/lib/lib/collapseAdjacentRules.js +27 -9
  26. package/lib/lib/collapseDuplicateDeclarations.js +12 -9
  27. package/lib/lib/content.js +176 -0
  28. package/lib/lib/defaultExtractor.js +225 -31
  29. package/lib/lib/detectNesting.js +13 -10
  30. package/lib/lib/evaluateTailwindFunctions.js +118 -55
  31. package/lib/lib/expandApplyAtRules.js +439 -190
  32. package/lib/lib/expandTailwindAtRules.js +151 -134
  33. package/lib/lib/findAtConfigPath.js +44 -0
  34. package/lib/lib/generateRules.js +454 -187
  35. package/lib/lib/getModuleDependencies.js +11 -8
  36. package/lib/lib/normalizeTailwindDirectives.js +36 -32
  37. package/lib/lib/offsets.js +217 -0
  38. package/lib/lib/partitionApplyAtRules.js +56 -0
  39. package/lib/lib/regex.js +60 -0
  40. package/lib/lib/resolveDefaultsAtRules.js +89 -67
  41. package/lib/lib/setupContextUtils.js +667 -376
  42. package/lib/lib/setupTrackingContext.js +38 -67
  43. package/lib/lib/sharedState.js +27 -14
  44. package/lib/lib/substituteScreenAtRules.js +11 -9
  45. package/lib/oxide/cli.d.js +1 -0
  46. package/lib/oxide/cli.js +2 -0
  47. package/lib/oxide/postcss-plugin.d.js +1 -0
  48. package/lib/oxide/postcss-plugin.js +2 -0
  49. package/lib/plugin.js +48 -0
  50. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  51. package/lib/postcss-plugins/nesting/index.js +19 -0
  52. package/lib/postcss-plugins/nesting/plugin.js +87 -0
  53. package/lib/processTailwindFeatures.js +35 -25
  54. package/lib/public/colors.js +247 -245
  55. package/lib/public/create-plugin.js +6 -4
  56. package/lib/public/default-config.js +7 -5
  57. package/lib/public/default-theme.js +7 -5
  58. package/lib/public/resolve-config.js +8 -5
  59. package/lib/util/bigSign.js +4 -1
  60. package/lib/util/buildMediaQuery.js +11 -6
  61. package/lib/util/cloneDeep.js +7 -6
  62. package/lib/util/cloneNodes.js +21 -3
  63. package/lib/util/color.js +53 -54
  64. package/lib/util/configurePlugins.js +5 -2
  65. package/lib/util/createPlugin.js +6 -6
  66. package/lib/util/createUtilityPlugin.js +12 -14
  67. package/lib/util/dataTypes.js +119 -110
  68. package/lib/util/defaults.js +4 -1
  69. package/lib/util/escapeClassName.js +7 -4
  70. package/lib/util/escapeCommas.js +5 -2
  71. package/lib/util/flattenColorPalette.js +9 -12
  72. package/lib/util/formatVariantSelector.js +184 -85
  73. package/lib/util/getAllConfigs.js +27 -8
  74. package/lib/util/hashConfig.js +6 -3
  75. package/lib/util/isKeyframeRule.js +5 -2
  76. package/lib/util/isPlainObject.js +5 -2
  77. package/lib/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +23 -15
  78. package/lib/util/log.js +20 -14
  79. package/lib/util/nameClass.js +20 -9
  80. package/lib/util/negateValue.js +23 -8
  81. package/lib/util/normalizeConfig.js +116 -72
  82. package/lib/util/normalizeScreens.js +120 -11
  83. package/lib/util/parseAnimationValue.js +42 -40
  84. package/lib/util/parseBoxShadowValue.js +30 -23
  85. package/lib/util/parseDependency.js +38 -56
  86. package/lib/util/parseGlob.js +34 -0
  87. package/lib/util/parseObjectStyles.js +11 -8
  88. package/lib/util/pluginUtils.js +147 -50
  89. package/lib/util/prefixSelector.js +10 -8
  90. package/lib/util/removeAlphaVariables.js +29 -0
  91. package/lib/util/resolveConfig.js +97 -85
  92. package/lib/util/resolveConfigPath.js +11 -9
  93. package/lib/util/responsive.js +8 -5
  94. package/lib/util/splitAtTopLevelOnly.js +43 -0
  95. package/lib/util/tap.js +4 -1
  96. package/lib/util/toColorValue.js +5 -3
  97. package/lib/util/toPath.js +20 -4
  98. package/lib/util/transformThemeValue.js +37 -29
  99. package/lib/util/validateConfig.js +24 -0
  100. package/lib/util/validateFormalSyntax.js +24 -0
  101. package/lib/util/withAlphaVariable.js +23 -15
  102. package/nesting/index.js +2 -12
  103. package/package.json +50 -45
  104. package/peers/index.js +11381 -7950
  105. package/plugin.d.ts +11 -0
  106. package/resolveConfig.d.ts +12 -0
  107. package/scripts/generate-types.js +105 -0
  108. package/scripts/release-channel.js +18 -0
  109. package/scripts/release-notes.js +21 -0
  110. package/scripts/type-utils.js +27 -0
  111. package/src/cli/build/deps.js +56 -0
  112. package/src/cli/build/index.js +49 -0
  113. package/src/cli/build/plugin.js +439 -0
  114. package/src/cli/build/utils.js +76 -0
  115. package/src/cli/build/watching.js +227 -0
  116. package/src/cli/help/index.js +70 -0
  117. package/src/cli/index.js +234 -0
  118. package/src/cli/init/index.js +50 -0
  119. package/src/cli/shared.js +6 -0
  120. package/src/cli-peer-dependencies.js +7 -1
  121. package/src/cli.js +4 -810
  122. package/src/corePluginList.js +1 -1
  123. package/src/corePlugins.js +532 -217
  124. package/src/css/preflight.css +5 -5
  125. package/src/featureFlags.js +15 -9
  126. package/src/index.js +4 -27
  127. package/src/lib/cacheInvalidation.js +52 -0
  128. package/src/lib/collapseAdjacentRules.js +21 -2
  129. package/src/lib/content.js +212 -0
  130. package/src/lib/defaultExtractor.js +196 -33
  131. package/src/lib/evaluateTailwindFunctions.js +78 -7
  132. package/src/lib/expandApplyAtRules.js +482 -183
  133. package/src/lib/expandTailwindAtRules.js +106 -85
  134. package/src/lib/findAtConfigPath.js +48 -0
  135. package/src/lib/generateRules.js +418 -129
  136. package/src/lib/normalizeTailwindDirectives.js +1 -0
  137. package/src/lib/offsets.js +270 -0
  138. package/src/lib/partitionApplyAtRules.js +52 -0
  139. package/src/lib/regex.js +74 -0
  140. package/src/lib/resolveDefaultsAtRules.js +51 -30
  141. package/src/lib/setupContextUtils.js +556 -208
  142. package/src/lib/setupTrackingContext.js +11 -48
  143. package/src/lib/sharedState.js +5 -0
  144. package/src/oxide/cli.d.ts +0 -0
  145. package/src/oxide/cli.ts +1 -0
  146. package/src/oxide/postcss-plugin.d.ts +0 -0
  147. package/src/oxide/postcss-plugin.ts +1 -0
  148. package/src/plugin.js +47 -0
  149. package/src/postcss-plugins/nesting/README.md +42 -0
  150. package/src/postcss-plugins/nesting/index.js +13 -0
  151. package/src/postcss-plugins/nesting/plugin.js +80 -0
  152. package/src/processTailwindFeatures.js +8 -0
  153. package/src/util/buildMediaQuery.js +5 -3
  154. package/src/util/cloneNodes.js +19 -2
  155. package/src/util/color.js +25 -21
  156. package/src/util/dataTypes.js +29 -21
  157. package/src/util/formatVariantSelector.js +184 -61
  158. package/src/util/getAllConfigs.js +19 -0
  159. package/src/util/{isValidArbitraryValue.js → isSyntacticallyValidPropertyValue.js} +1 -1
  160. package/src/util/log.js +8 -8
  161. package/src/util/nameClass.js +4 -0
  162. package/src/util/negateValue.js +11 -3
  163. package/src/util/normalizeConfig.js +44 -6
  164. package/src/util/normalizeScreens.js +99 -4
  165. package/src/util/parseBoxShadowValue.js +4 -3
  166. package/src/util/parseDependency.js +37 -42
  167. package/src/util/parseGlob.js +24 -0
  168. package/src/util/pluginUtils.js +132 -10
  169. package/src/util/prefixSelector.js +7 -5
  170. package/src/util/removeAlphaVariables.js +24 -0
  171. package/src/util/resolveConfig.js +70 -32
  172. package/src/util/splitAtTopLevelOnly.js +45 -0
  173. package/src/util/toPath.js +1 -1
  174. package/src/util/transformThemeValue.js +13 -3
  175. package/src/util/validateConfig.js +13 -0
  176. package/src/util/validateFormalSyntax.js +34 -0
  177. package/src/util/withAlphaVariable.js +1 -1
  178. package/stubs/defaultConfig.stub.js +167 -164
  179. package/stubs/simpleConfig.stub.js +1 -0
  180. package/types/config.d.ts +362 -0
  181. package/types/generated/.gitkeep +0 -0
  182. package/types/generated/colors.d.ts +276 -0
  183. package/types/generated/corePluginList.d.ts +1 -0
  184. package/types/generated/default-theme.d.ts +342 -0
  185. package/types/index.d.ts +7 -0
  186. package/nesting/plugin.js +0 -41
@@ -2,22 +2,44 @@ import postcss from 'postcss'
2
2
  import parser from 'postcss-selector-parser'
3
3
 
4
4
  import { resolveMatches } from './generateRules'
5
- import bigSign from '../util/bigSign'
6
5
  import escapeClassName from '../util/escapeClassName'
7
6
 
7
+ /** @typedef {Map<string, [any, import('postcss').Rule[]]>} ApplyCache */
8
+
8
9
  function extractClasses(node) {
9
- let classes = new Set()
10
+ /** @type {Map<string, Set<string>>} */
11
+ let groups = new Map()
12
+
10
13
  let container = postcss.root({ nodes: [node.clone()] })
11
14
 
12
15
  container.walkRules((rule) => {
13
16
  parser((selectors) => {
14
17
  selectors.walkClasses((classSelector) => {
18
+ let parentSelector = classSelector.parent.toString()
19
+
20
+ let classes = groups.get(parentSelector)
21
+ if (!classes) {
22
+ groups.set(parentSelector, (classes = new Set()))
23
+ }
24
+
15
25
  classes.add(classSelector.value)
16
26
  })
17
27
  }).processSync(rule.selector)
18
28
  })
19
29
 
20
- return Array.from(classes)
30
+ let normalizedGroups = Array.from(groups.values(), (classes) => Array.from(classes))
31
+ let classes = normalizedGroups.flat()
32
+
33
+ return Object.assign(classes, { groups: normalizedGroups })
34
+ }
35
+
36
+ let selectorExtractor = parser()
37
+
38
+ /**
39
+ * @param {string} ruleSelectors
40
+ */
41
+ function extractSelectors(ruleSelectors) {
42
+ return selectorExtractor.astSync(ruleSelectors)
21
43
  }
22
44
 
23
45
  function extractBaseCandidates(candidates, separator) {
@@ -35,6 +57,130 @@ function prefix(context, selector) {
35
57
  return typeof prefix === 'function' ? prefix(selector) : prefix + selector
36
58
  }
37
59
 
60
+ function* pathToRoot(node) {
61
+ yield node
62
+ while (node.parent) {
63
+ yield node.parent
64
+ node = node.parent
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Only clone the node itself and not its children
70
+ *
71
+ * @param {*} node
72
+ * @param {*} overrides
73
+ * @returns
74
+ */
75
+ function shallowClone(node, overrides = {}) {
76
+ let children = node.nodes
77
+ node.nodes = []
78
+
79
+ let tmp = node.clone(overrides)
80
+
81
+ node.nodes = children
82
+
83
+ return tmp
84
+ }
85
+
86
+ /**
87
+ * Clone just the nodes all the way to the top that are required to represent
88
+ * this singular rule in the tree.
89
+ *
90
+ * For example, if we have CSS like this:
91
+ * ```css
92
+ * @media (min-width: 768px) {
93
+ * @supports (display: grid) {
94
+ * .foo {
95
+ * display: grid;
96
+ * grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
97
+ * }
98
+ * }
99
+ *
100
+ * @supports (backdrop-filter: blur(1px)) {
101
+ * .bar {
102
+ * backdrop-filter: blur(1px);
103
+ * }
104
+ * }
105
+ *
106
+ * .baz {
107
+ * color: orange;
108
+ * }
109
+ * }
110
+ * ```
111
+ *
112
+ * And we're cloning `.bar` it'll return a cloned version of what's required for just that single node:
113
+ *
114
+ * ```css
115
+ * @media (min-width: 768px) {
116
+ * @supports (backdrop-filter: blur(1px)) {
117
+ * .bar {
118
+ * backdrop-filter: blur(1px);
119
+ * }
120
+ * }
121
+ * }
122
+ * ```
123
+ *
124
+ * @param {import('postcss').Node} node
125
+ */
126
+ function nestedClone(node) {
127
+ for (let parent of pathToRoot(node)) {
128
+ if (node === parent) {
129
+ continue
130
+ }
131
+
132
+ if (parent.type === 'root') {
133
+ break
134
+ }
135
+
136
+ node = shallowClone(parent, {
137
+ nodes: [node],
138
+ })
139
+ }
140
+
141
+ return node
142
+ }
143
+
144
+ /**
145
+ * @param {import('postcss').Root} root
146
+ */
147
+ function buildLocalApplyCache(root, context) {
148
+ /** @type {ApplyCache} */
149
+ let cache = new Map()
150
+
151
+ root.walkRules((rule) => {
152
+ // Ignore rules generated by Tailwind
153
+ for (let node of pathToRoot(rule)) {
154
+ if (node.raws.tailwind?.layer !== undefined) {
155
+ return
156
+ }
157
+ }
158
+
159
+ // Clone what's required to represent this singular rule in the tree
160
+ let container = nestedClone(rule)
161
+ let sort = context.offsets.create('user')
162
+
163
+ for (let className of extractClasses(rule)) {
164
+ let list = cache.get(className) || []
165
+ cache.set(className, list)
166
+
167
+ list.push([
168
+ {
169
+ layer: 'user',
170
+ sort,
171
+ important: false,
172
+ },
173
+ container,
174
+ ])
175
+ }
176
+ })
177
+
178
+ return cache
179
+ }
180
+
181
+ /**
182
+ * @returns {ApplyCache}
183
+ */
38
184
  function buildApplyCache(applyCandidates, context) {
39
185
  for (let candidate of applyCandidates) {
40
186
  if (context.notClassCache.has(candidate) || context.applyClassCache.has(candidate)) {
@@ -62,6 +208,43 @@ function buildApplyCache(applyCandidates, context) {
62
208
  return context.applyClassCache
63
209
  }
64
210
 
211
+ /**
212
+ * Build a cache only when it's first used
213
+ *
214
+ * @param {() => ApplyCache} buildCacheFn
215
+ * @returns {ApplyCache}
216
+ */
217
+ function lazyCache(buildCacheFn) {
218
+ let cache = null
219
+
220
+ return {
221
+ get: (name) => {
222
+ cache = cache || buildCacheFn()
223
+
224
+ return cache.get(name)
225
+ },
226
+ has: (name) => {
227
+ cache = cache || buildCacheFn()
228
+
229
+ return cache.has(name)
230
+ },
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Take a series of multiple caches and merge
236
+ * them so they act like one large cache
237
+ *
238
+ * @param {ApplyCache[]} caches
239
+ * @returns {ApplyCache}
240
+ */
241
+ function combineCaches(caches) {
242
+ return {
243
+ get: (name) => caches.flatMap((cache) => cache.get(name) || []),
244
+ has: (name) => caches.some((cache) => cache.has(name)),
245
+ }
246
+ }
247
+
65
248
  function extractApplyCandidates(params) {
66
249
  let candidates = params.split(/[\s\t\n]+/g)
67
250
 
@@ -72,7 +255,7 @@ function extractApplyCandidates(params) {
72
255
  return [candidates, false]
73
256
  }
74
257
 
75
- function processApply(root, context) {
258
+ function processApply(root, context, localCache) {
76
259
  let applyCandidates = new Set()
77
260
 
78
261
  // Collect all @apply rules and candidates
@@ -88,220 +271,336 @@ function processApply(root, context) {
88
271
  })
89
272
 
90
273
  // Start the @apply process if we have rules with @apply in them
91
- if (applies.length > 0) {
92
- // Fill up some caches!
93
- let applyClassCache = buildApplyCache(applyCandidates, context)
94
-
95
- /**
96
- * When we have an apply like this:
97
- *
98
- * .abc {
99
- * @apply hover:font-bold;
100
- * }
101
- *
102
- * What we essentially will do is resolve to this:
103
- *
104
- * .abc {
105
- * @apply .hover\:font-bold:hover {
106
- * font-weight: 500;
107
- * }
108
- * }
109
- *
110
- * Notice that the to-be-applied class is `.hover\:font-bold:hover` and that the utility candidate was `hover:font-bold`.
111
- * What happens in this function is that we prepend a `.` and escape the candidate.
112
- * This will result in `.hover\:font-bold`
113
- * Which means that we can replace `.hover\:font-bold` with `.abc` in `.hover\:font-bold:hover` resulting in `.abc:hover`
114
- */
115
- // TODO: Should we use postcss-selector-parser for this instead?
116
- function replaceSelector(selector, utilitySelectors, candidate) {
117
- let needle = `.${escapeClassName(candidate)}`
118
- let utilitySelectorsList = utilitySelectors.split(/\s*\,(?![^(]*\))\s*/g)
119
-
120
- return selector
121
- .split(/\s*\,(?![^(]*\))\s*/g)
122
- .map((s) => {
123
- let replaced = []
124
-
125
- for (let utilitySelector of utilitySelectorsList) {
126
- let replacedSelector = utilitySelector.replace(needle, s)
127
- if (replacedSelector === utilitySelector) {
128
- continue
129
- }
130
- replaced.push(replacedSelector)
274
+ if (applies.length === 0) {
275
+ return
276
+ }
277
+
278
+ // Fill up some caches!
279
+ let applyClassCache = combineCaches([localCache, buildApplyCache(applyCandidates, context)])
280
+
281
+ /**
282
+ * When we have an apply like this:
283
+ *
284
+ * .abc {
285
+ * @apply hover:font-bold;
286
+ * }
287
+ *
288
+ * What we essentially will do is resolve to this:
289
+ *
290
+ * .abc {
291
+ * @apply .hover\:font-bold:hover {
292
+ * font-weight: 500;
293
+ * }
294
+ * }
295
+ *
296
+ * Notice that the to-be-applied class is `.hover\:font-bold:hover` and that the utility candidate was `hover:font-bold`.
297
+ * What happens in this function is that we prepend a `.` and escape the candidate.
298
+ * This will result in `.hover\:font-bold`
299
+ * Which means that we can replace `.hover\:font-bold` with `.abc` in `.hover\:font-bold:hover` resulting in `.abc:hover`
300
+ *
301
+ * @param {string} selector
302
+ * @param {string} utilitySelectors
303
+ * @param {string} candidate
304
+ */
305
+ function replaceSelector(selector, utilitySelectors, candidate) {
306
+ let selectorList = extractSelectors(selector)
307
+ let utilitySelectorsList = extractSelectors(utilitySelectors)
308
+ let candidateList = extractSelectors(`.${escapeClassName(candidate)}`)
309
+ let candidateClass = candidateList.nodes[0].nodes[0]
310
+
311
+ selectorList.each((sel) => {
312
+ /** @type {Set<import('postcss-selector-parser').Selector>} */
313
+ let replaced = new Set()
314
+
315
+ utilitySelectorsList.each((utilitySelector) => {
316
+ let hasReplaced = false
317
+ utilitySelector = utilitySelector.clone()
318
+
319
+ utilitySelector.walkClasses((node) => {
320
+ if (node.value !== candidateClass.value) {
321
+ return
131
322
  }
132
- return replaced.join(', ')
133
- })
134
- .join(', ')
135
- }
136
323
 
137
- let perParentApplies = new Map()
324
+ // Don't replace multiple instances of the same class
325
+ // This is theoretically correct but only partially
326
+ // We'd need to generate every possible permutation of the replacement
327
+ // For example with `.foo + .foo { … }` and `section { @apply foo; }`
328
+ // We'd need to generate all of these:
329
+ // - `.foo + .foo`
330
+ // - `.foo + section`
331
+ // - `section + .foo`
332
+ // - `section + section`
333
+ if (hasReplaced) {
334
+ return
335
+ }
138
336
 
139
- // Collect all apply candidates and their rules
140
- for (let apply of applies) {
141
- let candidates = perParentApplies.get(apply.parent) || []
337
+ // Since you can only `@apply` class names this is sufficient
338
+ // We want to replace the matched class name with the selector the user is using
339
+ // Ex: Replace `.text-blue-500` with `.foo.bar:is(.something-cool)`
340
+ node.replaceWith(...sel.nodes.map((node) => node.clone()))
142
341
 
143
- perParentApplies.set(apply.parent, candidates)
342
+ // Record that we did something and we want to use this new selector
343
+ replaced.add(utilitySelector)
144
344
 
145
- let [applyCandidates, important] = extractApplyCandidates(apply.params)
345
+ hasReplaced = true
346
+ })
347
+ })
146
348
 
147
- if (apply.parent.type === 'atrule') {
148
- if (apply.parent.name === 'screen') {
149
- const screenType = apply.parent.params
349
+ // Sort tag names before class names (but only sort each group (separated by a combinator)
350
+ // separately and not in total)
351
+ // This happens when replacing `.bar` in `.foo.bar` with a tag like `section`
352
+ for (let sel of replaced) {
353
+ let groups = [[]]
354
+ for (let node of sel.nodes) {
355
+ if (node.type === 'combinator') {
356
+ groups.push(node)
357
+ groups.push([])
358
+ } else {
359
+ let last = groups[groups.length - 1]
360
+ last.push(node)
361
+ }
362
+ }
150
363
 
151
- throw apply.error(
152
- `@apply is not supported within nested at-rules like @screen. We suggest you write this as @apply ${applyCandidates
153
- .map((c) => `${screenType}:${c}`)
154
- .join(' ')} instead.`
155
- )
364
+ sel.nodes = []
365
+
366
+ for (let group of groups) {
367
+ if (Array.isArray(group)) {
368
+ group.sort((a, b) => {
369
+ if (a.type === 'tag' && b.type === 'class') {
370
+ return -1
371
+ } else if (a.type === 'class' && b.type === 'tag') {
372
+ return 1
373
+ } else if (a.type === 'class' && b.type === 'pseudo' && b.value.startsWith('::')) {
374
+ return -1
375
+ } else if (a.type === 'pseudo' && a.value.startsWith('::') && b.type === 'class') {
376
+ return 1
377
+ }
378
+
379
+ return 0
380
+ })
381
+ }
382
+
383
+ sel.nodes = sel.nodes.concat(group)
156
384
  }
385
+ }
386
+
387
+ sel.replaceWith(...replaced)
388
+ })
389
+
390
+ return selectorList.toString()
391
+ }
392
+
393
+ let perParentApplies = new Map()
394
+
395
+ // Collect all apply candidates and their rules
396
+ for (let apply of applies) {
397
+ let [candidates] = perParentApplies.get(apply.parent) || [[], apply.source]
398
+
399
+ perParentApplies.set(apply.parent, [candidates, apply.source])
400
+
401
+ let [applyCandidates, important] = extractApplyCandidates(apply.params)
402
+
403
+ if (apply.parent.type === 'atrule') {
404
+ if (apply.parent.name === 'screen') {
405
+ let screenType = apply.parent.params
157
406
 
158
407
  throw apply.error(
159
- `@apply is not supported within nested at-rules like @${apply.parent.name}. You can fix this by un-nesting @${apply.parent.name}.`
408
+ `@apply is not supported within nested at-rules like @screen. We suggest you write this as @apply ${applyCandidates
409
+ .map((c) => `${screenType}:${c}`)
410
+ .join(' ')} instead.`
160
411
  )
161
412
  }
162
413
 
163
- for (let applyCandidate of applyCandidates) {
164
- if (!applyClassCache.has(applyCandidate)) {
165
- if (applyCandidate === prefix(context, 'group')) {
166
- // TODO: Link to specific documentation page with error code.
167
- throw apply.error(`@apply should not be used with the '${applyCandidate}' utility`)
168
- }
169
-
170
- throw apply.error(
171
- `The \`${applyCandidate}\` class does not exist. If \`${applyCandidate}\` is a custom class, make sure it is defined within a \`@layer\` directive.`
172
- )
173
- }
414
+ throw apply.error(
415
+ `@apply is not supported within nested at-rules like @${apply.parent.name}. You can fix this by un-nesting @${apply.parent.name}.`
416
+ )
417
+ }
174
418
 
175
- let rules = applyClassCache.get(applyCandidate)
419
+ for (let applyCandidate of applyCandidates) {
420
+ if ([prefix(context, 'group'), prefix(context, 'peer')].includes(applyCandidate)) {
421
+ // TODO: Link to specific documentation page with error code.
422
+ throw apply.error(`@apply should not be used with the '${applyCandidate}' utility`)
423
+ }
176
424
 
177
- candidates.push([applyCandidate, important, rules])
425
+ if (!applyClassCache.has(applyCandidate)) {
426
+ throw apply.error(
427
+ `The \`${applyCandidate}\` class does not exist. If \`${applyCandidate}\` is a custom class, make sure it is defined within a \`@layer\` directive.`
428
+ )
178
429
  }
430
+
431
+ let rules = applyClassCache.get(applyCandidate)
432
+
433
+ candidates.push([applyCandidate, important, rules])
179
434
  }
435
+ }
436
+
437
+ for (let [parent, [candidates, atApplySource]] of perParentApplies) {
438
+ let siblings = []
180
439
 
181
- for (const [parent, candidates] of perParentApplies) {
182
- let siblings = []
183
-
184
- for (let [applyCandidate, important, rules] of candidates) {
185
- for (let [meta, node] of rules) {
186
- let parentClasses = extractClasses(parent)
187
- let nodeClasses = extractClasses(node)
188
-
189
- // Add base utility classes from the @apply node to the list of
190
- // classes to check whether it intersects and therefore results in a
191
- // circular dependency or not.
192
- //
193
- // E.g.:
194
- // .foo {
195
- // @apply hover:a; // This applies "a" but with a modifier
196
- // }
197
- //
198
- // We only have to do that with base classes of the `node`, not of the `parent`
199
- // E.g.:
200
- // .hover\:foo {
201
- // @apply bar;
202
- // }
203
- // .bar {
204
- // @apply foo;
205
- // }
206
- //
207
- // This should not result in a circular dependency because we are
208
- // just applying `.foo` and the rule above is `.hover\:foo` which is
209
- // unrelated. However, if we were to apply `hover:foo` then we _did_
210
- // have to include this one.
211
- nodeClasses = nodeClasses.concat(
212
- extractBaseCandidates(nodeClasses, context.tailwindConfig.separator)
440
+ for (let [applyCandidate, important, rules] of candidates) {
441
+ let potentialApplyCandidates = [
442
+ applyCandidate,
443
+ ...extractBaseCandidates([applyCandidate], context.tailwindConfig.separator),
444
+ ]
445
+
446
+ for (let [meta, node] of rules) {
447
+ let parentClasses = extractClasses(parent)
448
+ let nodeClasses = extractClasses(node)
449
+
450
+ // When we encounter a rule like `.dark .a, .b { … }` we only want to be left with `[.dark, .a]` if the base applyCandidate is `.a` or with `[.b]` if the base applyCandidate is `.b`
451
+ // So we've split them into groups
452
+ nodeClasses = nodeClasses.groups
453
+ .filter((classList) =>
454
+ classList.some((className) => potentialApplyCandidates.includes(className))
213
455
  )
456
+ .flat()
457
+
458
+ // Add base utility classes from the @apply node to the list of
459
+ // classes to check whether it intersects and therefore results in a
460
+ // circular dependency or not.
461
+ //
462
+ // E.g.:
463
+ // .foo {
464
+ // @apply hover:a; // This applies "a" but with a modifier
465
+ // }
466
+ //
467
+ // We only have to do that with base classes of the `node`, not of the `parent`
468
+ // E.g.:
469
+ // .hover\:foo {
470
+ // @apply bar;
471
+ // }
472
+ // .bar {
473
+ // @apply foo;
474
+ // }
475
+ //
476
+ // This should not result in a circular dependency because we are
477
+ // just applying `.foo` and the rule above is `.hover\:foo` which is
478
+ // unrelated. However, if we were to apply `hover:foo` then we _did_
479
+ // have to include this one.
480
+ nodeClasses = nodeClasses.concat(
481
+ extractBaseCandidates(nodeClasses, context.tailwindConfig.separator)
482
+ )
214
483
 
215
- let intersects = parentClasses.some((selector) => nodeClasses.includes(selector))
216
- if (intersects) {
217
- throw node.error(
218
- `You cannot \`@apply\` the \`${applyCandidate}\` utility here because it creates a circular dependency.`
219
- )
220
- }
484
+ let intersects = parentClasses.some((selector) => nodeClasses.includes(selector))
485
+ if (intersects) {
486
+ throw node.error(
487
+ `You cannot \`@apply\` the \`${applyCandidate}\` utility here because it creates a circular dependency.`
488
+ )
489
+ }
221
490
 
222
- let root = postcss.root({ nodes: [node.clone()] })
223
- let canRewriteSelector =
224
- node.type !== 'atrule' || (node.type === 'atrule' && node.name !== 'keyframes')
225
-
226
- if (canRewriteSelector) {
227
- root.walkRules((rule) => {
228
- // Let's imagine you have the following structure:
229
- //
230
- // .foo {
231
- // @apply bar;
232
- // }
233
- //
234
- // @supports (a: b) {
235
- // .bar {
236
- // color: blue
237
- // }
238
- //
239
- // .something-unrelated {}
240
- // }
241
- //
242
- // In this case we want to apply `.bar` but it happens to be in
243
- // an atrule node. We clone that node instead of the nested one
244
- // because we still want that @supports rule to be there once we
245
- // applied everything.
246
- //
247
- // However it happens to be that the `.something-unrelated` is
248
- // also in that same shared @supports atrule. This is not good,
249
- // and this should not be there. The good part is that this is
250
- // a clone already and it can be safely removed. The question is
251
- // how do we know we can remove it. Basically what we can do is
252
- // match it against the applyCandidate that you want to apply. If
253
- // it doesn't match the we can safely delete it.
254
- //
255
- // If we didn't do this, then the `replaceSelector` function
256
- // would have replaced this with something that didn't exist and
257
- // therefore it removed the selector altogether. In this specific
258
- // case it would result in `{}` instead of `.something-unrelated {}`
259
- if (!extractClasses(rule).some((candidate) => candidate === applyCandidate)) {
260
- rule.remove()
261
- return
262
- }
491
+ let root = postcss.root({ nodes: [node.clone()] })
492
+
493
+ // Make sure every node in the entire tree points back at the @apply rule that generated it
494
+ root.walk((node) => {
495
+ node.source = atApplySource
496
+ })
497
+
498
+ let canRewriteSelector =
499
+ node.type !== 'atrule' || (node.type === 'atrule' && node.name !== 'keyframes')
500
+
501
+ if (canRewriteSelector) {
502
+ root.walkRules((rule) => {
503
+ // Let's imagine you have the following structure:
504
+ //
505
+ // .foo {
506
+ // @apply bar;
507
+ // }
508
+ //
509
+ // @supports (a: b) {
510
+ // .bar {
511
+ // color: blue
512
+ // }
513
+ //
514
+ // .something-unrelated {}
515
+ // }
516
+ //
517
+ // In this case we want to apply `.bar` but it happens to be in
518
+ // an atrule node. We clone that node instead of the nested one
519
+ // because we still want that @supports rule to be there once we
520
+ // applied everything.
521
+ //
522
+ // However it happens to be that the `.something-unrelated` is
523
+ // also in that same shared @supports atrule. This is not good,
524
+ // and this should not be there. The good part is that this is
525
+ // a clone already and it can be safely removed. The question is
526
+ // how do we know we can remove it. Basically what we can do is
527
+ // match it against the applyCandidate that you want to apply. If
528
+ // it doesn't match the we can safely delete it.
529
+ //
530
+ // If we didn't do this, then the `replaceSelector` function
531
+ // would have replaced this with something that didn't exist and
532
+ // therefore it removed the selector altogether. In this specific
533
+ // case it would result in `{}` instead of `.something-unrelated {}`
534
+ if (!extractClasses(rule).some((candidate) => candidate === applyCandidate)) {
535
+ rule.remove()
536
+ return
537
+ }
538
+
539
+ // Strip the important selector from the parent selector if at the beginning
540
+ let importantSelector =
541
+ typeof context.tailwindConfig.important === 'string'
542
+ ? context.tailwindConfig.important
543
+ : null
544
+
545
+ // We only want to move the "important" selector if this is a Tailwind-generated utility
546
+ // We do *not* want to do this for user CSS that happens to be structured the same
547
+ let isGenerated = parent.raws.tailwind !== undefined
263
548
 
264
- rule.selector = replaceSelector(parent.selector, rule.selector, applyCandidate)
549
+ let parentSelector =
550
+ isGenerated && importantSelector && parent.selector.indexOf(importantSelector) === 0
551
+ ? parent.selector.slice(importantSelector.length)
552
+ : parent.selector
265
553
 
266
- rule.walkDecls((d) => {
267
- d.important = meta.important || important
268
- })
554
+ rule.selector = replaceSelector(parentSelector, rule.selector, applyCandidate)
555
+
556
+ // And then re-add it if it was removed
557
+ if (importantSelector && parentSelector !== parent.selector) {
558
+ rule.selector = `${importantSelector} ${rule.selector}`
559
+ }
560
+
561
+ rule.walkDecls((d) => {
562
+ d.important = meta.important || important
269
563
  })
270
- }
564
+ })
565
+ }
271
566
 
272
- // Insert it
273
- siblings.push([
274
- // Ensure that when we are sorting, that we take the layer order into account
275
- { ...meta, sort: meta.sort | context.layerOrder[meta.layer] },
276
- root.nodes[0],
277
- ])
567
+ // It could be that the node we were inserted was removed because the class didn't match
568
+ // If that was the *only* rule in the parent, then we have nothing add so we skip it
569
+ if (!root.nodes[0]) {
570
+ continue
278
571
  }
572
+
573
+ // Insert it
574
+ siblings.push([meta.sort, root.nodes[0]])
279
575
  }
576
+ }
280
577
 
281
- // Inject the rules, sorted, correctly
282
- let nodes = siblings.sort(([a], [z]) => bigSign(a.sort - z.sort)).map((s) => s[1])
578
+ // Inject the rules, sorted, correctly
579
+ let nodes = context.offsets.sort(siblings).map((s) => s[1])
283
580
 
284
- // `parent` refers to the node at `.abc` in: .abc { @apply mt-2 }
285
- parent.after(nodes)
286
- }
581
+ // `parent` refers to the node at `.abc` in: .abc { @apply mt-2 }
582
+ parent.after(nodes)
583
+ }
287
584
 
288
- for (let apply of applies) {
289
- // If there are left-over declarations, just remove the @apply
290
- if (apply.parent.nodes.length > 1) {
291
- apply.remove()
292
- } else {
293
- // The node is empty, drop the full node
294
- apply.parent.remove()
295
- }
585
+ for (let apply of applies) {
586
+ // If there are left-over declarations, just remove the @apply
587
+ if (apply.parent.nodes.length > 1) {
588
+ apply.remove()
589
+ } else {
590
+ // The node is empty, drop the full node
591
+ apply.parent.remove()
296
592
  }
297
-
298
- // Do it again, in case we have other `@apply` rules
299
- processApply(root, context)
300
593
  }
594
+
595
+ // Do it again, in case we have other `@apply` rules
596
+ processApply(root, context, localCache)
301
597
  }
302
598
 
303
599
  export default function expandApplyAtRules(context) {
304
600
  return (root) => {
305
- processApply(root, context)
601
+ // Build a cache of the user's CSS so we can use it to resolve classes used by @apply
602
+ let localCache = lazyCache(() => buildLocalApplyCache(root, context))
603
+
604
+ processApply(root, context, localCache)
306
605
  }
307
606
  }