tailwindcss 0.0.0-insiders.f4635e0 → 0.0.0-insiders.f4a2ccd

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 (238) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +15 -7
  3. package/colors.d.ts +3 -0
  4. package/colors.js +2 -304
  5. package/defaultConfig.d.ts +3 -0
  6. package/defaultConfig.js +2 -4
  7. package/defaultTheme.d.ts +4 -0
  8. package/defaultTheme.js +2 -4
  9. package/lib/cli/build/deps.js +54 -0
  10. package/lib/cli/build/index.js +52 -0
  11. package/lib/cli/build/plugin.js +376 -0
  12. package/lib/cli/build/utils.js +78 -0
  13. package/lib/cli/build/watching.js +180 -0
  14. package/lib/cli/help/index.js +71 -0
  15. package/lib/cli/index.js +230 -0
  16. package/lib/cli/init/index.js +61 -0
  17. package/lib/cli-peer-dependencies.js +22 -14
  18. package/lib/cli.js +4 -762
  19. package/lib/corePluginList.js +182 -5
  20. package/lib/corePlugins.js +4109 -2952
  21. package/lib/css/preflight.css +24 -9
  22. package/lib/featureFlags.js +68 -50
  23. package/lib/index.js +5 -30
  24. package/lib/lib/cacheInvalidation.js +90 -0
  25. package/lib/lib/collapseAdjacentRules.js +52 -36
  26. package/lib/lib/collapseDuplicateDeclarations.js +83 -0
  27. package/lib/lib/content.js +177 -0
  28. package/lib/lib/defaultExtractor.js +241 -0
  29. package/lib/lib/detectNesting.js +39 -17
  30. package/lib/lib/evaluateTailwindFunctions.js +203 -161
  31. package/lib/lib/expandApplyAtRules.js +503 -221
  32. package/lib/lib/expandTailwindAtRules.js +255 -243
  33. package/lib/lib/findAtConfigPath.js +44 -0
  34. package/lib/lib/generateRules.js +831 -317
  35. package/lib/lib/getModuleDependencies.js +93 -49
  36. package/lib/lib/load-config.js +36 -0
  37. package/lib/lib/normalizeTailwindDirectives.js +79 -60
  38. package/lib/lib/offsets.js +304 -0
  39. package/lib/lib/partitionApplyAtRules.js +56 -0
  40. package/lib/lib/regex.js +60 -0
  41. package/lib/lib/remap-bitfield.js +87 -0
  42. package/lib/lib/resolveDefaultsAtRules.js +150 -94
  43. package/lib/lib/setupContextUtils.js +1197 -685
  44. package/lib/lib/setupTrackingContext.js +150 -177
  45. package/lib/lib/sharedState.js +70 -21
  46. package/lib/lib/substituteScreenAtRules.js +26 -28
  47. package/lib/oxide/cli/build/deps.js +81 -0
  48. package/lib/oxide/cli/build/index.js +51 -0
  49. package/lib/oxide/cli/build/plugin.js +373 -0
  50. package/lib/oxide/cli/build/utils.js +77 -0
  51. package/lib/oxide/cli/build/watching.js +177 -0
  52. package/lib/oxide/cli/help/index.js +70 -0
  53. package/lib/oxide/cli/index.js +214 -0
  54. package/lib/oxide/cli/init/index.js +50 -0
  55. package/lib/oxide/cli.js +5 -0
  56. package/lib/oxide/postcss-plugin.js +2 -0
  57. package/lib/plugin.js +98 -0
  58. package/{nesting → lib/postcss-plugins/nesting}/README.md +2 -2
  59. package/lib/postcss-plugins/nesting/index.js +19 -0
  60. package/lib/postcss-plugins/nesting/plugin.js +87 -0
  61. package/lib/processTailwindFeatures.js +58 -56
  62. package/lib/public/colors.js +353 -0
  63. package/lib/public/create-plugin.js +15 -0
  64. package/lib/public/default-config.js +16 -0
  65. package/lib/public/default-theme.js +16 -0
  66. package/lib/public/load-config.js +10 -0
  67. package/lib/public/resolve-config.js +22 -0
  68. package/lib/util/applyImportantSelector.js +22 -0
  69. package/lib/util/bigSign.js +7 -6
  70. package/lib/util/buildMediaQuery.js +21 -32
  71. package/lib/util/cloneDeep.js +16 -14
  72. package/lib/util/cloneNodes.js +29 -15
  73. package/lib/util/color.js +104 -66
  74. package/lib/util/configurePlugins.js +17 -15
  75. package/lib/util/createPlugin.js +23 -26
  76. package/lib/util/createUtilityPlugin.js +46 -46
  77. package/lib/util/dataTypes.js +266 -0
  78. package/lib/util/defaults.js +20 -15
  79. package/lib/util/escapeClassName.js +18 -17
  80. package/lib/util/escapeCommas.js +7 -6
  81. package/lib/util/flattenColorPalette.js +13 -12
  82. package/lib/util/formatVariantSelector.js +341 -0
  83. package/lib/util/getAllConfigs.js +44 -18
  84. package/lib/util/hashConfig.js +15 -12
  85. package/lib/util/isKeyframeRule.js +7 -6
  86. package/lib/util/isPlainObject.js +11 -11
  87. package/lib/util/isSyntacticallyValidPropertyValue.js +72 -0
  88. package/lib/util/log.js +52 -33
  89. package/lib/util/nameClass.js +35 -29
  90. package/lib/util/negateValue.js +31 -17
  91. package/lib/util/normalizeConfig.js +294 -0
  92. package/lib/util/normalizeScreens.js +170 -0
  93. package/lib/util/parseAnimationValue.js +85 -54
  94. package/lib/util/parseBoxShadowValue.js +84 -0
  95. package/lib/util/parseDependency.js +41 -70
  96. package/lib/util/parseGlob.js +34 -0
  97. package/lib/util/parseObjectStyles.js +30 -24
  98. package/lib/util/pluginUtils.js +234 -288
  99. package/lib/util/prefixSelector.js +33 -19
  100. package/lib/util/removeAlphaVariables.js +29 -0
  101. package/lib/util/resolveConfig.js +221 -256
  102. package/lib/util/resolveConfigPath.js +55 -48
  103. package/lib/util/responsive.js +18 -14
  104. package/lib/util/splitAtTopLevelOnly.js +49 -0
  105. package/lib/util/tap.js +8 -7
  106. package/lib/util/toColorValue.js +7 -6
  107. package/lib/util/toPath.js +27 -8
  108. package/lib/util/transformThemeValue.js +67 -28
  109. package/lib/util/validateConfig.js +24 -0
  110. package/lib/util/validateFormalSyntax.js +24 -0
  111. package/lib/util/withAlphaVariable.js +67 -57
  112. package/loadConfig.d.ts +4 -0
  113. package/loadConfig.js +2 -0
  114. package/nesting/index.js +2 -12
  115. package/package.json +69 -66
  116. package/peers/index.js +76328 -84169
  117. package/plugin.d.ts +11 -0
  118. package/plugin.js +1 -2
  119. package/resolveConfig.d.ts +12 -0
  120. package/resolveConfig.js +2 -7
  121. package/scripts/create-plugin-list.js +2 -2
  122. package/scripts/generate-types.js +105 -0
  123. package/scripts/release-channel.js +18 -0
  124. package/scripts/release-notes.js +21 -0
  125. package/scripts/swap-engines.js +40 -0
  126. package/scripts/type-utils.js +27 -0
  127. package/src/cli/build/deps.js +56 -0
  128. package/src/cli/build/index.js +49 -0
  129. package/src/cli/build/plugin.js +444 -0
  130. package/src/cli/build/utils.js +76 -0
  131. package/src/cli/build/watching.js +229 -0
  132. package/src/cli/help/index.js +70 -0
  133. package/src/cli/index.js +216 -0
  134. package/src/cli/init/index.js +79 -0
  135. package/src/cli-peer-dependencies.js +7 -1
  136. package/src/cli.js +4 -756
  137. package/src/corePluginList.js +1 -1
  138. package/src/corePlugins.js +2526 -1956
  139. package/src/css/preflight.css +24 -9
  140. package/src/featureFlags.js +33 -10
  141. package/src/index.js +4 -33
  142. package/src/lib/cacheInvalidation.js +52 -0
  143. package/src/lib/collapseAdjacentRules.js +21 -2
  144. package/src/lib/collapseDuplicateDeclarations.js +93 -0
  145. package/src/lib/content.js +208 -0
  146. package/src/lib/defaultExtractor.js +217 -0
  147. package/src/lib/detectNesting.js +30 -3
  148. package/src/lib/evaluateTailwindFunctions.js +84 -10
  149. package/src/lib/expandApplyAtRules.js +509 -153
  150. package/src/lib/expandTailwindAtRules.js +125 -104
  151. package/src/lib/findAtConfigPath.js +48 -0
  152. package/src/lib/generateRules.js +678 -70
  153. package/src/lib/getModuleDependencies.js +70 -30
  154. package/src/lib/load-config.ts +27 -0
  155. package/src/lib/normalizeTailwindDirectives.js +10 -3
  156. package/src/lib/offsets.js +373 -0
  157. package/src/lib/partitionApplyAtRules.js +52 -0
  158. package/src/lib/regex.js +74 -0
  159. package/src/lib/remap-bitfield.js +82 -0
  160. package/src/lib/resolveDefaultsAtRules.js +105 -47
  161. package/src/lib/setupContextUtils.js +848 -247
  162. package/src/lib/setupTrackingContext.js +53 -64
  163. package/src/lib/sharedState.js +60 -6
  164. package/src/lib/substituteScreenAtRules.js +6 -3
  165. package/src/oxide/cli/build/deps.ts +91 -0
  166. package/src/oxide/cli/build/index.ts +47 -0
  167. package/src/oxide/cli/build/plugin.ts +442 -0
  168. package/src/oxide/cli/build/utils.ts +74 -0
  169. package/src/oxide/cli/build/watching.ts +225 -0
  170. package/src/oxide/cli/help/index.ts +69 -0
  171. package/src/oxide/cli/index.ts +204 -0
  172. package/src/oxide/cli/init/index.ts +59 -0
  173. package/src/oxide/cli.ts +1 -0
  174. package/src/oxide/postcss-plugin.ts +1 -0
  175. package/src/plugin.js +107 -0
  176. package/src/postcss-plugins/nesting/README.md +42 -0
  177. package/src/postcss-plugins/nesting/index.js +13 -0
  178. package/src/postcss-plugins/nesting/plugin.js +80 -0
  179. package/src/processTailwindFeatures.js +18 -3
  180. package/src/public/colors.js +322 -0
  181. package/src/public/create-plugin.js +2 -0
  182. package/src/public/default-config.js +4 -0
  183. package/src/public/default-theme.js +4 -0
  184. package/src/public/load-config.js +2 -0
  185. package/src/public/resolve-config.js +7 -0
  186. package/src/util/applyImportantSelector.js +19 -0
  187. package/src/util/buildMediaQuery.js +14 -16
  188. package/src/util/cloneNodes.js +19 -2
  189. package/src/util/color.js +43 -11
  190. package/src/util/createUtilityPlugin.js +2 -11
  191. package/src/util/dataTypes.js +281 -0
  192. package/src/util/defaults.js +6 -0
  193. package/src/util/formatVariantSelector.js +412 -0
  194. package/src/util/getAllConfigs.js +21 -2
  195. package/src/util/isSyntacticallyValidPropertyValue.js +61 -0
  196. package/src/util/log.js +23 -22
  197. package/src/util/nameClass.js +6 -2
  198. package/src/util/negateValue.js +15 -5
  199. package/src/util/normalizeConfig.js +318 -0
  200. package/src/util/normalizeScreens.js +140 -0
  201. package/src/util/parseAnimationValue.js +7 -1
  202. package/src/util/parseBoxShadowValue.js +72 -0
  203. package/src/util/parseDependency.js +37 -38
  204. package/src/util/parseGlob.js +24 -0
  205. package/src/util/pluginUtils.js +219 -219
  206. package/src/util/prefixSelector.js +30 -13
  207. package/src/util/removeAlphaVariables.js +24 -0
  208. package/src/util/resolveConfig.js +86 -91
  209. package/src/util/resolveConfigPath.js +12 -1
  210. package/src/util/splitAtTopLevelOnly.js +52 -0
  211. package/src/util/toPath.js +23 -1
  212. package/src/util/transformThemeValue.js +33 -8
  213. package/src/util/validateConfig.js +13 -0
  214. package/src/util/validateFormalSyntax.js +34 -0
  215. package/src/util/withAlphaVariable.js +14 -9
  216. package/stubs/.gitignore +1 -0
  217. package/stubs/.prettierrc.json +6 -0
  218. package/stubs/{defaultConfig.stub.js → config.full.js} +332 -249
  219. package/stubs/{simpleConfig.stub.js → config.simple.js} +0 -1
  220. package/stubs/postcss.config.js +6 -0
  221. package/stubs/tailwind.config.cjs +2 -0
  222. package/stubs/tailwind.config.js +2 -0
  223. package/stubs/tailwind.config.ts +3 -0
  224. package/types/config.d.ts +373 -0
  225. package/types/generated/.gitkeep +0 -0
  226. package/types/generated/colors.d.ts +298 -0
  227. package/types/generated/corePluginList.d.ts +1 -0
  228. package/types/generated/default-theme.d.ts +347 -0
  229. package/types/index.d.ts +7 -0
  230. package/CHANGELOG.md +0 -1742
  231. package/lib/constants.js +0 -37
  232. package/lib/lib/setupWatchingContext.js +0 -331
  233. package/nesting/plugin.js +0 -41
  234. package/scripts/install-integrations.js +0 -27
  235. package/scripts/rebuildFixtures.js +0 -68
  236. package/src/constants.js +0 -17
  237. package/src/lib/setupWatchingContext.js +0 -307
  238. /package/stubs/{defaultPostCssConfig.stub.js → postcss.config.cjs} +0 -0
@@ -1,245 +1,527 @@
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
+ const _applyImportantSelector = require("../util/applyImportantSelector");
14
+ function _interopRequireDefault(obj) {
15
+ return obj && obj.__esModule ? obj : {
16
+ default: obj
17
+ };
21
18
  }
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;
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());
32
54
  }
33
-
34
- let matches = Array.from((0, _generateRules.resolveMatches)(candidate, context));
35
-
36
- if (matches.length === 0) {
37
- context.notClassCache.add(candidate);
38
- continue;
55
+ return Array.from(baseClasses);
56
+ }
57
+ function prefix(context, 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;
39
66
  }
40
-
41
- context.applyClassCache.set(candidate, matches);
42
- }
43
-
44
- return context.applyClassCache;
45
67
  }
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];
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;
55
80
  }
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 = [];
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;
72
124
  }
73
-
74
- nodeGroups.push([node]);
75
- } else {
76
- lastGroup.push(node);
77
- }
78
- }
79
-
80
- if (lastGroup.length > 0) {
81
- nodeGroups.push(lastGroup);
125
+ if (parent.type === "root") {
126
+ break;
127
+ }
128
+ node = shallowClone(parent, {
129
+ nodes: [
130
+ node
131
+ ]
132
+ });
82
133
  }
83
-
84
- if (nodeGroups.length === 1) {
85
- continue;
134
+ return node;
135
+ }
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) {
169
+ for (let candidate of applyCandidates){
170
+ if (context.notClassCache.has(candidate) || context.applyClassCache.has(candidate)) {
171
+ continue;
172
+ }
173
+ if (context.classCache.has(candidate)) {
174
+ context.applyClassCache.set(candidate, context.classCache.get(candidate).map(([meta, rule])=>[
175
+ meta,
176
+ rule.clone()
177
+ ]));
178
+ continue;
179
+ }
180
+ let matches = Array.from((0, _generateRules.resolveMatches)(candidate, context));
181
+ if (matches.length === 0) {
182
+ context.notClassCache.add(candidate);
183
+ continue;
184
+ }
185
+ context.applyClassCache.set(candidate, matches);
86
186
  }
87
-
88
- for (let group of [...nodeGroups].reverse()) {
89
- let newParent = rule.clone({
90
- nodes: []
91
- });
92
- newParent.append(group);
93
- rule.after(newParent);
187
+ return context.applyClassCache;
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
+ }
219
+ function extractApplyCandidates(params) {
220
+ let candidates = params.split(/[\s\t\n]+/g);
221
+ if (candidates[candidates.length - 1] === "!important") {
222
+ return [
223
+ candidates.slice(0, -1),
224
+ true
225
+ ];
94
226
  }
95
-
96
- rule.remove();
97
- }
227
+ return [
228
+ candidates,
229
+ false
230
+ ];
98
231
  }
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);
232
+ function processApply(root, context, localCache) {
233
+ let applyCandidates = new Set();
234
+ // Collect all @apply rules and candidates
235
+ let applies = [];
236
+ root.walkAtRules("apply", (rule)=>{
237
+ let [candidates] = extractApplyCandidates(rule.params);
238
+ for (let util of candidates){
239
+ applyCandidates.add(util);
240
+ }
241
+ applies.push(rule);
242
+ });
243
+ // Start the @apply process if we have rules with @apply in them
244
+ if (applies.length === 0) {
245
+ return;
109
246
  }
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
247
  // Fill up some caches!
116
- let applyClassCache = buildApplyCache(applyCandidates, context);
248
+ let applyClassCache = combineCaches([
249
+ localCache,
250
+ buildApplyCache(applyCandidates, context)
251
+ ]);
117
252
  /**
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(', ');
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);
324
+ }
325
+ }
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
+ });
341
+ }
342
+ sel1.nodes = sel1.nodes.concat(group);
343
+ }
344
+ }
345
+ sel.replaceWith(...replaced);
346
+ });
347
+ return selectorList.toString();
157
348
  }
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.`);
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}.`);
170
367
  }
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.`);
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
+ ]);
183
385
  }
184
-
185
- let rules = applyClassCache.get(applyCandidate);
186
- candidates.push([applyCandidate, important, rules]);
187
- }
188
386
  }
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]]);
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;
489
+ });
490
+ });
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
+ ]);
502
+ }
503
+ }
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();
215
516
  }
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
517
  }
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
- }
518
+ // Do it again, in case we have other `@apply` rules
519
+ processApply(root, context, localCache);
238
520
  }
239
-
240
521
  function expandApplyAtRules(context) {
241
- return root => {
242
- partitionApplyParents(root);
243
- processApply(root, context);
244
- };
245
- }
522
+ return (root)=>{
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);
526
+ };
527
+ }