tailwindcss 0.0.0-insiders.d2b53cd → 0.0.0-insiders.d2fdf9e

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