tailwindcss 0.0.0-insiders.fb545bc → 0.0.0-insiders.fb796cd

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