tailwindcss-patch 9.0.1 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,14 +9,14 @@ import { createConsola } from "consola";
9
9
  import path$1 from "node:path";
10
10
  import { fileURLToPath, pathToFileURL } from "node:url";
11
11
  import { promises } from "node:fs";
12
+ import postcss from "postcss";
12
13
  import * as t from "@babel/types";
13
14
  import generate from "@babel/generator";
14
15
  import _babelTraverse from "@babel/traverse";
15
16
  import { parse, parse as parse$1 } from "@babel/parser";
16
- import postcss from "postcss";
17
17
  import { loadConfig } from "tailwindcss-config";
18
18
  //#region package.json
19
- var version = "9.0.1";
19
+ var version = "9.1.0";
20
20
  //#endregion
21
21
  //#region src/constants.ts
22
22
  const pkgName = "tailwindcss-patch";
@@ -1445,57 +1445,242 @@ async function loadPatchOptionsForWorkspace(cwd, overrides) {
1445
1445
  return merge(overrides ?? {}, base, { projectRoot: cwd });
1446
1446
  }
1447
1447
  //#endregion
1448
- //#region src/extraction/candidate-extractor.ts
1449
- let nodeImportPromise;
1450
- let oxideImportPromise;
1448
+ //#region src/v4/candidates.ts
1449
+ function resolveValidTailwindV4Candidates(designSystem, candidates) {
1450
+ const validCandidates = /* @__PURE__ */ new Set();
1451
+ const parsedCandidates = [];
1452
+ for (const candidate of candidates) {
1453
+ if (!candidate || parsedCandidates.includes(candidate)) continue;
1454
+ if (designSystem.parseCandidate(candidate).length > 0) parsedCandidates.push(candidate);
1455
+ }
1456
+ if (parsedCandidates.length === 0) return validCandidates;
1457
+ const cssByCandidate = designSystem.candidatesToCss(parsedCandidates);
1458
+ for (let index = 0; index < parsedCandidates.length; index++) {
1459
+ const candidate = parsedCandidates[index];
1460
+ const candidateCss = cssByCandidate[index];
1461
+ if (candidate && typeof candidateCss === "string" && candidateCss.trim().length > 0) validCandidates.add(candidate);
1462
+ }
1463
+ return validCandidates;
1464
+ }
1465
+ function splitTopLevel(value, separator) {
1466
+ const result = [];
1467
+ let start = 0;
1468
+ let depth = 0;
1469
+ let quote;
1470
+ for (let index = 0; index < value.length; index++) {
1471
+ const character = value[index];
1472
+ if (character === "\\") {
1473
+ index++;
1474
+ continue;
1475
+ }
1476
+ if (quote) {
1477
+ if (character === quote) quote = void 0;
1478
+ continue;
1479
+ }
1480
+ if (character === "\"" || character === "'") {
1481
+ quote = character;
1482
+ continue;
1483
+ }
1484
+ if (character === "(" || character === "[" || character === "{") {
1485
+ depth++;
1486
+ continue;
1487
+ }
1488
+ if (character === ")" || character === "]" || character === "}") {
1489
+ depth = Math.max(0, depth - 1);
1490
+ continue;
1491
+ }
1492
+ if (depth === 0 && character === separator) {
1493
+ const item = value.slice(start, index).trim();
1494
+ if (item) result.push(item);
1495
+ start = index + 1;
1496
+ }
1497
+ }
1498
+ const item = value.slice(start).trim();
1499
+ if (item) result.push(item);
1500
+ return result;
1501
+ }
1502
+ const sequencePattern = /^(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?$/;
1503
+ function expandSequence(value) {
1504
+ const match = value.match(sequencePattern);
1505
+ if (!match) return [value];
1506
+ const [, startValue, endValue, stepValue] = match;
1507
+ if (startValue === void 0 || endValue === void 0) return [value];
1508
+ const start = Number.parseInt(startValue, 10);
1509
+ const end = Number.parseInt(endValue, 10);
1510
+ let step = stepValue === void 0 ? start <= end ? 1 : -1 : Number.parseInt(stepValue, 10);
1511
+ if (step === 0) throw new Error("Step cannot be zero in Tailwind CSS v4 inline source sequence.");
1512
+ const ascending = start < end;
1513
+ if (ascending && step < 0) step = -step;
1514
+ if (!ascending && step > 0) step = -step;
1515
+ const result = [];
1516
+ for (let current = start; ascending ? current <= end : current >= end; current += step) result.push(current.toString());
1517
+ return result;
1518
+ }
1519
+ function expandInlinePattern(pattern) {
1520
+ const openIndex = pattern.indexOf("{");
1521
+ if (openIndex === -1) return [pattern];
1522
+ const prefix = pattern.slice(0, openIndex);
1523
+ const rest = pattern.slice(openIndex);
1524
+ let depth = 0;
1525
+ let closeIndex = -1;
1526
+ for (let index = 0; index < rest.length; index++) {
1527
+ const character = rest[index];
1528
+ if (character === "{") depth++;
1529
+ else if (character === "}") {
1530
+ depth--;
1531
+ if (depth === 0) {
1532
+ closeIndex = index;
1533
+ break;
1534
+ }
1535
+ }
1536
+ }
1537
+ if (closeIndex === -1) throw new Error(`The Tailwind CSS v4 inline source pattern "${pattern}" is not balanced.`);
1538
+ const body = rest.slice(1, closeIndex);
1539
+ const suffix = rest.slice(closeIndex + 1);
1540
+ const parts = sequencePattern.test(body) ? expandSequence(body) : splitTopLevel(body, ",").flatMap((part) => expandInlinePattern(part));
1541
+ const suffixes = expandInlinePattern(suffix);
1542
+ const result = [];
1543
+ for (const part of parts) for (const expandedSuffix of suffixes) result.push(`${prefix}${part}${expandedSuffix}`);
1544
+ return result;
1545
+ }
1546
+ function unquoteCssString(value) {
1547
+ const quote = value[0];
1548
+ if (quote !== "\"" && quote !== "'" || value[value.length - 1] !== quote) return;
1549
+ let result = "";
1550
+ for (let index = 1; index < value.length - 1; index++) {
1551
+ const character = value[index];
1552
+ if (character === "\\") {
1553
+ index++;
1554
+ result += value[index] ?? "";
1555
+ continue;
1556
+ }
1557
+ result += character;
1558
+ }
1559
+ return result;
1560
+ }
1561
+ function extractTailwindV4InlineSourceCandidates(css) {
1562
+ const included = /* @__PURE__ */ new Set();
1563
+ const excluded = /* @__PURE__ */ new Set();
1564
+ postcss.parse(css).walkAtRules("source", (rule) => {
1565
+ let params = rule.params.trim();
1566
+ if (!params) return;
1567
+ let negated = false;
1568
+ if (params.startsWith("not ")) {
1569
+ negated = true;
1570
+ params = params.slice(4).trim();
1571
+ }
1572
+ if (!params.startsWith("inline(") || !params.endsWith(")")) return;
1573
+ const inlineValue = unquoteCssString(params.slice(7, -1).trim());
1574
+ if (inlineValue === void 0) return;
1575
+ const target = negated ? excluded : included;
1576
+ for (const part of splitTopLevel(inlineValue, " ")) for (const candidate of expandInlinePattern(part)) target.add(candidate);
1577
+ });
1578
+ return {
1579
+ included,
1580
+ excluded
1581
+ };
1582
+ }
1583
+ //#endregion
1584
+ //#region src/v4/node-adapter.ts
1585
+ const nodeModulePromiseCache = /* @__PURE__ */ new Map();
1451
1586
  const designSystemPromiseCache = /* @__PURE__ */ new Map();
1452
- const designSystemCandidateCache = /* @__PURE__ */ new Map();
1453
- async function importNode() {
1454
- return import("@tailwindcss/node");
1587
+ function unique(values) {
1588
+ return Array.from(new Set(Array.from(values).filter(Boolean).map((value) => path.resolve(value))));
1455
1589
  }
1456
- async function importOxide() {
1457
- return import("@tailwindcss/oxide");
1590
+ function createRequireBase(base) {
1591
+ return path.join(base, "package.json");
1458
1592
  }
1459
- function getNodeModule() {
1460
- nodeImportPromise ??= importNode();
1461
- return nodeImportPromise;
1593
+ async function importResolvedModule(resolved) {
1594
+ return import(pathToFileURL(resolved).href);
1462
1595
  }
1463
- function getOxideModule() {
1464
- oxideImportPromise ??= importOxide();
1465
- return oxideImportPromise;
1596
+ async function importTailwindNodeFromBase(base) {
1597
+ try {
1598
+ return await importResolvedModule(createRequire(createRequireBase(base)).resolve("@tailwindcss/node"));
1599
+ } catch {
1600
+ return;
1601
+ }
1602
+ }
1603
+ async function importFallbackTailwindNode() {
1604
+ return import("@tailwindcss/node");
1605
+ }
1606
+ async function loadTailwindV4NodeModule(baseCandidates) {
1607
+ const bases = unique(baseCandidates);
1608
+ const cacheKey = JSON.stringify(bases);
1609
+ const cached = nodeModulePromiseCache.get(cacheKey);
1610
+ if (cached) return cached;
1611
+ const promise = (async () => {
1612
+ for (const base of bases) {
1613
+ const loaded = await importTailwindNodeFromBase(base);
1614
+ if (loaded) return loaded;
1615
+ }
1616
+ return importFallbackTailwindNode();
1617
+ })();
1618
+ nodeModulePromiseCache.set(cacheKey, promise);
1619
+ promise.catch(() => {
1620
+ if (nodeModulePromiseCache.get(cacheKey) === promise) nodeModulePromiseCache.delete(cacheKey);
1621
+ });
1622
+ return promise;
1466
1623
  }
1467
1624
  function createDesignSystemCacheKey(css, bases) {
1468
1625
  return JSON.stringify({
1469
1626
  css,
1470
- bases: Array.from(new Set(bases.filter(Boolean)))
1627
+ bases: unique(bases)
1471
1628
  });
1472
1629
  }
1473
- async function loadDesignSystem(css, bases) {
1474
- const uniqueBases = Array.from(new Set(bases.filter(Boolean)));
1475
- if (uniqueBases.length === 0) throw new Error("No base directories provided for Tailwind CSS design system.");
1476
- const cacheKey = createDesignSystemCacheKey(css, uniqueBases);
1630
+ function getTailwindV4DesignSystemCacheKey(source) {
1631
+ return createDesignSystemCacheKey(source.css, [source.base, ...source.baseFallbacks]);
1632
+ }
1633
+ async function loadTailwindV4DesignSystem(source) {
1634
+ const bases = unique([source.base, ...source.baseFallbacks]);
1635
+ if (bases.length === 0) throw new Error("No base directories provided for Tailwind CSS v4 design system.");
1636
+ const cacheKey = createDesignSystemCacheKey(source.css, bases);
1477
1637
  const cached = designSystemPromiseCache.get(cacheKey);
1478
1638
  if (cached) return cached;
1479
1639
  const promise = (async () => {
1480
- const { __unstable__loadDesignSystem } = await getNodeModule();
1640
+ const node = await loadTailwindV4NodeModule([source.projectRoot, ...bases]);
1481
1641
  let lastError;
1482
- for (const base of uniqueBases) try {
1483
- return await __unstable__loadDesignSystem(css, { base });
1642
+ for (const base of bases) try {
1643
+ return await node.__unstable__loadDesignSystem(source.css, { base });
1484
1644
  } catch (error) {
1485
1645
  lastError = error;
1486
1646
  }
1487
1647
  if (lastError instanceof Error) throw lastError;
1488
- throw new Error("Failed to load Tailwind CSS design system.");
1648
+ throw new Error("Failed to load Tailwind CSS v4 design system.");
1489
1649
  })();
1490
1650
  designSystemPromiseCache.set(cacheKey, promise);
1491
1651
  promise.catch(() => {
1492
- if (designSystemPromiseCache.get(cacheKey) === promise) {
1493
- designSystemPromiseCache.delete(cacheKey);
1494
- designSystemCandidateCache.delete(cacheKey);
1495
- }
1652
+ if (designSystemPromiseCache.get(cacheKey) === promise) designSystemPromiseCache.delete(cacheKey);
1496
1653
  });
1497
1654
  return promise;
1498
1655
  }
1656
+ async function compileTailwindV4Source(source) {
1657
+ const node = await loadTailwindV4NodeModule([
1658
+ source.projectRoot,
1659
+ source.base,
1660
+ ...source.baseFallbacks
1661
+ ]);
1662
+ const dependencies = new Set(source.dependencies);
1663
+ return {
1664
+ compiled: await node.compile(source.css, {
1665
+ base: source.base,
1666
+ onDependency(dependency) {
1667
+ dependencies.add(path.resolve(dependency));
1668
+ }
1669
+ }),
1670
+ dependencies
1671
+ };
1672
+ }
1673
+ //#endregion
1674
+ //#region src/extraction/candidate-extractor.ts
1675
+ let oxideImportPromise;
1676
+ const designSystemCandidateCache = /* @__PURE__ */ new Map();
1677
+ async function importOxide() {
1678
+ return import("@tailwindcss/oxide");
1679
+ }
1680
+ function getOxideModule() {
1681
+ oxideImportPromise ??= importOxide();
1682
+ return oxideImportPromise;
1683
+ }
1499
1684
  async function extractRawCandidatesWithPositions(content, extension = "html") {
1500
1685
  const { Scanner } = await getOxideModule();
1501
1686
  return new Scanner({}).getCandidatesWithPositions({
@@ -1526,8 +1711,15 @@ async function extractValidCandidates(options) {
1526
1711
  pattern: source.pattern,
1527
1712
  negated: source.negated
1528
1713
  }));
1529
- const designSystemKey = createDesignSystemCacheKey(css, [base, ...baseFallbacks]);
1530
- const designSystem = await loadDesignSystem(css, [base, ...baseFallbacks]);
1714
+ const source = {
1715
+ projectRoot: defaultCwd,
1716
+ base,
1717
+ baseFallbacks,
1718
+ css,
1719
+ dependencies: []
1720
+ };
1721
+ const designSystemKey = getTailwindV4DesignSystemCacheKey(source);
1722
+ const designSystem = await loadTailwindV4DesignSystem(source);
1531
1723
  const candidateCache = designSystemCandidateCache.get(designSystemKey) ?? /* @__PURE__ */ new Map();
1532
1724
  designSystemCandidateCache.set(designSystemKey, candidateCache);
1533
1725
  const candidates = await extractRawCandidates(sources);
@@ -1547,12 +1739,9 @@ async function extractValidCandidates(options) {
1547
1739
  candidateCache.set(rawCandidate, false);
1548
1740
  }
1549
1741
  if (uncachedCandidates.length === 0) return validCandidates;
1550
- const cssByCandidate = designSystem.candidatesToCss(uncachedCandidates);
1551
- for (let index = 0; index < uncachedCandidates.length; index++) {
1552
- const candidate = uncachedCandidates[index];
1553
- if (candidate === void 0) continue;
1554
- const candidateCss = cssByCandidate[index];
1555
- const isValid = typeof candidateCss === "string" && candidateCss.trim().length > 0;
1742
+ const validUncachedCandidates = resolveValidTailwindV4Candidates(designSystem, uncachedCandidates);
1743
+ for (const candidate of uncachedCandidates) {
1744
+ const isValid = validUncachedCandidates.has(candidate);
1556
1745
  candidateCache.set(candidate, isValid);
1557
1746
  if (!isValid) continue;
1558
1747
  validCandidates.push(candidate);
@@ -3333,4 +3522,4 @@ var ValidateCommandError = class extends Error {
3333
3522
  }
3334
3523
  };
3335
3524
  //#endregion
3336
- export { normalizeOptions as C, loadWorkspaceConfigModule as S, logger as T, extractRawCandidates as _, tailwindcssPatchCommands as a, groupTokensByFile as b, MIGRATION_REPORT_KIND as c, getPatchStatusReport as d, runTailwindBuild as f, extractProjectCandidatesWithPositions as g, collectClassesFromTailwindV4 as h, classifyValidateError as i, MIGRATION_REPORT_SCHEMA_VERSION as l, collectClassesFromContexts as m, VALIDATE_FAILURE_REASONS as n, migrateConfigFiles as o, loadRuntimeContexts as p, ValidateCommandError as r, restoreConfigFiles as s, VALIDATE_EXIT_CODES as t, TailwindcssPatcher as u, extractRawCandidatesWithPositions as v, CacheStore as w, loadPatchOptionsForWorkspace as x, extractValidCandidates as y };
3525
+ export { extractTailwindV4InlineSourceCandidates as C, normalizeOptions as D, loadWorkspaceConfigModule as E, CacheStore as O, loadTailwindV4DesignSystem as S, loadPatchOptionsForWorkspace as T, extractRawCandidates as _, tailwindcssPatchCommands as a, groupTokensByFile as b, MIGRATION_REPORT_KIND as c, getPatchStatusReport as d, runTailwindBuild as f, extractProjectCandidatesWithPositions as g, collectClassesFromTailwindV4 as h, classifyValidateError as i, logger as k, MIGRATION_REPORT_SCHEMA_VERSION as l, collectClassesFromContexts as m, VALIDATE_FAILURE_REASONS as n, migrateConfigFiles as o, loadRuntimeContexts as p, ValidateCommandError as r, restoreConfigFiles as s, VALIDATE_EXIT_CODES as t, TailwindcssPatcher as u, extractRawCandidatesWithPositions as v, resolveValidTailwindV4Candidates as w, compileTailwindV4Source as x, extractValidCandidates as y };