tailwindcss-patch 8.2.4 → 8.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -6
- package/dist/{chunk-OQKHXYAK.js → chunk-PF5ZTTYM.js} +611 -144
- package/dist/{chunk-4BI6FMCK.mjs → chunk-SHYTHM5B.mjs} +590 -123
- package/dist/cli.js +3 -233
- package/dist/cli.mjs +3 -233
- package/dist/index.d.mts +79 -2
- package/dist/index.d.ts +79 -2
- package/dist/index.js +8 -2
- package/dist/index.mjs +9 -3
- package/package.json +2 -2
|
@@ -285,105 +285,6 @@ function groupTokensByFile(report, options) {
|
|
|
285
285
|
}, {});
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
-
// src/options/legacy.ts
|
|
289
|
-
function normalizeLegacyFeatures(patch) {
|
|
290
|
-
const apply = patch?.applyPatches;
|
|
291
|
-
const extend = apply?.extendLengthUnits;
|
|
292
|
-
let extendOption = false;
|
|
293
|
-
if (extend && typeof extend === "object") {
|
|
294
|
-
extendOption = {
|
|
295
|
-
...extend,
|
|
296
|
-
enabled: true
|
|
297
|
-
};
|
|
298
|
-
} else if (extend === true) {
|
|
299
|
-
extendOption = {
|
|
300
|
-
enabled: true,
|
|
301
|
-
units: ["rpx"],
|
|
302
|
-
overwrite: patch?.overwrite
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
return {
|
|
306
|
-
exposeContext: apply?.exportContext ?? true,
|
|
307
|
-
extendLengthUnits: extendOption
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
function fromLegacyOptions(options) {
|
|
311
|
-
if (!options) {
|
|
312
|
-
return {};
|
|
313
|
-
}
|
|
314
|
-
const patch = options.patch;
|
|
315
|
-
const features = normalizeLegacyFeatures(patch);
|
|
316
|
-
const output = patch?.output;
|
|
317
|
-
const tailwindConfig = patch?.tailwindcss;
|
|
318
|
-
const tailwindVersion = tailwindConfig?.version;
|
|
319
|
-
const tailwindV2 = tailwindConfig?.v2;
|
|
320
|
-
const tailwindV3 = tailwindConfig?.v3;
|
|
321
|
-
const tailwindV4 = tailwindConfig?.v4;
|
|
322
|
-
const tailwindConfigPath = tailwindV3?.config ?? tailwindV2?.config;
|
|
323
|
-
const tailwindCwd = tailwindV3?.cwd ?? tailwindV2?.cwd ?? patch?.cwd;
|
|
324
|
-
return {
|
|
325
|
-
cwd: patch?.cwd,
|
|
326
|
-
overwrite: patch?.overwrite,
|
|
327
|
-
filter: patch?.filter,
|
|
328
|
-
cache: typeof options.cache === "boolean" ? options.cache : options.cache ? {
|
|
329
|
-
...options.cache,
|
|
330
|
-
enabled: options.cache.enabled ?? true
|
|
331
|
-
} : void 0,
|
|
332
|
-
output: output ? {
|
|
333
|
-
file: output.filename,
|
|
334
|
-
pretty: output.loose ? 2 : false,
|
|
335
|
-
removeUniversalSelector: output.removeUniversalSelector
|
|
336
|
-
} : void 0,
|
|
337
|
-
tailwind: {
|
|
338
|
-
packageName: patch?.packageName,
|
|
339
|
-
version: tailwindVersion,
|
|
340
|
-
resolve: patch?.resolve,
|
|
341
|
-
config: tailwindConfigPath,
|
|
342
|
-
cwd: tailwindCwd,
|
|
343
|
-
v2: tailwindV2,
|
|
344
|
-
v3: tailwindV3,
|
|
345
|
-
v4: tailwindV4
|
|
346
|
-
},
|
|
347
|
-
features: {
|
|
348
|
-
exposeContext: features.exposeContext,
|
|
349
|
-
extendLengthUnits: features.extendLengthUnits
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
function fromUnifiedConfig(registry) {
|
|
354
|
-
if (!registry) {
|
|
355
|
-
return {};
|
|
356
|
-
}
|
|
357
|
-
const tailwind = registry.tailwind;
|
|
358
|
-
const output = registry.output;
|
|
359
|
-
const pretty = (() => {
|
|
360
|
-
if (output?.pretty === void 0) {
|
|
361
|
-
return void 0;
|
|
362
|
-
}
|
|
363
|
-
if (typeof output.pretty === "boolean") {
|
|
364
|
-
return output.pretty ? 2 : false;
|
|
365
|
-
}
|
|
366
|
-
return output.pretty;
|
|
367
|
-
})();
|
|
368
|
-
return {
|
|
369
|
-
output: output ? {
|
|
370
|
-
file: output.file,
|
|
371
|
-
pretty,
|
|
372
|
-
removeUniversalSelector: output.stripUniversalSelector
|
|
373
|
-
} : void 0,
|
|
374
|
-
tailwind: tailwind ? {
|
|
375
|
-
version: tailwind.version,
|
|
376
|
-
packageName: tailwind.package,
|
|
377
|
-
resolve: tailwind.resolve,
|
|
378
|
-
config: tailwind.config,
|
|
379
|
-
cwd: tailwind.cwd,
|
|
380
|
-
v2: tailwind.legacy,
|
|
381
|
-
v3: tailwind.classic,
|
|
382
|
-
v4: tailwind.next
|
|
383
|
-
} : void 0
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
|
|
387
288
|
// src/options/normalize.ts
|
|
388
289
|
import process2 from "process";
|
|
389
290
|
import path2 from "pathe";
|
|
@@ -761,6 +662,105 @@ import { getPackageInfoSync } from "local-pkg";
|
|
|
761
662
|
import path8 from "pathe";
|
|
762
663
|
import { coerce } from "semver";
|
|
763
664
|
|
|
665
|
+
// src/options/legacy.ts
|
|
666
|
+
function normalizeLegacyFeatures(patch) {
|
|
667
|
+
const apply = patch?.applyPatches;
|
|
668
|
+
const extend = apply?.extendLengthUnits;
|
|
669
|
+
let extendOption = false;
|
|
670
|
+
if (extend && typeof extend === "object") {
|
|
671
|
+
extendOption = {
|
|
672
|
+
...extend,
|
|
673
|
+
enabled: true
|
|
674
|
+
};
|
|
675
|
+
} else if (extend === true) {
|
|
676
|
+
extendOption = {
|
|
677
|
+
enabled: true,
|
|
678
|
+
units: ["rpx"],
|
|
679
|
+
overwrite: patch?.overwrite
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
return {
|
|
683
|
+
exposeContext: apply?.exportContext ?? true,
|
|
684
|
+
extendLengthUnits: extendOption
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
function fromLegacyOptions(options) {
|
|
688
|
+
if (!options) {
|
|
689
|
+
return {};
|
|
690
|
+
}
|
|
691
|
+
const patch = options.patch;
|
|
692
|
+
const features = normalizeLegacyFeatures(patch);
|
|
693
|
+
const output = patch?.output;
|
|
694
|
+
const tailwindConfig = patch?.tailwindcss;
|
|
695
|
+
const tailwindVersion = tailwindConfig?.version;
|
|
696
|
+
const tailwindV2 = tailwindConfig?.v2;
|
|
697
|
+
const tailwindV3 = tailwindConfig?.v3;
|
|
698
|
+
const tailwindV4 = tailwindConfig?.v4;
|
|
699
|
+
const tailwindConfigPath = tailwindV3?.config ?? tailwindV2?.config;
|
|
700
|
+
const tailwindCwd = tailwindV3?.cwd ?? tailwindV2?.cwd ?? patch?.cwd;
|
|
701
|
+
return {
|
|
702
|
+
cwd: patch?.cwd,
|
|
703
|
+
overwrite: patch?.overwrite,
|
|
704
|
+
filter: patch?.filter,
|
|
705
|
+
cache: typeof options.cache === "boolean" ? options.cache : options.cache ? {
|
|
706
|
+
...options.cache,
|
|
707
|
+
enabled: options.cache.enabled ?? true
|
|
708
|
+
} : void 0,
|
|
709
|
+
output: output ? {
|
|
710
|
+
file: output.filename,
|
|
711
|
+
pretty: output.loose ? 2 : false,
|
|
712
|
+
removeUniversalSelector: output.removeUniversalSelector
|
|
713
|
+
} : void 0,
|
|
714
|
+
tailwind: {
|
|
715
|
+
packageName: patch?.packageName,
|
|
716
|
+
version: tailwindVersion,
|
|
717
|
+
resolve: patch?.resolve,
|
|
718
|
+
config: tailwindConfigPath,
|
|
719
|
+
cwd: tailwindCwd,
|
|
720
|
+
v2: tailwindV2,
|
|
721
|
+
v3: tailwindV3,
|
|
722
|
+
v4: tailwindV4
|
|
723
|
+
},
|
|
724
|
+
features: {
|
|
725
|
+
exposeContext: features.exposeContext,
|
|
726
|
+
extendLengthUnits: features.extendLengthUnits
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
function fromUnifiedConfig(registry) {
|
|
731
|
+
if (!registry) {
|
|
732
|
+
return {};
|
|
733
|
+
}
|
|
734
|
+
const tailwind = registry.tailwind;
|
|
735
|
+
const output = registry.output;
|
|
736
|
+
const pretty = (() => {
|
|
737
|
+
if (output?.pretty === void 0) {
|
|
738
|
+
return void 0;
|
|
739
|
+
}
|
|
740
|
+
if (typeof output.pretty === "boolean") {
|
|
741
|
+
return output.pretty ? 2 : false;
|
|
742
|
+
}
|
|
743
|
+
return output.pretty;
|
|
744
|
+
})();
|
|
745
|
+
return {
|
|
746
|
+
output: output ? {
|
|
747
|
+
file: output.file,
|
|
748
|
+
pretty,
|
|
749
|
+
removeUniversalSelector: output.stripUniversalSelector
|
|
750
|
+
} : void 0,
|
|
751
|
+
tailwind: tailwind ? {
|
|
752
|
+
version: tailwind.version,
|
|
753
|
+
packageName: tailwind.package,
|
|
754
|
+
resolve: tailwind.resolve,
|
|
755
|
+
config: tailwind.config,
|
|
756
|
+
cwd: tailwind.cwd,
|
|
757
|
+
v2: tailwind.legacy,
|
|
758
|
+
v3: tailwind.classic,
|
|
759
|
+
v4: tailwind.next
|
|
760
|
+
} : void 0
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
|
|
764
764
|
// src/patching/operations/export-context/index.ts
|
|
765
765
|
import fs5 from "fs-extra";
|
|
766
766
|
import path6 from "pathe";
|
|
@@ -802,8 +802,8 @@ function transformProcessTailwindFeaturesReturnContextV2(content) {
|
|
|
802
802
|
});
|
|
803
803
|
let hasPatched = false;
|
|
804
804
|
traverse(ast, {
|
|
805
|
-
FunctionDeclaration(
|
|
806
|
-
const node =
|
|
805
|
+
FunctionDeclaration(path10) {
|
|
806
|
+
const node = path10.node;
|
|
807
807
|
if (node.id?.name !== "processTailwindFeatures" || node.body.body.length !== 1 || !t.isReturnStatement(node.body.body[0])) {
|
|
808
808
|
return;
|
|
809
809
|
}
|
|
@@ -834,8 +834,8 @@ function transformPostcssPluginV2(content, options) {
|
|
|
834
834
|
const ast = parse(content);
|
|
835
835
|
let hasPatched = false;
|
|
836
836
|
traverse(ast, {
|
|
837
|
-
Program(
|
|
838
|
-
const program =
|
|
837
|
+
Program(path10) {
|
|
838
|
+
const program = path10.node;
|
|
839
839
|
const index = program.body.findIndex((statement) => {
|
|
840
840
|
return t.isFunctionDeclaration(statement) && statement.id?.name === "_default";
|
|
841
841
|
});
|
|
@@ -869,11 +869,11 @@ function transformPostcssPluginV2(content, options) {
|
|
|
869
869
|
);
|
|
870
870
|
}
|
|
871
871
|
},
|
|
872
|
-
FunctionDeclaration(
|
|
872
|
+
FunctionDeclaration(path10) {
|
|
873
873
|
if (hasPatched) {
|
|
874
874
|
return;
|
|
875
875
|
}
|
|
876
|
-
const fn =
|
|
876
|
+
const fn = path10.node;
|
|
877
877
|
if (fn.id?.name !== "_default") {
|
|
878
878
|
return;
|
|
879
879
|
}
|
|
@@ -962,8 +962,8 @@ function transformProcessTailwindFeaturesReturnContext(content) {
|
|
|
962
962
|
const ast = parse(content);
|
|
963
963
|
let hasPatched = false;
|
|
964
964
|
traverse(ast, {
|
|
965
|
-
FunctionDeclaration(
|
|
966
|
-
const node =
|
|
965
|
+
FunctionDeclaration(path10) {
|
|
966
|
+
const node = path10.node;
|
|
967
967
|
if (node.id?.name !== "processTailwindFeatures" || node.body.body.length !== 1) {
|
|
968
968
|
return;
|
|
969
969
|
}
|
|
@@ -995,8 +995,8 @@ function transformPostcssPlugin(content, { refProperty }) {
|
|
|
995
995
|
const valueMember = t2.memberExpression(refIdentifier, t2.identifier("value"));
|
|
996
996
|
let hasPatched = false;
|
|
997
997
|
traverse(ast, {
|
|
998
|
-
Program(
|
|
999
|
-
const program =
|
|
998
|
+
Program(path10) {
|
|
999
|
+
const program = path10.node;
|
|
1000
1000
|
const index = program.body.findIndex((statement) => {
|
|
1001
1001
|
return t2.isExpressionStatement(statement) && t2.isAssignmentExpression(statement.expression) && t2.isMemberExpression(statement.expression.left) && t2.isFunctionExpression(statement.expression.right) && statement.expression.right.id?.name === "tailwindcss";
|
|
1002
1002
|
});
|
|
@@ -1034,11 +1034,11 @@ function transformPostcssPlugin(content, { refProperty }) {
|
|
|
1034
1034
|
);
|
|
1035
1035
|
}
|
|
1036
1036
|
},
|
|
1037
|
-
FunctionExpression(
|
|
1037
|
+
FunctionExpression(path10) {
|
|
1038
1038
|
if (hasPatched) {
|
|
1039
1039
|
return;
|
|
1040
1040
|
}
|
|
1041
|
-
const fn =
|
|
1041
|
+
const fn = path10.node;
|
|
1042
1042
|
if (fn.id?.name !== "tailwindcss" || fn.body.body.length !== 1) {
|
|
1043
1043
|
return;
|
|
1044
1044
|
}
|
|
@@ -1204,21 +1204,21 @@ function updateLengthUnitsArray(content, options) {
|
|
|
1204
1204
|
let arrayRef;
|
|
1205
1205
|
let changed = false;
|
|
1206
1206
|
traverse(ast, {
|
|
1207
|
-
Identifier(
|
|
1208
|
-
if (
|
|
1209
|
-
arrayRef =
|
|
1207
|
+
Identifier(path10) {
|
|
1208
|
+
if (path10.node.name === variableName && t3.isVariableDeclarator(path10.parent) && t3.isArrayExpression(path10.parent.init)) {
|
|
1209
|
+
arrayRef = path10.parent.init;
|
|
1210
1210
|
const existing = new Set(
|
|
1211
|
-
|
|
1211
|
+
path10.parent.init.elements.map((element) => t3.isStringLiteral(element) ? element.value : void 0).filter(Boolean)
|
|
1212
1212
|
);
|
|
1213
1213
|
for (const unit of units) {
|
|
1214
1214
|
if (!existing.has(unit)) {
|
|
1215
|
-
|
|
1215
|
+
path10.parent.init.elements = path10.parent.init.elements.map((element) => {
|
|
1216
1216
|
if (t3.isStringLiteral(element)) {
|
|
1217
1217
|
return t3.stringLiteral(element.value);
|
|
1218
1218
|
}
|
|
1219
1219
|
return element;
|
|
1220
1220
|
});
|
|
1221
|
-
|
|
1221
|
+
path10.parent.init.elements.push(t3.stringLiteral(unit));
|
|
1222
1222
|
changed = true;
|
|
1223
1223
|
}
|
|
1224
1224
|
}
|
|
@@ -1299,13 +1299,13 @@ function applyExtendLengthUnitsPatchV4(rootDir, options) {
|
|
|
1299
1299
|
const { code, file, match } = item;
|
|
1300
1300
|
const ast = parse(match[0], { sourceType: "unambiguous" });
|
|
1301
1301
|
traverse(ast, {
|
|
1302
|
-
ArrayExpression(
|
|
1302
|
+
ArrayExpression(path10) {
|
|
1303
1303
|
for (const unit of opts.units) {
|
|
1304
|
-
if (
|
|
1304
|
+
if (path10.node.elements.some((element) => t3.isStringLiteral(element) && element.value === unit)) {
|
|
1305
1305
|
item.hasPatched = true;
|
|
1306
1306
|
return;
|
|
1307
1307
|
}
|
|
1308
|
-
|
|
1308
|
+
path10.node.elements.push(t3.stringLiteral(unit));
|
|
1309
1309
|
}
|
|
1310
1310
|
}
|
|
1311
1311
|
});
|
|
@@ -1558,6 +1558,472 @@ var TailwindcssPatcher = class {
|
|
|
1558
1558
|
}
|
|
1559
1559
|
};
|
|
1560
1560
|
|
|
1561
|
+
// src/cli/commands.ts
|
|
1562
|
+
import process5 from "process";
|
|
1563
|
+
import { CONFIG_NAME, getConfig, initConfig } from "@tailwindcss-mangle/config";
|
|
1564
|
+
|
|
1565
|
+
// ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
|
|
1566
|
+
function isPlainObject(value) {
|
|
1567
|
+
if (value === null || typeof value !== "object") {
|
|
1568
|
+
return false;
|
|
1569
|
+
}
|
|
1570
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1571
|
+
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
|
|
1572
|
+
return false;
|
|
1573
|
+
}
|
|
1574
|
+
if (Symbol.iterator in value) {
|
|
1575
|
+
return false;
|
|
1576
|
+
}
|
|
1577
|
+
if (Symbol.toStringTag in value) {
|
|
1578
|
+
return Object.prototype.toString.call(value) === "[object Module]";
|
|
1579
|
+
}
|
|
1580
|
+
return true;
|
|
1581
|
+
}
|
|
1582
|
+
function _defu(baseObject, defaults, namespace = ".", merger) {
|
|
1583
|
+
if (!isPlainObject(defaults)) {
|
|
1584
|
+
return _defu(baseObject, {}, namespace, merger);
|
|
1585
|
+
}
|
|
1586
|
+
const object = Object.assign({}, defaults);
|
|
1587
|
+
for (const key in baseObject) {
|
|
1588
|
+
if (key === "__proto__" || key === "constructor") {
|
|
1589
|
+
continue;
|
|
1590
|
+
}
|
|
1591
|
+
const value = baseObject[key];
|
|
1592
|
+
if (value === null || value === void 0) {
|
|
1593
|
+
continue;
|
|
1594
|
+
}
|
|
1595
|
+
if (merger && merger(object, key, value, namespace)) {
|
|
1596
|
+
continue;
|
|
1597
|
+
}
|
|
1598
|
+
if (Array.isArray(value) && Array.isArray(object[key])) {
|
|
1599
|
+
object[key] = [...value, ...object[key]];
|
|
1600
|
+
} else if (isPlainObject(value) && isPlainObject(object[key])) {
|
|
1601
|
+
object[key] = _defu(
|
|
1602
|
+
value,
|
|
1603
|
+
object[key],
|
|
1604
|
+
(namespace ? `${namespace}.` : "") + key.toString(),
|
|
1605
|
+
merger
|
|
1606
|
+
);
|
|
1607
|
+
} else {
|
|
1608
|
+
object[key] = value;
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
return object;
|
|
1612
|
+
}
|
|
1613
|
+
function createDefu(merger) {
|
|
1614
|
+
return (...arguments_) => (
|
|
1615
|
+
// eslint-disable-next-line unicorn/no-array-reduce
|
|
1616
|
+
arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
|
|
1617
|
+
);
|
|
1618
|
+
}
|
|
1619
|
+
var defu = createDefu();
|
|
1620
|
+
var defuFn = createDefu((object, key, currentValue) => {
|
|
1621
|
+
if (object[key] !== void 0 && typeof currentValue === "function") {
|
|
1622
|
+
object[key] = currentValue(object[key]);
|
|
1623
|
+
return true;
|
|
1624
|
+
}
|
|
1625
|
+
});
|
|
1626
|
+
var defuArrayFn = createDefu((object, key, currentValue) => {
|
|
1627
|
+
if (Array.isArray(object[key]) && typeof currentValue === "function") {
|
|
1628
|
+
object[key] = currentValue(object[key]);
|
|
1629
|
+
return true;
|
|
1630
|
+
}
|
|
1631
|
+
});
|
|
1632
|
+
|
|
1633
|
+
// ../shared/src/utils.ts
|
|
1634
|
+
var defuOverrideArray = createDefu((obj, key, value) => {
|
|
1635
|
+
if (Array.isArray(obj[key]) && Array.isArray(value)) {
|
|
1636
|
+
obj[key] = value;
|
|
1637
|
+
return true;
|
|
1638
|
+
}
|
|
1639
|
+
});
|
|
1640
|
+
var preserveClassNames = [
|
|
1641
|
+
// https://tailwindcss.com/docs/transition-timing-function start
|
|
1642
|
+
// https://github.com/sonofmagic/tailwindcss-mangle/issues/21
|
|
1643
|
+
"ease-out",
|
|
1644
|
+
"ease-linear",
|
|
1645
|
+
"ease-in",
|
|
1646
|
+
"ease-in-out"
|
|
1647
|
+
// https://tailwindcss.com/docs/transition-timing-function end
|
|
1648
|
+
];
|
|
1649
|
+
var preserveClassNamesMap = preserveClassNames.reduce((acc, cur) => {
|
|
1650
|
+
acc[cur] = true;
|
|
1651
|
+
return acc;
|
|
1652
|
+
}, {});
|
|
1653
|
+
var acceptChars = [..."abcdefghijklmnopqrstuvwxyz"];
|
|
1654
|
+
|
|
1655
|
+
// src/cli/commands.ts
|
|
1656
|
+
import cac from "cac";
|
|
1657
|
+
import fs8 from "fs-extra";
|
|
1658
|
+
import path9 from "pathe";
|
|
1659
|
+
var tailwindcssPatchCommands = ["install", "extract", "tokens", "init"];
|
|
1660
|
+
var TOKEN_FORMATS = ["json", "lines", "grouped-json"];
|
|
1661
|
+
var DEFAULT_TOKEN_REPORT = ".tw-patch/tw-token-report.json";
|
|
1662
|
+
function formatTokenLine(entry) {
|
|
1663
|
+
return `${entry.relativeFile}:${entry.line}:${entry.column} ${entry.rawCandidate} (${entry.start}-${entry.end})`;
|
|
1664
|
+
}
|
|
1665
|
+
function formatGroupedPreview(map, limit = 3) {
|
|
1666
|
+
const files = Object.keys(map);
|
|
1667
|
+
if (!files.length) {
|
|
1668
|
+
return { preview: "", moreFiles: 0 };
|
|
1669
|
+
}
|
|
1670
|
+
const lines = files.slice(0, limit).map((file) => {
|
|
1671
|
+
const tokens = map[file];
|
|
1672
|
+
const sample = tokens.slice(0, 3).map((token) => token.rawCandidate).join(", ");
|
|
1673
|
+
const suffix = tokens.length > 3 ? ", \u2026" : "";
|
|
1674
|
+
return `${file}: ${tokens.length} tokens (${sample}${suffix})`;
|
|
1675
|
+
});
|
|
1676
|
+
return {
|
|
1677
|
+
preview: lines.join("\n"),
|
|
1678
|
+
moreFiles: Math.max(0, files.length - limit)
|
|
1679
|
+
};
|
|
1680
|
+
}
|
|
1681
|
+
function resolveCwd(rawCwd) {
|
|
1682
|
+
if (!rawCwd) {
|
|
1683
|
+
return process5.cwd();
|
|
1684
|
+
}
|
|
1685
|
+
return path9.resolve(rawCwd);
|
|
1686
|
+
}
|
|
1687
|
+
function createDefaultRunner(factory) {
|
|
1688
|
+
let promise;
|
|
1689
|
+
return () => {
|
|
1690
|
+
if (!promise) {
|
|
1691
|
+
promise = factory();
|
|
1692
|
+
}
|
|
1693
|
+
return promise;
|
|
1694
|
+
};
|
|
1695
|
+
}
|
|
1696
|
+
async function loadPatchOptionsForCwd(cwd, overrides) {
|
|
1697
|
+
const { config } = await getConfig(cwd);
|
|
1698
|
+
const legacyConfig = config;
|
|
1699
|
+
const base = config?.registry ? fromUnifiedConfig(config.registry) : legacyConfig?.patch ? fromLegacyOptions({ patch: legacyConfig.patch }) : {};
|
|
1700
|
+
const merged = defu(overrides ?? {}, base);
|
|
1701
|
+
return merged;
|
|
1702
|
+
}
|
|
1703
|
+
function createCommandContext(cli, command, commandName, args, cwd) {
|
|
1704
|
+
let cachedOptions;
|
|
1705
|
+
let cachedPatcher;
|
|
1706
|
+
let cachedConfig;
|
|
1707
|
+
const loadPatchOptionsForContext = (overrides) => {
|
|
1708
|
+
if (overrides) {
|
|
1709
|
+
return loadPatchOptionsForCwd(cwd, overrides);
|
|
1710
|
+
}
|
|
1711
|
+
if (!cachedOptions) {
|
|
1712
|
+
cachedOptions = loadPatchOptionsForCwd(cwd);
|
|
1713
|
+
}
|
|
1714
|
+
return cachedOptions;
|
|
1715
|
+
};
|
|
1716
|
+
const createPatcherForContext = async (overrides) => {
|
|
1717
|
+
if (overrides) {
|
|
1718
|
+
const patchOptions = await loadPatchOptionsForCwd(cwd, overrides);
|
|
1719
|
+
return new TailwindcssPatcher(patchOptions);
|
|
1720
|
+
}
|
|
1721
|
+
if (!cachedPatcher) {
|
|
1722
|
+
cachedPatcher = loadPatchOptionsForContext().then((options) => new TailwindcssPatcher(options));
|
|
1723
|
+
}
|
|
1724
|
+
return cachedPatcher;
|
|
1725
|
+
};
|
|
1726
|
+
return {
|
|
1727
|
+
cli,
|
|
1728
|
+
command,
|
|
1729
|
+
commandName,
|
|
1730
|
+
args,
|
|
1731
|
+
cwd,
|
|
1732
|
+
logger: logger_default,
|
|
1733
|
+
loadConfig: () => {
|
|
1734
|
+
if (!cachedConfig) {
|
|
1735
|
+
cachedConfig = getConfig(cwd);
|
|
1736
|
+
}
|
|
1737
|
+
return cachedConfig;
|
|
1738
|
+
},
|
|
1739
|
+
loadPatchOptions: loadPatchOptionsForContext,
|
|
1740
|
+
createPatcher: createPatcherForContext
|
|
1741
|
+
};
|
|
1742
|
+
}
|
|
1743
|
+
function createCwdOptionDefinition(description = "Working directory") {
|
|
1744
|
+
return {
|
|
1745
|
+
flags: "--cwd <dir>",
|
|
1746
|
+
description,
|
|
1747
|
+
config: { default: process5.cwd() }
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
function buildDefaultCommandDefinitions() {
|
|
1751
|
+
return {
|
|
1752
|
+
install: {
|
|
1753
|
+
description: "Apply Tailwind CSS runtime patches",
|
|
1754
|
+
optionDefs: [createCwdOptionDefinition()]
|
|
1755
|
+
},
|
|
1756
|
+
extract: {
|
|
1757
|
+
description: "Collect generated class names into a cache file",
|
|
1758
|
+
optionDefs: [
|
|
1759
|
+
createCwdOptionDefinition(),
|
|
1760
|
+
{ flags: "--output <file>", description: "Override output file path" },
|
|
1761
|
+
{ flags: "--format <format>", description: "Output format (json|lines)" },
|
|
1762
|
+
{ flags: "--css <file>", description: "Tailwind CSS entry CSS when using v4" },
|
|
1763
|
+
{ flags: "--no-write", description: "Skip writing to disk" }
|
|
1764
|
+
]
|
|
1765
|
+
},
|
|
1766
|
+
tokens: {
|
|
1767
|
+
description: "Extract Tailwind tokens with file/position metadata",
|
|
1768
|
+
optionDefs: [
|
|
1769
|
+
createCwdOptionDefinition(),
|
|
1770
|
+
{ flags: "--output <file>", description: "Override output file path", config: { default: DEFAULT_TOKEN_REPORT } },
|
|
1771
|
+
{
|
|
1772
|
+
flags: "--format <format>",
|
|
1773
|
+
description: "Output format (json|lines|grouped-json)",
|
|
1774
|
+
config: { default: "json" }
|
|
1775
|
+
},
|
|
1776
|
+
{
|
|
1777
|
+
flags: "--group-key <key>",
|
|
1778
|
+
description: "Grouping key for grouped-json output (relative|absolute)",
|
|
1779
|
+
config: { default: "relative" }
|
|
1780
|
+
},
|
|
1781
|
+
{ flags: "--no-write", description: "Skip writing to disk" }
|
|
1782
|
+
]
|
|
1783
|
+
},
|
|
1784
|
+
init: {
|
|
1785
|
+
description: "Generate a tailwindcss-patch config file",
|
|
1786
|
+
optionDefs: [createCwdOptionDefinition()]
|
|
1787
|
+
}
|
|
1788
|
+
};
|
|
1789
|
+
}
|
|
1790
|
+
function addPrefixIfMissing(value, prefix) {
|
|
1791
|
+
if (!prefix || value.startsWith(prefix)) {
|
|
1792
|
+
return value;
|
|
1793
|
+
}
|
|
1794
|
+
return `${prefix}${value}`;
|
|
1795
|
+
}
|
|
1796
|
+
function resolveCommandNames(command, mountOptions, prefix) {
|
|
1797
|
+
const override = mountOptions.commandOptions?.[command];
|
|
1798
|
+
const baseName = override?.name ?? command;
|
|
1799
|
+
const name = addPrefixIfMissing(baseName, prefix);
|
|
1800
|
+
const aliases = (override?.aliases ?? []).map((alias) => addPrefixIfMissing(alias, prefix));
|
|
1801
|
+
return { name, aliases };
|
|
1802
|
+
}
|
|
1803
|
+
function resolveOptionDefinitions(defaults, override) {
|
|
1804
|
+
if (!override) {
|
|
1805
|
+
return defaults;
|
|
1806
|
+
}
|
|
1807
|
+
const appendDefaults = override.appendDefaultOptions ?? true;
|
|
1808
|
+
const customDefs = override.optionDefs ?? [];
|
|
1809
|
+
if (!appendDefaults) {
|
|
1810
|
+
return customDefs;
|
|
1811
|
+
}
|
|
1812
|
+
if (customDefs.length === 0) {
|
|
1813
|
+
return defaults;
|
|
1814
|
+
}
|
|
1815
|
+
return [...defaults, ...customDefs];
|
|
1816
|
+
}
|
|
1817
|
+
function applyCommandOptions(command, optionDefs) {
|
|
1818
|
+
for (const option of optionDefs) {
|
|
1819
|
+
command.option(option.flags, option.description ?? "", option.config);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
function runWithCommandHandler(cli, command, commandName, args, handler, defaultHandler) {
|
|
1823
|
+
const cwd = resolveCwd(args.cwd);
|
|
1824
|
+
const context = createCommandContext(cli, command, commandName, args, cwd);
|
|
1825
|
+
const runDefault = createDefaultRunner(() => defaultHandler(context));
|
|
1826
|
+
if (!handler) {
|
|
1827
|
+
return runDefault();
|
|
1828
|
+
}
|
|
1829
|
+
return handler(context, runDefault);
|
|
1830
|
+
}
|
|
1831
|
+
function resolveCommandMetadata(command, mountOptions, prefix, defaults) {
|
|
1832
|
+
const names = resolveCommandNames(command, mountOptions, prefix);
|
|
1833
|
+
const definition = defaults[command];
|
|
1834
|
+
const override = mountOptions.commandOptions?.[command];
|
|
1835
|
+
const description = override?.description ?? definition.description;
|
|
1836
|
+
const optionDefs = resolveOptionDefinitions(definition.optionDefs, override);
|
|
1837
|
+
return { ...names, description, optionDefs };
|
|
1838
|
+
}
|
|
1839
|
+
async function installCommandDefaultHandler(ctx) {
|
|
1840
|
+
const patcher = await ctx.createPatcher();
|
|
1841
|
+
await patcher.patch();
|
|
1842
|
+
logger_default.success("Tailwind CSS runtime patched successfully.");
|
|
1843
|
+
}
|
|
1844
|
+
async function extractCommandDefaultHandler(ctx) {
|
|
1845
|
+
const { args } = ctx;
|
|
1846
|
+
const overrides = {};
|
|
1847
|
+
let hasOverrides = false;
|
|
1848
|
+
if (args.output || args.format) {
|
|
1849
|
+
overrides.output = {
|
|
1850
|
+
file: args.output,
|
|
1851
|
+
format: args.format
|
|
1852
|
+
};
|
|
1853
|
+
hasOverrides = true;
|
|
1854
|
+
}
|
|
1855
|
+
if (args.css) {
|
|
1856
|
+
overrides.tailwind = {
|
|
1857
|
+
v4: {
|
|
1858
|
+
cssEntries: [args.css]
|
|
1859
|
+
}
|
|
1860
|
+
};
|
|
1861
|
+
hasOverrides = true;
|
|
1862
|
+
}
|
|
1863
|
+
const patcher = await ctx.createPatcher(hasOverrides ? overrides : void 0);
|
|
1864
|
+
const result = await patcher.extract({ write: args.write });
|
|
1865
|
+
if (result.filename) {
|
|
1866
|
+
logger_default.success(`Collected ${result.classList.length} classes \u2192 ${result.filename}`);
|
|
1867
|
+
} else {
|
|
1868
|
+
logger_default.success(`Collected ${result.classList.length} classes.`);
|
|
1869
|
+
}
|
|
1870
|
+
return result;
|
|
1871
|
+
}
|
|
1872
|
+
async function tokensCommandDefaultHandler(ctx) {
|
|
1873
|
+
const { args } = ctx;
|
|
1874
|
+
const patcher = await ctx.createPatcher();
|
|
1875
|
+
const report = await patcher.collectContentTokens();
|
|
1876
|
+
const shouldWrite = args.write ?? true;
|
|
1877
|
+
let format = args.format ?? "json";
|
|
1878
|
+
if (!TOKEN_FORMATS.includes(format)) {
|
|
1879
|
+
format = "json";
|
|
1880
|
+
}
|
|
1881
|
+
const targetFile = args.output ?? DEFAULT_TOKEN_REPORT;
|
|
1882
|
+
const groupKey = args.groupKey === "absolute" ? "absolute" : "relative";
|
|
1883
|
+
const buildGrouped = () => groupTokensByFile(report, {
|
|
1884
|
+
key: groupKey,
|
|
1885
|
+
stripAbsolutePaths: groupKey !== "absolute"
|
|
1886
|
+
});
|
|
1887
|
+
const grouped = format === "grouped-json" ? buildGrouped() : null;
|
|
1888
|
+
const resolveGrouped = () => grouped ?? buildGrouped();
|
|
1889
|
+
if (shouldWrite) {
|
|
1890
|
+
const target = path9.resolve(targetFile);
|
|
1891
|
+
await fs8.ensureDir(path9.dirname(target));
|
|
1892
|
+
if (format === "json") {
|
|
1893
|
+
await fs8.writeJSON(target, report, { spaces: 2 });
|
|
1894
|
+
} else if (format === "grouped-json") {
|
|
1895
|
+
await fs8.writeJSON(target, resolveGrouped(), { spaces: 2 });
|
|
1896
|
+
} else {
|
|
1897
|
+
const lines = report.entries.map(formatTokenLine);
|
|
1898
|
+
await fs8.writeFile(target, `${lines.join("\n")}
|
|
1899
|
+
`, "utf8");
|
|
1900
|
+
}
|
|
1901
|
+
logger_default.success(`Collected ${report.entries.length} tokens (${format}) \u2192 ${target.replace(process5.cwd(), ".")}`);
|
|
1902
|
+
} else {
|
|
1903
|
+
logger_default.success(`Collected ${report.entries.length} tokens from ${report.filesScanned} files.`);
|
|
1904
|
+
if (format === "lines") {
|
|
1905
|
+
const preview = report.entries.slice(0, 5).map(formatTokenLine).join("\n");
|
|
1906
|
+
if (preview) {
|
|
1907
|
+
logger_default.log("");
|
|
1908
|
+
logger_default.info(preview);
|
|
1909
|
+
if (report.entries.length > 5) {
|
|
1910
|
+
logger_default.info(`\u2026and ${report.entries.length - 5} more.`);
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
} else if (format === "grouped-json") {
|
|
1914
|
+
const map = resolveGrouped();
|
|
1915
|
+
const { preview, moreFiles } = formatGroupedPreview(map);
|
|
1916
|
+
if (preview) {
|
|
1917
|
+
logger_default.log("");
|
|
1918
|
+
logger_default.info(preview);
|
|
1919
|
+
if (moreFiles > 0) {
|
|
1920
|
+
logger_default.info(`\u2026and ${moreFiles} more files.`);
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
} else {
|
|
1924
|
+
const previewEntries = report.entries.slice(0, 3);
|
|
1925
|
+
if (previewEntries.length) {
|
|
1926
|
+
logger_default.log("");
|
|
1927
|
+
logger_default.info(JSON.stringify(previewEntries, null, 2));
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
if (report.skippedFiles.length) {
|
|
1932
|
+
logger_default.warn("Skipped files:");
|
|
1933
|
+
for (const skipped of report.skippedFiles) {
|
|
1934
|
+
logger_default.warn(` \u2022 ${skipped.file} (${skipped.reason})`);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
return report;
|
|
1938
|
+
}
|
|
1939
|
+
async function initCommandDefaultHandler(ctx) {
|
|
1940
|
+
await initConfig(ctx.cwd);
|
|
1941
|
+
logger_default.success(`\u2728 ${CONFIG_NAME}.config.ts initialized!`);
|
|
1942
|
+
}
|
|
1943
|
+
function mountTailwindcssPatchCommands(cli, options = {}) {
|
|
1944
|
+
const prefix = options.commandPrefix ?? "";
|
|
1945
|
+
const selectedCommands = options.commands ?? tailwindcssPatchCommands;
|
|
1946
|
+
const defaultDefinitions = buildDefaultCommandDefinitions();
|
|
1947
|
+
const registrars = {
|
|
1948
|
+
install: () => {
|
|
1949
|
+
const metadata = resolveCommandMetadata("install", options, prefix, defaultDefinitions);
|
|
1950
|
+
const command = cli.command(metadata.name, metadata.description);
|
|
1951
|
+
applyCommandOptions(command, metadata.optionDefs);
|
|
1952
|
+
command.action(async (args) => {
|
|
1953
|
+
return runWithCommandHandler(
|
|
1954
|
+
cli,
|
|
1955
|
+
command,
|
|
1956
|
+
"install",
|
|
1957
|
+
args,
|
|
1958
|
+
options.commandHandlers?.install,
|
|
1959
|
+
installCommandDefaultHandler
|
|
1960
|
+
);
|
|
1961
|
+
});
|
|
1962
|
+
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
1963
|
+
},
|
|
1964
|
+
extract: () => {
|
|
1965
|
+
const metadata = resolveCommandMetadata("extract", options, prefix, defaultDefinitions);
|
|
1966
|
+
const command = cli.command(metadata.name, metadata.description);
|
|
1967
|
+
applyCommandOptions(command, metadata.optionDefs);
|
|
1968
|
+
command.action(async (args) => {
|
|
1969
|
+
return runWithCommandHandler(
|
|
1970
|
+
cli,
|
|
1971
|
+
command,
|
|
1972
|
+
"extract",
|
|
1973
|
+
args,
|
|
1974
|
+
options.commandHandlers?.extract,
|
|
1975
|
+
extractCommandDefaultHandler
|
|
1976
|
+
);
|
|
1977
|
+
});
|
|
1978
|
+
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
1979
|
+
},
|
|
1980
|
+
tokens: () => {
|
|
1981
|
+
const metadata = resolveCommandMetadata("tokens", options, prefix, defaultDefinitions);
|
|
1982
|
+
const command = cli.command(metadata.name, metadata.description);
|
|
1983
|
+
applyCommandOptions(command, metadata.optionDefs);
|
|
1984
|
+
command.action(async (args) => {
|
|
1985
|
+
return runWithCommandHandler(
|
|
1986
|
+
cli,
|
|
1987
|
+
command,
|
|
1988
|
+
"tokens",
|
|
1989
|
+
args,
|
|
1990
|
+
options.commandHandlers?.tokens,
|
|
1991
|
+
tokensCommandDefaultHandler
|
|
1992
|
+
);
|
|
1993
|
+
});
|
|
1994
|
+
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
1995
|
+
},
|
|
1996
|
+
init: () => {
|
|
1997
|
+
const metadata = resolveCommandMetadata("init", options, prefix, defaultDefinitions);
|
|
1998
|
+
const command = cli.command(metadata.name, metadata.description);
|
|
1999
|
+
applyCommandOptions(command, metadata.optionDefs);
|
|
2000
|
+
command.action(async (args) => {
|
|
2001
|
+
return runWithCommandHandler(
|
|
2002
|
+
cli,
|
|
2003
|
+
command,
|
|
2004
|
+
"init",
|
|
2005
|
+
args,
|
|
2006
|
+
options.commandHandlers?.init,
|
|
2007
|
+
initCommandDefaultHandler
|
|
2008
|
+
);
|
|
2009
|
+
});
|
|
2010
|
+
metadata.aliases.forEach((alias) => command.alias(alias));
|
|
2011
|
+
}
|
|
2012
|
+
};
|
|
2013
|
+
for (const name of selectedCommands) {
|
|
2014
|
+
const register = registrars[name];
|
|
2015
|
+
if (register) {
|
|
2016
|
+
register();
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
return cli;
|
|
2020
|
+
}
|
|
2021
|
+
function createTailwindcssPatchCli(options = {}) {
|
|
2022
|
+
const cli = cac(options.name ?? "tw-patch");
|
|
2023
|
+
mountTailwindcssPatchCommands(cli, options.mountOptions);
|
|
2024
|
+
return cli;
|
|
2025
|
+
}
|
|
2026
|
+
|
|
1561
2027
|
export {
|
|
1562
2028
|
logger_default,
|
|
1563
2029
|
CacheStore,
|
|
@@ -1566,12 +2032,13 @@ export {
|
|
|
1566
2032
|
extractValidCandidates,
|
|
1567
2033
|
extractProjectCandidatesWithPositions,
|
|
1568
2034
|
groupTokensByFile,
|
|
1569
|
-
fromLegacyOptions,
|
|
1570
|
-
fromUnifiedConfig,
|
|
1571
2035
|
normalizeOptions,
|
|
1572
2036
|
collectClassesFromContexts,
|
|
1573
2037
|
collectClassesFromTailwindV4,
|
|
1574
2038
|
loadRuntimeContexts,
|
|
1575
2039
|
runTailwindBuild,
|
|
1576
|
-
TailwindcssPatcher
|
|
2040
|
+
TailwindcssPatcher,
|
|
2041
|
+
tailwindcssPatchCommands,
|
|
2042
|
+
mountTailwindcssPatchCommands,
|
|
2043
|
+
createTailwindcssPatchCli
|
|
1577
2044
|
};
|