tailwindcss-patch 7.1.5 → 8.0.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/dist/index.d.mts CHANGED
@@ -1,45 +1,128 @@
1
- import { PatchUserConfig } from '@tailwindcss-mangle/config';
2
- export { TailwindcssUserConfig, defineConfig } from '@tailwindcss-mangle/config';
3
- import { Node, Rule } from 'postcss';
1
+ import postcss, { Node, Rule } from 'postcss';
4
2
  import { Config } from 'tailwindcss';
5
- import { PackageInfo } from 'local-pkg';
6
3
  import { SourceEntry } from '@tailwindcss/oxide';
4
+ import { PackageResolvingOptions, PackageInfo } from 'local-pkg';
5
+ import { TailwindLocatorOptions, TailwindNextOptions } from '@tailwindcss-mangle/config';
6
+ export { defineConfig } from '@tailwindcss-mangle/config';
7
7
  import * as consola from 'consola';
8
8
 
9
9
  type CacheStrategy = 'merge' | 'overwrite';
10
-
11
- interface CacheOptions {
12
- dir?: string;
10
+ interface CacheUserOptions {
11
+ enabled?: boolean;
13
12
  cwd?: string;
13
+ dir?: string;
14
14
  file?: string;
15
15
  strategy?: CacheStrategy;
16
16
  }
17
- interface InternalCacheOptions extends CacheOptions {
18
- enable?: boolean;
17
+ interface OutputUserOptions {
18
+ enabled?: boolean;
19
+ file?: string;
20
+ format?: 'json' | 'lines';
21
+ pretty?: number | boolean;
22
+ removeUniversalSelector?: boolean;
23
+ }
24
+ interface ExposeContextUserOptions {
25
+ refProperty?: string;
26
+ }
27
+ interface ExtendLengthUnitsUserOptions extends Partial<ILengthUnitsPatchOptions> {
28
+ enabled?: boolean;
19
29
  }
20
- interface PatchOptions extends PatchUserConfig {
30
+ interface FeatureUserOptions {
31
+ exposeContext?: boolean | ExposeContextUserOptions;
32
+ extendLengthUnits?: false | ExtendLengthUnitsUserOptions;
33
+ }
34
+ interface TailwindConfigUserOptions {
35
+ config?: string;
36
+ cwd?: string;
37
+ postcssPlugin?: string;
38
+ }
39
+ interface TailwindV4UserOptions {
40
+ base?: string;
41
+ css?: string;
42
+ cssEntries?: string[];
43
+ sources?: SourceEntry[];
44
+ }
45
+ interface TailwindUserOptions extends TailwindConfigUserOptions {
46
+ /**
47
+ * Optional hint for picking the patch strategy.
48
+ * When omitted we infer from the installed Tailwind CSS package version.
49
+ */
50
+ version?: 2 | 3 | 4;
51
+ packageName?: string;
52
+ resolve?: PackageResolvingOptions;
53
+ v2?: TailwindConfigUserOptions;
54
+ v3?: TailwindConfigUserOptions;
55
+ v4?: TailwindV4UserOptions;
56
+ }
57
+ interface TailwindcssPatchOptions {
58
+ /**
59
+ * Base directory used when resolving Tailwind resources.
60
+ * Defaults to `process.cwd()`.
61
+ */
62
+ cwd?: string;
21
63
  overwrite?: boolean;
22
- paths?: string[];
23
- basedir?: string;
24
- applyPatches?: {
25
- exportContext?: boolean;
26
- extendLengthUnits?: boolean | ILengthUnitsPatchOptions;
27
- };
64
+ tailwind?: TailwindUserOptions;
65
+ features?: FeatureUserOptions;
28
66
  filter?: (className: string) => boolean;
29
- cwd?: string;
67
+ cache?: boolean | CacheUserOptions;
68
+ output?: OutputUserOptions;
69
+ }
70
+ interface NormalizedOutputOptions {
71
+ enabled: boolean;
72
+ file?: string;
73
+ format: 'json' | 'lines';
74
+ pretty: number | false;
75
+ removeUniversalSelector: boolean;
76
+ }
77
+ interface NormalizedCacheOptions {
78
+ enabled: boolean;
79
+ cwd: string;
80
+ dir: string;
81
+ file: string;
82
+ path: string;
83
+ strategy: CacheStrategy;
84
+ }
85
+ interface NormalizedExposeContextOptions {
86
+ enabled: boolean;
87
+ refProperty: string;
88
+ }
89
+ interface NormalizedExtendLengthUnitsOptions extends ILengthUnitsPatchOptions {
90
+ enabled: boolean;
91
+ }
92
+ interface NormalizedTailwindV4Options {
93
+ base: string;
94
+ css?: string;
95
+ cssEntries: string[];
96
+ sources: SourceEntry[];
30
97
  }
31
- interface InternalPatchOptions extends PatchOptions {
32
- version?: string;
98
+ interface NormalizedTailwindConfigOptions extends TailwindConfigUserOptions {
99
+ packageName: string;
100
+ versionHint?: 2 | 3 | 4;
101
+ resolve?: PackageResolvingOptions;
102
+ v2?: TailwindConfigUserOptions;
103
+ v3?: TailwindConfigUserOptions;
104
+ v4?: NormalizedTailwindV4Options;
33
105
  }
34
- interface TailwindcssPatcherOptions {
35
- cache?: CacheOptions | boolean;
36
- patch?: PatchOptions;
106
+ interface NormalizedFeatureOptions {
107
+ exposeContext: NormalizedExposeContextOptions;
108
+ extendLengthUnits: NormalizedExtendLengthUnitsOptions | null;
37
109
  }
38
- type TailwindcssClassCache = Map<string, ({
110
+ interface NormalizedTailwindcssPatchOptions {
111
+ projectRoot: string;
112
+ overwrite: boolean;
113
+ tailwind: NormalizedTailwindConfigOptions;
114
+ features: NormalizedFeatureOptions;
115
+ output: NormalizedOutputOptions;
116
+ cache: NormalizedCacheOptions;
117
+ filter: (className: string) => boolean;
118
+ }
119
+
120
+ type TailwindcssClassCacheEntry = Rule | {
39
121
  layer: string;
40
122
  options: Record<string, any>;
41
123
  sort: Record<string, any>;
42
- } | Rule)[]>;
124
+ };
125
+ type TailwindcssClassCache = Map<string, TailwindcssClassCacheEntry[]>;
43
126
  interface TailwindcssRuntimeContext {
44
127
  applyClassCache: Map<any, any>;
45
128
  candidateRuleCache: Map<string, Set<[
@@ -58,11 +141,11 @@ interface TailwindcssRuntimeContext {
58
141
  changedContent: any[];
59
142
  classCache: TailwindcssClassCache;
60
143
  disposables: any[];
61
- getClassList: Function;
62
- getClassOrder: Function;
63
- getVariants: Function;
64
- markInvalidUtilityCandidate: Function;
65
- markInvalidUtilityNode: Function;
144
+ getClassList: (...args: any[]) => any;
145
+ getClassOrder: (...args: any[]) => any;
146
+ getVariants: (...args: any[]) => any;
147
+ markInvalidUtilityCandidate: (...args: any[]) => any;
148
+ markInvalidUtilityNode: (...args: any[]) => any;
66
149
  notClassCache: Set<string>;
67
150
  offsets: {
68
151
  layerPositions: object;
@@ -75,12 +158,19 @@ interface TailwindcssRuntimeContext {
75
158
  stylesheetCache: Record<string, Set<any>>;
76
159
  tailwindConfig: Config;
77
160
  userConfigPath: string | null;
78
- variantMap: Map<string, [[object, Function]]>;
161
+ variantMap: Map<string, [[object, (...args: any[]) => unknown]]>;
79
162
  variantOptions: Map<string, object>;
80
163
  }
81
- type DeepRequired<T> = {
82
- [K in keyof T]: Required<DeepRequired<T[K]>>;
83
- };
164
+ interface ExtractResult {
165
+ classList: string[];
166
+ classSet: Set<string>;
167
+ filename?: string;
168
+ }
169
+ interface TailwindPatchRuntime {
170
+ options: NormalizedTailwindcssPatchOptions;
171
+ majorVersion: 2 | 3 | 4;
172
+ }
173
+
84
174
  interface ILengthUnitsPatchOptions {
85
175
  units: string[];
86
176
  lengthUnitsFilePath?: string;
@@ -89,17 +179,79 @@ interface ILengthUnitsPatchOptions {
89
179
  destPath?: string;
90
180
  }
91
181
 
92
- declare function getCacheOptions(options?: CacheOptions | boolean): InternalCacheOptions;
93
- declare class CacheManager {
94
- options: Required<CacheOptions> & {
95
- filename: string;
96
- };
97
- constructor(options?: CacheOptions);
98
- getOptions(options?: CacheOptions): Required<CacheOptions> & {
99
- filename: string;
182
+ interface ExposeContextPatchParams {
183
+ rootDir: string;
184
+ refProperty: string;
185
+ overwrite: boolean;
186
+ majorVersion: 2 | 3;
187
+ }
188
+ interface ExposeContextPatchResult {
189
+ applied: boolean;
190
+ files: Record<string, string>;
191
+ }
192
+ declare function applyExposeContextPatch(params: ExposeContextPatchParams): ExposeContextPatchResult;
193
+
194
+ declare function applyExtendLengthUnitsPatchV3(rootDir: string, options: NormalizedExtendLengthUnitsOptions): {
195
+ changed: boolean;
196
+ code: undefined;
197
+ } | {
198
+ changed: boolean;
199
+ code: string;
200
+ };
201
+ interface V4FilePatch {
202
+ file: string;
203
+ code: string;
204
+ hasPatched: boolean;
205
+ }
206
+ interface V4Candidate extends V4FilePatch {
207
+ match: RegExpExecArray;
208
+ }
209
+ declare function applyExtendLengthUnitsPatchV4(rootDir: string, options: NormalizedExtendLengthUnitsOptions): {
210
+ changed: boolean;
211
+ files: V4Candidate[];
212
+ };
213
+
214
+ interface PatchRunnerResult {
215
+ exposeContext?: ReturnType<typeof applyExposeContextPatch>;
216
+ extendLengthUnits?: ReturnType<typeof applyExtendLengthUnitsPatchV3> | ReturnType<typeof applyExtendLengthUnitsPatchV4>;
217
+ }
218
+
219
+ interface LegacyCacheOptions {
220
+ dir?: string;
221
+ cwd?: string;
222
+ file?: string;
223
+ strategy?: 'merge' | 'overwrite';
224
+ enabled?: boolean;
225
+ }
226
+ interface LegacyOutputOptions {
227
+ filename?: string;
228
+ loose?: boolean;
229
+ removeUniversalSelector?: boolean;
230
+ }
231
+ interface LegacyTailwindcssOptions {
232
+ version?: 2 | 3 | 4;
233
+ v2?: TailwindLocatorOptions;
234
+ v3?: TailwindLocatorOptions;
235
+ v4?: TailwindNextOptions;
236
+ config?: string;
237
+ cwd?: string;
238
+ }
239
+ interface LegacyPatchOptions {
240
+ packageName?: string;
241
+ output?: LegacyOutputOptions;
242
+ tailwindcss?: LegacyTailwindcssOptions;
243
+ overwrite?: boolean;
244
+ applyPatches?: {
245
+ exportContext?: boolean;
246
+ extendLengthUnits?: boolean | ILengthUnitsPatchOptions;
100
247
  };
101
- write(data: Set<string>): string | undefined;
102
- read(): Set<string>;
248
+ filter?: (className: string) => boolean;
249
+ cwd?: string;
250
+ resolve?: PackageResolvingOptions;
251
+ }
252
+ interface LegacyTailwindcssPatcherOptions {
253
+ cache?: LegacyCacheOptions | boolean;
254
+ patch?: LegacyPatchOptions;
103
255
  }
104
256
 
105
257
  interface ExtractValidCandidatesOption {
@@ -108,59 +260,57 @@ interface ExtractValidCandidatesOption {
108
260
  css?: string;
109
261
  cwd?: string;
110
262
  }
263
+ declare function extractRawCandidatesWithPositions(content: string, extension?: string): Promise<{
264
+ rawCandidate: string;
265
+ start: number;
266
+ end: number;
267
+ }[]>;
268
+ declare function extractRawCandidates(sources?: SourceEntry[]): Promise<string[]>;
111
269
  declare function extractValidCandidates(options?: ExtractValidCandidatesOption): Promise<string[]>;
112
270
 
113
- interface PatchExtractOptions {
114
- write?: boolean;
115
- }
271
+ type TailwindMajorVersion = 2 | 3 | 4;
272
+ type TailwindcssPatcherInitOptions = TailwindcssPatchOptions | LegacyTailwindcssPatcherOptions;
116
273
  declare class TailwindcssPatcher {
117
- rawOptions: TailwindcssPatcherOptions;
118
- cacheOptions: InternalCacheOptions;
119
- patchOptions: InternalPatchOptions;
120
- patch: () => void;
121
- cacheManager: CacheManager;
122
- packageInfo: PackageInfo;
123
- majorVersion?: number;
124
- filter?: (className: string) => boolean;
125
- constructor(options?: TailwindcssPatcherOptions);
126
- setCache(set: Set<string>): string | undefined;
127
- getCache(): Set<string>;
274
+ readonly options: NormalizedTailwindcssPatchOptions;
275
+ readonly packageInfo: PackageInfo;
276
+ readonly majorVersion: TailwindMajorVersion;
277
+ private readonly cacheStore;
278
+ constructor(options?: TailwindcssPatcherInitOptions);
279
+ patch(): Promise<PatchRunnerResult>;
128
280
  getContexts(): TailwindcssRuntimeContext[];
129
- getClassCaches(): TailwindcssClassCache[];
130
- getClassCacheSet(): Promise<Set<string>>;
131
- getClassCacheSetV3(): Set<string>;
132
- /**
133
- * @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
134
- */
281
+ private runTailwindBuildIfNeeded;
282
+ private collectClassSet;
283
+ private mergeWithCache;
135
284
  getClassSet(): Promise<Set<string>>;
136
- getClassSetV3(): Set<string>;
137
- extract(options?: PatchExtractOptions): Promise<{
138
- classList: string[];
139
- classSet: Set<string>;
140
- filename?: string;
141
- } | undefined>;
285
+ extract(options?: {
286
+ write?: boolean;
287
+ }): Promise<ExtractResult>;
142
288
  extractValidCandidates: typeof extractValidCandidates;
143
289
  }
144
290
 
145
- declare function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatchOptions): Record<string, any> | undefined;
146
- declare function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatchOptions): Record<string, any>;
147
-
148
- declare function monkeyPatchForSupportingCustomUnitV3(rootDir: string, options?: Partial<ILengthUnitsPatchOptions>): {
149
- [x: string]: string;
150
- } | undefined;
151
- interface V4GuessFile {
152
- code: string;
153
- hasPatched: boolean;
154
- file: string;
155
- matches: RegExpMatchArray;
291
+ declare class CacheStore {
292
+ private readonly options;
293
+ constructor(options: NormalizedCacheOptions);
294
+ private ensureDir;
295
+ write(data: Set<string>): Promise<string | undefined>;
296
+ read(): Promise<Set<string>>;
156
297
  }
157
- declare function monkeyPatchForSupportingCustomUnitV4(rootDir: string, options?: Partial<ILengthUnitsPatchOptions>): {
158
- files: V4GuessFile[];
159
- };
160
-
161
- declare function internalPatch(pkgJsonPath: PackageInfo, options: InternalPatchOptions): any;
162
- declare function internalPatch(pkgJsonPath: string, options: InternalPatchOptions): any;
163
298
 
164
299
  declare const logger: consola.ConsolaInstance;
165
300
 
166
- export { CacheManager, type CacheOptions, type CacheStrategy, type DeepRequired, type ILengthUnitsPatchOptions, type InternalCacheOptions, type InternalPatchOptions, type PatchExtractOptions, type PatchOptions, type TailwindcssClassCache, TailwindcssPatcher, type TailwindcssPatcherOptions, type TailwindcssRuntimeContext, getCacheOptions, internalPatch, logger, monkeyPatchForExposingContextV2, monkeyPatchForExposingContextV3, monkeyPatchForSupportingCustomUnitV3, monkeyPatchForSupportingCustomUnitV4 };
301
+ declare function normalizeOptions(options?: TailwindcssPatchOptions): NormalizedTailwindcssPatchOptions;
302
+
303
+ declare function collectClassesFromContexts(contexts: TailwindcssRuntimeContext[], filter: (className: string) => boolean): Set<string>;
304
+ declare function collectClassesFromTailwindV4(options: NormalizedTailwindcssPatchOptions): Promise<Set<string>>;
305
+
306
+ declare function loadRuntimeContexts(packageInfo: PackageInfo, majorVersion: 2 | 3 | 4, refProperty: string): TailwindcssRuntimeContext[];
307
+
308
+ interface TailwindBuildOptions {
309
+ cwd: string;
310
+ config?: string;
311
+ majorVersion: 2 | 3 | 4;
312
+ postcssPlugin?: string;
313
+ }
314
+ declare function runTailwindBuild(options: TailwindBuildOptions): Promise<postcss.Result<postcss.Root>>;
315
+
316
+ export { CacheStore, type CacheStrategy, type ExtractResult, type ILengthUnitsPatchOptions, type NormalizedTailwindcssPatchOptions, type TailwindPatchRuntime, type TailwindcssClassCache, type TailwindcssPatchOptions, TailwindcssPatcher, type TailwindcssRuntimeContext, collectClassesFromContexts, collectClassesFromTailwindV4, extractRawCandidates, extractRawCandidatesWithPositions, extractValidCandidates, loadRuntimeContexts, logger, normalizeOptions, runTailwindBuild };