tailwindcss-patch 1.2.7 → 2.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.
@@ -0,0 +1,165 @@
1
+ import { Rule, Node } from 'postcss';
2
+ import { Config } from 'tailwindcss';
3
+ import * as c12 from 'c12';
4
+ import { SyncOpts } from 'resolve';
5
+
6
+ type CacheStrategy = 'merge' | 'overwrite';
7
+ interface CacheOptions {
8
+ dir?: string;
9
+ cwd?: string;
10
+ file?: string;
11
+ strategy?: CacheStrategy;
12
+ }
13
+ type InternalCacheOptions = CacheOptions & {
14
+ enable?: boolean;
15
+ };
16
+ interface PatchOptions {
17
+ overwrite?: boolean;
18
+ paths?: string[];
19
+ basedir?: string;
20
+ custom?: (dir: string, ctx: Record<string, any>) => void;
21
+ }
22
+ interface InternalPatchOptions {
23
+ overwrite: boolean;
24
+ paths?: string[];
25
+ basedir?: string;
26
+ custom?: (dir: string, ctx: Record<string, any>) => void;
27
+ version?: string;
28
+ }
29
+ interface TailwindcssPatcherOptions {
30
+ cache?: CacheOptions | boolean;
31
+ patch?: PatchOptions;
32
+ }
33
+ type TailwindcssClassCache = Map<string, ({
34
+ layer: string;
35
+ options: Record<string, any>;
36
+ sort: Record<string, any>;
37
+ } | Rule)[]>;
38
+ type TailwindcssRuntimeContext = {
39
+ applyClassCache: Map<any, any>;
40
+ candidateRuleCache: Map<string | String, Set<[
41
+ {
42
+ arbitrary: any;
43
+ index: any;
44
+ layer: string;
45
+ options: any[];
46
+ parallelIndex: any;
47
+ parentLayer: string;
48
+ variants: any;
49
+ },
50
+ Node
51
+ ]>>;
52
+ candidateRuleMap: Map<string | String, [object, Node][]>;
53
+ changedContent: any[];
54
+ classCache: TailwindcssClassCache;
55
+ disposables: any[];
56
+ getClassList: Function;
57
+ getClassOrder: Function;
58
+ getVariants: Function;
59
+ markInvalidUtilityCandidate: Function;
60
+ markInvalidUtilityNode: Function;
61
+ notClassCache: Set<String>;
62
+ offsets: {
63
+ layerPositions: object;
64
+ offsets: object;
65
+ reservedVariantBits: any;
66
+ variantOffsets: Map<string, any>;
67
+ };
68
+ postCssNodeCache: Map<object, [Node]>;
69
+ ruleCache: Set<[object, Node]>;
70
+ stylesheetCache: Record<string, Set<any>>;
71
+ tailwindConfig: Config;
72
+ userConfigPath: string | null;
73
+ variantMap: Map<string, [[object, Function]]>;
74
+ variantOptions: Map<string, object>;
75
+ };
76
+ interface UserConfig {
77
+ output?: {
78
+ filename?: string;
79
+ loose?: boolean;
80
+ /**
81
+ * @description remove * in output json
82
+ */
83
+ removeUniversalSelector?: boolean;
84
+ };
85
+ postcss?: {
86
+ configDir?: string;
87
+ };
88
+ tailwindcss?: {};
89
+ }
90
+ type DeepRequired<T> = {
91
+ [K in keyof T]: Required<DeepRequired<T[K]>>;
92
+ };
93
+
94
+ declare function getCacheOptions(options?: CacheOptions | boolean): InternalCacheOptions;
95
+ declare class CacheManager {
96
+ options: Required<CacheOptions> & {
97
+ filename: string;
98
+ };
99
+ constructor(options?: CacheOptions);
100
+ mkdir(cacheDirectory: string): string;
101
+ getOptions(options?: CacheOptions): Required<CacheOptions> & {
102
+ filename: string;
103
+ };
104
+ write(data: Set<string>): string | undefined;
105
+ read(): Set<string> | undefined;
106
+ }
107
+
108
+ declare class TailwindcssPatcher {
109
+ rawOptions: TailwindcssPatcherOptions;
110
+ cacheOptions: InternalCacheOptions;
111
+ patchOptions: InternalPatchOptions;
112
+ patch: () => void;
113
+ cacheManager: CacheManager;
114
+ constructor(options?: TailwindcssPatcherOptions);
115
+ getPkgEntry(basedir?: string): string;
116
+ setCache(set: Set<string>): string | undefined;
117
+ getCache(): Set<string> | undefined;
118
+ /**
119
+ * @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
120
+ * 详见 taro weapp-tailwindcss 独立分包
121
+ * @param basedir
122
+ * @returns
123
+ */
124
+ getClassSet(options?: {
125
+ basedir?: string;
126
+ cacheStrategy?: CacheStrategy;
127
+ removeUniversalSelector?: boolean;
128
+ }): Set<string>;
129
+ getContexts(basedir?: string): TailwindcssRuntimeContext[];
130
+ extract(options: DeepRequired<UserConfig>): Promise<string | undefined>;
131
+ }
132
+
133
+ declare function getTailwindcssEntry(basedir?: string): string;
134
+ declare function getContexts(basedir?: string): TailwindcssRuntimeContext[];
135
+ declare function getClassCaches(basedir?: string): TailwindcssClassCache[];
136
+ declare function getClassCacheSet(basedir?: string, options?: {
137
+ removeUniversalSelector?: boolean;
138
+ }): Set<string>;
139
+
140
+ declare function inspectProcessTailwindFeaturesReturnContext(content: string): {
141
+ code: string;
142
+ hasPatched: boolean;
143
+ };
144
+ declare function inspectPostcssPlugin(content: string): {
145
+ code: string;
146
+ hasPatched: boolean;
147
+ };
148
+
149
+ declare function getInstalledPkgJsonPath(options?: PatchOptions): string | undefined;
150
+ declare function getPatchOptions(options?: PatchOptions): InternalPatchOptions;
151
+ declare function createPatch(opt: InternalPatchOptions): () => any;
152
+ declare function monkeyPatchForExposingContext(twDir: string, opt: InternalPatchOptions): {
153
+ processTailwindFeatures?: string | undefined;
154
+ plugin?: string | undefined;
155
+ } & Record<string, any>;
156
+ declare function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions): any | undefined;
157
+
158
+ declare function getConfig(): Promise<c12.ResolvedConfig<DeepRequired<UserConfig>, c12.ConfigLayerMeta>>;
159
+ declare const defineConfig: c12.DefineConfig<UserConfig, c12.ConfigLayerMeta>;
160
+
161
+ declare function ensureFileContent(filepaths: string | string[]): string | undefined;
162
+ declare function requireResolve(id: string, opts?: SyncOpts): string;
163
+ declare function ensureDir(p: string): Promise<void>;
164
+
165
+ export { CacheManager, CacheOptions, CacheStrategy, DeepRequired, InternalCacheOptions, InternalPatchOptions, PatchOptions, TailwindcssClassCache, TailwindcssPatcher, TailwindcssPatcherOptions, TailwindcssRuntimeContext, UserConfig, createPatch, defineConfig, ensureDir, ensureFileContent, getCacheOptions, getClassCacheSet, getClassCaches, getConfig, getContexts, getInstalledPkgJsonPath, getPatchOptions, getTailwindcssEntry, inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext, internalPatch, monkeyPatchForExposingContext, requireResolve };
package/dist/index.mjs ADDED
@@ -0,0 +1,519 @@
1
+ import path, { dirname } from 'node:path';
2
+ import fs from 'node:fs';
3
+ import fs$1 from 'node:fs/promises';
4
+ import pkg from 'resolve';
5
+ import { gte } from 'semver';
6
+ import * as t from '@babel/types';
7
+ import generate from '@babel/generator';
8
+ import traverse from '@babel/traverse';
9
+ import { parse } from '@babel/parser';
10
+ import postcss from 'postcss';
11
+ import postcssrc from 'postcss-load-config';
12
+ import { createDefineConfig, loadConfig } from 'c12';
13
+
14
+
15
+
16
+ // -- Unbuild CommonJS Shims --
17
+ import __cjs_url__ from 'url';
18
+ import __cjs_path__ from 'path';
19
+ import __cjs_mod__ from 'module';
20
+ const __filename = __cjs_url__.fileURLToPath(import.meta.url);
21
+ const __dirname = __cjs_path__.dirname(__filename);
22
+ const require = __cjs_mod__.createRequire(import.meta.url);
23
+ const { sync } = pkg;
24
+ function ensureFileContent(filepaths) {
25
+ if (typeof filepaths === "string") {
26
+ filepaths = [filepaths];
27
+ }
28
+ let content;
29
+ for (const filepath of filepaths) {
30
+ if (fs.existsSync(filepath)) {
31
+ content = fs.readFileSync(filepath, {
32
+ encoding: "utf8"
33
+ });
34
+ break;
35
+ }
36
+ }
37
+ return content;
38
+ }
39
+ function requireResolve(id, opts) {
40
+ return sync(id, opts);
41
+ }
42
+ async function ensureDir(p) {
43
+ try {
44
+ await fs$1.access(p);
45
+ } catch {
46
+ await fs$1.mkdir(p, {
47
+ recursive: true
48
+ });
49
+ }
50
+ }
51
+
52
+ function getTailwindcssEntry(basedir = process.cwd()) {
53
+ return requireResolve("tailwindcss");
54
+ }
55
+ function getContexts(basedir) {
56
+ const twPath = getTailwindcssEntry(basedir);
57
+ const distPath = path.dirname(twPath);
58
+ let injectFilePath = path.join(distPath, "plugin.js");
59
+ if (!fs.existsSync(injectFilePath)) {
60
+ injectFilePath = path.join(distPath, "index.js");
61
+ }
62
+ const mo = require(injectFilePath);
63
+ if (mo.contextRef) {
64
+ return mo.contextRef.value;
65
+ }
66
+ return [];
67
+ }
68
+ function getClassCaches(basedir) {
69
+ const contexts = getContexts(basedir);
70
+ return contexts.map((x) => x.classCache);
71
+ }
72
+ function getClassCacheSet(basedir, options) {
73
+ const classCaches = getClassCaches(basedir);
74
+ const classSet = /* @__PURE__ */ new Set();
75
+ for (const classCacheMap of classCaches) {
76
+ const keys = classCacheMap.keys();
77
+ for (const key of keys) {
78
+ if (options?.removeUniversalSelector && key.toString() === "*") {
79
+ continue;
80
+ }
81
+ classSet.add(key.toString());
82
+ }
83
+ }
84
+ return classSet;
85
+ }
86
+
87
+ const pkgName = "tailwindcss-patch";
88
+
89
+ function log(message, ...optionalParams) {
90
+ return console.log(`[${pkgName}]:` + message, ...optionalParams);
91
+ }
92
+
93
+ function getCacheOptions(options) {
94
+ let cache;
95
+ switch (typeof options) {
96
+ case "undefined": {
97
+ cache = {
98
+ enable: false
99
+ };
100
+ break;
101
+ }
102
+ case "boolean": {
103
+ cache = {
104
+ enable: options
105
+ };
106
+ break;
107
+ }
108
+ case "object": {
109
+ cache = { ...options, enable: true };
110
+ break;
111
+ }
112
+ }
113
+ return cache;
114
+ }
115
+ class CacheManager {
116
+ constructor(options = {}) {
117
+ this.options = this.getOptions(options);
118
+ }
119
+ mkdir(cacheDirectory) {
120
+ const exists = fs.existsSync(cacheDirectory);
121
+ if (!exists) {
122
+ fs.mkdirSync(cacheDirectory, {
123
+ recursive: true
124
+ });
125
+ }
126
+ return cacheDirectory;
127
+ }
128
+ getOptions(options = {}) {
129
+ const cwd = options.cwd ?? process.cwd();
130
+ const dir = options.dir ?? path.resolve(cwd, "node_modules/.cache", pkgName);
131
+ const file = options.file ?? "index.json";
132
+ const filename = path.resolve(dir, file);
133
+ return {
134
+ cwd,
135
+ dir,
136
+ file,
137
+ filename,
138
+ strategy: "merge"
139
+ };
140
+ }
141
+ write(data) {
142
+ try {
143
+ const { dir, filename } = this.options;
144
+ this.mkdir(dir);
145
+ fs.writeFileSync(filename, JSON.stringify([...data], void 0, 2), "utf8");
146
+ return filename;
147
+ } catch {
148
+ log("write cache file fail!");
149
+ }
150
+ }
151
+ read() {
152
+ const { filename } = this.options;
153
+ try {
154
+ if (fs.existsSync(filename)) {
155
+ const data = fs.readFileSync(filename, "utf8");
156
+ return new Set(JSON.parse(data));
157
+ }
158
+ } catch {
159
+ log("parse cache content fail! path:" + filename);
160
+ try {
161
+ fs.unlinkSync(filename);
162
+ } catch {
163
+ log("delete cache file fail! path:" + filename);
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ function inspectProcessTailwindFeaturesReturnContext(content) {
170
+ const ast = parse(content);
171
+ let hasPatched = false;
172
+ traverse(ast, {
173
+ FunctionDeclaration(p) {
174
+ const n = p.node;
175
+ if (n.id?.name === "processTailwindFeatures" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
176
+ const rts = n.body.body[0];
177
+ if (t.isFunctionExpression(rts.argument)) {
178
+ const body = rts.argument.body.body;
179
+ const lastStatement = body.at(-1);
180
+ hasPatched = t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === "context";
181
+ if (!hasPatched) {
182
+ const rts2 = t.returnStatement(t.identifier("context"));
183
+ body.push(rts2);
184
+ }
185
+ }
186
+ }
187
+ }
188
+ });
189
+ return {
190
+ code: hasPatched ? content : generate(ast).code,
191
+ hasPatched
192
+ };
193
+ }
194
+ function inspectPostcssPlugin(content) {
195
+ const ast = parse(content);
196
+ const exportKey = "contextRef";
197
+ const variableName = "contextRef";
198
+ const valueKey = "value";
199
+ let hasPatched = false;
200
+ traverse(ast, {
201
+ Program(p) {
202
+ const n = p.node;
203
+ const idx = n.body.findIndex((x) => {
204
+ return t.isExpressionStatement(x) && t.isAssignmentExpression(x.expression) && t.isMemberExpression(x.expression.left) && t.isFunctionExpression(x.expression.right) && x.expression.right.id?.name === "tailwindcss";
205
+ });
206
+ if (idx > -1) {
207
+ const prevStatement = n.body[idx - 1];
208
+ const lastStatement = n.body.at(-1);
209
+ const hasPatchedCondition0 = prevStatement && t.isVariableDeclaration(prevStatement) && prevStatement.declarations.length === 1 && t.isIdentifier(prevStatement.declarations[0].id) && prevStatement.declarations[0].id.name === variableName;
210
+ const hasPatchedCondition1 = t.isExpressionStatement(lastStatement) && t.isAssignmentExpression(lastStatement.expression) && t.isIdentifier(lastStatement.expression.right) && lastStatement.expression.right.name === variableName;
211
+ hasPatched = hasPatchedCondition0 || hasPatchedCondition1;
212
+ if (!hasPatched) {
213
+ const statement = t.variableDeclaration("const", [
214
+ t.variableDeclarator(t.identifier(variableName), t.objectExpression([t.objectProperty(t.identifier(valueKey), t.arrayExpression())]))
215
+ ]);
216
+ n.body.splice(idx, 0, statement);
217
+ n.body.push(
218
+ t.expressionStatement(
219
+ t.assignmentExpression(
220
+ "=",
221
+ t.memberExpression(t.memberExpression(t.identifier("module"), t.identifier("exports")), t.identifier(exportKey)),
222
+ t.identifier(variableName)
223
+ )
224
+ )
225
+ );
226
+ }
227
+ }
228
+ },
229
+ FunctionExpression(p) {
230
+ if (hasPatched) {
231
+ return;
232
+ }
233
+ const n = p.node;
234
+ if (n.id?.name === "tailwindcss" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
235
+ const returnStatement = n.body.body[0];
236
+ if (t.isObjectExpression(returnStatement.argument) && returnStatement.argument.properties.length === 2) {
237
+ const properties = returnStatement.argument.properties;
238
+ if (t.isObjectProperty(properties[0]) && t.isObjectProperty(properties[1])) {
239
+ const keyMatched = t.isIdentifier(properties[0].key) && properties[0].key.name === "postcssPlugin";
240
+ const pluginsMatched = t.isIdentifier(properties[1].key) && properties[1].key.name === "plugins";
241
+ if (pluginsMatched && keyMatched && t.isCallExpression(properties[1].value) && t.isMemberExpression(properties[1].value.callee) && t.isArrayExpression(properties[1].value.callee.object)) {
242
+ const pluginsCode = properties[1].value.callee.object.elements;
243
+ if (pluginsCode[1] && t.isFunctionExpression(pluginsCode[1])) {
244
+ const targetBlockStatement = pluginsCode[1].body;
245
+ const lastStatement = targetBlockStatement.body.at(-1);
246
+ if (t.isExpressionStatement(lastStatement)) {
247
+ const newExpressionStatement = t.expressionStatement(
248
+ t.callExpression(
249
+ t.memberExpression(
250
+ t.memberExpression(t.identifier(variableName), t.identifier("value")),
251
+ t.identifier("push")
252
+ ),
253
+ [lastStatement.expression]
254
+ )
255
+ );
256
+ targetBlockStatement.body[targetBlockStatement.body.length - 1] = newExpressionStatement;
257
+ }
258
+ const ifIdx = targetBlockStatement.body.findIndex((x) => t.isIfStatement(x));
259
+ if (ifIdx > -1) {
260
+ const ifRoot = targetBlockStatement.body[ifIdx];
261
+ if (t.isBlockStatement(ifRoot.consequent) && ifRoot.consequent.body[1] && t.isForOfStatement(ifRoot.consequent.body[1])) {
262
+ const forOf = ifRoot.consequent.body[1];
263
+ if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1 && t.isIfStatement(forOf.body.body[0])) {
264
+ const if2 = forOf.body.body[0];
265
+ if (t.isBlockStatement(if2.consequent) && if2.consequent.body.length === 1 && t.isExpressionStatement(if2.consequent.body[0])) {
266
+ const target = if2.consequent.body[0];
267
+ const newExpressionStatement = t.expressionStatement(
268
+ t.callExpression(t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier("value")), t.identifier("push")), [target.expression])
269
+ );
270
+ if2.consequent.body[0] = newExpressionStatement;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ targetBlockStatement.body.unshift(
276
+ // contentRef.value = []
277
+ // t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))
278
+ // contentRef.value.length = 0
279
+ t.expressionStatement(
280
+ t.assignmentExpression(
281
+ "=",
282
+ t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.identifier("length")),
283
+ t.numericLiteral(0)
284
+ )
285
+ )
286
+ );
287
+ }
288
+ }
289
+ }
290
+ }
291
+ }
292
+ }
293
+ // BlockStatement(p) {
294
+ // const n = p.node
295
+ // if (start && p.parent.type === 'FunctionExpression' && !p.parent.id) {
296
+ // n.body.unshift(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression())))
297
+ // }
298
+ // }
299
+ });
300
+ return {
301
+ code: hasPatched ? content : generate(ast).code,
302
+ hasPatched
303
+ };
304
+ }
305
+
306
+ function isObject(value) {
307
+ return value !== null && typeof value === "object";
308
+ }
309
+ function _defu(baseObject, defaults, namespace = ".", merger) {
310
+ if (!isObject(defaults)) {
311
+ return _defu(baseObject, {}, namespace, merger);
312
+ }
313
+ const object = Object.assign({}, defaults);
314
+ for (const key in baseObject) {
315
+ if (key === "__proto__" || key === "constructor") {
316
+ continue;
317
+ }
318
+ const value = baseObject[key];
319
+ if (value === null || value === void 0) {
320
+ continue;
321
+ }
322
+ if (merger && merger(object, key, value, namespace)) {
323
+ continue;
324
+ }
325
+ if (Array.isArray(value) && Array.isArray(object[key])) {
326
+ object[key] = [...value, ...object[key]];
327
+ } else if (isObject(value) && isObject(object[key])) {
328
+ object[key] = _defu(
329
+ value,
330
+ object[key],
331
+ (namespace ? `${namespace}.` : "") + key.toString(),
332
+ merger
333
+ );
334
+ } else {
335
+ object[key] = value;
336
+ }
337
+ }
338
+ return object;
339
+ }
340
+ function createDefu(merger) {
341
+ return (...arguments_) => (
342
+ // eslint-disable-next-line unicorn/no-array-reduce
343
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
344
+ );
345
+ }
346
+ const defu = createDefu();
347
+
348
+ const defaultOptions = {
349
+ overwrite: true
350
+ };
351
+ function getDefaultUserConfig() {
352
+ return {
353
+ output: {
354
+ filename: ".tw-patch/tw-class-list.json",
355
+ removeUniversalSelector: true,
356
+ loose: true
357
+ },
358
+ postcss: {
359
+ configDir: process.cwd()
360
+ }
361
+ };
362
+ }
363
+
364
+ function getInstalledPkgJsonPath(options = {}) {
365
+ try {
366
+ const tmpJsonPath = requireResolve(`tailwindcss/package.json`, {
367
+ paths: options.paths
368
+ });
369
+ return tmpJsonPath;
370
+ } catch (error) {
371
+ if (error.code === "MODULE_NOT_FOUND") {
372
+ console.warn("Can't find npm pkg: `tailwindcss`, Please ensure it has been installed!");
373
+ }
374
+ }
375
+ }
376
+ function getPatchOptions(options = {}) {
377
+ return defu(
378
+ options,
379
+ {
380
+ basedir: process.cwd()
381
+ },
382
+ defaultOptions
383
+ );
384
+ }
385
+ function createPatch(opt) {
386
+ return () => {
387
+ try {
388
+ const pkgJsonPath = getInstalledPkgJsonPath(opt);
389
+ return internalPatch(pkgJsonPath, opt);
390
+ } catch (error) {
391
+ console.warn(`patch tailwindcss failed:` + error.message);
392
+ }
393
+ };
394
+ }
395
+ function monkeyPatchForExposingContext(twDir, opt) {
396
+ const processTailwindFeaturesFilePath = path.resolve(twDir, "lib/processTailwindFeatures.js");
397
+ const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath);
398
+ const result = {};
399
+ if (processTailwindFeaturesContent) {
400
+ const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent);
401
+ if (!hasPatched && opt.overwrite) {
402
+ fs.writeFileSync(processTailwindFeaturesFilePath, code, {
403
+ encoding: "utf8"
404
+ });
405
+ console.log("patch tailwindcss processTailwindFeatures for return content successfully!");
406
+ }
407
+ result.processTailwindFeatures = code;
408
+ }
409
+ const pluginFilePath = path.resolve(twDir, "lib/plugin.js");
410
+ const indexFilePath = path.resolve(twDir, "lib/index.js");
411
+ const pluginContent = ensureFileContent([pluginFilePath, indexFilePath]);
412
+ if (pluginContent) {
413
+ const { code, hasPatched } = inspectPostcssPlugin(pluginContent);
414
+ if (!hasPatched && opt.overwrite) {
415
+ fs.writeFileSync(pluginFilePath, code, {
416
+ encoding: "utf8"
417
+ });
418
+ console.log("patch tailwindcss for expose runtime content successfully!");
419
+ }
420
+ result.plugin = code;
421
+ }
422
+ opt.custom && typeof opt.custom === "function" && opt.custom(twDir, result);
423
+ return result;
424
+ }
425
+ function internalPatch(pkgJsonPath, options) {
426
+ if (pkgJsonPath) {
427
+ const pkgJson = require(pkgJsonPath);
428
+ const twDir = path.dirname(pkgJsonPath);
429
+ if (gte(pkgJson.version, "3.0.0")) {
430
+ options.version = pkgJson.version;
431
+ const result = monkeyPatchForExposingContext(twDir, options);
432
+ return result;
433
+ }
434
+ }
435
+ }
436
+
437
+ async function getCss(p) {
438
+ const { options, plugins } = await postcssrc(void 0, p);
439
+ const res = await postcss(plugins).process("@tailwind base;@tailwind components;@tailwind utilities;", {
440
+ from: void 0,
441
+ ...options
442
+ });
443
+ return res;
444
+ }
445
+
446
+ class TailwindcssPatcher {
447
+ constructor(options = {}) {
448
+ this.rawOptions = options;
449
+ this.cacheOptions = getCacheOptions(options.cache);
450
+ this.patchOptions = getPatchOptions(options.patch);
451
+ this.patch = createPatch(this.patchOptions);
452
+ this.cacheManager = new CacheManager(this.cacheOptions);
453
+ }
454
+ getPkgEntry(basedir) {
455
+ return getTailwindcssEntry(basedir);
456
+ }
457
+ setCache(set) {
458
+ if (this.cacheOptions.enable) {
459
+ return this.cacheManager.write(set);
460
+ }
461
+ }
462
+ getCache() {
463
+ return this.cacheManager.read();
464
+ }
465
+ /**
466
+ * @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
467
+ * 详见 taro weapp-tailwindcss 独立分包
468
+ * @param basedir
469
+ * @returns
470
+ */
471
+ getClassSet(options) {
472
+ const { basedir, cacheStrategy = this.cacheOptions.strategy ?? "merge", removeUniversalSelector = true } = options ?? {};
473
+ const set = getClassCacheSet(basedir, {
474
+ removeUniversalSelector
475
+ });
476
+ if (cacheStrategy === "overwrite") {
477
+ set.size > 0 && this.setCache(set);
478
+ } else if (cacheStrategy === "merge") {
479
+ const cacheSet = this.getCache();
480
+ if (cacheSet) {
481
+ for (const x of cacheSet) {
482
+ set.add(x);
483
+ }
484
+ }
485
+ this.setCache(set);
486
+ }
487
+ return set;
488
+ }
489
+ getContexts(basedir) {
490
+ return getContexts(basedir);
491
+ }
492
+ async extract(options) {
493
+ const { output, postcss } = options;
494
+ if (output && postcss) {
495
+ const { removeUniversalSelector, filename, loose } = output;
496
+ const { configDir } = postcss;
497
+ await getCss(configDir);
498
+ const set = this.getClassSet({
499
+ removeUniversalSelector
500
+ });
501
+ await ensureDir(dirname(filename));
502
+ const classList = [...set];
503
+ await fs$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
504
+ return filename;
505
+ }
506
+ }
507
+ }
508
+
509
+ function getConfig() {
510
+ return loadConfig({
511
+ name: "tailwindcss-patch",
512
+ defaults: {
513
+ ...getDefaultUserConfig()
514
+ }
515
+ });
516
+ }
517
+ const defineConfig = createDefineConfig();
518
+
519
+ export { CacheManager, TailwindcssPatcher, createPatch, defineConfig, ensureDir, ensureFileContent, getCacheOptions, getClassCacheSet, getClassCaches, getConfig, getContexts, getInstalledPkgJsonPath, getPatchOptions, getTailwindcssEntry, inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext, internalPatch, monkeyPatchForExposingContext, requireResolve };