|
| 1 | +# Migration Guide |
| 2 | + |
| 3 | +This document describes the changes introduced by the full refactor of `tailwindcss-patch`. It helps teams upgrading from the 7.x line (or earlier) to the new architecture. |
| 4 | + |
| 5 | +> TL;DR — the public API now revolves around the `TailwindcssPatcher` class, helpers live in explicit folders, and the CLI exposes clearer options. Legacy configuration objects are still accepted, but adopting the new structure unlocks additional features. |
| 6 | +
|
| 7 | +## 1. Package layout |
| 8 | + |
| 9 | +| Before | After | |
| 10 | +| --- | --- | |
| 11 | +| `src/core/**` mix of cache, patch and runtime helpers | Dedicated folders: `api/`, `cache/`, `patching/`, `runtime/`, `options/`, `extraction/` | |
| 12 | +| `CacheManager`, `InternalPatchOptions`, ad-hoc exports | `CacheStore`, `normalizeOptions`, typed helpers with explicit imports | |
| 13 | +| `processTailwindcss` | `runTailwindBuild` in `runtime/process-tailwindcss.ts` | |
| 14 | + |
| 15 | +Imports such as `@/core/patcher` or `@/core/cache` must be updated. Use the new entry-points: |
| 16 | + |
| 17 | +```ts |
| 18 | +import { TailwindcssPatcher } from 'tailwindcss-patch' |
| 19 | +import { CacheStore } from 'tailwindcss-patch/cache/store' |
| 20 | +import { applyTailwindPatches } from 'tailwindcss-patch/patching/patch-runner' |
| 21 | +``` |
| 22 | + |
| 23 | +## 2. TailwindcssPatcher options |
| 24 | + |
| 25 | +### Previous shape |
| 26 | + |
| 27 | +```ts |
| 28 | +new TailwindcssPatcher({ |
| 29 | + cache: { dir: '.cache', file: 'classes.json' }, |
| 30 | + patch: { |
| 31 | + overwrite: true, |
| 32 | + output: { filename: '.tw-patch/tw-class-list.json', loose: true }, |
| 33 | + tailwindcss: { version: 3, v3: { cwd, config } }, |
| 34 | + applyPatches: { extendLengthUnits: true }, |
| 35 | + }, |
| 36 | +}) |
| 37 | +``` |
| 38 | + |
| 39 | +### New shape |
| 40 | + |
| 41 | +```ts |
| 42 | +new TailwindcssPatcher({ |
| 43 | + overwrite: true, |
| 44 | + cache: { |
| 45 | + enabled: true, |
| 46 | + dir: '.tw-patch/cache', |
| 47 | + strategy: 'merge', |
| 48 | + }, |
| 49 | + output: { |
| 50 | + file: '.tw-patch/tw-class-list.json', |
| 51 | + format: 'json', |
| 52 | + removeUniversalSelector: true, |
| 53 | + }, |
| 54 | + features: { |
| 55 | + exposeContext: { refProperty: 'runtimeContexts' }, |
| 56 | + extendLengthUnits: { units: ['rpx'] }, |
| 57 | + }, |
| 58 | + tailwind: { |
| 59 | + version: 4, |
| 60 | + v4: { |
| 61 | + base: './src', |
| 62 | + cssEntries: ['dist/tailwind.css'], |
| 63 | + }, |
| 64 | + }, |
| 65 | +}) |
| 66 | +``` |
| 67 | + |
| 68 | +Both shapes are accepted. When the constructor detects `patch`/`cache` keys it automatically converts them via `fromLegacyOptions()`. This allows step-by-step migrations. |
| 69 | + |
| 70 | +## 3. CLI changes |
| 71 | + |
| 72 | +- `tw-patch install` still applies the runtime patch, but logging and error handling were refreshed. |
| 73 | +- `tw-patch extract` gained new flags (`--output`, `--format`, `--no-write`, `--css`). |
| 74 | +- Commands resolve configuration from `tailwindcss-patch.config.ts` via `@tailwindcss-mangle/config`. Existing configuration files continue to work without changes. |
| 75 | + |
| 76 | +## 4. Cache handling |
| 77 | + |
| 78 | +`CacheManager` has been replaced by `CacheStore`: |
| 79 | + |
| 80 | +```ts |
| 81 | +const cache = new CacheStore(normalized.cache) |
| 82 | +await cache.write(new Set(['text-lg'])) |
| 83 | +const values = await cache.read() |
| 84 | +``` |
| 85 | + |
| 86 | +This ensures consistent async behaviour and reliable error recovery (invalid JSON files are removed automatically). |
| 87 | + |
| 88 | +## 5. Exported helpers |
| 89 | + |
| 90 | +- Runtime context access now lives in `runtime/context-registry.ts` (`loadRuntimeContexts`). |
| 91 | +- Tailwind v4 candidate extraction moved to `extraction/candidate-extractor.ts`. |
| 92 | +- Custom unit support is available from `patching/operations/extend-length-units.ts`. |
| 93 | + |
| 94 | +Update imports accordingly when consuming these helpers directly. |
| 95 | + |
| 96 | +## 6. Configuration advice |
| 97 | + |
| 98 | +`defineConfig` from `tailwindcss-patch` (provided by `@tailwindcss-mangle/config`) still emits the legacy `patch` object. All new fields—`output.format`, extended `tailwindcss.v4` options, `applyPatches.extendLengthUnits` objects—are handled transparently. You may migrate gradually by adding the new keys into the existing `patch` block. |
| 99 | + |
| 100 | +If you want the new structure inside application code, prefer creating the patcher manually and pass the modern object as demonstrated above. |
| 101 | + |
| 102 | +## 7. Feature highlights |
| 103 | + |
| 104 | +- Tailwind v4 is supported without monkey patching. Provide CSS entries and content sources to `tailwind.v4` and call `extract()`. |
| 105 | +- Custom length units patching (`extendLengthUnits`) now supports Tailwind v3 and v4 with a single option object. |
| 106 | +- Filters are composed with the `output.removeUniversalSelector` flag so `'*'` can be kept when desired. |
| 107 | + |
| 108 | +## 8. Removal summary |
| 109 | + |
| 110 | +- The entire `src/core/**` directory was removed. Update imports to their new locations. |
| 111 | +- `defaults.ts`, `InternalPatchOptions`, and related helpers were removed. |
| 112 | +- Old tests referencing `CacheManager` or `processTailwindcss` no longer apply. |
| 113 | + |
| 114 | +## 9. Checklist for upgrading projects |
| 115 | + |
| 116 | +1. Update the dependency to the latest version of `tailwindcss-patch`. |
| 117 | +2. Review custom imports from `tailwindcss-patch/core/*` and switch to the new module paths. |
| 118 | +3. If you instantiate the patcher manually, adopt the new options object (or keep legacy options temporarily). |
| 119 | +4. Refresh CLI usage in scripts (e.g. add `--output` or `--no-write` where appropriate). |
| 120 | +5. For Tailwind v4 projects, configure `tailwind.v4.cssEntries` and `sources` so that `extract()` can discover candidates. |
| 121 | +6. Run your extraction workflow and ensure the generated class list matches expectations. |
| 122 | + |
| 123 | +For any regressions or gaps discovered during migration, please open an issue with reproduction details so we can iterate quickly. |
0 commit comments