tailwindcss 0.0.0-insiders.d3e754a → 0.0.0-insiders.d3f2221

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