tailwindcss-patch 8.6.1 → 8.7.1
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/README.md +193 -11
- package/dist/chunk-K3NZYVML.js +4743 -0
- package/dist/chunk-OK2PBYIA.mjs +4739 -0
- package/dist/cli.js +20 -5
- package/dist/cli.mjs +20 -5
- package/dist/index.d.mts +282 -23
- package/dist/index.d.ts +282 -23
- package/dist/index.js +16 -2
- package/dist/index.mjs +15 -1
- package/package.json +18 -14
- package/schema/migration-report.schema.json +144 -0
- package/schema/restore-result.schema.json +68 -0
- package/schema/validate-result.schema.json +122 -0
- package/dist/chunk-DRPYVUDA.js +0 -2462
- package/dist/chunk-LOJHMBK5.mjs +0 -2458
package/dist/chunk-LOJHMBK5.mjs
DELETED
|
@@ -1,2458 +0,0 @@
|
|
|
1
|
-
// src/logger.ts
|
|
2
|
-
import { createConsola } from "consola";
|
|
3
|
-
var logger = createConsola();
|
|
4
|
-
var logger_default = logger;
|
|
5
|
-
|
|
6
|
-
// src/cache/store.ts
|
|
7
|
-
import process from "process";
|
|
8
|
-
import fs from "fs-extra";
|
|
9
|
-
function isErrnoException(error) {
|
|
10
|
-
return error instanceof Error && typeof error.code === "string";
|
|
11
|
-
}
|
|
12
|
-
function isAccessDenied(error) {
|
|
13
|
-
return isErrnoException(error) && Boolean(error.code && ["EPERM", "EBUSY", "EACCES"].includes(error.code));
|
|
14
|
-
}
|
|
15
|
-
var CacheStore = class {
|
|
16
|
-
constructor(options) {
|
|
17
|
-
this.options = options;
|
|
18
|
-
this.driver = options.driver ?? "file";
|
|
19
|
-
}
|
|
20
|
-
driver;
|
|
21
|
-
memoryCache = null;
|
|
22
|
-
async ensureDir() {
|
|
23
|
-
await fs.ensureDir(this.options.dir);
|
|
24
|
-
}
|
|
25
|
-
ensureDirSync() {
|
|
26
|
-
fs.ensureDirSync(this.options.dir);
|
|
27
|
-
}
|
|
28
|
-
createTempPath() {
|
|
29
|
-
const uniqueSuffix = `${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
30
|
-
return `${this.options.path}.${uniqueSuffix}.tmp`;
|
|
31
|
-
}
|
|
32
|
-
async replaceCacheFile(tempPath) {
|
|
33
|
-
try {
|
|
34
|
-
await fs.rename(tempPath, this.options.path);
|
|
35
|
-
return true;
|
|
36
|
-
} catch (error) {
|
|
37
|
-
if (isErrnoException(error) && (error.code === "EEXIST" || error.code === "EPERM")) {
|
|
38
|
-
try {
|
|
39
|
-
await fs.remove(this.options.path);
|
|
40
|
-
} catch (removeError) {
|
|
41
|
-
if (isAccessDenied(removeError)) {
|
|
42
|
-
logger_default.debug("Tailwind class cache locked or read-only, skipping update.", removeError);
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
if (!isErrnoException(removeError) || removeError.code !== "ENOENT") {
|
|
46
|
-
throw removeError;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
await fs.rename(tempPath, this.options.path);
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
replaceCacheFileSync(tempPath) {
|
|
56
|
-
try {
|
|
57
|
-
fs.renameSync(tempPath, this.options.path);
|
|
58
|
-
return true;
|
|
59
|
-
} catch (error) {
|
|
60
|
-
if (isErrnoException(error) && (error.code === "EEXIST" || error.code === "EPERM")) {
|
|
61
|
-
try {
|
|
62
|
-
fs.removeSync(this.options.path);
|
|
63
|
-
} catch (removeError) {
|
|
64
|
-
if (isAccessDenied(removeError)) {
|
|
65
|
-
logger_default.debug("Tailwind class cache locked or read-only, skipping update.", removeError);
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
if (!isErrnoException(removeError) || removeError.code !== "ENOENT") {
|
|
69
|
-
throw removeError;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
fs.renameSync(tempPath, this.options.path);
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async cleanupTempFile(tempPath) {
|
|
79
|
-
try {
|
|
80
|
-
await fs.remove(tempPath);
|
|
81
|
-
} catch {
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
cleanupTempFileSync(tempPath) {
|
|
85
|
-
try {
|
|
86
|
-
fs.removeSync(tempPath);
|
|
87
|
-
} catch {
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async write(data) {
|
|
91
|
-
if (!this.options.enabled) {
|
|
92
|
-
return void 0;
|
|
93
|
-
}
|
|
94
|
-
if (this.driver === "noop") {
|
|
95
|
-
return void 0;
|
|
96
|
-
}
|
|
97
|
-
if (this.driver === "memory") {
|
|
98
|
-
this.memoryCache = new Set(data);
|
|
99
|
-
return "memory";
|
|
100
|
-
}
|
|
101
|
-
const tempPath = this.createTempPath();
|
|
102
|
-
try {
|
|
103
|
-
await this.ensureDir();
|
|
104
|
-
await fs.writeJSON(tempPath, Array.from(data));
|
|
105
|
-
const replaced = await this.replaceCacheFile(tempPath);
|
|
106
|
-
if (replaced) {
|
|
107
|
-
return this.options.path;
|
|
108
|
-
}
|
|
109
|
-
await this.cleanupTempFile(tempPath);
|
|
110
|
-
return void 0;
|
|
111
|
-
} catch (error) {
|
|
112
|
-
await this.cleanupTempFile(tempPath);
|
|
113
|
-
logger_default.error("Unable to persist Tailwind class cache", error);
|
|
114
|
-
return void 0;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
writeSync(data) {
|
|
118
|
-
if (!this.options.enabled) {
|
|
119
|
-
return void 0;
|
|
120
|
-
}
|
|
121
|
-
if (this.driver === "noop") {
|
|
122
|
-
return void 0;
|
|
123
|
-
}
|
|
124
|
-
if (this.driver === "memory") {
|
|
125
|
-
this.memoryCache = new Set(data);
|
|
126
|
-
return "memory";
|
|
127
|
-
}
|
|
128
|
-
const tempPath = this.createTempPath();
|
|
129
|
-
try {
|
|
130
|
-
this.ensureDirSync();
|
|
131
|
-
fs.writeJSONSync(tempPath, Array.from(data));
|
|
132
|
-
const replaced = this.replaceCacheFileSync(tempPath);
|
|
133
|
-
if (replaced) {
|
|
134
|
-
return this.options.path;
|
|
135
|
-
}
|
|
136
|
-
this.cleanupTempFileSync(tempPath);
|
|
137
|
-
return void 0;
|
|
138
|
-
} catch (error) {
|
|
139
|
-
this.cleanupTempFileSync(tempPath);
|
|
140
|
-
logger_default.error("Unable to persist Tailwind class cache", error);
|
|
141
|
-
return void 0;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
async read() {
|
|
145
|
-
if (!this.options.enabled) {
|
|
146
|
-
return /* @__PURE__ */ new Set();
|
|
147
|
-
}
|
|
148
|
-
if (this.driver === "noop") {
|
|
149
|
-
return /* @__PURE__ */ new Set();
|
|
150
|
-
}
|
|
151
|
-
if (this.driver === "memory") {
|
|
152
|
-
return new Set(this.memoryCache ?? []);
|
|
153
|
-
}
|
|
154
|
-
try {
|
|
155
|
-
const exists = await fs.pathExists(this.options.path);
|
|
156
|
-
if (!exists) {
|
|
157
|
-
return /* @__PURE__ */ new Set();
|
|
158
|
-
}
|
|
159
|
-
const data = await fs.readJSON(this.options.path);
|
|
160
|
-
if (Array.isArray(data)) {
|
|
161
|
-
return new Set(data.filter((item) => typeof item === "string"));
|
|
162
|
-
}
|
|
163
|
-
} catch (error) {
|
|
164
|
-
if (isErrnoException(error) && error.code === "ENOENT") {
|
|
165
|
-
return /* @__PURE__ */ new Set();
|
|
166
|
-
}
|
|
167
|
-
logger_default.warn("Unable to read Tailwind class cache, removing invalid file.", error);
|
|
168
|
-
try {
|
|
169
|
-
await fs.remove(this.options.path);
|
|
170
|
-
} catch (cleanupError) {
|
|
171
|
-
logger_default.error("Failed to clean up invalid cache file", cleanupError);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return /* @__PURE__ */ new Set();
|
|
175
|
-
}
|
|
176
|
-
readSync() {
|
|
177
|
-
if (!this.options.enabled) {
|
|
178
|
-
return /* @__PURE__ */ new Set();
|
|
179
|
-
}
|
|
180
|
-
if (this.driver === "noop") {
|
|
181
|
-
return /* @__PURE__ */ new Set();
|
|
182
|
-
}
|
|
183
|
-
if (this.driver === "memory") {
|
|
184
|
-
return new Set(this.memoryCache ?? []);
|
|
185
|
-
}
|
|
186
|
-
try {
|
|
187
|
-
const exists = fs.pathExistsSync(this.options.path);
|
|
188
|
-
if (!exists) {
|
|
189
|
-
return /* @__PURE__ */ new Set();
|
|
190
|
-
}
|
|
191
|
-
const data = fs.readJSONSync(this.options.path);
|
|
192
|
-
if (Array.isArray(data)) {
|
|
193
|
-
return new Set(data.filter((item) => typeof item === "string"));
|
|
194
|
-
}
|
|
195
|
-
} catch (error) {
|
|
196
|
-
if (isErrnoException(error) && error.code === "ENOENT") {
|
|
197
|
-
return /* @__PURE__ */ new Set();
|
|
198
|
-
}
|
|
199
|
-
logger_default.warn("Unable to read Tailwind class cache, removing invalid file.", error);
|
|
200
|
-
try {
|
|
201
|
-
fs.removeSync(this.options.path);
|
|
202
|
-
} catch (cleanupError) {
|
|
203
|
-
logger_default.error("Failed to clean up invalid cache file", cleanupError);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return /* @__PURE__ */ new Set();
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
// src/extraction/candidate-extractor.ts
|
|
211
|
-
import { promises as fs2 } from "fs";
|
|
212
|
-
import process2 from "process";
|
|
213
|
-
import path from "pathe";
|
|
214
|
-
async function importNode() {
|
|
215
|
-
return import("@tailwindcss/node");
|
|
216
|
-
}
|
|
217
|
-
async function importOxide() {
|
|
218
|
-
return import("@tailwindcss/oxide");
|
|
219
|
-
}
|
|
220
|
-
async function loadDesignSystem(css, bases) {
|
|
221
|
-
const uniqueBases = Array.from(new Set(bases.filter(Boolean)));
|
|
222
|
-
if (uniqueBases.length === 0) {
|
|
223
|
-
throw new Error("No base directories provided for Tailwind CSS design system.");
|
|
224
|
-
}
|
|
225
|
-
const { __unstable__loadDesignSystem } = await importNode();
|
|
226
|
-
let lastError;
|
|
227
|
-
for (const base of uniqueBases) {
|
|
228
|
-
try {
|
|
229
|
-
return await __unstable__loadDesignSystem(css, { base });
|
|
230
|
-
} catch (error) {
|
|
231
|
-
lastError = error;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (lastError instanceof Error) {
|
|
235
|
-
throw lastError;
|
|
236
|
-
}
|
|
237
|
-
throw new Error("Failed to load Tailwind CSS design system.");
|
|
238
|
-
}
|
|
239
|
-
async function extractRawCandidatesWithPositions(content, extension = "html") {
|
|
240
|
-
const { Scanner } = await importOxide();
|
|
241
|
-
const scanner = new Scanner({});
|
|
242
|
-
const result = scanner.getCandidatesWithPositions({ content, extension });
|
|
243
|
-
return result.map(({ candidate, position }) => ({
|
|
244
|
-
rawCandidate: candidate,
|
|
245
|
-
start: position,
|
|
246
|
-
end: position + candidate.length
|
|
247
|
-
}));
|
|
248
|
-
}
|
|
249
|
-
async function extractRawCandidates(sources) {
|
|
250
|
-
const { Scanner } = await importOxide();
|
|
251
|
-
const scanner = new Scanner({
|
|
252
|
-
sources
|
|
253
|
-
});
|
|
254
|
-
return scanner.scan();
|
|
255
|
-
}
|
|
256
|
-
async function extractValidCandidates(options) {
|
|
257
|
-
const providedOptions = options ?? {};
|
|
258
|
-
const defaultCwd = providedOptions.cwd ?? process2.cwd();
|
|
259
|
-
const base = providedOptions.base ?? defaultCwd;
|
|
260
|
-
const baseFallbacks = providedOptions.baseFallbacks ?? [];
|
|
261
|
-
const css = providedOptions.css ?? '@import "tailwindcss";';
|
|
262
|
-
const sources = (providedOptions.sources ?? [
|
|
263
|
-
{
|
|
264
|
-
base: defaultCwd,
|
|
265
|
-
pattern: "**/*",
|
|
266
|
-
negated: false
|
|
267
|
-
}
|
|
268
|
-
]).map((source) => ({
|
|
269
|
-
base: source.base ?? defaultCwd,
|
|
270
|
-
pattern: source.pattern,
|
|
271
|
-
negated: source.negated
|
|
272
|
-
}));
|
|
273
|
-
const designSystem = await loadDesignSystem(css, [base, ...baseFallbacks]);
|
|
274
|
-
const candidates = await extractRawCandidates(sources);
|
|
275
|
-
const parsedCandidates = candidates.filter(
|
|
276
|
-
(rawCandidate) => designSystem.parseCandidate(rawCandidate).length > 0
|
|
277
|
-
);
|
|
278
|
-
if (parsedCandidates.length === 0) {
|
|
279
|
-
return parsedCandidates;
|
|
280
|
-
}
|
|
281
|
-
const cssByCandidate = designSystem.candidatesToCss(parsedCandidates);
|
|
282
|
-
const validCandidates = [];
|
|
283
|
-
for (let index = 0; index < parsedCandidates.length; index++) {
|
|
284
|
-
const css2 = cssByCandidate[index];
|
|
285
|
-
if (typeof css2 === "string" && css2.trim().length > 0) {
|
|
286
|
-
validCandidates.push(parsedCandidates[index]);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return validCandidates;
|
|
290
|
-
}
|
|
291
|
-
function normalizeSources(sources, cwd) {
|
|
292
|
-
const baseSources = sources?.length ? sources : [
|
|
293
|
-
{
|
|
294
|
-
base: cwd,
|
|
295
|
-
pattern: "**/*",
|
|
296
|
-
negated: false
|
|
297
|
-
}
|
|
298
|
-
];
|
|
299
|
-
return baseSources.map((source) => ({
|
|
300
|
-
base: source.base ?? cwd,
|
|
301
|
-
pattern: source.pattern,
|
|
302
|
-
negated: source.negated
|
|
303
|
-
}));
|
|
304
|
-
}
|
|
305
|
-
function buildLineOffsets(content) {
|
|
306
|
-
const offsets = [0];
|
|
307
|
-
for (let i = 0; i < content.length; i++) {
|
|
308
|
-
if (content[i] === "\n") {
|
|
309
|
-
offsets.push(i + 1);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
if (offsets[offsets.length - 1] !== content.length) {
|
|
313
|
-
offsets.push(content.length);
|
|
314
|
-
}
|
|
315
|
-
return offsets;
|
|
316
|
-
}
|
|
317
|
-
function resolveLineMeta(content, offsets, index) {
|
|
318
|
-
let low = 0;
|
|
319
|
-
let high = offsets.length - 1;
|
|
320
|
-
while (low <= high) {
|
|
321
|
-
const mid = Math.floor((low + high) / 2);
|
|
322
|
-
const start = offsets[mid];
|
|
323
|
-
const nextStart = offsets[mid + 1] ?? content.length;
|
|
324
|
-
if (index < start) {
|
|
325
|
-
high = mid - 1;
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
328
|
-
if (index >= nextStart) {
|
|
329
|
-
low = mid + 1;
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
const line = mid + 1;
|
|
333
|
-
const column = index - start + 1;
|
|
334
|
-
const lineEnd = content.indexOf("\n", start);
|
|
335
|
-
const lineText = content.slice(start, lineEnd === -1 ? content.length : lineEnd);
|
|
336
|
-
return { line, column, lineText };
|
|
337
|
-
}
|
|
338
|
-
const lastStart = offsets[offsets.length - 2] ?? 0;
|
|
339
|
-
return {
|
|
340
|
-
line: offsets.length - 1,
|
|
341
|
-
column: index - lastStart + 1,
|
|
342
|
-
lineText: content.slice(lastStart)
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
function toExtension(filename) {
|
|
346
|
-
const ext = path.extname(filename).replace(/^\./, "");
|
|
347
|
-
return ext || "txt";
|
|
348
|
-
}
|
|
349
|
-
function toRelativeFile(cwd, filename) {
|
|
350
|
-
const relative = path.relative(cwd, filename);
|
|
351
|
-
return relative === "" ? path.basename(filename) : relative;
|
|
352
|
-
}
|
|
353
|
-
async function extractProjectCandidatesWithPositions(options) {
|
|
354
|
-
const cwd = options?.cwd ? path.resolve(options.cwd) : process2.cwd();
|
|
355
|
-
const normalizedSources = normalizeSources(options?.sources, cwd);
|
|
356
|
-
const { Scanner } = await importOxide();
|
|
357
|
-
const scanner = new Scanner({
|
|
358
|
-
sources: normalizedSources
|
|
359
|
-
});
|
|
360
|
-
const files = scanner.files ?? [];
|
|
361
|
-
const entries = [];
|
|
362
|
-
const skipped = [];
|
|
363
|
-
for (const file of files) {
|
|
364
|
-
let content;
|
|
365
|
-
try {
|
|
366
|
-
content = await fs2.readFile(file, "utf8");
|
|
367
|
-
} catch (error) {
|
|
368
|
-
skipped.push({
|
|
369
|
-
file,
|
|
370
|
-
reason: error instanceof Error ? error.message : "Unknown error"
|
|
371
|
-
});
|
|
372
|
-
continue;
|
|
373
|
-
}
|
|
374
|
-
const extension = toExtension(file);
|
|
375
|
-
const matches = scanner.getCandidatesWithPositions({
|
|
376
|
-
file,
|
|
377
|
-
content,
|
|
378
|
-
extension
|
|
379
|
-
});
|
|
380
|
-
if (!matches.length) {
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
const offsets = buildLineOffsets(content);
|
|
384
|
-
const relativeFile = toRelativeFile(cwd, file);
|
|
385
|
-
for (const match of matches) {
|
|
386
|
-
const info = resolveLineMeta(content, offsets, match.position);
|
|
387
|
-
entries.push({
|
|
388
|
-
rawCandidate: match.candidate,
|
|
389
|
-
file,
|
|
390
|
-
relativeFile,
|
|
391
|
-
extension,
|
|
392
|
-
start: match.position,
|
|
393
|
-
end: match.position + match.candidate.length,
|
|
394
|
-
length: match.candidate.length,
|
|
395
|
-
line: info.line,
|
|
396
|
-
column: info.column,
|
|
397
|
-
lineText: info.lineText
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
return {
|
|
402
|
-
entries,
|
|
403
|
-
filesScanned: files.length,
|
|
404
|
-
skippedFiles: skipped,
|
|
405
|
-
sources: normalizedSources
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
function groupTokensByFile(report, options) {
|
|
409
|
-
const key = options?.key ?? "relative";
|
|
410
|
-
const stripAbsolute = options?.stripAbsolutePaths ?? key !== "absolute";
|
|
411
|
-
return report.entries.reduce((acc, entry) => {
|
|
412
|
-
const bucketKey = key === "absolute" ? entry.file : entry.relativeFile;
|
|
413
|
-
if (!acc[bucketKey]) {
|
|
414
|
-
acc[bucketKey] = [];
|
|
415
|
-
}
|
|
416
|
-
const value = stripAbsolute ? {
|
|
417
|
-
...entry,
|
|
418
|
-
file: entry.relativeFile
|
|
419
|
-
} : entry;
|
|
420
|
-
acc[bucketKey].push(value);
|
|
421
|
-
return acc;
|
|
422
|
-
}, {});
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
// src/options/normalize.ts
|
|
426
|
-
import process3 from "process";
|
|
427
|
-
import path2 from "pathe";
|
|
428
|
-
|
|
429
|
-
// src/constants.ts
|
|
430
|
-
var pkgName = "tailwindcss-patch";
|
|
431
|
-
|
|
432
|
-
// src/options/normalize.ts
|
|
433
|
-
function toPrettyValue(value) {
|
|
434
|
-
if (typeof value === "number") {
|
|
435
|
-
return value > 0 ? value : false;
|
|
436
|
-
}
|
|
437
|
-
if (value === true) {
|
|
438
|
-
return 2;
|
|
439
|
-
}
|
|
440
|
-
return false;
|
|
441
|
-
}
|
|
442
|
-
function normalizeCacheDriver(driver) {
|
|
443
|
-
if (driver === "memory" || driver === "noop") {
|
|
444
|
-
return driver;
|
|
445
|
-
}
|
|
446
|
-
return "file";
|
|
447
|
-
}
|
|
448
|
-
function normalizeCacheOptions(cache, projectRoot) {
|
|
449
|
-
let enabled = false;
|
|
450
|
-
let cwd = projectRoot;
|
|
451
|
-
let dir = path2.resolve(cwd, "node_modules/.cache", pkgName);
|
|
452
|
-
let file = "class-cache.json";
|
|
453
|
-
let strategy = "merge";
|
|
454
|
-
let driver = "file";
|
|
455
|
-
if (typeof cache === "boolean") {
|
|
456
|
-
enabled = cache;
|
|
457
|
-
} else if (typeof cache === "object" && cache) {
|
|
458
|
-
enabled = cache.enabled ?? true;
|
|
459
|
-
cwd = cache.cwd ?? cwd;
|
|
460
|
-
dir = cache.dir ? path2.resolve(cache.dir) : path2.resolve(cwd, "node_modules/.cache", pkgName);
|
|
461
|
-
file = cache.file ?? file;
|
|
462
|
-
strategy = cache.strategy ?? strategy;
|
|
463
|
-
driver = normalizeCacheDriver(cache.driver);
|
|
464
|
-
}
|
|
465
|
-
const filename = path2.resolve(dir, file);
|
|
466
|
-
return {
|
|
467
|
-
enabled,
|
|
468
|
-
cwd,
|
|
469
|
-
dir,
|
|
470
|
-
file,
|
|
471
|
-
path: filename,
|
|
472
|
-
strategy,
|
|
473
|
-
driver
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
function normalizeOutputOptions(output) {
|
|
477
|
-
const enabled = output?.enabled ?? true;
|
|
478
|
-
const file = output?.file ?? ".tw-patch/tw-class-list.json";
|
|
479
|
-
const format = output?.format ?? "json";
|
|
480
|
-
const pretty = toPrettyValue(output?.pretty ?? true);
|
|
481
|
-
const removeUniversalSelector = output?.removeUniversalSelector ?? true;
|
|
482
|
-
return {
|
|
483
|
-
enabled,
|
|
484
|
-
file,
|
|
485
|
-
format,
|
|
486
|
-
pretty,
|
|
487
|
-
removeUniversalSelector
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
function normalizeExposeContextOptions(features) {
|
|
491
|
-
if (features?.exposeContext === false) {
|
|
492
|
-
return {
|
|
493
|
-
enabled: false,
|
|
494
|
-
refProperty: "contextRef"
|
|
495
|
-
};
|
|
496
|
-
}
|
|
497
|
-
if (typeof features?.exposeContext === "object" && features.exposeContext) {
|
|
498
|
-
return {
|
|
499
|
-
enabled: true,
|
|
500
|
-
refProperty: features.exposeContext.refProperty ?? "contextRef"
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
return {
|
|
504
|
-
enabled: true,
|
|
505
|
-
refProperty: "contextRef"
|
|
506
|
-
};
|
|
507
|
-
}
|
|
508
|
-
function normalizeExtendLengthUnitsOptions(features) {
|
|
509
|
-
const extend = features?.extendLengthUnits;
|
|
510
|
-
if (extend === false || extend === void 0) {
|
|
511
|
-
return null;
|
|
512
|
-
}
|
|
513
|
-
if (extend.enabled === false) {
|
|
514
|
-
return null;
|
|
515
|
-
}
|
|
516
|
-
const base = {
|
|
517
|
-
units: ["rpx"],
|
|
518
|
-
overwrite: true
|
|
519
|
-
};
|
|
520
|
-
return {
|
|
521
|
-
...base,
|
|
522
|
-
...extend,
|
|
523
|
-
enabled: extend.enabled ?? true,
|
|
524
|
-
units: extend.units ?? base.units,
|
|
525
|
-
overwrite: extend.overwrite ?? base.overwrite
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
|
-
function normalizeTailwindV4Options(v4, fallbackBase) {
|
|
529
|
-
const configuredBase = v4?.base ? path2.resolve(v4.base) : void 0;
|
|
530
|
-
const base = configuredBase ?? fallbackBase;
|
|
531
|
-
const cssEntries = Array.isArray(v4?.cssEntries) ? v4.cssEntries.filter((entry) => Boolean(entry)).map((entry) => path2.resolve(entry)) : [];
|
|
532
|
-
const userSources = v4?.sources;
|
|
533
|
-
const hasUserDefinedSources = Boolean(userSources?.length);
|
|
534
|
-
const sources = hasUserDefinedSources ? userSources : [
|
|
535
|
-
{
|
|
536
|
-
base: fallbackBase,
|
|
537
|
-
pattern: "**/*",
|
|
538
|
-
negated: false
|
|
539
|
-
}
|
|
540
|
-
];
|
|
541
|
-
return {
|
|
542
|
-
base,
|
|
543
|
-
configuredBase,
|
|
544
|
-
css: v4?.css,
|
|
545
|
-
cssEntries,
|
|
546
|
-
sources,
|
|
547
|
-
hasUserDefinedSources
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
|
-
function normalizeTailwindOptions(tailwind, projectRoot) {
|
|
551
|
-
const packageName = tailwind?.packageName ?? "tailwindcss";
|
|
552
|
-
const versionHint = tailwind?.version;
|
|
553
|
-
const resolve = tailwind?.resolve;
|
|
554
|
-
const cwd = tailwind?.cwd ?? projectRoot;
|
|
555
|
-
const config = tailwind?.config;
|
|
556
|
-
const postcssPlugin = tailwind?.postcssPlugin;
|
|
557
|
-
const v4 = normalizeTailwindV4Options(tailwind?.v4, cwd);
|
|
558
|
-
return {
|
|
559
|
-
packageName,
|
|
560
|
-
versionHint,
|
|
561
|
-
resolve,
|
|
562
|
-
cwd,
|
|
563
|
-
config,
|
|
564
|
-
postcssPlugin,
|
|
565
|
-
v2: tailwind?.v2,
|
|
566
|
-
v3: tailwind?.v3,
|
|
567
|
-
v4
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
function normalizeOptions(options = {}) {
|
|
571
|
-
const projectRoot = options.cwd ? path2.resolve(options.cwd) : process3.cwd();
|
|
572
|
-
const overwrite = options.overwrite ?? true;
|
|
573
|
-
const output = normalizeOutputOptions(options.output);
|
|
574
|
-
const cache = normalizeCacheOptions(options.cache, projectRoot);
|
|
575
|
-
const tailwind = normalizeTailwindOptions(options.tailwind, projectRoot);
|
|
576
|
-
const exposeContext = normalizeExposeContextOptions(options.features);
|
|
577
|
-
const extendLengthUnits = normalizeExtendLengthUnitsOptions(options.features);
|
|
578
|
-
const filter = (className) => {
|
|
579
|
-
if (output.removeUniversalSelector && className === "*") {
|
|
580
|
-
return false;
|
|
581
|
-
}
|
|
582
|
-
if (typeof options.filter === "function") {
|
|
583
|
-
return options.filter(className) !== false;
|
|
584
|
-
}
|
|
585
|
-
return true;
|
|
586
|
-
};
|
|
587
|
-
return {
|
|
588
|
-
projectRoot,
|
|
589
|
-
overwrite,
|
|
590
|
-
tailwind,
|
|
591
|
-
features: {
|
|
592
|
-
exposeContext,
|
|
593
|
-
extendLengthUnits
|
|
594
|
-
},
|
|
595
|
-
output,
|
|
596
|
-
cache,
|
|
597
|
-
filter
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
// src/patching/status.ts
|
|
602
|
-
import * as t4 from "@babel/types";
|
|
603
|
-
import fs4 from "fs-extra";
|
|
604
|
-
import path4 from "pathe";
|
|
605
|
-
|
|
606
|
-
// src/babel/index.ts
|
|
607
|
-
import _babelGenerate from "@babel/generator";
|
|
608
|
-
import _babelTraverse from "@babel/traverse";
|
|
609
|
-
import { parse, parseExpression } from "@babel/parser";
|
|
610
|
-
function _interopDefaultCompat(e) {
|
|
611
|
-
return e && typeof e === "object" && "default" in e ? e.default : e;
|
|
612
|
-
}
|
|
613
|
-
var generate = _interopDefaultCompat(_babelGenerate);
|
|
614
|
-
var traverse = _interopDefaultCompat(_babelTraverse);
|
|
615
|
-
|
|
616
|
-
// src/patching/operations/export-context/postcss-v2.ts
|
|
617
|
-
import * as t from "@babel/types";
|
|
618
|
-
var IDENTIFIER_RE = /^[A-Z_$][\w$]*$/i;
|
|
619
|
-
function toIdentifierName(property) {
|
|
620
|
-
if (!property) {
|
|
621
|
-
return "contextRef";
|
|
622
|
-
}
|
|
623
|
-
const sanitized = property.replace(/[^\w$]/gu, "_");
|
|
624
|
-
if (/^\d/.test(sanitized)) {
|
|
625
|
-
return `_${sanitized}`;
|
|
626
|
-
}
|
|
627
|
-
return sanitized || "contextRef";
|
|
628
|
-
}
|
|
629
|
-
function createExportsMember(property) {
|
|
630
|
-
if (IDENTIFIER_RE.test(property)) {
|
|
631
|
-
return t.memberExpression(t.identifier("exports"), t.identifier(property));
|
|
632
|
-
}
|
|
633
|
-
return t.memberExpression(t.identifier("exports"), t.stringLiteral(property), true);
|
|
634
|
-
}
|
|
635
|
-
function transformProcessTailwindFeaturesReturnContextV2(content) {
|
|
636
|
-
const ast = parse(content, {
|
|
637
|
-
sourceType: "unambiguous"
|
|
638
|
-
});
|
|
639
|
-
let hasPatched = false;
|
|
640
|
-
traverse(ast, {
|
|
641
|
-
FunctionDeclaration(path11) {
|
|
642
|
-
const node = path11.node;
|
|
643
|
-
if (node.id?.name !== "processTailwindFeatures" || node.body.body.length !== 1 || !t.isReturnStatement(node.body.body[0])) {
|
|
644
|
-
return;
|
|
645
|
-
}
|
|
646
|
-
const returnStatement3 = node.body.body[0];
|
|
647
|
-
if (!t.isFunctionExpression(returnStatement3.argument)) {
|
|
648
|
-
return;
|
|
649
|
-
}
|
|
650
|
-
const body = returnStatement3.argument.body.body;
|
|
651
|
-
const lastStatement = body[body.length - 1];
|
|
652
|
-
const alreadyReturnsContext = Boolean(
|
|
653
|
-
t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === "context"
|
|
654
|
-
);
|
|
655
|
-
hasPatched = alreadyReturnsContext;
|
|
656
|
-
if (!alreadyReturnsContext) {
|
|
657
|
-
body.push(t.returnStatement(t.identifier("context")));
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
});
|
|
661
|
-
return {
|
|
662
|
-
code: hasPatched ? content : generate(ast).code,
|
|
663
|
-
hasPatched
|
|
664
|
-
};
|
|
665
|
-
}
|
|
666
|
-
function transformPostcssPluginV2(content, options) {
|
|
667
|
-
const refIdentifier = t.identifier(toIdentifierName(options.refProperty));
|
|
668
|
-
const exportMember = createExportsMember(options.refProperty);
|
|
669
|
-
const valueMember = t.memberExpression(refIdentifier, t.identifier("value"));
|
|
670
|
-
const ast = parse(content);
|
|
671
|
-
let hasPatched = false;
|
|
672
|
-
traverse(ast, {
|
|
673
|
-
Program(path11) {
|
|
674
|
-
const program = path11.node;
|
|
675
|
-
const index = program.body.findIndex((statement) => {
|
|
676
|
-
return t.isFunctionDeclaration(statement) && statement.id?.name === "_default";
|
|
677
|
-
});
|
|
678
|
-
if (index === -1) {
|
|
679
|
-
return;
|
|
680
|
-
}
|
|
681
|
-
const previous = program.body[index - 1];
|
|
682
|
-
const beforePrevious = program.body[index - 2];
|
|
683
|
-
const alreadyHasVariable = Boolean(
|
|
684
|
-
previous && t.isVariableDeclaration(previous) && previous.declarations.length === 1 && t.isIdentifier(previous.declarations[0].id) && previous.declarations[0].id.name === refIdentifier.name
|
|
685
|
-
);
|
|
686
|
-
const alreadyAssignsExports = Boolean(
|
|
687
|
-
beforePrevious && t.isExpressionStatement(beforePrevious) && t.isAssignmentExpression(beforePrevious.expression) && t.isMemberExpression(beforePrevious.expression.left) && t.isIdentifier(beforePrevious.expression.right) && beforePrevious.expression.right.name === refIdentifier.name && generate(beforePrevious.expression.left).code === generate(exportMember).code
|
|
688
|
-
);
|
|
689
|
-
hasPatched = alreadyHasVariable && alreadyAssignsExports;
|
|
690
|
-
if (!alreadyHasVariable) {
|
|
691
|
-
program.body.splice(
|
|
692
|
-
index,
|
|
693
|
-
0,
|
|
694
|
-
t.variableDeclaration("var", [
|
|
695
|
-
t.variableDeclarator(
|
|
696
|
-
refIdentifier,
|
|
697
|
-
t.objectExpression([
|
|
698
|
-
t.objectProperty(t.identifier("value"), t.arrayExpression())
|
|
699
|
-
])
|
|
700
|
-
)
|
|
701
|
-
]),
|
|
702
|
-
t.expressionStatement(
|
|
703
|
-
t.assignmentExpression("=", exportMember, refIdentifier)
|
|
704
|
-
)
|
|
705
|
-
);
|
|
706
|
-
}
|
|
707
|
-
},
|
|
708
|
-
FunctionDeclaration(path11) {
|
|
709
|
-
if (hasPatched) {
|
|
710
|
-
return;
|
|
711
|
-
}
|
|
712
|
-
const fn = path11.node;
|
|
713
|
-
if (fn.id?.name !== "_default") {
|
|
714
|
-
return;
|
|
715
|
-
}
|
|
716
|
-
if (fn.body.body.length !== 1 || !t.isReturnStatement(fn.body.body[0])) {
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
const returnStatement3 = fn.body.body[0];
|
|
720
|
-
if (!t.isCallExpression(returnStatement3.argument) || !t.isMemberExpression(returnStatement3.argument.callee) || !t.isArrayExpression(returnStatement3.argument.callee.object)) {
|
|
721
|
-
return;
|
|
722
|
-
}
|
|
723
|
-
const fnExpression = returnStatement3.argument.callee.object.elements[1];
|
|
724
|
-
if (!fnExpression || !t.isFunctionExpression(fnExpression)) {
|
|
725
|
-
return;
|
|
726
|
-
}
|
|
727
|
-
const block = fnExpression.body;
|
|
728
|
-
const statements = block.body;
|
|
729
|
-
if (t.isExpressionStatement(statements[0]) && t.isAssignmentExpression(statements[0].expression) && t.isNumericLiteral(statements[0].expression.right)) {
|
|
730
|
-
hasPatched = true;
|
|
731
|
-
return;
|
|
732
|
-
}
|
|
733
|
-
const lastStatement = statements[statements.length - 1];
|
|
734
|
-
if (lastStatement && t.isExpressionStatement(lastStatement)) {
|
|
735
|
-
statements[statements.length - 1] = t.expressionStatement(
|
|
736
|
-
t.callExpression(
|
|
737
|
-
t.memberExpression(valueMember, t.identifier("push")),
|
|
738
|
-
[lastStatement.expression]
|
|
739
|
-
)
|
|
740
|
-
);
|
|
741
|
-
}
|
|
742
|
-
const index = statements.findIndex((statement) => t.isIfStatement(statement));
|
|
743
|
-
if (index > -1) {
|
|
744
|
-
const ifStatement = statements[index];
|
|
745
|
-
if (t.isBlockStatement(ifStatement.consequent) && ifStatement.consequent.body[1] && t.isForOfStatement(ifStatement.consequent.body[1])) {
|
|
746
|
-
const forOf = ifStatement.consequent.body[1];
|
|
747
|
-
if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1) {
|
|
748
|
-
const nestedIf = forOf.body.body[0];
|
|
749
|
-
if (nestedIf && t.isIfStatement(nestedIf) && t.isBlockStatement(nestedIf.consequent) && nestedIf.consequent.body.length === 1 && t.isExpressionStatement(nestedIf.consequent.body[0])) {
|
|
750
|
-
nestedIf.consequent.body[0] = t.expressionStatement(
|
|
751
|
-
t.callExpression(
|
|
752
|
-
t.memberExpression(valueMember, t.identifier("push")),
|
|
753
|
-
[nestedIf.consequent.body[0].expression]
|
|
754
|
-
)
|
|
755
|
-
);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
statements.unshift(
|
|
761
|
-
t.expressionStatement(
|
|
762
|
-
t.assignmentExpression(
|
|
763
|
-
"=",
|
|
764
|
-
t.memberExpression(valueMember, t.identifier("length")),
|
|
765
|
-
t.numericLiteral(0)
|
|
766
|
-
)
|
|
767
|
-
)
|
|
768
|
-
);
|
|
769
|
-
}
|
|
770
|
-
});
|
|
771
|
-
return {
|
|
772
|
-
code: hasPatched ? content : generate(ast).code,
|
|
773
|
-
hasPatched
|
|
774
|
-
};
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
// src/patching/operations/export-context/postcss-v3.ts
|
|
778
|
-
import * as t2 from "@babel/types";
|
|
779
|
-
var IDENTIFIER_RE2 = /^[A-Z_$][\w$]*$/i;
|
|
780
|
-
function toIdentifierName2(property) {
|
|
781
|
-
if (!property) {
|
|
782
|
-
return "contextRef";
|
|
783
|
-
}
|
|
784
|
-
const sanitized = property.replace(/[^\w$]/gu, "_");
|
|
785
|
-
if (/^\d/.test(sanitized)) {
|
|
786
|
-
return `_${sanitized}`;
|
|
787
|
-
}
|
|
788
|
-
return sanitized || "contextRef";
|
|
789
|
-
}
|
|
790
|
-
function createModuleExportsMember(property) {
|
|
791
|
-
const object = t2.memberExpression(t2.identifier("module"), t2.identifier("exports"));
|
|
792
|
-
if (IDENTIFIER_RE2.test(property)) {
|
|
793
|
-
return t2.memberExpression(object, t2.identifier(property));
|
|
794
|
-
}
|
|
795
|
-
return t2.memberExpression(object, t2.stringLiteral(property), true);
|
|
796
|
-
}
|
|
797
|
-
function transformProcessTailwindFeaturesReturnContext(content) {
|
|
798
|
-
const ast = parse(content);
|
|
799
|
-
let hasPatched = false;
|
|
800
|
-
traverse(ast, {
|
|
801
|
-
FunctionDeclaration(path11) {
|
|
802
|
-
const node = path11.node;
|
|
803
|
-
if (node.id?.name !== "processTailwindFeatures" || node.body.body.length !== 1) {
|
|
804
|
-
return;
|
|
805
|
-
}
|
|
806
|
-
const [returnStatement3] = node.body.body;
|
|
807
|
-
if (!t2.isReturnStatement(returnStatement3) || !t2.isFunctionExpression(returnStatement3.argument)) {
|
|
808
|
-
return;
|
|
809
|
-
}
|
|
810
|
-
const expression = returnStatement3.argument;
|
|
811
|
-
const body = expression.body.body;
|
|
812
|
-
const lastStatement = body[body.length - 1];
|
|
813
|
-
const alreadyReturnsContext = Boolean(
|
|
814
|
-
t2.isReturnStatement(lastStatement) && t2.isIdentifier(lastStatement.argument) && lastStatement.argument.name === "context"
|
|
815
|
-
);
|
|
816
|
-
hasPatched = alreadyReturnsContext;
|
|
817
|
-
if (!alreadyReturnsContext) {
|
|
818
|
-
body.push(t2.returnStatement(t2.identifier("context")));
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
});
|
|
822
|
-
return {
|
|
823
|
-
code: hasPatched ? content : generate(ast).code,
|
|
824
|
-
hasPatched
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
function transformPostcssPlugin(content, { refProperty }) {
|
|
828
|
-
const ast = parse(content);
|
|
829
|
-
const refIdentifier = t2.identifier(toIdentifierName2(refProperty));
|
|
830
|
-
const moduleExportsMember = createModuleExportsMember(refProperty);
|
|
831
|
-
const valueMember = t2.memberExpression(refIdentifier, t2.identifier("value"));
|
|
832
|
-
let hasPatched = false;
|
|
833
|
-
traverse(ast, {
|
|
834
|
-
Program(path11) {
|
|
835
|
-
const program = path11.node;
|
|
836
|
-
const index = program.body.findIndex((statement) => {
|
|
837
|
-
return t2.isExpressionStatement(statement) && t2.isAssignmentExpression(statement.expression) && t2.isMemberExpression(statement.expression.left) && t2.isFunctionExpression(statement.expression.right) && statement.expression.right.id?.name === "tailwindcss";
|
|
838
|
-
});
|
|
839
|
-
if (index === -1) {
|
|
840
|
-
return;
|
|
841
|
-
}
|
|
842
|
-
const previousStatement = program.body[index - 1];
|
|
843
|
-
const lastStatement = program.body[program.body.length - 1];
|
|
844
|
-
const alreadyHasVariable = Boolean(
|
|
845
|
-
previousStatement && t2.isVariableDeclaration(previousStatement) && previousStatement.declarations.length === 1 && t2.isIdentifier(previousStatement.declarations[0].id) && previousStatement.declarations[0].id.name === refIdentifier.name
|
|
846
|
-
);
|
|
847
|
-
const alreadyAssignsModuleExports = Boolean(
|
|
848
|
-
t2.isExpressionStatement(lastStatement) && t2.isAssignmentExpression(lastStatement.expression) && t2.isMemberExpression(lastStatement.expression.left) && t2.isIdentifier(lastStatement.expression.right) && lastStatement.expression.right.name === refIdentifier.name && generate(lastStatement.expression.left).code === generate(moduleExportsMember).code
|
|
849
|
-
);
|
|
850
|
-
hasPatched = alreadyHasVariable && alreadyAssignsModuleExports;
|
|
851
|
-
if (!alreadyHasVariable) {
|
|
852
|
-
program.body.splice(
|
|
853
|
-
index,
|
|
854
|
-
0,
|
|
855
|
-
t2.variableDeclaration("const", [
|
|
856
|
-
t2.variableDeclarator(
|
|
857
|
-
refIdentifier,
|
|
858
|
-
t2.objectExpression([
|
|
859
|
-
t2.objectProperty(t2.identifier("value"), t2.arrayExpression())
|
|
860
|
-
])
|
|
861
|
-
)
|
|
862
|
-
])
|
|
863
|
-
);
|
|
864
|
-
}
|
|
865
|
-
if (!alreadyAssignsModuleExports) {
|
|
866
|
-
program.body.push(
|
|
867
|
-
t2.expressionStatement(
|
|
868
|
-
t2.assignmentExpression("=", moduleExportsMember, refIdentifier)
|
|
869
|
-
)
|
|
870
|
-
);
|
|
871
|
-
}
|
|
872
|
-
},
|
|
873
|
-
FunctionExpression(path11) {
|
|
874
|
-
if (hasPatched) {
|
|
875
|
-
return;
|
|
876
|
-
}
|
|
877
|
-
const fn = path11.node;
|
|
878
|
-
if (fn.id?.name !== "tailwindcss" || fn.body.body.length !== 1) {
|
|
879
|
-
return;
|
|
880
|
-
}
|
|
881
|
-
const [returnStatement3] = fn.body.body;
|
|
882
|
-
if (!returnStatement3 || !t2.isReturnStatement(returnStatement3) || !t2.isObjectExpression(returnStatement3.argument)) {
|
|
883
|
-
return;
|
|
884
|
-
}
|
|
885
|
-
const properties = returnStatement3.argument.properties;
|
|
886
|
-
if (properties.length !== 2) {
|
|
887
|
-
return;
|
|
888
|
-
}
|
|
889
|
-
const pluginsProperty = properties.find(
|
|
890
|
-
(prop) => t2.isObjectProperty(prop) && t2.isIdentifier(prop.key) && prop.key.name === "plugins"
|
|
891
|
-
);
|
|
892
|
-
if (!pluginsProperty || !t2.isObjectProperty(pluginsProperty) || !t2.isCallExpression(pluginsProperty.value) || !t2.isMemberExpression(pluginsProperty.value.callee) || !t2.isArrayExpression(pluginsProperty.value.callee.object)) {
|
|
893
|
-
return;
|
|
894
|
-
}
|
|
895
|
-
const pluginsArray = pluginsProperty.value.callee.object.elements;
|
|
896
|
-
const targetPlugin = pluginsArray[1];
|
|
897
|
-
if (!targetPlugin || !t2.isFunctionExpression(targetPlugin)) {
|
|
898
|
-
return;
|
|
899
|
-
}
|
|
900
|
-
const block = targetPlugin.body;
|
|
901
|
-
const statements = block.body;
|
|
902
|
-
const last = statements[statements.length - 1];
|
|
903
|
-
if (last && t2.isExpressionStatement(last)) {
|
|
904
|
-
statements[statements.length - 1] = t2.expressionStatement(
|
|
905
|
-
t2.callExpression(
|
|
906
|
-
t2.memberExpression(valueMember, t2.identifier("push")),
|
|
907
|
-
[last.expression]
|
|
908
|
-
)
|
|
909
|
-
);
|
|
910
|
-
}
|
|
911
|
-
const index = statements.findIndex((s) => t2.isIfStatement(s));
|
|
912
|
-
if (index > -1) {
|
|
913
|
-
const ifStatement = statements[index];
|
|
914
|
-
if (t2.isBlockStatement(ifStatement.consequent)) {
|
|
915
|
-
const [, second] = ifStatement.consequent.body;
|
|
916
|
-
if (second && t2.isForOfStatement(second) && t2.isBlockStatement(second.body)) {
|
|
917
|
-
const bodyStatement = second.body.body[0];
|
|
918
|
-
if (bodyStatement && t2.isIfStatement(bodyStatement) && t2.isBlockStatement(bodyStatement.consequent) && bodyStatement.consequent.body.length === 1 && t2.isExpressionStatement(bodyStatement.consequent.body[0])) {
|
|
919
|
-
bodyStatement.consequent.body[0] = t2.expressionStatement(
|
|
920
|
-
t2.callExpression(
|
|
921
|
-
t2.memberExpression(valueMember, t2.identifier("push")),
|
|
922
|
-
[bodyStatement.consequent.body[0].expression]
|
|
923
|
-
)
|
|
924
|
-
);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
statements.unshift(
|
|
930
|
-
t2.expressionStatement(
|
|
931
|
-
t2.assignmentExpression(
|
|
932
|
-
"=",
|
|
933
|
-
t2.memberExpression(valueMember, t2.identifier("length")),
|
|
934
|
-
t2.numericLiteral(0)
|
|
935
|
-
)
|
|
936
|
-
)
|
|
937
|
-
);
|
|
938
|
-
}
|
|
939
|
-
});
|
|
940
|
-
return {
|
|
941
|
-
code: hasPatched ? content : generate(ast).code,
|
|
942
|
-
hasPatched
|
|
943
|
-
};
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
// src/patching/operations/extend-length-units.ts
|
|
947
|
-
import * as t3 from "@babel/types";
|
|
948
|
-
import fs3 from "fs-extra";
|
|
949
|
-
import path3 from "pathe";
|
|
950
|
-
|
|
951
|
-
// src/utils.ts
|
|
952
|
-
function isObject(val) {
|
|
953
|
-
return val !== null && typeof val === "object" && Array.isArray(val) === false;
|
|
954
|
-
}
|
|
955
|
-
function spliceChangesIntoString(str, changes) {
|
|
956
|
-
if (!changes[0]) {
|
|
957
|
-
return str;
|
|
958
|
-
}
|
|
959
|
-
changes.sort((a, b) => {
|
|
960
|
-
return a.end - b.end || a.start - b.start;
|
|
961
|
-
});
|
|
962
|
-
let result = "";
|
|
963
|
-
let previous = changes[0];
|
|
964
|
-
result += str.slice(0, previous.start);
|
|
965
|
-
result += previous.replacement;
|
|
966
|
-
for (let i = 1; i < changes.length; ++i) {
|
|
967
|
-
const change = changes[i];
|
|
968
|
-
result += str.slice(previous.end, change.start);
|
|
969
|
-
result += change.replacement;
|
|
970
|
-
previous = change;
|
|
971
|
-
}
|
|
972
|
-
result += str.slice(previous.end);
|
|
973
|
-
return result;
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
// src/patching/operations/extend-length-units.ts
|
|
977
|
-
function updateLengthUnitsArray(content, options) {
|
|
978
|
-
const { variableName = "lengthUnits", units } = options;
|
|
979
|
-
const ast = parse(content);
|
|
980
|
-
let arrayRef;
|
|
981
|
-
let changed = false;
|
|
982
|
-
traverse(ast, {
|
|
983
|
-
Identifier(path11) {
|
|
984
|
-
if (path11.node.name === variableName && t3.isVariableDeclarator(path11.parent) && t3.isArrayExpression(path11.parent.init)) {
|
|
985
|
-
arrayRef = path11.parent.init;
|
|
986
|
-
const existing = new Set(
|
|
987
|
-
path11.parent.init.elements.map((element) => t3.isStringLiteral(element) ? element.value : void 0).filter(Boolean)
|
|
988
|
-
);
|
|
989
|
-
for (const unit of units) {
|
|
990
|
-
if (!existing.has(unit)) {
|
|
991
|
-
path11.parent.init.elements = path11.parent.init.elements.map((element) => {
|
|
992
|
-
if (t3.isStringLiteral(element)) {
|
|
993
|
-
return t3.stringLiteral(element.value);
|
|
994
|
-
}
|
|
995
|
-
return element;
|
|
996
|
-
});
|
|
997
|
-
path11.parent.init.elements.push(t3.stringLiteral(unit));
|
|
998
|
-
changed = true;
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
});
|
|
1004
|
-
return {
|
|
1005
|
-
arrayRef,
|
|
1006
|
-
changed
|
|
1007
|
-
};
|
|
1008
|
-
}
|
|
1009
|
-
function applyExtendLengthUnitsPatchV3(rootDir, options) {
|
|
1010
|
-
if (!options.enabled) {
|
|
1011
|
-
return { changed: false, code: void 0 };
|
|
1012
|
-
}
|
|
1013
|
-
const opts = {
|
|
1014
|
-
...options,
|
|
1015
|
-
lengthUnitsFilePath: options.lengthUnitsFilePath ?? "lib/util/dataTypes.js",
|
|
1016
|
-
variableName: options.variableName ?? "lengthUnits"
|
|
1017
|
-
};
|
|
1018
|
-
const dataTypesFilePath = path3.resolve(rootDir, opts.lengthUnitsFilePath);
|
|
1019
|
-
const exists = fs3.existsSync(dataTypesFilePath);
|
|
1020
|
-
if (!exists) {
|
|
1021
|
-
return { changed: false, code: void 0 };
|
|
1022
|
-
}
|
|
1023
|
-
const content = fs3.readFileSync(dataTypesFilePath, "utf8");
|
|
1024
|
-
const { arrayRef, changed } = updateLengthUnitsArray(content, opts);
|
|
1025
|
-
if (!arrayRef || !changed) {
|
|
1026
|
-
return { changed: false, code: void 0 };
|
|
1027
|
-
}
|
|
1028
|
-
const { code } = generate(arrayRef, {
|
|
1029
|
-
jsescOption: { quotes: "single" }
|
|
1030
|
-
});
|
|
1031
|
-
if (arrayRef.start != null && arrayRef.end != null) {
|
|
1032
|
-
const nextCode = `${content.slice(0, arrayRef.start)}${code}${content.slice(arrayRef.end)}`;
|
|
1033
|
-
if (opts.overwrite) {
|
|
1034
|
-
const target = opts.destPath ? path3.resolve(opts.destPath) : dataTypesFilePath;
|
|
1035
|
-
fs3.writeFileSync(target, nextCode, "utf8");
|
|
1036
|
-
logger_default.success("Patched Tailwind CSS length unit list (v3).");
|
|
1037
|
-
}
|
|
1038
|
-
return {
|
|
1039
|
-
changed: true,
|
|
1040
|
-
code: nextCode
|
|
1041
|
-
};
|
|
1042
|
-
}
|
|
1043
|
-
return {
|
|
1044
|
-
changed: false,
|
|
1045
|
-
code: void 0
|
|
1046
|
-
};
|
|
1047
|
-
}
|
|
1048
|
-
function applyExtendLengthUnitsPatchV4(rootDir, options) {
|
|
1049
|
-
if (!options.enabled) {
|
|
1050
|
-
return { files: [], changed: false };
|
|
1051
|
-
}
|
|
1052
|
-
const opts = { ...options };
|
|
1053
|
-
const distDir = path3.resolve(rootDir, "dist");
|
|
1054
|
-
if (!fs3.existsSync(distDir)) {
|
|
1055
|
-
return { files: [], changed: false };
|
|
1056
|
-
}
|
|
1057
|
-
const entries = fs3.readdirSync(distDir);
|
|
1058
|
-
const chunkNames = entries.filter((entry) => entry.endsWith(".js") || entry.endsWith(".mjs"));
|
|
1059
|
-
const pattern = /\[\s*["']cm["'],\s*["']mm["'],[\w,"']+\]/;
|
|
1060
|
-
const candidates = chunkNames.map((chunkName) => {
|
|
1061
|
-
const file = path3.join(distDir, chunkName);
|
|
1062
|
-
const code = fs3.readFileSync(file, "utf8");
|
|
1063
|
-
const match = pattern.exec(code);
|
|
1064
|
-
if (!match) {
|
|
1065
|
-
return null;
|
|
1066
|
-
}
|
|
1067
|
-
return {
|
|
1068
|
-
file,
|
|
1069
|
-
code,
|
|
1070
|
-
match,
|
|
1071
|
-
hasPatched: false
|
|
1072
|
-
};
|
|
1073
|
-
}).filter((candidate) => candidate !== null);
|
|
1074
|
-
for (const item of candidates) {
|
|
1075
|
-
const { code, file, match } = item;
|
|
1076
|
-
const ast = parse(match[0], { sourceType: "unambiguous" });
|
|
1077
|
-
traverse(ast, {
|
|
1078
|
-
ArrayExpression(path11) {
|
|
1079
|
-
for (const unit of opts.units) {
|
|
1080
|
-
if (path11.node.elements.some((element) => t3.isStringLiteral(element) && element.value === unit)) {
|
|
1081
|
-
item.hasPatched = true;
|
|
1082
|
-
return;
|
|
1083
|
-
}
|
|
1084
|
-
path11.node.elements.push(t3.stringLiteral(unit));
|
|
1085
|
-
}
|
|
1086
|
-
}
|
|
1087
|
-
});
|
|
1088
|
-
if (item.hasPatched) {
|
|
1089
|
-
continue;
|
|
1090
|
-
}
|
|
1091
|
-
const { code: replacement } = generate(ast, { minified: true });
|
|
1092
|
-
const start = match.index ?? 0;
|
|
1093
|
-
const end = start + match[0].length;
|
|
1094
|
-
item.code = spliceChangesIntoString(code, [
|
|
1095
|
-
{
|
|
1096
|
-
start,
|
|
1097
|
-
end,
|
|
1098
|
-
replacement: replacement.endsWith(";") ? replacement.slice(0, -1) : replacement
|
|
1099
|
-
}
|
|
1100
|
-
]);
|
|
1101
|
-
if (opts.overwrite) {
|
|
1102
|
-
fs3.writeFileSync(file, item.code, "utf8");
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
if (candidates.some((file) => !file.hasPatched)) {
|
|
1106
|
-
logger_default.success("Patched Tailwind CSS length unit list (v4).");
|
|
1107
|
-
}
|
|
1108
|
-
return {
|
|
1109
|
-
changed: candidates.some((file) => !file.hasPatched),
|
|
1110
|
-
files: candidates
|
|
1111
|
-
};
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
|
-
// src/patching/status.ts
|
|
1115
|
-
function inspectLengthUnitsArray(content, variableName, units) {
|
|
1116
|
-
const ast = parse(content);
|
|
1117
|
-
let found = false;
|
|
1118
|
-
let missingUnits = [];
|
|
1119
|
-
traverse(ast, {
|
|
1120
|
-
Identifier(path11) {
|
|
1121
|
-
if (path11.node.name === variableName && t4.isVariableDeclarator(path11.parent) && t4.isArrayExpression(path11.parent.init)) {
|
|
1122
|
-
found = true;
|
|
1123
|
-
const existing = new Set(
|
|
1124
|
-
path11.parent.init.elements.map((element) => t4.isStringLiteral(element) ? element.value : void 0).filter(Boolean)
|
|
1125
|
-
);
|
|
1126
|
-
missingUnits = units.filter((unit) => !existing.has(unit));
|
|
1127
|
-
path11.stop();
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
});
|
|
1131
|
-
return {
|
|
1132
|
-
found,
|
|
1133
|
-
missingUnits
|
|
1134
|
-
};
|
|
1135
|
-
}
|
|
1136
|
-
function checkExposeContextPatch(context) {
|
|
1137
|
-
const { packageInfo, options, majorVersion } = context;
|
|
1138
|
-
const refProperty = options.features.exposeContext.refProperty;
|
|
1139
|
-
if (!options.features.exposeContext.enabled) {
|
|
1140
|
-
return {
|
|
1141
|
-
name: "exposeContext",
|
|
1142
|
-
status: "skipped",
|
|
1143
|
-
reason: "exposeContext feature disabled",
|
|
1144
|
-
files: []
|
|
1145
|
-
};
|
|
1146
|
-
}
|
|
1147
|
-
if (majorVersion === 4) {
|
|
1148
|
-
return {
|
|
1149
|
-
name: "exposeContext",
|
|
1150
|
-
status: "unsupported",
|
|
1151
|
-
reason: "Context export patch is only required for Tailwind v2/v3",
|
|
1152
|
-
files: []
|
|
1153
|
-
};
|
|
1154
|
-
}
|
|
1155
|
-
const checks = [];
|
|
1156
|
-
function inspectFile(relative, transform) {
|
|
1157
|
-
const filePath = path4.resolve(packageInfo.rootPath, relative);
|
|
1158
|
-
if (!fs4.existsSync(filePath)) {
|
|
1159
|
-
checks.push({ relative, exists: false, patched: false });
|
|
1160
|
-
return;
|
|
1161
|
-
}
|
|
1162
|
-
const content = fs4.readFileSync(filePath, "utf8");
|
|
1163
|
-
const { hasPatched } = transform(content);
|
|
1164
|
-
checks.push({
|
|
1165
|
-
relative,
|
|
1166
|
-
exists: true,
|
|
1167
|
-
patched: hasPatched
|
|
1168
|
-
});
|
|
1169
|
-
}
|
|
1170
|
-
if (majorVersion === 3) {
|
|
1171
|
-
inspectFile("lib/processTailwindFeatures.js", transformProcessTailwindFeaturesReturnContext);
|
|
1172
|
-
const pluginCandidates = ["lib/plugin.js", "lib/index.js"];
|
|
1173
|
-
const pluginRelative = pluginCandidates.find((candidate) => fs4.existsSync(path4.resolve(packageInfo.rootPath, candidate)));
|
|
1174
|
-
if (pluginRelative) {
|
|
1175
|
-
inspectFile(pluginRelative, (content) => transformPostcssPlugin(content, { refProperty }));
|
|
1176
|
-
} else {
|
|
1177
|
-
checks.push({ relative: "lib/plugin.js", exists: false, patched: false });
|
|
1178
|
-
}
|
|
1179
|
-
} else {
|
|
1180
|
-
inspectFile("lib/jit/processTailwindFeatures.js", transformProcessTailwindFeaturesReturnContextV2);
|
|
1181
|
-
inspectFile("lib/jit/index.js", (content) => transformPostcssPluginV2(content, { refProperty }));
|
|
1182
|
-
}
|
|
1183
|
-
const files = checks.filter((check) => check.exists).map((check) => check.relative);
|
|
1184
|
-
const missingFiles = checks.filter((check) => !check.exists);
|
|
1185
|
-
const unpatchedFiles = checks.filter((check) => check.exists && !check.patched);
|
|
1186
|
-
const reasons = [];
|
|
1187
|
-
if (missingFiles.length) {
|
|
1188
|
-
reasons.push(`missing files: ${missingFiles.map((item) => item.relative).join(", ")}`);
|
|
1189
|
-
}
|
|
1190
|
-
if (unpatchedFiles.length) {
|
|
1191
|
-
reasons.push(`unpatched files: ${unpatchedFiles.map((item) => item.relative).join(", ")}`);
|
|
1192
|
-
}
|
|
1193
|
-
return {
|
|
1194
|
-
name: "exposeContext",
|
|
1195
|
-
status: reasons.length ? "not-applied" : "applied",
|
|
1196
|
-
reason: reasons.length ? reasons.join("; ") : void 0,
|
|
1197
|
-
files
|
|
1198
|
-
};
|
|
1199
|
-
}
|
|
1200
|
-
function checkExtendLengthUnitsV3(rootDir, options) {
|
|
1201
|
-
const lengthUnitsFilePath = options.lengthUnitsFilePath ?? "lib/util/dataTypes.js";
|
|
1202
|
-
const variableName = options.variableName ?? "lengthUnits";
|
|
1203
|
-
const target = path4.resolve(rootDir, lengthUnitsFilePath);
|
|
1204
|
-
const files = fs4.existsSync(target) ? [path4.relative(rootDir, target)] : [];
|
|
1205
|
-
if (!fs4.existsSync(target)) {
|
|
1206
|
-
return {
|
|
1207
|
-
name: "extendLengthUnits",
|
|
1208
|
-
status: "not-applied",
|
|
1209
|
-
reason: `missing ${lengthUnitsFilePath}`,
|
|
1210
|
-
files
|
|
1211
|
-
};
|
|
1212
|
-
}
|
|
1213
|
-
const content = fs4.readFileSync(target, "utf8");
|
|
1214
|
-
const { found, missingUnits } = inspectLengthUnitsArray(content, variableName, options.units);
|
|
1215
|
-
if (!found) {
|
|
1216
|
-
return {
|
|
1217
|
-
name: "extendLengthUnits",
|
|
1218
|
-
status: "not-applied",
|
|
1219
|
-
reason: `could not locate ${variableName} array in ${lengthUnitsFilePath}`,
|
|
1220
|
-
files
|
|
1221
|
-
};
|
|
1222
|
-
}
|
|
1223
|
-
if (missingUnits.length) {
|
|
1224
|
-
return {
|
|
1225
|
-
name: "extendLengthUnits",
|
|
1226
|
-
status: "not-applied",
|
|
1227
|
-
reason: `missing units: ${missingUnits.join(", ")}`,
|
|
1228
|
-
files
|
|
1229
|
-
};
|
|
1230
|
-
}
|
|
1231
|
-
return {
|
|
1232
|
-
name: "extendLengthUnits",
|
|
1233
|
-
status: "applied",
|
|
1234
|
-
files
|
|
1235
|
-
};
|
|
1236
|
-
}
|
|
1237
|
-
function checkExtendLengthUnitsV4(rootDir, options) {
|
|
1238
|
-
const distDir = path4.resolve(rootDir, "dist");
|
|
1239
|
-
if (!fs4.existsSync(distDir)) {
|
|
1240
|
-
return {
|
|
1241
|
-
name: "extendLengthUnits",
|
|
1242
|
-
status: "not-applied",
|
|
1243
|
-
reason: "dist directory not found for Tailwind v4 package",
|
|
1244
|
-
files: []
|
|
1245
|
-
};
|
|
1246
|
-
}
|
|
1247
|
-
const result = applyExtendLengthUnitsPatchV4(rootDir, {
|
|
1248
|
-
...options,
|
|
1249
|
-
enabled: true,
|
|
1250
|
-
overwrite: false
|
|
1251
|
-
});
|
|
1252
|
-
if (result.files.length === 0) {
|
|
1253
|
-
return {
|
|
1254
|
-
name: "extendLengthUnits",
|
|
1255
|
-
status: "not-applied",
|
|
1256
|
-
reason: "no bundle chunks matched the length unit pattern",
|
|
1257
|
-
files: []
|
|
1258
|
-
};
|
|
1259
|
-
}
|
|
1260
|
-
const files = result.files.map((file) => path4.relative(rootDir, file.file));
|
|
1261
|
-
const pending = result.files.filter((file) => !file.hasPatched);
|
|
1262
|
-
if (pending.length) {
|
|
1263
|
-
return {
|
|
1264
|
-
name: "extendLengthUnits",
|
|
1265
|
-
status: "not-applied",
|
|
1266
|
-
reason: `missing units in ${pending.length} bundle${pending.length > 1 ? "s" : ""}`,
|
|
1267
|
-
files: pending.map((file) => path4.relative(rootDir, file.file))
|
|
1268
|
-
};
|
|
1269
|
-
}
|
|
1270
|
-
return {
|
|
1271
|
-
name: "extendLengthUnits",
|
|
1272
|
-
status: "applied",
|
|
1273
|
-
files
|
|
1274
|
-
};
|
|
1275
|
-
}
|
|
1276
|
-
function checkExtendLengthUnitsPatch(context) {
|
|
1277
|
-
const { packageInfo, options, majorVersion } = context;
|
|
1278
|
-
if (!options.features.extendLengthUnits) {
|
|
1279
|
-
return {
|
|
1280
|
-
name: "extendLengthUnits",
|
|
1281
|
-
status: "skipped",
|
|
1282
|
-
reason: "extendLengthUnits feature disabled",
|
|
1283
|
-
files: []
|
|
1284
|
-
};
|
|
1285
|
-
}
|
|
1286
|
-
if (majorVersion === 2) {
|
|
1287
|
-
return {
|
|
1288
|
-
name: "extendLengthUnits",
|
|
1289
|
-
status: "unsupported",
|
|
1290
|
-
reason: "length unit extension is only applied for Tailwind v3/v4",
|
|
1291
|
-
files: []
|
|
1292
|
-
};
|
|
1293
|
-
}
|
|
1294
|
-
if (majorVersion === 3) {
|
|
1295
|
-
return checkExtendLengthUnitsV3(packageInfo.rootPath, options.features.extendLengthUnits);
|
|
1296
|
-
}
|
|
1297
|
-
return checkExtendLengthUnitsV4(packageInfo.rootPath, options.features.extendLengthUnits);
|
|
1298
|
-
}
|
|
1299
|
-
function getPatchStatusReport(context) {
|
|
1300
|
-
return {
|
|
1301
|
-
package: {
|
|
1302
|
-
name: context.packageInfo.name ?? context.packageInfo.packageJson?.name,
|
|
1303
|
-
version: context.packageInfo.version,
|
|
1304
|
-
root: context.packageInfo.rootPath
|
|
1305
|
-
},
|
|
1306
|
-
majorVersion: context.majorVersion,
|
|
1307
|
-
entries: [
|
|
1308
|
-
checkExposeContextPatch(context),
|
|
1309
|
-
checkExtendLengthUnitsPatch(context)
|
|
1310
|
-
]
|
|
1311
|
-
};
|
|
1312
|
-
}
|
|
1313
|
-
|
|
1314
|
-
// src/runtime/class-collector.ts
|
|
1315
|
-
import process4 from "process";
|
|
1316
|
-
import fs5 from "fs-extra";
|
|
1317
|
-
import path5 from "pathe";
|
|
1318
|
-
function collectClassesFromContexts(contexts, filter) {
|
|
1319
|
-
const set = /* @__PURE__ */ new Set();
|
|
1320
|
-
for (const context of contexts) {
|
|
1321
|
-
if (!isObject(context) || !context.classCache) {
|
|
1322
|
-
continue;
|
|
1323
|
-
}
|
|
1324
|
-
for (const key of context.classCache.keys()) {
|
|
1325
|
-
const className = key.toString();
|
|
1326
|
-
if (filter(className)) {
|
|
1327
|
-
set.add(className);
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
}
|
|
1331
|
-
return set;
|
|
1332
|
-
}
|
|
1333
|
-
async function collectClassesFromTailwindV4(options) {
|
|
1334
|
-
const set = /* @__PURE__ */ new Set();
|
|
1335
|
-
const v4Options = options.tailwind.v4;
|
|
1336
|
-
if (!v4Options) {
|
|
1337
|
-
return set;
|
|
1338
|
-
}
|
|
1339
|
-
const toAbsolute = (value) => {
|
|
1340
|
-
if (!value) {
|
|
1341
|
-
return void 0;
|
|
1342
|
-
}
|
|
1343
|
-
return path5.isAbsolute(value) ? value : path5.resolve(options.projectRoot, value);
|
|
1344
|
-
};
|
|
1345
|
-
const resolvedConfiguredBase = toAbsolute(v4Options.configuredBase);
|
|
1346
|
-
const resolvedDefaultBase = toAbsolute(v4Options.base) ?? process4.cwd();
|
|
1347
|
-
const resolveSources = (base) => {
|
|
1348
|
-
if (!v4Options.sources?.length) {
|
|
1349
|
-
return void 0;
|
|
1350
|
-
}
|
|
1351
|
-
return v4Options.sources.map((source) => ({
|
|
1352
|
-
base: source.base ?? base,
|
|
1353
|
-
pattern: source.pattern,
|
|
1354
|
-
negated: source.negated
|
|
1355
|
-
}));
|
|
1356
|
-
};
|
|
1357
|
-
if (v4Options.cssEntries.length > 0) {
|
|
1358
|
-
for (const entry of v4Options.cssEntries) {
|
|
1359
|
-
const filePath = path5.isAbsolute(entry) ? entry : path5.resolve(options.projectRoot, entry);
|
|
1360
|
-
if (!await fs5.pathExists(filePath)) {
|
|
1361
|
-
continue;
|
|
1362
|
-
}
|
|
1363
|
-
const css = await fs5.readFile(filePath, "utf8");
|
|
1364
|
-
const entryDir = path5.dirname(filePath);
|
|
1365
|
-
const designSystemBases = resolvedConfiguredBase && resolvedConfiguredBase !== entryDir ? [entryDir, resolvedConfiguredBase] : [entryDir];
|
|
1366
|
-
const sourcesBase = resolvedConfiguredBase ?? entryDir;
|
|
1367
|
-
const sources = resolveSources(sourcesBase);
|
|
1368
|
-
const candidates = await extractValidCandidates({
|
|
1369
|
-
cwd: options.projectRoot,
|
|
1370
|
-
base: designSystemBases[0],
|
|
1371
|
-
baseFallbacks: designSystemBases.slice(1),
|
|
1372
|
-
css,
|
|
1373
|
-
sources
|
|
1374
|
-
});
|
|
1375
|
-
for (const candidate of candidates) {
|
|
1376
|
-
if (options.filter(candidate)) {
|
|
1377
|
-
set.add(candidate);
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
|
-
} else {
|
|
1382
|
-
const baseForCss = resolvedConfiguredBase ?? resolvedDefaultBase;
|
|
1383
|
-
const sources = resolveSources(baseForCss);
|
|
1384
|
-
const candidates = await extractValidCandidates({
|
|
1385
|
-
cwd: options.projectRoot,
|
|
1386
|
-
base: baseForCss,
|
|
1387
|
-
css: v4Options.css,
|
|
1388
|
-
sources
|
|
1389
|
-
});
|
|
1390
|
-
for (const candidate of candidates) {
|
|
1391
|
-
if (options.filter(candidate)) {
|
|
1392
|
-
set.add(candidate);
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
return set;
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
// src/runtime/context-registry.ts
|
|
1400
|
-
import { createRequire } from "module";
|
|
1401
|
-
import fs6 from "fs-extra";
|
|
1402
|
-
import path6 from "pathe";
|
|
1403
|
-
var require2 = createRequire(import.meta.url);
|
|
1404
|
-
function resolveRuntimeEntry(packageInfo, majorVersion) {
|
|
1405
|
-
const root = packageInfo.rootPath;
|
|
1406
|
-
if (majorVersion === 2) {
|
|
1407
|
-
const jitIndex = path6.join(root, "lib/jit/index.js");
|
|
1408
|
-
if (fs6.existsSync(jitIndex)) {
|
|
1409
|
-
return jitIndex;
|
|
1410
|
-
}
|
|
1411
|
-
} else if (majorVersion === 3) {
|
|
1412
|
-
const plugin = path6.join(root, "lib/plugin.js");
|
|
1413
|
-
const index = path6.join(root, "lib/index.js");
|
|
1414
|
-
if (fs6.existsSync(plugin)) {
|
|
1415
|
-
return plugin;
|
|
1416
|
-
}
|
|
1417
|
-
if (fs6.existsSync(index)) {
|
|
1418
|
-
return index;
|
|
1419
|
-
}
|
|
1420
|
-
}
|
|
1421
|
-
return void 0;
|
|
1422
|
-
}
|
|
1423
|
-
function loadRuntimeContexts(packageInfo, majorVersion, refProperty) {
|
|
1424
|
-
if (majorVersion === 4) {
|
|
1425
|
-
return [];
|
|
1426
|
-
}
|
|
1427
|
-
const entry = resolveRuntimeEntry(packageInfo, majorVersion);
|
|
1428
|
-
if (!entry) {
|
|
1429
|
-
return [];
|
|
1430
|
-
}
|
|
1431
|
-
const moduleExports = require2(entry);
|
|
1432
|
-
if (!moduleExports) {
|
|
1433
|
-
return [];
|
|
1434
|
-
}
|
|
1435
|
-
const ref = moduleExports[refProperty];
|
|
1436
|
-
if (!ref) {
|
|
1437
|
-
return [];
|
|
1438
|
-
}
|
|
1439
|
-
if (Array.isArray(ref)) {
|
|
1440
|
-
return ref;
|
|
1441
|
-
}
|
|
1442
|
-
if (typeof ref === "object" && Array.isArray(ref.value)) {
|
|
1443
|
-
return ref.value;
|
|
1444
|
-
}
|
|
1445
|
-
return [];
|
|
1446
|
-
}
|
|
1447
|
-
|
|
1448
|
-
// src/runtime/process-tailwindcss.ts
|
|
1449
|
-
import { createRequire as createRequire2 } from "module";
|
|
1450
|
-
import path7 from "pathe";
|
|
1451
|
-
import postcss from "postcss";
|
|
1452
|
-
import { loadConfig } from "tailwindcss-config";
|
|
1453
|
-
var require3 = createRequire2(import.meta.url);
|
|
1454
|
-
async function resolveConfigPath(options) {
|
|
1455
|
-
if (options.config && path7.isAbsolute(options.config)) {
|
|
1456
|
-
return options.config;
|
|
1457
|
-
}
|
|
1458
|
-
const result = await loadConfig({ cwd: options.cwd });
|
|
1459
|
-
if (!result) {
|
|
1460
|
-
throw new Error(`Unable to locate Tailwind CSS config from ${options.cwd}`);
|
|
1461
|
-
}
|
|
1462
|
-
return result.filepath;
|
|
1463
|
-
}
|
|
1464
|
-
async function runTailwindBuild(options) {
|
|
1465
|
-
const configPath = await resolveConfigPath(options);
|
|
1466
|
-
const pluginName = options.postcssPlugin ?? (options.majorVersion === 4 ? "@tailwindcss/postcss" : "tailwindcss");
|
|
1467
|
-
if (options.majorVersion === 4) {
|
|
1468
|
-
return postcss([
|
|
1469
|
-
require3(pluginName)({
|
|
1470
|
-
config: configPath
|
|
1471
|
-
})
|
|
1472
|
-
]).process("@import 'tailwindcss';", {
|
|
1473
|
-
from: void 0
|
|
1474
|
-
});
|
|
1475
|
-
}
|
|
1476
|
-
return postcss([
|
|
1477
|
-
require3(pluginName)({
|
|
1478
|
-
config: configPath
|
|
1479
|
-
})
|
|
1480
|
-
]).process("@tailwind base;@tailwind components;@tailwind utilities;", {
|
|
1481
|
-
from: void 0
|
|
1482
|
-
});
|
|
1483
|
-
}
|
|
1484
|
-
|
|
1485
|
-
// src/api/tailwindcss-patcher.ts
|
|
1486
|
-
import process5 from "process";
|
|
1487
|
-
import fs8 from "fs-extra";
|
|
1488
|
-
import { getPackageInfoSync } from "local-pkg";
|
|
1489
|
-
import path9 from "pathe";
|
|
1490
|
-
import { coerce } from "semver";
|
|
1491
|
-
|
|
1492
|
-
// src/options/legacy.ts
|
|
1493
|
-
function normalizeLegacyFeatures(patch) {
|
|
1494
|
-
const apply = patch?.applyPatches;
|
|
1495
|
-
const extend = apply?.extendLengthUnits;
|
|
1496
|
-
let extendOption = false;
|
|
1497
|
-
if (extend && typeof extend === "object") {
|
|
1498
|
-
extendOption = {
|
|
1499
|
-
...extend,
|
|
1500
|
-
enabled: true
|
|
1501
|
-
};
|
|
1502
|
-
} else if (extend === true) {
|
|
1503
|
-
extendOption = {
|
|
1504
|
-
enabled: true,
|
|
1505
|
-
units: ["rpx"],
|
|
1506
|
-
overwrite: patch?.overwrite
|
|
1507
|
-
};
|
|
1508
|
-
}
|
|
1509
|
-
return {
|
|
1510
|
-
exposeContext: apply?.exportContext ?? true,
|
|
1511
|
-
extendLengthUnits: extendOption
|
|
1512
|
-
};
|
|
1513
|
-
}
|
|
1514
|
-
function fromLegacyOptions(options) {
|
|
1515
|
-
if (!options) {
|
|
1516
|
-
return {};
|
|
1517
|
-
}
|
|
1518
|
-
const patch = options.patch;
|
|
1519
|
-
const features = normalizeLegacyFeatures(patch);
|
|
1520
|
-
const output = patch?.output;
|
|
1521
|
-
const tailwindConfig = patch?.tailwindcss;
|
|
1522
|
-
const tailwindVersion = tailwindConfig?.version;
|
|
1523
|
-
const tailwindV2 = tailwindConfig?.v2;
|
|
1524
|
-
const tailwindV3 = tailwindConfig?.v3;
|
|
1525
|
-
const tailwindV4 = tailwindConfig?.v4;
|
|
1526
|
-
const tailwindConfigPath = tailwindV3?.config ?? tailwindV2?.config;
|
|
1527
|
-
const tailwindCwd = tailwindV3?.cwd ?? tailwindV2?.cwd ?? patch?.cwd;
|
|
1528
|
-
return {
|
|
1529
|
-
cwd: patch?.cwd,
|
|
1530
|
-
overwrite: patch?.overwrite,
|
|
1531
|
-
filter: patch?.filter,
|
|
1532
|
-
cache: typeof options.cache === "boolean" ? options.cache : options.cache ? {
|
|
1533
|
-
...options.cache,
|
|
1534
|
-
enabled: options.cache.enabled ?? true
|
|
1535
|
-
} : void 0,
|
|
1536
|
-
output: output ? {
|
|
1537
|
-
file: output.filename,
|
|
1538
|
-
pretty: output.loose ? 2 : false,
|
|
1539
|
-
removeUniversalSelector: output.removeUniversalSelector
|
|
1540
|
-
} : void 0,
|
|
1541
|
-
tailwind: {
|
|
1542
|
-
packageName: patch?.packageName,
|
|
1543
|
-
version: tailwindVersion,
|
|
1544
|
-
resolve: patch?.resolve,
|
|
1545
|
-
config: tailwindConfigPath,
|
|
1546
|
-
cwd: tailwindCwd,
|
|
1547
|
-
v2: tailwindV2,
|
|
1548
|
-
v3: tailwindV3,
|
|
1549
|
-
v4: tailwindV4
|
|
1550
|
-
},
|
|
1551
|
-
features: {
|
|
1552
|
-
exposeContext: features.exposeContext,
|
|
1553
|
-
extendLengthUnits: features.extendLengthUnits
|
|
1554
|
-
}
|
|
1555
|
-
};
|
|
1556
|
-
}
|
|
1557
|
-
function fromUnifiedConfig(registry) {
|
|
1558
|
-
if (!registry) {
|
|
1559
|
-
return {};
|
|
1560
|
-
}
|
|
1561
|
-
const tailwind = registry.tailwind;
|
|
1562
|
-
const output = registry.output;
|
|
1563
|
-
const pretty = (() => {
|
|
1564
|
-
if (output?.pretty === void 0) {
|
|
1565
|
-
return void 0;
|
|
1566
|
-
}
|
|
1567
|
-
if (typeof output.pretty === "boolean") {
|
|
1568
|
-
return output.pretty ? 2 : false;
|
|
1569
|
-
}
|
|
1570
|
-
return output.pretty;
|
|
1571
|
-
})();
|
|
1572
|
-
return {
|
|
1573
|
-
output: output ? {
|
|
1574
|
-
file: output.file,
|
|
1575
|
-
pretty,
|
|
1576
|
-
removeUniversalSelector: output.stripUniversalSelector
|
|
1577
|
-
} : void 0,
|
|
1578
|
-
tailwind: tailwind ? {
|
|
1579
|
-
version: tailwind.version,
|
|
1580
|
-
packageName: tailwind.package,
|
|
1581
|
-
resolve: tailwind.resolve,
|
|
1582
|
-
config: tailwind.config,
|
|
1583
|
-
cwd: tailwind.cwd,
|
|
1584
|
-
v2: tailwind.legacy,
|
|
1585
|
-
v3: tailwind.classic,
|
|
1586
|
-
v4: tailwind.next
|
|
1587
|
-
} : void 0
|
|
1588
|
-
};
|
|
1589
|
-
}
|
|
1590
|
-
|
|
1591
|
-
// src/patching/operations/export-context/index.ts
|
|
1592
|
-
import fs7 from "fs-extra";
|
|
1593
|
-
import path8 from "pathe";
|
|
1594
|
-
function writeFileIfRequired(filePath, code, overwrite, successMessage) {
|
|
1595
|
-
if (!overwrite) {
|
|
1596
|
-
return;
|
|
1597
|
-
}
|
|
1598
|
-
fs7.writeFileSync(filePath, code, {
|
|
1599
|
-
encoding: "utf8"
|
|
1600
|
-
});
|
|
1601
|
-
logger_default.success(successMessage);
|
|
1602
|
-
}
|
|
1603
|
-
function applyExposeContextPatch(params) {
|
|
1604
|
-
const { rootDir, refProperty, overwrite, majorVersion } = params;
|
|
1605
|
-
const result = {
|
|
1606
|
-
applied: false,
|
|
1607
|
-
files: {}
|
|
1608
|
-
};
|
|
1609
|
-
if (majorVersion === 3) {
|
|
1610
|
-
const processFileRelative = "lib/processTailwindFeatures.js";
|
|
1611
|
-
const processFilePath = path8.resolve(rootDir, processFileRelative);
|
|
1612
|
-
if (fs7.existsSync(processFilePath)) {
|
|
1613
|
-
const content = fs7.readFileSync(processFilePath, "utf8");
|
|
1614
|
-
const { code, hasPatched } = transformProcessTailwindFeaturesReturnContext(content);
|
|
1615
|
-
result.files[processFileRelative] = code;
|
|
1616
|
-
if (!hasPatched) {
|
|
1617
|
-
writeFileIfRequired(
|
|
1618
|
-
processFilePath,
|
|
1619
|
-
code,
|
|
1620
|
-
overwrite,
|
|
1621
|
-
"Patched Tailwind CSS processTailwindFeatures to expose runtime context."
|
|
1622
|
-
);
|
|
1623
|
-
result.applied = true;
|
|
1624
|
-
}
|
|
1625
|
-
}
|
|
1626
|
-
const pluginCandidates = ["lib/plugin.js", "lib/index.js"];
|
|
1627
|
-
const pluginRelative = pluginCandidates.find((candidate) => fs7.existsSync(path8.resolve(rootDir, candidate)));
|
|
1628
|
-
if (pluginRelative) {
|
|
1629
|
-
const pluginPath = path8.resolve(rootDir, pluginRelative);
|
|
1630
|
-
const content = fs7.readFileSync(pluginPath, "utf8");
|
|
1631
|
-
const { code, hasPatched } = transformPostcssPlugin(content, { refProperty });
|
|
1632
|
-
result.files[pluginRelative] = code;
|
|
1633
|
-
if (!hasPatched) {
|
|
1634
|
-
writeFileIfRequired(
|
|
1635
|
-
pluginPath,
|
|
1636
|
-
code,
|
|
1637
|
-
overwrite,
|
|
1638
|
-
"Patched Tailwind CSS plugin entry to collect runtime contexts."
|
|
1639
|
-
);
|
|
1640
|
-
result.applied = true;
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
} else if (majorVersion === 2) {
|
|
1644
|
-
const processFileRelative = "lib/jit/processTailwindFeatures.js";
|
|
1645
|
-
const processFilePath = path8.resolve(rootDir, processFileRelative);
|
|
1646
|
-
if (fs7.existsSync(processFilePath)) {
|
|
1647
|
-
const content = fs7.readFileSync(processFilePath, "utf8");
|
|
1648
|
-
const { code, hasPatched } = transformProcessTailwindFeaturesReturnContextV2(content);
|
|
1649
|
-
result.files[processFileRelative] = code;
|
|
1650
|
-
if (!hasPatched) {
|
|
1651
|
-
writeFileIfRequired(
|
|
1652
|
-
processFilePath,
|
|
1653
|
-
code,
|
|
1654
|
-
overwrite,
|
|
1655
|
-
"Patched Tailwind CSS JIT processTailwindFeatures to expose runtime context."
|
|
1656
|
-
);
|
|
1657
|
-
result.applied = true;
|
|
1658
|
-
}
|
|
1659
|
-
}
|
|
1660
|
-
const pluginRelative = "lib/jit/index.js";
|
|
1661
|
-
const pluginPath = path8.resolve(rootDir, pluginRelative);
|
|
1662
|
-
if (fs7.existsSync(pluginPath)) {
|
|
1663
|
-
const content = fs7.readFileSync(pluginPath, "utf8");
|
|
1664
|
-
const { code, hasPatched } = transformPostcssPluginV2(content, { refProperty });
|
|
1665
|
-
result.files[pluginRelative] = code;
|
|
1666
|
-
if (!hasPatched) {
|
|
1667
|
-
writeFileIfRequired(
|
|
1668
|
-
pluginPath,
|
|
1669
|
-
code,
|
|
1670
|
-
overwrite,
|
|
1671
|
-
"Patched Tailwind CSS JIT entry to collect runtime contexts."
|
|
1672
|
-
);
|
|
1673
|
-
result.applied = true;
|
|
1674
|
-
}
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
|
-
return result;
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
|
-
// src/patching/patch-runner.ts
|
|
1681
|
-
function applyTailwindPatches(context) {
|
|
1682
|
-
const { packageInfo, options, majorVersion } = context;
|
|
1683
|
-
const results = {};
|
|
1684
|
-
if (options.features.exposeContext.enabled && (majorVersion === 2 || majorVersion === 3)) {
|
|
1685
|
-
results.exposeContext = applyExposeContextPatch({
|
|
1686
|
-
rootDir: packageInfo.rootPath,
|
|
1687
|
-
refProperty: options.features.exposeContext.refProperty,
|
|
1688
|
-
overwrite: options.overwrite,
|
|
1689
|
-
majorVersion
|
|
1690
|
-
});
|
|
1691
|
-
}
|
|
1692
|
-
if (options.features.extendLengthUnits?.enabled) {
|
|
1693
|
-
if (majorVersion === 3) {
|
|
1694
|
-
results.extendLengthUnits = applyExtendLengthUnitsPatchV3(
|
|
1695
|
-
packageInfo.rootPath,
|
|
1696
|
-
options.features.extendLengthUnits
|
|
1697
|
-
);
|
|
1698
|
-
} else if (majorVersion === 4) {
|
|
1699
|
-
results.extendLengthUnits = applyExtendLengthUnitsPatchV4(
|
|
1700
|
-
packageInfo.rootPath,
|
|
1701
|
-
options.features.extendLengthUnits
|
|
1702
|
-
);
|
|
1703
|
-
}
|
|
1704
|
-
}
|
|
1705
|
-
return results;
|
|
1706
|
-
}
|
|
1707
|
-
|
|
1708
|
-
// src/api/tailwindcss-patcher.ts
|
|
1709
|
-
function resolveMajorVersion(version, hint) {
|
|
1710
|
-
if (hint && [2, 3, 4].includes(hint)) {
|
|
1711
|
-
return hint;
|
|
1712
|
-
}
|
|
1713
|
-
if (version) {
|
|
1714
|
-
const coerced = coerce(version);
|
|
1715
|
-
if (coerced) {
|
|
1716
|
-
const major = coerced.major;
|
|
1717
|
-
if (major === 2 || major === 3 || major === 4) {
|
|
1718
|
-
return major;
|
|
1719
|
-
}
|
|
1720
|
-
if (major >= 4) {
|
|
1721
|
-
return 4;
|
|
1722
|
-
}
|
|
1723
|
-
}
|
|
1724
|
-
}
|
|
1725
|
-
return 3;
|
|
1726
|
-
}
|
|
1727
|
-
function resolveTailwindExecutionOptions(normalized, majorVersion) {
|
|
1728
|
-
const base = normalized.tailwind;
|
|
1729
|
-
if (majorVersion === 2 && base.v2) {
|
|
1730
|
-
return {
|
|
1731
|
-
cwd: base.v2.cwd ?? base.cwd ?? normalized.projectRoot,
|
|
1732
|
-
config: base.v2.config ?? base.config,
|
|
1733
|
-
postcssPlugin: base.v2.postcssPlugin ?? base.postcssPlugin
|
|
1734
|
-
};
|
|
1735
|
-
}
|
|
1736
|
-
if (majorVersion === 3 && base.v3) {
|
|
1737
|
-
return {
|
|
1738
|
-
cwd: base.v3.cwd ?? base.cwd ?? normalized.projectRoot,
|
|
1739
|
-
config: base.v3.config ?? base.config,
|
|
1740
|
-
postcssPlugin: base.v3.postcssPlugin ?? base.postcssPlugin
|
|
1741
|
-
};
|
|
1742
|
-
}
|
|
1743
|
-
return {
|
|
1744
|
-
cwd: base.cwd ?? normalized.projectRoot,
|
|
1745
|
-
config: base.config,
|
|
1746
|
-
postcssPlugin: base.postcssPlugin
|
|
1747
|
-
};
|
|
1748
|
-
}
|
|
1749
|
-
var TailwindcssPatcher = class {
|
|
1750
|
-
options;
|
|
1751
|
-
packageInfo;
|
|
1752
|
-
majorVersion;
|
|
1753
|
-
cacheStore;
|
|
1754
|
-
constructor(options = {}) {
|
|
1755
|
-
const resolvedOptions = options && typeof options === "object" && "patch" in options ? fromLegacyOptions(options) : options;
|
|
1756
|
-
this.options = normalizeOptions(resolvedOptions);
|
|
1757
|
-
const packageInfo = getPackageInfoSync(
|
|
1758
|
-
this.options.tailwind.packageName,
|
|
1759
|
-
this.options.tailwind.resolve
|
|
1760
|
-
);
|
|
1761
|
-
if (!packageInfo) {
|
|
1762
|
-
throw new Error(`Unable to locate Tailwind CSS package "${this.options.tailwind.packageName}".`);
|
|
1763
|
-
}
|
|
1764
|
-
this.packageInfo = packageInfo;
|
|
1765
|
-
this.majorVersion = resolveMajorVersion(
|
|
1766
|
-
this.packageInfo.version,
|
|
1767
|
-
this.options.tailwind.versionHint
|
|
1768
|
-
);
|
|
1769
|
-
this.cacheStore = new CacheStore(this.options.cache);
|
|
1770
|
-
}
|
|
1771
|
-
async patch() {
|
|
1772
|
-
return applyTailwindPatches({
|
|
1773
|
-
packageInfo: this.packageInfo,
|
|
1774
|
-
options: this.options,
|
|
1775
|
-
majorVersion: this.majorVersion
|
|
1776
|
-
});
|
|
1777
|
-
}
|
|
1778
|
-
async getPatchStatus() {
|
|
1779
|
-
return getPatchStatusReport({
|
|
1780
|
-
packageInfo: this.packageInfo,
|
|
1781
|
-
options: this.options,
|
|
1782
|
-
majorVersion: this.majorVersion
|
|
1783
|
-
});
|
|
1784
|
-
}
|
|
1785
|
-
getContexts() {
|
|
1786
|
-
return loadRuntimeContexts(
|
|
1787
|
-
this.packageInfo,
|
|
1788
|
-
this.majorVersion,
|
|
1789
|
-
this.options.features.exposeContext.refProperty
|
|
1790
|
-
);
|
|
1791
|
-
}
|
|
1792
|
-
async runTailwindBuildIfNeeded() {
|
|
1793
|
-
if (this.majorVersion === 2 || this.majorVersion === 3) {
|
|
1794
|
-
const executionOptions = resolveTailwindExecutionOptions(this.options, this.majorVersion);
|
|
1795
|
-
await runTailwindBuild({
|
|
1796
|
-
cwd: executionOptions.cwd,
|
|
1797
|
-
config: executionOptions.config,
|
|
1798
|
-
majorVersion: this.majorVersion,
|
|
1799
|
-
postcssPlugin: executionOptions.postcssPlugin
|
|
1800
|
-
});
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
async collectClassSet() {
|
|
1804
|
-
if (this.majorVersion === 4) {
|
|
1805
|
-
return collectClassesFromTailwindV4(this.options);
|
|
1806
|
-
}
|
|
1807
|
-
const contexts = this.getContexts();
|
|
1808
|
-
return collectClassesFromContexts(contexts, this.options.filter);
|
|
1809
|
-
}
|
|
1810
|
-
async mergeWithCache(set) {
|
|
1811
|
-
if (!this.options.cache.enabled) {
|
|
1812
|
-
return set;
|
|
1813
|
-
}
|
|
1814
|
-
const existing = await this.cacheStore.read();
|
|
1815
|
-
if (this.options.cache.strategy === "merge") {
|
|
1816
|
-
for (const value of existing) {
|
|
1817
|
-
set.add(value);
|
|
1818
|
-
}
|
|
1819
|
-
await this.cacheStore.write(set);
|
|
1820
|
-
} else {
|
|
1821
|
-
if (set.size > 0) {
|
|
1822
|
-
await this.cacheStore.write(set);
|
|
1823
|
-
} else {
|
|
1824
|
-
return existing;
|
|
1825
|
-
}
|
|
1826
|
-
}
|
|
1827
|
-
return set;
|
|
1828
|
-
}
|
|
1829
|
-
mergeWithCacheSync(set) {
|
|
1830
|
-
if (!this.options.cache.enabled) {
|
|
1831
|
-
return set;
|
|
1832
|
-
}
|
|
1833
|
-
const existing = this.cacheStore.readSync();
|
|
1834
|
-
if (this.options.cache.strategy === "merge") {
|
|
1835
|
-
for (const value of existing) {
|
|
1836
|
-
set.add(value);
|
|
1837
|
-
}
|
|
1838
|
-
this.cacheStore.writeSync(set);
|
|
1839
|
-
} else {
|
|
1840
|
-
if (set.size > 0) {
|
|
1841
|
-
this.cacheStore.writeSync(set);
|
|
1842
|
-
} else {
|
|
1843
|
-
return existing;
|
|
1844
|
-
}
|
|
1845
|
-
}
|
|
1846
|
-
return set;
|
|
1847
|
-
}
|
|
1848
|
-
async getClassSet() {
|
|
1849
|
-
await this.runTailwindBuildIfNeeded();
|
|
1850
|
-
const set = await this.collectClassSet();
|
|
1851
|
-
return this.mergeWithCache(set);
|
|
1852
|
-
}
|
|
1853
|
-
getClassSetSync() {
|
|
1854
|
-
if (this.majorVersion === 4) {
|
|
1855
|
-
throw new Error("getClassSetSync is not supported for Tailwind CSS v4 projects. Use getClassSet instead.");
|
|
1856
|
-
}
|
|
1857
|
-
const contexts = this.getContexts();
|
|
1858
|
-
const set = collectClassesFromContexts(contexts, this.options.filter);
|
|
1859
|
-
const merged = this.mergeWithCacheSync(set);
|
|
1860
|
-
if (contexts.length === 0 && merged.size === 0) {
|
|
1861
|
-
return void 0;
|
|
1862
|
-
}
|
|
1863
|
-
return merged;
|
|
1864
|
-
}
|
|
1865
|
-
async extract(options) {
|
|
1866
|
-
const shouldWrite = options?.write ?? this.options.output.enabled;
|
|
1867
|
-
const classSet = await this.getClassSet();
|
|
1868
|
-
const classList = Array.from(classSet);
|
|
1869
|
-
const result = {
|
|
1870
|
-
classList,
|
|
1871
|
-
classSet
|
|
1872
|
-
};
|
|
1873
|
-
if (!shouldWrite || !this.options.output.file) {
|
|
1874
|
-
return result;
|
|
1875
|
-
}
|
|
1876
|
-
const target = path9.resolve(this.options.output.file);
|
|
1877
|
-
await fs8.ensureDir(path9.dirname(target));
|
|
1878
|
-
if (this.options.output.format === "json") {
|
|
1879
|
-
const spaces = typeof this.options.output.pretty === "number" ? this.options.output.pretty : void 0;
|
|
1880
|
-
await fs8.writeJSON(target, classList, { spaces });
|
|
1881
|
-
} else {
|
|
1882
|
-
await fs8.writeFile(target, `${classList.join("\n")}
|
|
1883
|
-
`, "utf8");
|
|
1884
|
-
}
|
|
1885
|
-
logger_default.success(`Tailwind CSS class list saved to ${target.replace(process5.cwd(), ".")}`);
|
|
1886
|
-
return {
|
|
1887
|
-
...result,
|
|
1888
|
-
filename: target
|
|
1889
|
-
};
|
|
1890
|
-
}
|
|
1891
|
-
// Backwards compatibility helper used by tests and API consumers.
|
|
1892
|
-
extractValidCandidates = extractValidCandidates;
|
|
1893
|
-
async collectContentTokens(options) {
|
|
1894
|
-
return extractProjectCandidatesWithPositions({
|
|
1895
|
-
cwd: options?.cwd ?? this.options.projectRoot,
|
|
1896
|
-
sources: options?.sources ?? this.options.tailwind.v4?.sources ?? []
|
|
1897
|
-
});
|
|
1898
|
-
}
|
|
1899
|
-
async collectContentTokensByFile(options) {
|
|
1900
|
-
const report = await this.collectContentTokens({
|
|
1901
|
-
cwd: options?.cwd,
|
|
1902
|
-
sources: options?.sources
|
|
1903
|
-
});
|
|
1904
|
-
return groupTokensByFile(report, {
|
|
1905
|
-
key: options?.key,
|
|
1906
|
-
stripAbsolutePaths: options?.stripAbsolutePaths
|
|
1907
|
-
});
|
|
1908
|
-
}
|
|
1909
|
-
};
|
|
1910
|
-
|
|
1911
|
-
// src/cli/commands.ts
|
|
1912
|
-
import process6 from "process";
|
|
1913
|
-
import { CONFIG_NAME, getConfig, initConfig } from "@tailwindcss-mangle/config";
|
|
1914
|
-
|
|
1915
|
-
// ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
|
|
1916
|
-
function isPlainObject(value) {
|
|
1917
|
-
if (value === null || typeof value !== "object") {
|
|
1918
|
-
return false;
|
|
1919
|
-
}
|
|
1920
|
-
const prototype = Object.getPrototypeOf(value);
|
|
1921
|
-
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
|
|
1922
|
-
return false;
|
|
1923
|
-
}
|
|
1924
|
-
if (Symbol.iterator in value) {
|
|
1925
|
-
return false;
|
|
1926
|
-
}
|
|
1927
|
-
if (Symbol.toStringTag in value) {
|
|
1928
|
-
return Object.prototype.toString.call(value) === "[object Module]";
|
|
1929
|
-
}
|
|
1930
|
-
return true;
|
|
1931
|
-
}
|
|
1932
|
-
function _defu(baseObject, defaults, namespace = ".", merger) {
|
|
1933
|
-
if (!isPlainObject(defaults)) {
|
|
1934
|
-
return _defu(baseObject, {}, namespace, merger);
|
|
1935
|
-
}
|
|
1936
|
-
const object = Object.assign({}, defaults);
|
|
1937
|
-
for (const key in baseObject) {
|
|
1938
|
-
if (key === "__proto__" || key === "constructor") {
|
|
1939
|
-
continue;
|
|
1940
|
-
}
|
|
1941
|
-
const value = baseObject[key];
|
|
1942
|
-
if (value === null || value === void 0) {
|
|
1943
|
-
continue;
|
|
1944
|
-
}
|
|
1945
|
-
if (merger && merger(object, key, value, namespace)) {
|
|
1946
|
-
continue;
|
|
1947
|
-
}
|
|
1948
|
-
if (Array.isArray(value) && Array.isArray(object[key])) {
|
|
1949
|
-
object[key] = [...value, ...object[key]];
|
|
1950
|
-
} else if (isPlainObject(value) && isPlainObject(object[key])) {
|
|
1951
|
-
object[key] = _defu(
|
|
1952
|
-
value,
|
|
1953
|
-
object[key],
|
|
1954
|
-
(namespace ? `${namespace}.` : "") + key.toString(),
|
|
1955
|
-
merger
|
|
1956
|
-
);
|
|
1957
|
-
} else {
|
|
1958
|
-
object[key] = value;
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
return object;
|
|
1962
|
-
}
|
|
1963
|
-
function createDefu(merger) {
|
|
1964
|
-
return (...arguments_) => (
|
|
1965
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
1966
|
-
arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
|
|
1967
|
-
);
|
|
1968
|
-
}
|
|
1969
|
-
var defu = createDefu();
|
|
1970
|
-
var defuFn = createDefu((object, key, currentValue) => {
|
|
1971
|
-
if (object[key] !== void 0 && typeof currentValue === "function") {
|
|
1972
|
-
object[key] = currentValue(object[key]);
|
|
1973
|
-
return true;
|
|
1974
|
-
}
|
|
1975
|
-
});
|
|
1976
|
-
var defuArrayFn = createDefu((object, key, currentValue) => {
|
|
1977
|
-
if (Array.isArray(object[key]) && typeof currentValue === "function") {
|
|
1978
|
-
object[key] = currentValue(object[key]);
|
|
1979
|
-
return true;
|
|
1980
|
-
}
|
|
1981
|
-
});
|
|
1982
|
-
|
|
1983
|
-
// ../shared/src/utils.ts
|
|
1984
|
-
var defuOverrideArray = createDefu((obj, key, value) => {
|
|
1985
|
-
if (Array.isArray(obj[key]) && Array.isArray(value)) {
|
|
1986
|
-
obj[key] = value;
|
|
1987
|
-
return true;
|
|
1988
|
-
}
|
|
1989
|
-
});
|
|
1990
|
-
var preserveClassNames = [
|
|
1991
|
-
// https://tailwindcss.com/docs/transition-timing-function start
|
|
1992
|
-
// https://github.com/sonofmagic/tailwindcss-mangle/issues/21
|
|
1993
|
-
"ease-out",
|
|
1994
|
-
"ease-linear",
|
|
1995
|
-
"ease-in",
|
|
1996
|
-
"ease-in-out"
|
|
1997
|
-
// https://tailwindcss.com/docs/transition-timing-function end
|
|
1998
|
-
];
|
|
1999
|
-
var preserveClassNamesMap = preserveClassNames.reduce((acc, cur) => {
|
|
2000
|
-
acc[cur] = true;
|
|
2001
|
-
return acc;
|
|
2002
|
-
}, {});
|
|
2003
|
-
var acceptChars = [..."abcdefghijklmnopqrstuvwxyz"];
|
|
2004
|
-
|
|
2005
|
-
// src/cli/commands.ts
|
|
2006
|
-
import cac from "cac";
|
|
2007
|
-
import fs9 from "fs-extra";
|
|
2008
|
-
import path10 from "pathe";
|
|
2009
|
-
var tailwindcssPatchCommands = ["install", "extract", "tokens", "init", "status"];
|
|
2010
|
-
var TOKEN_FORMATS = ["json", "lines", "grouped-json"];
|
|
2011
|
-
var DEFAULT_TOKEN_REPORT = ".tw-patch/tw-token-report.json";
|
|
2012
|
-
function formatTokenLine(entry) {
|
|
2013
|
-
return `${entry.relativeFile}:${entry.line}:${entry.column} ${entry.rawCandidate} (${entry.start}-${entry.end})`;
|
|
2014
|
-
}
|
|
2015
|
-
function formatGroupedPreview(map, limit = 3) {
|
|
2016
|
-
const files = Object.keys(map);
|
|
2017
|
-
if (!files.length) {
|
|
2018
|
-
return { preview: "", moreFiles: 0 };
|
|
2019
|
-
}
|
|
2020
|
-
const lines = files.slice(0, limit).map((file) => {
|
|
2021
|
-
const tokens = map[file];
|
|
2022
|
-
const sample = tokens.slice(0, 3).map((token) => token.rawCandidate).join(", ");
|
|
2023
|
-
const suffix = tokens.length > 3 ? ", \u2026" : "";
|
|
2024
|
-
return `${file}: ${tokens.length} tokens (${sample}${suffix})`;
|
|
2025
|
-
});
|
|
2026
|
-
return {
|
|
2027
|
-
preview: lines.join("\n"),
|
|
2028
|
-
moreFiles: Math.max(0, files.length - limit)
|
|
2029
|
-
};
|
|
2030
|
-
}
|
|
2031
|
-
function resolveCwd(rawCwd) {
|
|
2032
|
-
if (!rawCwd) {
|
|
2033
|
-
return process6.cwd();
|
|
2034
|
-
}
|
|
2035
|
-
return path10.resolve(rawCwd);
|
|
2036
|
-
}
|
|
2037
|
-
function createDefaultRunner(factory) {
|
|
2038
|
-
let promise;
|
|
2039
|
-
return () => {
|
|
2040
|
-
if (!promise) {
|
|
2041
|
-
promise = factory();
|
|
2042
|
-
}
|
|
2043
|
-
return promise;
|
|
2044
|
-
};
|
|
2045
|
-
}
|
|
2046
|
-
async function loadPatchOptionsForCwd(cwd, overrides) {
|
|
2047
|
-
const { config } = await getConfig(cwd);
|
|
2048
|
-
const legacyConfig = config;
|
|
2049
|
-
const base = config?.registry ? fromUnifiedConfig(config.registry) : legacyConfig?.patch ? fromLegacyOptions({ patch: legacyConfig.patch }) : {};
|
|
2050
|
-
const merged = defu(overrides ?? {}, base);
|
|
2051
|
-
return merged;
|
|
2052
|
-
}
|
|
2053
|
-
function createCommandContext(cli, command, commandName, args, cwd) {
|
|
2054
|
-
let cachedOptions;
|
|
2055
|
-
let cachedPatcher;
|
|
2056
|
-
let cachedConfig;
|
|
2057
|
-
const loadPatchOptionsForContext = (overrides) => {
|
|
2058
|
-
if (overrides) {
|
|
2059
|
-
return loadPatchOptionsForCwd(cwd, overrides);
|
|
2060
|
-
}
|
|
2061
|
-
if (!cachedOptions) {
|
|
2062
|
-
cachedOptions = loadPatchOptionsForCwd(cwd);
|
|
2063
|
-
}
|
|
2064
|
-
return cachedOptions;
|
|
2065
|
-
};
|
|
2066
|
-
const createPatcherForContext = async (overrides) => {
|
|
2067
|
-
if (overrides) {
|
|
2068
|
-
const patchOptions = await loadPatchOptionsForCwd(cwd, overrides);
|
|
2069
|
-
return new TailwindcssPatcher(patchOptions);
|
|
2070
|
-
}
|
|
2071
|
-
if (!cachedPatcher) {
|
|
2072
|
-
cachedPatcher = loadPatchOptionsForContext().then((options) => new TailwindcssPatcher(options));
|
|
2073
|
-
}
|
|
2074
|
-
return cachedPatcher;
|
|
2075
|
-
};
|
|
2076
|
-
return {
|
|
2077
|
-
cli,
|
|
2078
|
-
command,
|
|
2079
|
-
commandName,
|
|
2080
|
-
args,
|
|
2081
|
-
cwd,
|
|
2082
|
-
logger: logger_default,
|
|
2083
|
-
loadConfig: () => {
|
|
2084
|
-
if (!cachedConfig) {
|
|
2085
|
-
cachedConfig = getConfig(cwd);
|
|
2086
|
-
}
|
|
2087
|
-
return cachedConfig;
|
|
2088
|
-
},
|
|
2089
|
-
loadPatchOptions: loadPatchOptionsForContext,
|
|
2090
|
-
createPatcher: createPatcherForContext
|
|
2091
|
-
};
|
|
2092
|
-
}
|
|
2093
|
-
function createCwdOptionDefinition(description = "Working directory") {
|
|
2094
|
-
return {
|
|
2095
|
-
flags: "--cwd <dir>",
|
|
2096
|
-
description,
|
|
2097
|
-
config: { default: process6.cwd() }
|
|
2098
|
-
};
|
|
2099
|
-
}
|
|
2100
|
-
function buildDefaultCommandDefinitions() {
|
|
2101
|
-
return {
|
|
2102
|
-
install: {
|
|
2103
|
-
description: "Apply Tailwind CSS runtime patches",
|
|
2104
|
-
optionDefs: [createCwdOptionDefinition()]
|
|
2105
|
-
},
|
|
2106
|
-
extract: {
|
|
2107
|
-
description: "Collect generated class names into a cache file",
|
|
2108
|
-
optionDefs: [
|
|
2109
|
-
createCwdOptionDefinition(),
|
|
2110
|
-
{ flags: "--output <file>", description: "Override output file path" },
|
|
2111
|
-
{ flags: "--format <format>", description: "Output format (json|lines)" },
|
|
2112
|
-
{ flags: "--css <file>", description: "Tailwind CSS entry CSS when using v4" },
|
|
2113
|
-
{ flags: "--no-write", description: "Skip writing to disk" }
|
|
2114
|
-
]
|
|
2115
|
-
},
|
|
2116
|
-
tokens: {
|
|
2117
|
-
description: "Extract Tailwind tokens with file/position metadata",
|
|
2118
|
-
optionDefs: [
|
|
2119
|
-
createCwdOptionDefinition(),
|
|
2120
|
-
{ flags: "--output <file>", description: "Override output file path", config: { default: DEFAULT_TOKEN_REPORT } },
|
|
2121
|
-
{
|
|
2122
|
-
flags: "--format <format>",
|
|
2123
|
-
description: "Output format (json|lines|grouped-json)",
|
|
2124
|
-
config: { default: "json" }
|
|
2125
|
-
},
|
|
2126
|
-
{
|
|
2127
|
-
flags: "--group-key <key>",
|
|
2128
|
-
description: "Grouping key for grouped-json output (relative|absolute)",
|
|
2129
|
-
config: { default: "relative" }
|
|
2130
|
-
},
|
|
2131
|
-
{ flags: "--no-write", description: "Skip writing to disk" }
|
|
2132
|
-
]
|
|
2133
|
-
},
|
|
2134
|
-
init: {
|
|
2135
|
-
description: "Generate a tailwindcss-patch config file",
|
|
2136
|
-
optionDefs: [createCwdOptionDefinition()]
|
|
2137
|
-
},
|
|
2138
|
-
status: {
|
|
2139
|
-
description: "Check which Tailwind patches are applied",
|
|
2140
|
-
optionDefs: [
|
|
2141
|
-
createCwdOptionDefinition(),
|
|
2142
|
-
{ flags: "--json", description: "Print a JSON report of patch status" }
|
|
2143
|
-
]
|
|
2144
|
-
}
|
|
2145
|
-
};
|
|
2146
|
-
}
|
|
2147
|
-
function addPrefixIfMissing(value, prefix) {
|
|
2148
|
-
if (!prefix || value.startsWith(prefix)) {
|
|
2149
|
-
return value;
|
|
2150
|
-
}
|
|
2151
|
-
return `${prefix}${value}`;
|
|
2152
|
-
}
|
|
2153
|
-
function resolveCommandNames(command, mountOptions, prefix) {
|
|
2154
|
-
const override = mountOptions.commandOptions?.[command];
|
|
2155
|
-
const baseName = override?.name ?? command;
|
|
2156
|
-
const name = addPrefixIfMissing(baseName, prefix);
|
|
2157
|
-
const aliases = (override?.aliases ?? []).map((alias) => addPrefixIfMissing(alias, prefix));
|
|
2158
|
-
return { name, aliases };
|
|
2159
|
-
}
|
|
2160
|
-
function resolveOptionDefinitions(defaults, override) {
|
|
2161
|
-
if (!override) {
|
|
2162
|
-
return defaults;
|
|
2163
|
-
}
|
|
2164
|
-
const appendDefaults = override.appendDefaultOptions ?? true;
|
|
2165
|
-
const customDefs = override.optionDefs ?? [];
|
|
2166
|
-
if (!appendDefaults) {
|
|
2167
|
-
return customDefs;
|
|
2168
|
-
}
|
|
2169
|
-
if (customDefs.length === 0) {
|
|
2170
|
-
return defaults;
|
|
2171
|
-
}
|
|
2172
|
-
return [...defaults, ...customDefs];
|
|
2173
|
-
}
|
|
2174
|
-
function applyCommandOptions(command, optionDefs) {
|
|
2175
|
-
for (const option of optionDefs) {
|
|
2176
|
-
command.option(option.flags, option.description ?? "", option.config);
|
|
2177
|
-
}
|
|
2178
|
-
}
|
|
2179
|
-
function runWithCommandHandler(cli, command, commandName, args, handler, defaultHandler) {
|
|
2180
|
-
const cwd = resolveCwd(args.cwd);
|
|
2181
|
-
const context = createCommandContext(cli, command, commandName, args, cwd);
|
|
2182
|
-
const runDefault = createDefaultRunner(() => defaultHandler(context));
|
|
2183
|
-
if (!handler) {
|
|
2184
|
-
return runDefault();
|
|
2185
|
-
}
|
|
2186
|
-
return handler(context, runDefault);
|
|
2187
|
-
}
|
|
2188
|
-
function resolveCommandMetadata(command, mountOptions, prefix, defaults) {
|
|
2189
|
-
const names = resolveCommandNames(command, mountOptions, prefix);
|
|
2190
|
-
const definition = defaults[command];
|
|
2191
|
-
const override = mountOptions.commandOptions?.[command];
|
|
2192
|
-
const description = override?.description ?? definition.description;
|
|
2193
|
-
const optionDefs = resolveOptionDefinitions(definition.optionDefs, override);
|
|
2194
|
-
return { ...names, description, optionDefs };
|
|
2195
|
-
}
|
|
2196
|
-
async function installCommandDefaultHandler(ctx) {
|
|
2197
|
-
const patcher = await ctx.createPatcher();
|
|
2198
|
-
await patcher.patch();
|
|
2199
|
-
logger_default.success("Tailwind CSS runtime patched successfully.");
|
|
2200
|
-
}
|
|
2201
|
-
async function extractCommandDefaultHandler(ctx) {
|
|
2202
|
-
const { args } = ctx;
|
|
2203
|
-
const overrides = {};
|
|
2204
|
-
let hasOverrides = false;
|
|
2205
|
-
if (args.output || args.format) {
|
|
2206
|
-
overrides.output = {
|
|
2207
|
-
file: args.output,
|
|
2208
|
-
format: args.format
|
|
2209
|
-
};
|
|
2210
|
-
hasOverrides = true;
|
|
2211
|
-
}
|
|
2212
|
-
if (args.css) {
|
|
2213
|
-
overrides.tailwind = {
|
|
2214
|
-
v4: {
|
|
2215
|
-
cssEntries: [args.css]
|
|
2216
|
-
}
|
|
2217
|
-
};
|
|
2218
|
-
hasOverrides = true;
|
|
2219
|
-
}
|
|
2220
|
-
const patcher = await ctx.createPatcher(hasOverrides ? overrides : void 0);
|
|
2221
|
-
const result = await patcher.extract({ write: args.write });
|
|
2222
|
-
if (result.filename) {
|
|
2223
|
-
logger_default.success(`Collected ${result.classList.length} classes \u2192 ${result.filename}`);
|
|
2224
|
-
} else {
|
|
2225
|
-
logger_default.success(`Collected ${result.classList.length} classes.`);
|
|
2226
|
-
}
|
|
2227
|
-
return result;
|
|
2228
|
-
}
|
|
2229
|
-
async function tokensCommandDefaultHandler(ctx) {
|
|
2230
|
-
const { args } = ctx;
|
|
2231
|
-
const patcher = await ctx.createPatcher();
|
|
2232
|
-
const report = await patcher.collectContentTokens();
|
|
2233
|
-
const shouldWrite = args.write ?? true;
|
|
2234
|
-
let format = args.format ?? "json";
|
|
2235
|
-
if (!TOKEN_FORMATS.includes(format)) {
|
|
2236
|
-
format = "json";
|
|
2237
|
-
}
|
|
2238
|
-
const targetFile = args.output ?? DEFAULT_TOKEN_REPORT;
|
|
2239
|
-
const groupKey = args.groupKey === "absolute" ? "absolute" : "relative";
|
|
2240
|
-
const buildGrouped = () => groupTokensByFile(report, {
|
|
2241
|
-
key: groupKey,
|
|
2242
|
-
stripAbsolutePaths: groupKey !== "absolute"
|
|
2243
|
-
});
|
|
2244
|
-
const grouped = format === "grouped-json" ? buildGrouped() : null;
|
|
2245
|
-
const resolveGrouped = () => grouped ?? buildGrouped();
|
|
2246
|
-
if (shouldWrite) {
|
|
2247
|
-
const target = path10.resolve(targetFile);
|
|
2248
|
-
await fs9.ensureDir(path10.dirname(target));
|
|
2249
|
-
if (format === "json") {
|
|
2250
|
-
await fs9.writeJSON(target, report, { spaces: 2 });
|
|
2251
|
-
} else if (format === "grouped-json") {
|
|
2252
|
-
await fs9.writeJSON(target, resolveGrouped(), { spaces: 2 });
|
|
2253
|
-
} else {
|
|
2254
|
-
const lines = report.entries.map(formatTokenLine);
|
|
2255
|
-
await fs9.writeFile(target, `${lines.join("\n")}
|
|
2256
|
-
`, "utf8");
|
|
2257
|
-
}
|
|
2258
|
-
logger_default.success(`Collected ${report.entries.length} tokens (${format}) \u2192 ${target.replace(process6.cwd(), ".")}`);
|
|
2259
|
-
} else {
|
|
2260
|
-
logger_default.success(`Collected ${report.entries.length} tokens from ${report.filesScanned} files.`);
|
|
2261
|
-
if (format === "lines") {
|
|
2262
|
-
const preview = report.entries.slice(0, 5).map(formatTokenLine).join("\n");
|
|
2263
|
-
if (preview) {
|
|
2264
|
-
logger_default.log("");
|
|
2265
|
-
logger_default.info(preview);
|
|
2266
|
-
if (report.entries.length > 5) {
|
|
2267
|
-
logger_default.info(`\u2026and ${report.entries.length - 5} more.`);
|
|
2268
|
-
}
|
|
2269
|
-
}
|
|
2270
|
-
} else if (format === "grouped-json") {
|
|
2271
|
-
const map = resolveGrouped();
|
|
2272
|
-
const { preview, moreFiles } = formatGroupedPreview(map);
|
|
2273
|
-
if (preview) {
|
|
2274
|
-
logger_default.log("");
|
|
2275
|
-
logger_default.info(preview);
|
|
2276
|
-
if (moreFiles > 0) {
|
|
2277
|
-
logger_default.info(`\u2026and ${moreFiles} more files.`);
|
|
2278
|
-
}
|
|
2279
|
-
}
|
|
2280
|
-
} else {
|
|
2281
|
-
const previewEntries = report.entries.slice(0, 3);
|
|
2282
|
-
if (previewEntries.length) {
|
|
2283
|
-
logger_default.log("");
|
|
2284
|
-
logger_default.info(JSON.stringify(previewEntries, null, 2));
|
|
2285
|
-
}
|
|
2286
|
-
}
|
|
2287
|
-
}
|
|
2288
|
-
if (report.skippedFiles.length) {
|
|
2289
|
-
logger_default.warn("Skipped files:");
|
|
2290
|
-
for (const skipped of report.skippedFiles) {
|
|
2291
|
-
logger_default.warn(` \u2022 ${skipped.file} (${skipped.reason})`);
|
|
2292
|
-
}
|
|
2293
|
-
}
|
|
2294
|
-
return report;
|
|
2295
|
-
}
|
|
2296
|
-
async function initCommandDefaultHandler(ctx) {
|
|
2297
|
-
await initConfig(ctx.cwd);
|
|
2298
|
-
logger_default.success(`\u2728 ${CONFIG_NAME}.config.ts initialized!`);
|
|
2299
|
-
}
|
|
2300
|
-
function formatFilesHint(entry) {
|
|
2301
|
-
if (!entry.files.length) {
|
|
2302
|
-
return "";
|
|
2303
|
-
}
|
|
2304
|
-
return ` (${entry.files.join(", ")})`;
|
|
2305
|
-
}
|
|
2306
|
-
async function statusCommandDefaultHandler(ctx) {
|
|
2307
|
-
const patcher = await ctx.createPatcher();
|
|
2308
|
-
const report = await patcher.getPatchStatus();
|
|
2309
|
-
if (ctx.args.json) {
|
|
2310
|
-
logger_default.log(JSON.stringify(report, null, 2));
|
|
2311
|
-
return report;
|
|
2312
|
-
}
|
|
2313
|
-
const applied = report.entries.filter((entry) => entry.status === "applied");
|
|
2314
|
-
const pending = report.entries.filter((entry) => entry.status === "not-applied");
|
|
2315
|
-
const skipped = report.entries.filter((entry) => entry.status === "skipped" || entry.status === "unsupported");
|
|
2316
|
-
const packageLabel = `${report.package.name ?? "tailwindcss"}@${report.package.version ?? "unknown"}`;
|
|
2317
|
-
logger_default.info(`Patch status for ${packageLabel} (v${report.majorVersion})`);
|
|
2318
|
-
if (applied.length) {
|
|
2319
|
-
logger_default.success("Applied:");
|
|
2320
|
-
applied.forEach((entry) => logger_default.success(` \u2022 ${entry.name}${formatFilesHint(entry)}`));
|
|
2321
|
-
}
|
|
2322
|
-
if (pending.length) {
|
|
2323
|
-
logger_default.warn("Needs attention:");
|
|
2324
|
-
pending.forEach((entry) => {
|
|
2325
|
-
const details = entry.reason ? ` \u2013 ${entry.reason}` : "";
|
|
2326
|
-
logger_default.warn(` \u2022 ${entry.name}${formatFilesHint(entry)}${details}`);
|
|
2327
|
-
});
|
|
2328
|
-
} else {
|
|
2329
|
-
logger_default.success("All applicable patches are applied.");
|
|
2330
|
-
}
|
|
2331
|
-
if (skipped.length) {
|
|
2332
|
-
logger_default.info("Skipped:");
|
|
2333
|
-
skipped.forEach((entry) => {
|
|
2334
|
-
const details = entry.reason ? ` \u2013 ${entry.reason}` : "";
|
|
2335
|
-
logger_default.info(` \u2022 ${entry.name}${details}`);
|
|
2336
|
-
});
|
|
2337
|
-
}
|
|
2338
|
-
return report;
|
|
2339
|
-
}
|
|
2340
|
-
function mountTailwindcssPatchCommands(cli, options = {}) {
|
|
2341
|
-
const prefix = options.commandPrefix ?? "";
|
|
2342
|
-
const selectedCommands = options.commands ?? tailwindcssPatchCommands;
|
|
2343
|
-
const defaultDefinitions = buildDefaultCommandDefinitions();
|
|
2344
|
-
const registrars = {
|
|
2345
|
-
install: () => {
|
|
2346
|
-
const metadata = resolveCommandMetadata("install", options, prefix, defaultDefinitions);
|
|
2347
|
-
const command = cli.command(metadata.name, metadata.description);
|
|
2348
|
-
applyCommandOptions(command, metadata.optionDefs);
|
|
2349
|
-
command.action(async (args) => {
|
|
2350
|
-
return runWithCommandHandler(
|
|
2351
|
-
cli,
|
|
2352
|
-
command,
|
|
2353
|
-
"install",
|
|
2354
|
-
args,
|
|
2355
|
-
options.commandHandlers?.install,
|
|
2356
|
-
installCommandDefaultHandler
|
|
2357
|
-
);
|
|
2358
|
-
});
|
|
2359
|
-
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
2360
|
-
},
|
|
2361
|
-
extract: () => {
|
|
2362
|
-
const metadata = resolveCommandMetadata("extract", options, prefix, defaultDefinitions);
|
|
2363
|
-
const command = cli.command(metadata.name, metadata.description);
|
|
2364
|
-
applyCommandOptions(command, metadata.optionDefs);
|
|
2365
|
-
command.action(async (args) => {
|
|
2366
|
-
return runWithCommandHandler(
|
|
2367
|
-
cli,
|
|
2368
|
-
command,
|
|
2369
|
-
"extract",
|
|
2370
|
-
args,
|
|
2371
|
-
options.commandHandlers?.extract,
|
|
2372
|
-
extractCommandDefaultHandler
|
|
2373
|
-
);
|
|
2374
|
-
});
|
|
2375
|
-
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
2376
|
-
},
|
|
2377
|
-
tokens: () => {
|
|
2378
|
-
const metadata = resolveCommandMetadata("tokens", options, prefix, defaultDefinitions);
|
|
2379
|
-
const command = cli.command(metadata.name, metadata.description);
|
|
2380
|
-
applyCommandOptions(command, metadata.optionDefs);
|
|
2381
|
-
command.action(async (args) => {
|
|
2382
|
-
return runWithCommandHandler(
|
|
2383
|
-
cli,
|
|
2384
|
-
command,
|
|
2385
|
-
"tokens",
|
|
2386
|
-
args,
|
|
2387
|
-
options.commandHandlers?.tokens,
|
|
2388
|
-
tokensCommandDefaultHandler
|
|
2389
|
-
);
|
|
2390
|
-
});
|
|
2391
|
-
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
2392
|
-
},
|
|
2393
|
-
init: () => {
|
|
2394
|
-
const metadata = resolveCommandMetadata("init", options, prefix, defaultDefinitions);
|
|
2395
|
-
const command = cli.command(metadata.name, metadata.description);
|
|
2396
|
-
applyCommandOptions(command, metadata.optionDefs);
|
|
2397
|
-
command.action(async (args) => {
|
|
2398
|
-
return runWithCommandHandler(
|
|
2399
|
-
cli,
|
|
2400
|
-
command,
|
|
2401
|
-
"init",
|
|
2402
|
-
args,
|
|
2403
|
-
options.commandHandlers?.init,
|
|
2404
|
-
initCommandDefaultHandler
|
|
2405
|
-
);
|
|
2406
|
-
});
|
|
2407
|
-
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
2408
|
-
},
|
|
2409
|
-
status: () => {
|
|
2410
|
-
const metadata = resolveCommandMetadata("status", options, prefix, defaultDefinitions);
|
|
2411
|
-
const command = cli.command(metadata.name, metadata.description);
|
|
2412
|
-
applyCommandOptions(command, metadata.optionDefs);
|
|
2413
|
-
command.action(async (args) => {
|
|
2414
|
-
return runWithCommandHandler(
|
|
2415
|
-
cli,
|
|
2416
|
-
command,
|
|
2417
|
-
"status",
|
|
2418
|
-
args,
|
|
2419
|
-
options.commandHandlers?.status,
|
|
2420
|
-
statusCommandDefaultHandler
|
|
2421
|
-
);
|
|
2422
|
-
});
|
|
2423
|
-
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
2424
|
-
}
|
|
2425
|
-
};
|
|
2426
|
-
for (const name of selectedCommands) {
|
|
2427
|
-
const register = registrars[name];
|
|
2428
|
-
if (register) {
|
|
2429
|
-
register();
|
|
2430
|
-
}
|
|
2431
|
-
}
|
|
2432
|
-
return cli;
|
|
2433
|
-
}
|
|
2434
|
-
function createTailwindcssPatchCli(options = {}) {
|
|
2435
|
-
const cli = cac(options.name ?? "tw-patch");
|
|
2436
|
-
mountTailwindcssPatchCommands(cli, options.mountOptions);
|
|
2437
|
-
return cli;
|
|
2438
|
-
}
|
|
2439
|
-
|
|
2440
|
-
export {
|
|
2441
|
-
logger_default,
|
|
2442
|
-
CacheStore,
|
|
2443
|
-
extractRawCandidatesWithPositions,
|
|
2444
|
-
extractRawCandidates,
|
|
2445
|
-
extractValidCandidates,
|
|
2446
|
-
extractProjectCandidatesWithPositions,
|
|
2447
|
-
groupTokensByFile,
|
|
2448
|
-
normalizeOptions,
|
|
2449
|
-
getPatchStatusReport,
|
|
2450
|
-
collectClassesFromContexts,
|
|
2451
|
-
collectClassesFromTailwindV4,
|
|
2452
|
-
loadRuntimeContexts,
|
|
2453
|
-
runTailwindBuild,
|
|
2454
|
-
TailwindcssPatcher,
|
|
2455
|
-
tailwindcssPatchCommands,
|
|
2456
|
-
mountTailwindcssPatchCommands,
|
|
2457
|
-
createTailwindcssPatchCli
|
|
2458
|
-
};
|