tailwindcss-patch 8.7.4-alpha.0 → 9.0.0-alpha.2
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 +65 -5
- package/dist/{chunk-ZXW4S356.js → chunk-TOAZIPHJ.js} +295 -285
- package/dist/{chunk-6ZDYMYHE.mjs → chunk-VDWTCQ74.mjs} +259 -249
- package/dist/cli.js +4 -4
- package/dist/cli.mjs +1 -1
- package/dist/index.d.mts +46 -134
- package/dist/index.d.ts +46 -134
- package/dist/index.js +5 -3
- package/dist/index.mjs +4 -2
- package/package.json +8 -3
- package/src/api/tailwindcss-patcher.ts +424 -0
- package/src/babel/index.ts +12 -0
- package/src/cache/context.ts +212 -0
- package/src/cache/store.ts +1440 -0
- package/src/cache/types.ts +71 -0
- package/src/cli.ts +20 -0
- package/src/commands/basic-handlers.ts +145 -0
- package/src/commands/cli.ts +56 -0
- package/src/commands/command-context.ts +77 -0
- package/src/commands/command-definitions.ts +102 -0
- package/src/commands/command-metadata.ts +68 -0
- package/src/commands/command-registrar.ts +39 -0
- package/src/commands/command-runtime.ts +33 -0
- package/src/commands/default-handler-map.ts +25 -0
- package/src/commands/migrate-config.ts +104 -0
- package/src/commands/migrate-handler.ts +67 -0
- package/src/commands/migration-aggregation.ts +100 -0
- package/src/commands/migration-args.ts +85 -0
- package/src/commands/migration-file-executor.ts +189 -0
- package/src/commands/migration-output.ts +115 -0
- package/src/commands/migration-report-loader.ts +26 -0
- package/src/commands/migration-report.ts +21 -0
- package/src/commands/migration-source.ts +318 -0
- package/src/commands/migration-target-files.ts +161 -0
- package/src/commands/migration-target-resolver.ts +34 -0
- package/src/commands/migration-types.ts +65 -0
- package/src/commands/restore-handler.ts +24 -0
- package/src/commands/status-handler.ts +17 -0
- package/src/commands/status-output.ts +60 -0
- package/src/commands/token-output.ts +30 -0
- package/src/commands/types.ts +137 -0
- package/src/commands/validate-handler.ts +42 -0
- package/src/commands/validate.ts +83 -0
- package/src/config/index.ts +25 -0
- package/src/config/workspace.ts +87 -0
- package/src/constants.ts +4 -0
- package/src/extraction/candidate-extractor.ts +354 -0
- package/src/index.ts +57 -0
- package/src/install/class-collector.ts +1 -0
- package/src/install/context-registry.ts +1 -0
- package/src/install/index.ts +5 -0
- package/src/install/patch-runner.ts +1 -0
- package/src/install/process-tailwindcss.ts +1 -0
- package/src/install/status.ts +1 -0
- package/src/logger.ts +5 -0
- package/src/options/legacy.ts +93 -0
- package/src/options/normalize.ts +262 -0
- package/src/options/types.ts +217 -0
- package/src/patching/operations/export-context/index.ts +110 -0
- package/src/patching/operations/export-context/postcss-v2.ts +235 -0
- package/src/patching/operations/export-context/postcss-v3.ts +249 -0
- package/src/patching/operations/extend-length-units.ts +197 -0
- package/src/patching/patch-runner.ts +46 -0
- package/src/patching/status.ts +262 -0
- package/src/runtime/class-collector.ts +105 -0
- package/src/runtime/collector.ts +148 -0
- package/src/runtime/context-registry.ts +65 -0
- package/src/runtime/process-tailwindcss.ts +115 -0
- package/src/types.ts +159 -0
- package/src/utils.ts +52 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type { ILengthUnitsPatchOptions } from '../types'
|
|
2
|
+
import type {
|
|
3
|
+
CacheDriver,
|
|
4
|
+
CacheStrategy,
|
|
5
|
+
ApplyOptions,
|
|
6
|
+
ExtractOptions,
|
|
7
|
+
NormalizedCacheOptions,
|
|
8
|
+
NormalizedTailwindCssPatchOptions,
|
|
9
|
+
NormalizedFeatureOptions,
|
|
10
|
+
NormalizedOutputOptions,
|
|
11
|
+
NormalizedTailwindConfigOptions,
|
|
12
|
+
NormalizedTailwindV4Options,
|
|
13
|
+
TailwindCssOptions,
|
|
14
|
+
TailwindCssPatchOptions,
|
|
15
|
+
TailwindV4Options,
|
|
16
|
+
} from './types'
|
|
17
|
+
import process from 'node:process'
|
|
18
|
+
import fs from 'fs-extra'
|
|
19
|
+
import path from 'pathe'
|
|
20
|
+
import { pkgName } from '../constants'
|
|
21
|
+
|
|
22
|
+
function resolveRealpathSafe(value: string) {
|
|
23
|
+
const resolved = path.resolve(value)
|
|
24
|
+
try {
|
|
25
|
+
return path.normalize(fs.realpathSync(resolved))
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return path.normalize(resolved)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function toPrettyValue(value: ExtractOptions['pretty']): number | false {
|
|
33
|
+
if (typeof value === 'number') {
|
|
34
|
+
return value > 0 ? value : false
|
|
35
|
+
}
|
|
36
|
+
if (value === true) {
|
|
37
|
+
return 2
|
|
38
|
+
}
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function normalizeCacheDriver(driver: CacheDriver | undefined): CacheDriver {
|
|
43
|
+
if (driver === 'memory' || driver === 'noop') {
|
|
44
|
+
return driver
|
|
45
|
+
}
|
|
46
|
+
return 'file'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function normalizeCacheOptions(
|
|
50
|
+
cache: TailwindCssPatchOptions['cache'],
|
|
51
|
+
projectRoot: string,
|
|
52
|
+
): NormalizedCacheOptions {
|
|
53
|
+
let enabled = false
|
|
54
|
+
let cwd = resolveRealpathSafe(projectRoot)
|
|
55
|
+
let dir = path.resolve(cwd, 'node_modules/.cache', pkgName)
|
|
56
|
+
let file = 'class-cache.json'
|
|
57
|
+
let strategy: CacheStrategy = 'merge'
|
|
58
|
+
let driver: CacheDriver = 'file'
|
|
59
|
+
|
|
60
|
+
if (typeof cache === 'boolean') {
|
|
61
|
+
enabled = cache
|
|
62
|
+
}
|
|
63
|
+
else if (typeof cache === 'object' && cache) {
|
|
64
|
+
enabled = cache.enabled ?? true
|
|
65
|
+
cwd = cache.cwd ? resolveRealpathSafe(cache.cwd) : cwd
|
|
66
|
+
dir = cache.dir ? path.resolve(cache.dir) : path.resolve(cwd, 'node_modules/.cache', pkgName)
|
|
67
|
+
file = cache.file ?? file
|
|
68
|
+
strategy = cache.strategy ?? strategy
|
|
69
|
+
driver = normalizeCacheDriver(cache.driver)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const filename = path.resolve(dir, file)
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
enabled,
|
|
76
|
+
cwd,
|
|
77
|
+
dir,
|
|
78
|
+
file,
|
|
79
|
+
path: filename,
|
|
80
|
+
strategy,
|
|
81
|
+
driver,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function normalizeOutputOptions(output: ExtractOptions | undefined): NormalizedOutputOptions {
|
|
86
|
+
const enabled = output?.write ?? true
|
|
87
|
+
const file = output?.file ?? '.tw-patch/tw-class-list.json'
|
|
88
|
+
const format = output?.format ?? 'json'
|
|
89
|
+
const pretty = toPrettyValue(output?.pretty ?? true)
|
|
90
|
+
const removeUniversalSelector = output?.removeUniversalSelector ?? true
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
enabled,
|
|
94
|
+
file,
|
|
95
|
+
format,
|
|
96
|
+
pretty,
|
|
97
|
+
removeUniversalSelector,
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function normalizeExposeContextOptions(exposeContext: ApplyOptions['exposeContext'] | undefined): NormalizedFeatureOptions['exposeContext'] {
|
|
102
|
+
if (exposeContext === false) {
|
|
103
|
+
return {
|
|
104
|
+
enabled: false,
|
|
105
|
+
refProperty: 'contextRef',
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof exposeContext === 'object' && exposeContext) {
|
|
110
|
+
return {
|
|
111
|
+
enabled: true,
|
|
112
|
+
refProperty: exposeContext.refProperty ?? 'contextRef',
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
enabled: true,
|
|
118
|
+
refProperty: 'contextRef',
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function normalizeExtendLengthUnitsOptions(extend: ApplyOptions['extendLengthUnits'] | undefined): NormalizedFeatureOptions['extendLengthUnits'] {
|
|
123
|
+
if (extend === false || extend === undefined) {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (extend.enabled === false) {
|
|
128
|
+
return null
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const base: ILengthUnitsPatchOptions = {
|
|
132
|
+
units: ['rpx'],
|
|
133
|
+
overwrite: true,
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
...base,
|
|
138
|
+
...extend,
|
|
139
|
+
enabled: extend.enabled ?? true,
|
|
140
|
+
units: extend.units ?? base.units,
|
|
141
|
+
overwrite: extend.overwrite ?? base.overwrite!,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function normalizeTailwindV4Options(
|
|
146
|
+
v4: TailwindV4Options | undefined,
|
|
147
|
+
fallbackBase: string,
|
|
148
|
+
): NormalizedTailwindV4Options {
|
|
149
|
+
const configuredBase = v4?.base ? path.resolve(v4.base) : undefined
|
|
150
|
+
const base = configuredBase ?? fallbackBase
|
|
151
|
+
const cssEntries = Array.isArray(v4?.cssEntries)
|
|
152
|
+
? v4!.cssEntries.filter((entry): entry is string => Boolean(entry)).map(entry => path.resolve(entry))
|
|
153
|
+
: []
|
|
154
|
+
const userSources = v4?.sources
|
|
155
|
+
const hasUserDefinedSources = Boolean(userSources?.length)
|
|
156
|
+
const sources: NormalizedTailwindV4Options['sources'] = hasUserDefinedSources
|
|
157
|
+
? userSources!
|
|
158
|
+
: [
|
|
159
|
+
{
|
|
160
|
+
base: fallbackBase,
|
|
161
|
+
pattern: '**/*',
|
|
162
|
+
negated: false,
|
|
163
|
+
},
|
|
164
|
+
]
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
base,
|
|
168
|
+
...(configuredBase === undefined ? {} : { configuredBase }),
|
|
169
|
+
...(v4?.css === undefined ? {} : { css: v4.css }),
|
|
170
|
+
cssEntries,
|
|
171
|
+
sources,
|
|
172
|
+
hasUserDefinedSources,
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function normalizeTailwindOptions(
|
|
177
|
+
tailwind: TailwindCssOptions | undefined,
|
|
178
|
+
projectRoot: string,
|
|
179
|
+
): NormalizedTailwindConfigOptions {
|
|
180
|
+
const packageName = tailwind?.packageName ?? 'tailwindcss'
|
|
181
|
+
const versionHint = tailwind?.version
|
|
182
|
+
const resolve = tailwind?.resolve
|
|
183
|
+
|
|
184
|
+
const cwd = tailwind?.cwd ?? projectRoot
|
|
185
|
+
const config = tailwind?.config
|
|
186
|
+
const postcssPlugin = tailwind?.postcssPlugin
|
|
187
|
+
|
|
188
|
+
const v4 = normalizeTailwindV4Options(tailwind?.v4, cwd)
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
packageName,
|
|
192
|
+
cwd,
|
|
193
|
+
...(versionHint === undefined ? {} : { versionHint }),
|
|
194
|
+
...(resolve === undefined ? {} : { resolve }),
|
|
195
|
+
...(config === undefined ? {} : { config }),
|
|
196
|
+
...(postcssPlugin === undefined ? {} : { postcssPlugin }),
|
|
197
|
+
...(tailwind?.v2 === undefined ? {} : { v2: tailwind.v2 }),
|
|
198
|
+
...(tailwind?.v3 === undefined ? {} : { v3: tailwind.v3 }),
|
|
199
|
+
v4,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const deprecatedOptionMapping = {
|
|
204
|
+
cwd: 'projectRoot',
|
|
205
|
+
overwrite: 'apply.overwrite',
|
|
206
|
+
tailwind: 'tailwindcss',
|
|
207
|
+
features: 'apply',
|
|
208
|
+
output: 'extract',
|
|
209
|
+
} as const
|
|
210
|
+
|
|
211
|
+
type DeprecatedTopLevelOptionKey = keyof typeof deprecatedOptionMapping
|
|
212
|
+
|
|
213
|
+
function assertNoDeprecatedOptions(options: TailwindCssPatchOptions) {
|
|
214
|
+
const used = (Object.keys(deprecatedOptionMapping) as DeprecatedTopLevelOptionKey[])
|
|
215
|
+
.filter(key => Object.prototype.hasOwnProperty.call(options, key))
|
|
216
|
+
|
|
217
|
+
if (used.length === 0) {
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const mapping = used.map(key => `${key} -> ${deprecatedOptionMapping[key]}`).join(', ')
|
|
222
|
+
throw new Error(
|
|
223
|
+
`Legacy TailwindcssPatcher options are no longer supported: ${used.join(', ')}. Use the modern fields instead: ${mapping}.`,
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function normalizeOptions(options: TailwindCssPatchOptions = {}): NormalizedTailwindCssPatchOptions {
|
|
228
|
+
assertNoDeprecatedOptions(options)
|
|
229
|
+
|
|
230
|
+
const projectRoot = resolveRealpathSafe(options.projectRoot ? path.resolve(options.projectRoot) : process.cwd())
|
|
231
|
+
const overwrite = options.apply?.overwrite ?? true
|
|
232
|
+
|
|
233
|
+
const output = normalizeOutputOptions(options.extract)
|
|
234
|
+
const cache = normalizeCacheOptions(options.cache, projectRoot)
|
|
235
|
+
const tailwind = normalizeTailwindOptions(options.tailwindcss, projectRoot)
|
|
236
|
+
const exposeContext = normalizeExposeContextOptions(options.apply?.exposeContext)
|
|
237
|
+
const extendLengthUnits = normalizeExtendLengthUnitsOptions(options.apply?.extendLengthUnits)
|
|
238
|
+
|
|
239
|
+
const filter = (className: string) => {
|
|
240
|
+
if (output.removeUniversalSelector && className === '*') {
|
|
241
|
+
return false
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (typeof options.filter === 'function') {
|
|
245
|
+
return options.filter(className) !== false
|
|
246
|
+
}
|
|
247
|
+
return true
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
projectRoot,
|
|
252
|
+
overwrite,
|
|
253
|
+
tailwind,
|
|
254
|
+
features: {
|
|
255
|
+
exposeContext,
|
|
256
|
+
extendLengthUnits,
|
|
257
|
+
},
|
|
258
|
+
output,
|
|
259
|
+
cache,
|
|
260
|
+
filter,
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type { SourceEntry } from '@tailwindcss/oxide'
|
|
2
|
+
import type { PackageResolvingOptions } from 'local-pkg'
|
|
3
|
+
import type { ILengthUnitsPatchOptions } from '../types'
|
|
4
|
+
|
|
5
|
+
export type CacheStrategy = 'merge' | 'overwrite'
|
|
6
|
+
export type CacheDriver = 'file' | 'memory' | 'noop'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Configures how the Tailwind class cache is stored and where it lives on disk.
|
|
10
|
+
*/
|
|
11
|
+
export interface CacheOptions {
|
|
12
|
+
/** Whether caching is enabled. */
|
|
13
|
+
enabled?: boolean
|
|
14
|
+
/** Working directory used when resolving cache paths. */
|
|
15
|
+
cwd?: string
|
|
16
|
+
/** Directory where cache files are written. */
|
|
17
|
+
dir?: string
|
|
18
|
+
/**
|
|
19
|
+
* Cache filename. Defaults to `class-cache.json` inside the derived cache folder
|
|
20
|
+
* when omitted.
|
|
21
|
+
*/
|
|
22
|
+
file?: string
|
|
23
|
+
/** Strategy used when merging new class lists with an existing cache. */
|
|
24
|
+
strategy?: CacheStrategy
|
|
25
|
+
/** Backend used to persist the cache (`file`, `memory`, or `noop`). Defaults to `file`. */
|
|
26
|
+
driver?: CacheDriver
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Preferred options for extraction output behavior.
|
|
31
|
+
*/
|
|
32
|
+
export interface ExtractOptions {
|
|
33
|
+
/** Whether to produce an output file. */
|
|
34
|
+
write?: boolean
|
|
35
|
+
/** Optional absolute or relative path to the output file. */
|
|
36
|
+
file?: string
|
|
37
|
+
/** Output format, defaults to JSON when omitted. */
|
|
38
|
+
format?: 'json' | 'lines'
|
|
39
|
+
/** Pretty-print spacing (truthy value enables indentation). */
|
|
40
|
+
pretty?: number | boolean
|
|
41
|
+
/** Whether to strip the universal selector (`*`) from the final list. */
|
|
42
|
+
removeUniversalSelector?: boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Options controlling how Tailwind contexts are exposed during runtime patching.
|
|
47
|
+
*/
|
|
48
|
+
export interface ExposeContextOptions {
|
|
49
|
+
/** Name of the property used to reference an exposed context. */
|
|
50
|
+
refProperty?: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Extends the built-in length-unit patch with custom defaults.
|
|
55
|
+
*/
|
|
56
|
+
export interface ExtendLengthUnitsOptions extends Partial<ILengthUnitsPatchOptions> {
|
|
57
|
+
/** Enables or disables the length-unit patch. */
|
|
58
|
+
enabled?: boolean
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Preferred options for runtime patch behavior.
|
|
63
|
+
*/
|
|
64
|
+
export interface ApplyOptions {
|
|
65
|
+
/** Whether patched files can be overwritten on disk. */
|
|
66
|
+
overwrite?: boolean
|
|
67
|
+
/** Whether to expose runtime Tailwind contexts (or configure how they are exposed). */
|
|
68
|
+
exposeContext?: boolean | ExposeContextOptions
|
|
69
|
+
/** Extends the length-unit patch or disables it entirely. */
|
|
70
|
+
extendLengthUnits?: false | ExtendLengthUnitsOptions
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface TailwindRuntimeOptionsBase {
|
|
74
|
+
/** Path to a Tailwind config file when auto-detection is insufficient. */
|
|
75
|
+
config?: string
|
|
76
|
+
/** Custom working directory used when resolving config-relative paths. */
|
|
77
|
+
cwd?: string
|
|
78
|
+
/** Optional PostCSS plugin name to use instead of the default. */
|
|
79
|
+
postcssPlugin?: string
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Configuration specific to Tailwind CSS v2 patching flows.
|
|
84
|
+
*/
|
|
85
|
+
export interface TailwindV2Options extends TailwindRuntimeOptionsBase {}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Configuration specific to Tailwind CSS v3 patching flows.
|
|
89
|
+
*/
|
|
90
|
+
export interface TailwindV3Options extends TailwindRuntimeOptionsBase {}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Additional configuration specific to Tailwind CSS v4 extraction.
|
|
94
|
+
*/
|
|
95
|
+
export interface TailwindV4Options {
|
|
96
|
+
/** Base directory used when resolving v4 content sources and configs. */
|
|
97
|
+
base?: string
|
|
98
|
+
/** Raw CSS passed directly to the v4 design system. */
|
|
99
|
+
css?: string
|
|
100
|
+
/** Set of CSS entry files that should be scanned for `@config` directives. */
|
|
101
|
+
cssEntries?: string[]
|
|
102
|
+
/** Overrides the content sources scanned by the oxide scanner. */
|
|
103
|
+
sources?: SourceEntry[]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* High-level Tailwind patch configuration shared across versions.
|
|
108
|
+
*/
|
|
109
|
+
export interface TailwindCssOptions extends TailwindRuntimeOptionsBase {
|
|
110
|
+
/** Explicit Tailwind CSS major version used by the current project. When omitted, the installed package version is inferred. */
|
|
111
|
+
version?: 2 | 3 | 4
|
|
112
|
+
/** Tailwind package name if the project uses a fork. */
|
|
113
|
+
packageName?: string
|
|
114
|
+
/** Package resolution options forwarded to `local-pkg`. */
|
|
115
|
+
resolve?: PackageResolvingOptions
|
|
116
|
+
/** Overrides applied when patching Tailwind CSS v2. */
|
|
117
|
+
v2?: TailwindV2Options
|
|
118
|
+
/** Overrides applied when patching Tailwind CSS v3. */
|
|
119
|
+
v3?: TailwindV3Options
|
|
120
|
+
/** Options specific to Tailwind CSS v4 patching. */
|
|
121
|
+
v4?: TailwindV4Options
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Root configuration consumed by the Tailwind CSS patch runner.
|
|
126
|
+
*/
|
|
127
|
+
export interface TailwindCssPatchOptions {
|
|
128
|
+
/**
|
|
129
|
+
* Base directory used when resolving Tailwind resources.
|
|
130
|
+
* Defaults to `process.cwd()`.
|
|
131
|
+
*/
|
|
132
|
+
projectRoot?: string
|
|
133
|
+
/** Preferred Tailwind runtime configuration. */
|
|
134
|
+
tailwindcss?: TailwindCssOptions
|
|
135
|
+
/** Preferred patch toggles. */
|
|
136
|
+
apply?: ApplyOptions
|
|
137
|
+
/** Preferred extraction output settings. */
|
|
138
|
+
extract?: ExtractOptions
|
|
139
|
+
/** Optional function that filters final class names. */
|
|
140
|
+
filter?: (className: string) => boolean
|
|
141
|
+
/** Cache configuration or boolean to enable/disable quickly. */
|
|
142
|
+
cache?: boolean | CacheOptions
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Stable shape for output configuration after normalization.
|
|
147
|
+
*/
|
|
148
|
+
export interface NormalizedOutputOptions {
|
|
149
|
+
enabled: boolean
|
|
150
|
+
file?: string
|
|
151
|
+
format: 'json' | 'lines'
|
|
152
|
+
pretty: number | false
|
|
153
|
+
removeUniversalSelector: boolean
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Stable cache configuration used internally after defaults are applied.
|
|
158
|
+
*/
|
|
159
|
+
export interface NormalizedCacheOptions {
|
|
160
|
+
enabled: boolean
|
|
161
|
+
cwd: string
|
|
162
|
+
dir: string
|
|
163
|
+
file: string
|
|
164
|
+
path: string
|
|
165
|
+
strategy: CacheStrategy
|
|
166
|
+
driver: CacheDriver
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Tracks whether runtime contexts should be exposed and via which property. */
|
|
170
|
+
export interface NormalizedExposeContextOptions {
|
|
171
|
+
enabled: boolean
|
|
172
|
+
refProperty: string
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Normalized representation of the extend-length-units feature flag. */
|
|
176
|
+
export interface NormalizedExtendLengthUnitsOptions extends ILengthUnitsPatchOptions {
|
|
177
|
+
enabled: boolean
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Normalized Tailwind v4 configuration consumed by runtime helpers. */
|
|
181
|
+
export interface NormalizedTailwindV4Options {
|
|
182
|
+
base: string
|
|
183
|
+
configuredBase?: string
|
|
184
|
+
css?: string
|
|
185
|
+
cssEntries: string[]
|
|
186
|
+
sources: SourceEntry[]
|
|
187
|
+
hasUserDefinedSources: boolean
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Tailwind configuration ready for consumption by the runtime after normalization.
|
|
192
|
+
*/
|
|
193
|
+
export interface NormalizedTailwindConfigOptions extends TailwindRuntimeOptionsBase {
|
|
194
|
+
packageName: string
|
|
195
|
+
versionHint?: 2 | 3 | 4
|
|
196
|
+
resolve?: PackageResolvingOptions
|
|
197
|
+
v2?: TailwindV2Options
|
|
198
|
+
v3?: TailwindV3Options
|
|
199
|
+
v4?: NormalizedTailwindV4Options
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Grouped normalized feature flags. */
|
|
203
|
+
export interface NormalizedFeatureOptions {
|
|
204
|
+
exposeContext: NormalizedExposeContextOptions
|
|
205
|
+
extendLengthUnits: NormalizedExtendLengthUnitsOptions | null
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Final normalized shape consumed throughout the patch runtime. */
|
|
209
|
+
export interface NormalizedTailwindCssPatchOptions {
|
|
210
|
+
projectRoot: string
|
|
211
|
+
overwrite: boolean
|
|
212
|
+
tailwind: NormalizedTailwindConfigOptions
|
|
213
|
+
features: NormalizedFeatureOptions
|
|
214
|
+
output: NormalizedOutputOptions
|
|
215
|
+
cache: NormalizedCacheOptions
|
|
216
|
+
filter: (className: string) => boolean
|
|
217
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import path from 'pathe'
|
|
3
|
+
import logger from '../../../logger'
|
|
4
|
+
import { transformPostcssPluginV2, transformProcessTailwindFeaturesReturnContextV2 } from './postcss-v2'
|
|
5
|
+
import { transformPostcssPlugin, transformProcessTailwindFeaturesReturnContext } from './postcss-v3'
|
|
6
|
+
|
|
7
|
+
export interface ExposeContextPatchParams {
|
|
8
|
+
rootDir: string
|
|
9
|
+
refProperty: string
|
|
10
|
+
overwrite: boolean
|
|
11
|
+
majorVersion: 2 | 3
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ExposeContextPatchResult {
|
|
15
|
+
applied: boolean
|
|
16
|
+
files: Record<string, string>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function writeFileIfRequired(filePath: string, code: string, overwrite: boolean, successMessage: string) {
|
|
20
|
+
if (!overwrite) {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fs.writeFileSync(filePath, code, {
|
|
25
|
+
encoding: 'utf8',
|
|
26
|
+
})
|
|
27
|
+
logger.success(successMessage)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function applyExposeContextPatch(params: ExposeContextPatchParams): ExposeContextPatchResult {
|
|
31
|
+
const { rootDir, refProperty, overwrite, majorVersion } = params
|
|
32
|
+
const result: ExposeContextPatchResult = {
|
|
33
|
+
applied: false,
|
|
34
|
+
files: {},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (majorVersion === 3) {
|
|
38
|
+
const processFileRelative = 'lib/processTailwindFeatures.js'
|
|
39
|
+
const processFilePath = path.resolve(rootDir, processFileRelative)
|
|
40
|
+
if (fs.existsSync(processFilePath)) {
|
|
41
|
+
const content = fs.readFileSync(processFilePath, 'utf8')
|
|
42
|
+
const { code, hasPatched } = transformProcessTailwindFeaturesReturnContext(content)
|
|
43
|
+
result.files[processFileRelative] = code
|
|
44
|
+
if (!hasPatched) {
|
|
45
|
+
writeFileIfRequired(
|
|
46
|
+
processFilePath,
|
|
47
|
+
code,
|
|
48
|
+
overwrite,
|
|
49
|
+
'Patched Tailwind CSS processTailwindFeatures to expose runtime context.',
|
|
50
|
+
)
|
|
51
|
+
result.applied = true
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const pluginCandidates = ['lib/plugin.js', 'lib/index.js']
|
|
56
|
+
const pluginRelative = pluginCandidates.find(candidate => fs.existsSync(path.resolve(rootDir, candidate)))
|
|
57
|
+
if (pluginRelative) {
|
|
58
|
+
const pluginPath = path.resolve(rootDir, pluginRelative)
|
|
59
|
+
const content = fs.readFileSync(pluginPath, 'utf8')
|
|
60
|
+
const { code, hasPatched } = transformPostcssPlugin(content, { refProperty })
|
|
61
|
+
result.files[pluginRelative] = code
|
|
62
|
+
if (!hasPatched) {
|
|
63
|
+
writeFileIfRequired(
|
|
64
|
+
pluginPath,
|
|
65
|
+
code,
|
|
66
|
+
overwrite,
|
|
67
|
+
'Patched Tailwind CSS plugin entry to collect runtime contexts.',
|
|
68
|
+
)
|
|
69
|
+
result.applied = true
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else if (majorVersion === 2) {
|
|
74
|
+
const processFileRelative = 'lib/jit/processTailwindFeatures.js'
|
|
75
|
+
const processFilePath = path.resolve(rootDir, processFileRelative)
|
|
76
|
+
if (fs.existsSync(processFilePath)) {
|
|
77
|
+
const content = fs.readFileSync(processFilePath, 'utf8')
|
|
78
|
+
const { code, hasPatched } = transformProcessTailwindFeaturesReturnContextV2(content)
|
|
79
|
+
result.files[processFileRelative] = code
|
|
80
|
+
if (!hasPatched) {
|
|
81
|
+
writeFileIfRequired(
|
|
82
|
+
processFilePath,
|
|
83
|
+
code,
|
|
84
|
+
overwrite,
|
|
85
|
+
'Patched Tailwind CSS JIT processTailwindFeatures to expose runtime context.',
|
|
86
|
+
)
|
|
87
|
+
result.applied = true
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const pluginRelative = 'lib/jit/index.js'
|
|
92
|
+
const pluginPath = path.resolve(rootDir, pluginRelative)
|
|
93
|
+
if (fs.existsSync(pluginPath)) {
|
|
94
|
+
const content = fs.readFileSync(pluginPath, 'utf8')
|
|
95
|
+
const { code, hasPatched } = transformPostcssPluginV2(content, { refProperty })
|
|
96
|
+
result.files[pluginRelative] = code
|
|
97
|
+
if (!hasPatched) {
|
|
98
|
+
writeFileIfRequired(
|
|
99
|
+
pluginPath,
|
|
100
|
+
code,
|
|
101
|
+
overwrite,
|
|
102
|
+
'Patched Tailwind CSS JIT entry to collect runtime contexts.',
|
|
103
|
+
)
|
|
104
|
+
result.applied = true
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return result
|
|
110
|
+
}
|