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