tailwindcss 3.0.23 → 3.1.1

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