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

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