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