@eightshift/frontend-libs-tailwind 3.3.0 → 3.5.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/.github/CODEOWNERS +1 -1
- package/CHANGELOG.md +27 -1
- package/README.md +45 -0
- package/bin/tw-sweep.mjs +281 -0
- package/bun.lock +163 -152
- package/linters/oxlint.config.mjs +65 -0
- package/linters/tw-ignore.mjs +45 -0
- package/linters/tw-sweep-extract.mjs +545 -0
- package/package.json +24 -16
- package/scripts/editor/tailwindcss.js +60 -220
- package/test/fixtures/theme/src/Blocks/custom/card/card.php +23 -0
- package/test/fixtures/theme/src/Blocks/custom/card/manifest.json +13 -0
- package/test/fixtures/theme/src/Plugins/Forms.php +15 -0
- package/test/fixtures/theme/src/card.js +2 -0
- package/test/fixtures/theme/tailwind.css +6 -0
- package/test/fixtures/theme/tw-ignore.json +1 -0
- package/test/tw-sweep.test.mjs +119 -0
- package/.claude/settings.local.json +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eightshift/frontend-libs-tailwind",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "A framework for creating modern Gutenberg themes with styling provided by Tailwind CSS.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Eightshift team",
|
|
@@ -27,48 +27,56 @@
|
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"lintJs": "eslint",
|
|
30
|
+
"lint:tw": "node bin/tw-sweep.mjs",
|
|
31
|
+
"test": "node --test 'test/*.test.mjs'",
|
|
30
32
|
"prepare": "husky"
|
|
31
33
|
},
|
|
34
|
+
"bin": {
|
|
35
|
+
"tw-sweep": "bin/tw-sweep.mjs"
|
|
36
|
+
},
|
|
32
37
|
"homepage": "https://github.com/infinum/eightshift-frontend-libs-tailwind#readme",
|
|
33
38
|
"license": "MIT",
|
|
34
39
|
"dependencies": {
|
|
35
40
|
"@stylistic/eslint-plugin": "^5.10.0",
|
|
36
|
-
"@stylistic/stylelint-plugin": "^5.2.
|
|
37
|
-
"@swc/core": "^1.15.
|
|
38
|
-
"@tailwindcss/
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
+
"@stylistic/stylelint-plugin": "^5.2.1",
|
|
42
|
+
"@swc/core": "^1.15.43",
|
|
43
|
+
"@tailwindcss/node": "^4.3.3",
|
|
44
|
+
"@tailwindcss/webpack": "^4.3.3",
|
|
45
|
+
"@wordpress/dependency-extraction-webpack-plugin": "^6.51.0",
|
|
46
|
+
"browserslist": "^4.28.6",
|
|
41
47
|
"css-loader": "^7.1.4",
|
|
42
48
|
"css-minimizer-webpack-plugin": "^8.0.0",
|
|
43
|
-
"eslint": "^10.
|
|
49
|
+
"eslint": "^10.7.0",
|
|
44
50
|
"eslint-config-prettier": "^10.1.8",
|
|
51
|
+
"eslint-plugin-better-tailwindcss": "^4.7.0",
|
|
45
52
|
"eslint-plugin-prettier": "^5.5.6",
|
|
46
53
|
"eslint-plugin-react": "^7.37.5",
|
|
47
54
|
"eslint-plugin-react-hooks": "^7.1.1",
|
|
48
55
|
"eslint-plugin-unused-imports": "^4.4.1",
|
|
49
56
|
"glob": "^13.0.6",
|
|
50
|
-
"globals": "^17.
|
|
57
|
+
"globals": "^17.7.0",
|
|
51
58
|
"husky": "^9.1.7",
|
|
52
59
|
"lightningcss": "^1.32.0",
|
|
53
60
|
"mini-css-extract-plugin": "^2.10.2",
|
|
54
|
-
"
|
|
55
|
-
"prettier
|
|
61
|
+
"oxlint": "^1.82.0",
|
|
62
|
+
"prettier": "^3.9.5",
|
|
63
|
+
"prettier-plugin-tailwindcss": "^0.8.1",
|
|
56
64
|
"sonner": "^2.0.7",
|
|
57
|
-
"stylelint": "^17.
|
|
65
|
+
"stylelint": "^17.14.0",
|
|
58
66
|
"stylelint-config-standard": "^40.0.0",
|
|
59
67
|
"swc-loader": "^0.2.7",
|
|
60
68
|
"terser-webpack-plugin": "^5.6.1",
|
|
61
|
-
"webpack": "^5.
|
|
62
|
-
"webpack-cli": "^7.
|
|
69
|
+
"webpack": "^5.108.4",
|
|
70
|
+
"webpack-cli": "^7.2.1",
|
|
63
71
|
"webpack-manifest-plugin": "^6.0.1",
|
|
64
72
|
"webpack-merge": "^6.0.1"
|
|
65
73
|
},
|
|
66
74
|
"devDependencies": {
|
|
67
|
-
"@wordpress/api-fetch": "^7.
|
|
68
|
-
"lint-staged": "^
|
|
75
|
+
"@wordpress/api-fetch": "^7.51.0",
|
|
76
|
+
"lint-staged": "^17.1.0"
|
|
69
77
|
},
|
|
70
78
|
"peerDependencies": {
|
|
71
|
-
"@eightshift/ui-components": "^
|
|
79
|
+
"@eightshift/ui-components": "^8.0.0"
|
|
72
80
|
},
|
|
73
81
|
"sideEffects": false,
|
|
74
82
|
"lint-staged": {
|
|
@@ -1,214 +1,6 @@
|
|
|
1
1
|
import { checkAttr } from '../editor/attributes';
|
|
2
2
|
import { clsx } from '@eightshift/ui-components/utilities';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Gets Tailwind classes for the provided part.
|
|
6
|
-
*
|
|
7
|
-
* The part needs to be defined within the manifest, in the `tailwind` object.
|
|
8
|
-
*
|
|
9
|
-
* @param {string} part - Part name.
|
|
10
|
-
* @param {Object<string, mixed>} manifest - Component/block manifest.
|
|
11
|
-
* @param {...string?} custom - Custom classes to include in the output.
|
|
12
|
-
*
|
|
13
|
-
* @returns {string} Output classes.
|
|
14
|
-
*
|
|
15
|
-
* @deprecated Since 1.4.0. Use `tailwindClasses` instead.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* const classes = getTwPart('intro', manifest);
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* const classes = getTwPart('intro', manifest, 'p-4 bg-gray-100');
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
export const getTwPart = (part, manifest, ...custom) => {
|
|
25
|
-
if (!part || !manifest || !manifest?.tailwind || Object.keys(manifest?.tailwind ?? {}).length === 0) {
|
|
26
|
-
return clsx(...custom);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let partClasses = manifest?.tailwind?.parts?.[part]?.twClassesEditor ?? manifest?.tailwind?.parts?.[part]?.twClasses ?? '';
|
|
30
|
-
|
|
31
|
-
if (Array.isArray(partClasses)) {
|
|
32
|
-
partClasses = partClasses.join(' ');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return clsx(partClasses, ...custom);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Gets Tailwind classes for the provided dynamic part.
|
|
40
|
-
*
|
|
41
|
-
* The part needs to be defined within the manifest, in the `tailwind` object.
|
|
42
|
-
*
|
|
43
|
-
* @param {string} part - Part name.
|
|
44
|
-
* @param {Object<string, mixed>} attributes - Current attribute value.
|
|
45
|
-
* @param {Object<string, mixed>} manifest - Component/block manifest.
|
|
46
|
-
* @param {...string?} custom - Custom classes to include in the output.
|
|
47
|
-
*
|
|
48
|
-
* @returns {string} Output classes.
|
|
49
|
-
*
|
|
50
|
-
* @deprecated Since 1.4.0. Use `tailwindClasses` instead.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* const classes = getTwPart('intro', manifest);
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* const classes = getTwPart('intro', manifest, 'p-4 bg-gray-100');
|
|
57
|
-
*
|
|
58
|
-
*/
|
|
59
|
-
export const getTwDynamicPart = (part, attributes, manifest, ...custom) => {
|
|
60
|
-
if (!part || !manifest || !manifest?.tailwind || Object.keys(manifest?.tailwind ?? {}).length === 0) {
|
|
61
|
-
return clsx(...custom);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const baseClasses = manifest?.tailwind?.parts?.[part]?.twClassesEditor ?? manifest?.tailwind?.parts?.[part]?.twClasses ?? '';
|
|
65
|
-
|
|
66
|
-
const mainClasses = Object.entries(manifest?.tailwind?.options ?? {}).reduce((current, [attributeName, { responsive, twClasses, twClassesEditor, part: partName }]) => {
|
|
67
|
-
if (partName !== part) {
|
|
68
|
-
return current;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const value = checkAttr(attributeName, attributes, manifest, true);
|
|
72
|
-
|
|
73
|
-
if (!value) {
|
|
74
|
-
return current;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!responsive) {
|
|
78
|
-
const currentClasses = twClassesEditor?.[value] ?? twClasses?.[value];
|
|
79
|
-
|
|
80
|
-
if (Array.isArray(currentClasses)) {
|
|
81
|
-
return [...current, ...currentClasses];
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return [...current, currentClasses];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const responsiveClasses = Object.keys(value).reduce((curr, breakpoint) => {
|
|
88
|
-
if (breakpoint === '_desktopFirst') {
|
|
89
|
-
return curr;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
let currentClasses = twClassesEditor?.[value[breakpoint]] ?? twClasses?.[value[breakpoint]];
|
|
93
|
-
|
|
94
|
-
if (!Array.isArray(currentClasses)) {
|
|
95
|
-
currentClasses = [currentClasses];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (breakpoint === '_default') {
|
|
99
|
-
return [...curr, ...currentClasses];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return [...curr, ...currentClasses.split(' ').map((currentClass) => `${breakpoint}:${currentClass}`)];
|
|
103
|
-
}, []);
|
|
104
|
-
|
|
105
|
-
return [...current, ...responsiveClasses];
|
|
106
|
-
}, []);
|
|
107
|
-
|
|
108
|
-
return clsx(baseClasses, ...mainClasses, ...custom);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Get Tailwind classes from attributes and manifest.
|
|
113
|
-
*
|
|
114
|
-
* @param {Object<string, mixed>} attributes - Current attribute value.
|
|
115
|
-
* @param {Object<string, mixed>} manifest - Component/block manifest.
|
|
116
|
-
* @param {...string?} custom - Custom classes to include in the output.
|
|
117
|
-
*
|
|
118
|
-
* @returns {string} Output classes
|
|
119
|
-
*
|
|
120
|
-
* @deprecated Since 1.4.0. Use `tailwindClasses` instead.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* const classes = getTwClasses(attributes, manifest);
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* const classes = getTwClasses(attributes, manifest, 'p-4 bg-gray-100');
|
|
127
|
-
*
|
|
128
|
-
*/
|
|
129
|
-
export const getTwClasses = (attributes, manifest, ...custom) => {
|
|
130
|
-
if (!attributes || !manifest || !manifest?.tailwind || Object.keys(manifest?.tailwind ?? {}).length === 0) {
|
|
131
|
-
return clsx(...custom);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
let baseClasses = manifest?.tailwind?.base?.twClassesEditor ?? manifest?.tailwind?.base?.twClasses ?? '';
|
|
135
|
-
|
|
136
|
-
if (Array.isArray(baseClasses)) {
|
|
137
|
-
baseClasses = baseClasses.join(' ');
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const mainClasses = Object.entries(manifest?.tailwind?.options ?? {}).reduce((current, [attributeName, { responsive, twClasses, twClassesEditor, part: partName }]) => {
|
|
141
|
-
if (partName) {
|
|
142
|
-
return current;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const value = checkAttr(attributeName, attributes, manifest, true);
|
|
146
|
-
|
|
147
|
-
if (!value) {
|
|
148
|
-
return current;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (!responsive) {
|
|
152
|
-
let currentClasses = twClassesEditor?.[value] ?? twClasses?.[value];
|
|
153
|
-
|
|
154
|
-
if (Array.isArray(currentClasses)) {
|
|
155
|
-
currentClasses = currentClasses.join(' ');
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return [...current, currentClasses];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const responsiveClasses = Object.keys(value).reduce((curr, breakpoint) => {
|
|
162
|
-
if (breakpoint === '_desktopFirst') {
|
|
163
|
-
return curr;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let currentClasses = twClassesEditor?.[value[breakpoint]] ?? twClasses?.[value[breakpoint]];
|
|
167
|
-
|
|
168
|
-
if (!Array.isArray(currentClasses)) {
|
|
169
|
-
currentClasses = [currentClasses];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (breakpoint === '_default') {
|
|
173
|
-
return [...curr, ...currentClasses];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return [...curr, ...currentClasses.map((currentClass) => `${breakpoint}:${currentClass}`)];
|
|
177
|
-
}, []);
|
|
178
|
-
|
|
179
|
-
return [...current, ...responsiveClasses];
|
|
180
|
-
}, []);
|
|
181
|
-
|
|
182
|
-
const combinationClasses =
|
|
183
|
-
manifest?.tailwind?.combinations?.reduce((current, { attributes: conditions, twClasses, twClassesEditor }) => {
|
|
184
|
-
const conditionKeys = Object.keys(conditions);
|
|
185
|
-
|
|
186
|
-
for (const key of conditionKeys) {
|
|
187
|
-
const value = checkAttr(key, attributes, manifest, true);
|
|
188
|
-
|
|
189
|
-
const isArrayCondition = Array.isArray(conditions[key]);
|
|
190
|
-
|
|
191
|
-
if (!value) {
|
|
192
|
-
return current;
|
|
193
|
-
} else if (isArrayCondition && !conditions[key].includes(value)) {
|
|
194
|
-
return current;
|
|
195
|
-
} else if (!isArrayCondition && value !== conditions[key]) {
|
|
196
|
-
return current;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
let currentClasses = twClassesEditor ?? twClasses;
|
|
201
|
-
|
|
202
|
-
if (!Array.isArray(currentClasses)) {
|
|
203
|
-
currentClasses = [currentClasses];
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return [...current, ...currentClasses];
|
|
207
|
-
}, []) ?? [];
|
|
208
|
-
|
|
209
|
-
return clsx(baseClasses, ...mainClasses, ...combinationClasses, ...custom);
|
|
210
|
-
};
|
|
211
|
-
|
|
212
4
|
/**
|
|
213
5
|
* Custom transformer for JSON files. Required for processing responsive classes.
|
|
214
6
|
*
|
|
@@ -348,7 +140,9 @@ const unifyClasses = (classes) => {
|
|
|
348
140
|
return classes.trim();
|
|
349
141
|
};
|
|
350
142
|
|
|
351
|
-
const processOption = (partName, optionValue, defs) => {
|
|
143
|
+
const processOption = (partName, optionValue, defs, options) => {
|
|
144
|
+
const { ignoreEditorClasses } = options;
|
|
145
|
+
|
|
352
146
|
let optionClasses = [];
|
|
353
147
|
|
|
354
148
|
const isResponsive = defs?.responsive === true;
|
|
@@ -365,7 +159,7 @@ const processOption = (partName, optionValue, defs) => {
|
|
|
365
159
|
}
|
|
366
160
|
|
|
367
161
|
// Non-responsive options.
|
|
368
|
-
if (!isResponsive) {
|
|
162
|
+
if (!isResponsive && !ignoreEditorClasses) {
|
|
369
163
|
const rawValueBase = defs?.twClassesEditorOnly?.[optionValue] ?? defs?.twClasses?.[optionValue] ?? defs?.[partName]?.twClassesEditorOnly?.[optionValue] ?? defs?.[partName]?.twClasses?.[optionValue] ?? '';
|
|
370
164
|
|
|
371
165
|
const rawValueEditor = defs?.twClassesEditor?.[optionValue] ?? defs?.[partName]?.twClassesEditor?.[optionValue] ?? '';
|
|
@@ -373,6 +167,12 @@ const processOption = (partName, optionValue, defs) => {
|
|
|
373
167
|
return clsx(unifyClasses(rawValueBase), unifyClasses(rawValueEditor));
|
|
374
168
|
}
|
|
375
169
|
|
|
170
|
+
if (!isResponsive && ignoreEditorClasses) {
|
|
171
|
+
const rawValueBase = defs?.twClasses?.[optionValue] ?? defs?.[partName]?.twClasses?.[optionValue] ?? '';
|
|
172
|
+
|
|
173
|
+
return unifyClasses(rawValueBase);
|
|
174
|
+
}
|
|
175
|
+
|
|
376
176
|
// Responsive options.
|
|
377
177
|
const breakpoints = Object.keys(optionValue).filter((key) => key !== '_desktopFirst');
|
|
378
178
|
|
|
@@ -383,9 +183,15 @@ const processOption = (partName, optionValue, defs) => {
|
|
|
383
183
|
continue;
|
|
384
184
|
}
|
|
385
185
|
|
|
386
|
-
|
|
186
|
+
let rawValueBase = '';
|
|
187
|
+
let rawValueEditor = '';
|
|
387
188
|
|
|
388
|
-
|
|
189
|
+
if (ignoreEditorClasses) {
|
|
190
|
+
rawValueBase = defs?.twClasses?.[breakpointValue] ?? defs?.[partName]?.twClasses?.[breakpointValue] ?? '';
|
|
191
|
+
} else {
|
|
192
|
+
rawValueBase = defs?.twClassesEditorOnly?.[breakpointValue] ?? defs?.twClasses?.[breakpointValue] ?? defs?.[partName]?.twClassesEditorOnly?.[breakpointValue] ?? defs?.[partName]?.twClasses?.[breakpointValue] ?? '';
|
|
193
|
+
rawValueEditor = defs?.twClassesEditor?.[breakpointValue] ?? defs?.[partName]?.twClassesEditor?.[breakpointValue] ?? '';
|
|
194
|
+
}
|
|
389
195
|
|
|
390
196
|
if (breakpoint === '_default') {
|
|
391
197
|
optionClasses = [...optionClasses, unifyClasses(rawValueBase), unifyClasses(rawValueEditor)];
|
|
@@ -409,7 +215,9 @@ const processOption = (partName, optionValue, defs) => {
|
|
|
409
215
|
return unifyClasses(optionClasses);
|
|
410
216
|
};
|
|
411
217
|
|
|
412
|
-
const processCombination = (partName, combo, attributes, manifest) => {
|
|
218
|
+
const processCombination = (partName, combo, attributes, manifest, options) => {
|
|
219
|
+
const { ignoreEditorClasses } = options;
|
|
220
|
+
|
|
413
221
|
let matches = true;
|
|
414
222
|
|
|
415
223
|
for (const [attributeName, allowedValue] of Object.entries(combo?.attributes ?? {})) {
|
|
@@ -437,9 +245,15 @@ const processCombination = (partName, combo, attributes, manifest) => {
|
|
|
437
245
|
return '';
|
|
438
246
|
}
|
|
439
247
|
|
|
440
|
-
|
|
248
|
+
let rawValueBase = '';
|
|
249
|
+
let rawValueEditor = '';
|
|
441
250
|
|
|
442
|
-
|
|
251
|
+
if (ignoreEditorClasses) {
|
|
252
|
+
rawValueBase = combo?.output?.[partName]?.twClasses ?? combo?.twClasses ?? '';
|
|
253
|
+
} else {
|
|
254
|
+
rawValueBase = combo?.output?.[partName]?.twClassesEditorOnly ?? combo?.output?.[partName]?.twClasses ?? combo?.twClassesEditorOnly ?? combo?.twClasses ?? '';
|
|
255
|
+
rawValueEditor = combo?.output?.[partName]?.twClassesEditor ?? combo?.twClassesEditor ?? '';
|
|
256
|
+
}
|
|
443
257
|
|
|
444
258
|
if (!Array.isArray(rawValueBase) && typeof rawValueBase !== 'string') {
|
|
445
259
|
throw new Error('Combination classes/editor-only classes were not defined correctly. Please check the combination definition in the manifest.');
|
|
@@ -462,6 +276,14 @@ const processCombination = (partName, combo, attributes, manifest) => {
|
|
|
462
276
|
*
|
|
463
277
|
* @returns {string} Output classes
|
|
464
278
|
*
|
|
279
|
+
* ### Note
|
|
280
|
+
* `_twcOptions` can be passed in the `attributes` object to customize the behavior of this function.
|
|
281
|
+
*
|
|
282
|
+
* Options include:
|
|
283
|
+
* - `ignoreEditorClasses` (boolean): If true, editor/editor-only classes will be ignored. Default: `false`.
|
|
284
|
+
* - `noPartDebug` (boolean): If true, debug classes for parts will not be added. Default: `false`.
|
|
285
|
+
* - `ssr` (boolean): If true, indicates server-side rendering. Default: determined by checking if `window` is `undefined`.
|
|
286
|
+
*
|
|
465
287
|
* @example
|
|
466
288
|
* const classes = tailwindClasses(attributes, manifest);
|
|
467
289
|
*
|
|
@@ -470,6 +292,11 @@ const processCombination = (partName, combo, attributes, manifest) => {
|
|
|
470
292
|
*
|
|
471
293
|
*/
|
|
472
294
|
export const tailwindClasses = (part, attributes, manifest, ...custom) => {
|
|
295
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
296
|
+
const twcOptions = attributes?._twcOptions ?? {};
|
|
297
|
+
|
|
298
|
+
const { ignoreEditorClasses = false, noPartDebug = false, ssr = typeof window === 'undefined' } = twcOptions;
|
|
299
|
+
|
|
473
300
|
// If nothing is set, return custom classes as a fallback.
|
|
474
301
|
if (!attributes || !manifest || !manifest?.tailwind || Object.keys(manifest?.tailwind ?? {}).length === 0) {
|
|
475
302
|
return clsx(...custom);
|
|
@@ -486,8 +313,15 @@ export const tailwindClasses = (part, attributes, manifest, ...custom) => {
|
|
|
486
313
|
}
|
|
487
314
|
|
|
488
315
|
// Base classes.
|
|
489
|
-
|
|
490
|
-
|
|
316
|
+
let baseBaseClasses = '';
|
|
317
|
+
let baseEditorClasses = '';
|
|
318
|
+
|
|
319
|
+
if (ignoreEditorClasses) {
|
|
320
|
+
baseBaseClasses = manifest?.tailwind?.parts?.[partName]?.twClasses ?? manifest?.tailwind?.base?.twClasses ?? [''];
|
|
321
|
+
} else {
|
|
322
|
+
baseBaseClasses = manifest?.tailwind?.parts?.[partName]?.twClassesEditorOnly ?? manifest?.tailwind?.parts?.[partName]?.twClasses ?? manifest?.tailwind?.base?.twClassesEditorOnly ?? manifest?.tailwind?.base?.twClasses ?? [''];
|
|
323
|
+
baseEditorClasses = manifest?.tailwind?.parts?.[partName]?.twClassesEditor ?? manifest?.tailwind?.base?.twClassesEditor ?? [''];
|
|
324
|
+
}
|
|
491
325
|
|
|
492
326
|
// Option classes.
|
|
493
327
|
const options = manifest?.tailwind?.options ?? {};
|
|
@@ -497,7 +331,7 @@ export const tailwindClasses = (part, attributes, manifest, ...custom) => {
|
|
|
497
331
|
for (const [attributeName, defs] of Object.entries(options)) {
|
|
498
332
|
const optionValue = checkAttr(attributeName, attributes, manifest, true);
|
|
499
333
|
|
|
500
|
-
optionClasses = [...optionClasses, processOption(partName, optionValue, defs)];
|
|
334
|
+
optionClasses = [...optionClasses, processOption(partName, optionValue, defs, twcOptions)];
|
|
501
335
|
}
|
|
502
336
|
|
|
503
337
|
// Combinations.
|
|
@@ -506,10 +340,16 @@ export const tailwindClasses = (part, attributes, manifest, ...custom) => {
|
|
|
506
340
|
let combinationClasses = [];
|
|
507
341
|
|
|
508
342
|
for (const combo of combinations) {
|
|
509
|
-
combinationClasses = [...combinationClasses, processCombination(partName, combo, attributes, manifest)];
|
|
343
|
+
combinationClasses = [...combinationClasses, processCombination(partName, combo, attributes, manifest, twcOptions)];
|
|
510
344
|
}
|
|
511
345
|
|
|
512
346
|
const partPrefix = manifest.title.replace(/[^a-zA-Z]+/g, '-').toLowerCase();
|
|
513
347
|
|
|
514
|
-
|
|
348
|
+
let extras = [];
|
|
349
|
+
|
|
350
|
+
if (!ssr && !noPartDebug) {
|
|
351
|
+
extras = [...extras, document?.body?.classList?.contains('es-wp-debug') && `_es__${partPrefix}/${part}`];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return clsx(...extras, unifyClasses(baseBaseClasses), unifyClasses(baseEditorClasses), ...optionClasses, ...combinationClasses, ...custom);
|
|
515
355
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
$classes = Helpers::tailwindClasses('base-part', $attributes, $manifest, 'text-lg w-[16px]', "js-{$name}");
|
|
3
|
+
$third = Helpers::tailwindClasses(
|
|
4
|
+
// The card's part, then its classes.
|
|
5
|
+
'title-part',
|
|
6
|
+
$attributes,
|
|
7
|
+
$manifest,
|
|
8
|
+
'w-[16px]',
|
|
9
|
+
);
|
|
10
|
+
$notAClass = 'text-unknown-after';
|
|
11
|
+
$foreign = FormsHelper::getTwPart($data, 'field', 'label-icon', 'item-inner');
|
|
12
|
+
$nested = Helpers::clsx([Helpers::selector($isRequired, $component, '', 'is-required'), 'w-[16px]']);
|
|
13
|
+
$compared = Helpers::clsx([$layout === 'layout-grid-half' ? 'w-[16px]' : 'h-full', 'layout-other' !== $layout ? 'flex' : '']);
|
|
14
|
+
$other = Helpers::clsx(['h-full w-full', 'text-xs' => $isSmall]);
|
|
15
|
+
// $old = Helpers::clsx(['text-unknown-line-comment']);
|
|
16
|
+
/* echo '<div class="text-unknown-block-comment"></div>'; */
|
|
17
|
+
?>
|
|
18
|
+
<p>C#</p><div class="card__wrap aspect-[5/3]">
|
|
19
|
+
<p>Don't skip this.</p>
|
|
20
|
+
<span class="<?php echo esc_attr($classes); ?>"></span>
|
|
21
|
+
<span class="text-%s"></span>
|
|
22
|
+
<!-- <div class="text-unknown-html-comment"></div> -->
|
|
23
|
+
</div>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"blockName": "card",
|
|
3
|
+
"tailwind": {
|
|
4
|
+
"base": { "twClasses": "flex text-sm js-card" },
|
|
5
|
+
"parts": {
|
|
6
|
+
"title": { "twClasses": ["aspect-[5/3]", "text-body-sm"] },
|
|
7
|
+
"icon": { "twClassesEditor": "h-[16px]\nh-full\t[.card\\_\\_icon_&]:w-[16px] [&_.card\\_icon]:w-[16px] \u0068-0" }
|
|
8
|
+
},
|
|
9
|
+
"options": {
|
|
10
|
+
"size": { "large": { "twClasses": "card__large w-[16px]" } }
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
$outside = ['text-unknown-outside'];
|
|
3
|
+
// tw-sweep-classes-start
|
|
4
|
+
$selectors['field-wrap']['parts']['label-inner'] = [
|
|
5
|
+
// The plugin's own comment must not open a string.
|
|
6
|
+
'text-xl',
|
|
7
|
+
'[.es-field\_\_content-wrap_&]:rounded-none',
|
|
8
|
+
'w-[16px]',
|
|
9
|
+
];
|
|
10
|
+
$map = ['select-choices' => 'text-body-sm'];
|
|
11
|
+
$single = ['aspect-[5/3]'];
|
|
12
|
+
$indexed = $selectors['text-unknown-index'];
|
|
13
|
+
$wrapped = Helpers::getTwClasses($attributes, $manifest, 'h-[16px]');
|
|
14
|
+
$escaped = ["w-[16px]\nh-full", '[&_.card\_icon]:w-[16px] content-[\'x\']'];
|
|
15
|
+
// tw-sweep-classes-end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
["^js-", "^card__"]
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { cpSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { join, resolve } from 'node:path';
|
|
7
|
+
|
|
8
|
+
const bin = resolve(import.meta.dirname, '../bin/tw-sweep.mjs');
|
|
9
|
+
const fixture = resolve(import.meta.dirname, 'fixtures/theme');
|
|
10
|
+
|
|
11
|
+
const run = (cwd, ...args) => {
|
|
12
|
+
const result = spawnSync(process.execPath, [bin, ...args], { cwd, encoding: 'utf8' });
|
|
13
|
+
|
|
14
|
+
return { code: result.status, out: result.stdout, err: result.stderr };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const reported = (out) =>
|
|
18
|
+
out
|
|
19
|
+
.trim()
|
|
20
|
+
.split('\n')
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.map((line) => line.replace(/:\d+:\d+/, '').replace(/\s+/g, ' '));
|
|
23
|
+
|
|
24
|
+
test('reports unknown and non-canonical classes from manifests, PHP calls, class attributes and regions, once each and outside comments', () => {
|
|
25
|
+
const { code, out } = run(fixture);
|
|
26
|
+
|
|
27
|
+
assert.equal(code, 1);
|
|
28
|
+
assert.deepEqual(reported(out).sort(), [
|
|
29
|
+
'src/Blocks/custom/card/card.php aspect-[5/3] → aspect-5/3',
|
|
30
|
+
'src/Blocks/custom/card/card.php unknown text-lg',
|
|
31
|
+
'src/Blocks/custom/card/card.php unknown text-xs',
|
|
32
|
+
'src/Blocks/custom/card/card.php w-[16px] → w-4',
|
|
33
|
+
'src/Blocks/custom/card/card.php w-[16px] → w-4',
|
|
34
|
+
'src/Blocks/custom/card/card.php w-[16px] → w-4',
|
|
35
|
+
'src/Blocks/custom/card/card.php w-[16px] → w-4',
|
|
36
|
+
'src/Blocks/custom/card/manifest.json [&_.card\\_icon]:w-[16px] → [&_.card\\_icon]:w-4',
|
|
37
|
+
'src/Blocks/custom/card/manifest.json aspect-[5/3] → aspect-5/3',
|
|
38
|
+
'src/Blocks/custom/card/manifest.json h-[16px] → h-4',
|
|
39
|
+
'src/Blocks/custom/card/manifest.json unknown text-sm',
|
|
40
|
+
'src/Blocks/custom/card/manifest.json w-[16px] → w-4',
|
|
41
|
+
'src/Plugins/Forms.php [&_.card\\_icon]:w-[16px] → [&_.card\\_icon]:w-4',
|
|
42
|
+
'src/Plugins/Forms.php aspect-[5/3] → aspect-5/3',
|
|
43
|
+
'src/Plugins/Forms.php h-[16px] → h-4',
|
|
44
|
+
'src/Plugins/Forms.php unknown text-xl',
|
|
45
|
+
'src/Plugins/Forms.php w-[16px] → w-4',
|
|
46
|
+
'src/Plugins/Forms.php w-[16px] → w-4',
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('--write rewrites non-canonical classes, keeps unknown ones and still fails on them', () => {
|
|
51
|
+
const dir = mkdtempSync(join(tmpdir(), 'tw-sweep-'));
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
cpSync(fixture, dir, { recursive: true });
|
|
55
|
+
|
|
56
|
+
const { code } = run(dir, '--write', '--entry', resolve(fixture, 'tailwind.css'));
|
|
57
|
+
const php = readFileSync(join(dir, 'src/Blocks/custom/card/card.php'), 'utf8');
|
|
58
|
+
const forms = readFileSync(join(dir, 'src/Plugins/Forms.php'), 'utf8');
|
|
59
|
+
|
|
60
|
+
assert.equal(code, 1);
|
|
61
|
+
assert.match(php, /'text-lg w-4'/);
|
|
62
|
+
assert.match(php, /class="card__wrap aspect-5\/3"/);
|
|
63
|
+
assert.match(forms, /\$single = \['aspect-5\/3'\];\n/);
|
|
64
|
+
assert.match(forms, /\$wrapped = Helpers::getTwClasses\(\$attributes, \$manifest, 'h-4'\);\n/);
|
|
65
|
+
assert.match(forms, /\$escaped = \["w-4\\nh-full", '\[&_\.card\\_icon\]:w-4 content-\[\\'x\\'\]'\];\n\/\/ tw-sweep-classes-end/);
|
|
66
|
+
assert.equal(JSON.parse(readFileSync(join(dir, 'src/Blocks/custom/card/manifest.json'), 'utf8')).tailwind.parts.icon.twClassesEditor, 'h-4\nh-full\t[.card\\_\\_icon_&]:w-[16px] [&_.card\\_icon]:w-4 h-0');
|
|
67
|
+
assert.deepEqual(reported(run(dir, '--no-unknown', '--entry', resolve(fixture, 'tailwind.css')).out), []);
|
|
68
|
+
} finally {
|
|
69
|
+
rmSync(dir, { recursive: true, force: true });
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('JS string escapes are decoded for checking and re-escaped by --write', () => {
|
|
74
|
+
const dir = mkdtempSync(join(tmpdir(), 'tw-sweep-'));
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
cpSync(fixture, dir, { recursive: true });
|
|
78
|
+
|
|
79
|
+
const { code, out } = run(dir, 'src/card.js', '--write', '--entry', resolve(fixture, 'tailwind.css'));
|
|
80
|
+
|
|
81
|
+
assert.equal(code, 0);
|
|
82
|
+
assert.deepEqual(reported(out), ['src/card.js w-[16px] → w-4', 'src/card.js [&_.a\\_b]:w-[16px] → [&_.a\\_b]:w-4']);
|
|
83
|
+
assert.ok(readFileSync(join(dir, 'src/card.js'), 'utf8').includes("\nexport const card = { className: 'w-4\\nh-full [&_.a\\\\_b]:w-4 content-[\\'x\\']' };\n"));
|
|
84
|
+
} finally {
|
|
85
|
+
rmSync(dir, { recursive: true, force: true });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('an invalid ignore pattern is a usage error naming the pattern', () => {
|
|
90
|
+
const dir = mkdtempSync(join(tmpdir(), 'tw-sweep-'));
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
cpSync(fixture, dir, { recursive: true });
|
|
94
|
+
writeFileSync(join(dir, 'tw-ignore.json'), '["^js-", "card__("]');
|
|
95
|
+
|
|
96
|
+
const { code, err } = run(dir);
|
|
97
|
+
|
|
98
|
+
assert.equal(code, 2);
|
|
99
|
+
assert.match(err, /card__\(/);
|
|
100
|
+
} finally {
|
|
101
|
+
rmSync(dir, { recursive: true, force: true });
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('a tailwind.css that does not import tailwindcss is not used as the entry', () => {
|
|
106
|
+
const dir = mkdtempSync(join(tmpdir(), 'tw-sweep-'));
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
cpSync(fixture, dir, { recursive: true });
|
|
110
|
+
writeFileSync(join(dir, 'tailwind.css'), '@theme {\n\t--text-body-sm: 0.875rem;\n}\n');
|
|
111
|
+
|
|
112
|
+
const { code, err } = run(dir);
|
|
113
|
+
|
|
114
|
+
assert.equal(code, 2);
|
|
115
|
+
assert.match(err, /imports 'tailwindcss'/);
|
|
116
|
+
} finally {
|
|
117
|
+
rmSync(dir, { recursive: true, force: true });
|
|
118
|
+
}
|
|
119
|
+
});
|