tailwindcss-patch 2.0.4 → 2.0.5

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/cli.d.mts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/dist/cli.js ADDED
@@ -0,0 +1,643 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+
30
+ // src/cli.ts
31
+ var import_cac = __toESM(require("cac"));
32
+
33
+ // src/core/index.ts
34
+ var core_exports = {};
35
+ __export(core_exports, {
36
+ CacheManager: () => CacheManager,
37
+ TailwindcssPatcher: () => TailwindcssPatcher,
38
+ createPatch: () => createPatch,
39
+ getCacheOptions: () => getCacheOptions,
40
+ getClassCacheSet: () => getClassCacheSet,
41
+ getClassCaches: () => getClassCaches,
42
+ getContexts: () => getContexts,
43
+ getInstalledPkgJsonPath: () => getInstalledPkgJsonPath,
44
+ getPatchOptions: () => getPatchOptions,
45
+ getTailwindcssEntry: () => getTailwindcssEntry,
46
+ inspectPostcssPlugin: () => inspectPostcssPlugin,
47
+ inspectProcessTailwindFeaturesReturnContext: () => inspectProcessTailwindFeaturesReturnContext,
48
+ internalPatch: () => internalPatch,
49
+ monkeyPatchForExposingContext: () => monkeyPatchForExposingContext
50
+ });
51
+
52
+ // src/core/tw-patcher.ts
53
+ var import_promises2 = __toESM(require("fs/promises"));
54
+ var import_node_path5 = require("path");
55
+
56
+ // src/core/exposeContext.ts
57
+ var import_node_path = __toESM(require("path"));
58
+ var import_node_fs2 = __toESM(require("fs"));
59
+
60
+ // src/utils.ts
61
+ var import_node_fs = __toESM(require("fs"));
62
+ var import_promises = __toESM(require("fs/promises"));
63
+ var import_resolve = __toESM(require("resolve"));
64
+ var { sync } = import_resolve.default;
65
+ function ensureFileContent(filepaths) {
66
+ if (typeof filepaths === "string") {
67
+ filepaths = [filepaths];
68
+ }
69
+ let content;
70
+ for (const filepath of filepaths) {
71
+ if (import_node_fs.default.existsSync(filepath)) {
72
+ content = import_node_fs.default.readFileSync(filepath, {
73
+ encoding: "utf8"
74
+ });
75
+ break;
76
+ }
77
+ }
78
+ return content;
79
+ }
80
+ function requireResolve(id, opts) {
81
+ return sync(id, opts);
82
+ }
83
+ async function ensureDir(p) {
84
+ try {
85
+ await import_promises.default.access(p);
86
+ } catch {
87
+ await import_promises.default.mkdir(p, {
88
+ recursive: true
89
+ });
90
+ }
91
+ }
92
+
93
+ // src/core/exposeContext.ts
94
+ function getTailwindcssEntry(basedir = process.cwd()) {
95
+ return requireResolve("tailwindcss");
96
+ }
97
+ function getContexts(basedir) {
98
+ const twPath = getTailwindcssEntry(basedir);
99
+ const distPath = import_node_path.default.dirname(twPath);
100
+ let injectFilePath = import_node_path.default.join(distPath, "plugin.js");
101
+ if (!import_node_fs2.default.existsSync(injectFilePath)) {
102
+ injectFilePath = import_node_path.default.join(distPath, "index.js");
103
+ }
104
+ const mo = require(injectFilePath);
105
+ if (mo.contextRef) {
106
+ return mo.contextRef.value;
107
+ }
108
+ return [];
109
+ }
110
+ function getClassCaches(basedir) {
111
+ const contexts = getContexts(basedir);
112
+ return contexts.map((x) => x.classCache);
113
+ }
114
+ function getClassCacheSet(basedir, options) {
115
+ const classCaches = getClassCaches(basedir);
116
+ const classSet = /* @__PURE__ */ new Set();
117
+ for (const classCacheMap of classCaches) {
118
+ const keys = classCacheMap.keys();
119
+ for (const key of keys) {
120
+ const v = key.toString();
121
+ if (options?.removeUniversalSelector && v === "*") {
122
+ continue;
123
+ }
124
+ classSet.add(v);
125
+ }
126
+ }
127
+ return classSet;
128
+ }
129
+
130
+ // src/core/cache.ts
131
+ var import_node_fs3 = __toESM(require("fs"));
132
+ var import_node_path2 = __toESM(require("path"));
133
+
134
+ // src/constants.ts
135
+ var pkgName = "tailwindcss-patch";
136
+
137
+ // src/logger.ts
138
+ function log(message, ...optionalParams) {
139
+ return console.log(`[${pkgName}]:` + message, ...optionalParams);
140
+ }
141
+
142
+ // src/core/cache.ts
143
+ function getCacheOptions(options) {
144
+ let cache;
145
+ switch (typeof options) {
146
+ case "undefined": {
147
+ cache = {
148
+ enable: false
149
+ };
150
+ break;
151
+ }
152
+ case "boolean": {
153
+ cache = {
154
+ enable: options
155
+ };
156
+ break;
157
+ }
158
+ case "object": {
159
+ cache = { ...options, enable: true };
160
+ break;
161
+ }
162
+ }
163
+ return cache;
164
+ }
165
+ var CacheManager = class {
166
+ options;
167
+ constructor(options = {}) {
168
+ this.options = this.getOptions(options);
169
+ }
170
+ mkdir(cacheDirectory) {
171
+ const exists = import_node_fs3.default.existsSync(cacheDirectory);
172
+ if (!exists) {
173
+ import_node_fs3.default.mkdirSync(cacheDirectory, {
174
+ recursive: true
175
+ });
176
+ }
177
+ return cacheDirectory;
178
+ }
179
+ getOptions(options = {}) {
180
+ const cwd = options.cwd ?? process.cwd();
181
+ const dir = options.dir ?? import_node_path2.default.resolve(cwd, "node_modules/.cache", pkgName);
182
+ const file = options.file ?? "index.json";
183
+ const filename = import_node_path2.default.resolve(dir, file);
184
+ return {
185
+ cwd,
186
+ dir,
187
+ file,
188
+ filename,
189
+ strategy: "merge"
190
+ };
191
+ }
192
+ write(data) {
193
+ try {
194
+ const { dir, filename } = this.options;
195
+ this.mkdir(dir);
196
+ import_node_fs3.default.writeFileSync(filename, JSON.stringify([...data], void 0, 2), "utf8");
197
+ return filename;
198
+ } catch {
199
+ log("write cache file fail!");
200
+ }
201
+ }
202
+ read() {
203
+ const { filename } = this.options;
204
+ try {
205
+ if (import_node_fs3.default.existsSync(filename)) {
206
+ const data = import_node_fs3.default.readFileSync(filename, "utf8");
207
+ return new Set(JSON.parse(data));
208
+ }
209
+ } catch {
210
+ log("parse cache content fail! path:" + filename);
211
+ try {
212
+ import_node_fs3.default.unlinkSync(filename);
213
+ } catch {
214
+ log("delete cache file fail! path:" + filename);
215
+ }
216
+ }
217
+ }
218
+ };
219
+
220
+ // src/core/runtime-patcher.ts
221
+ var import_node_path3 = __toESM(require("path"));
222
+ var import_node_fs4 = __toESM(require("fs"));
223
+ var import_semver = require("semver");
224
+
225
+ // ../../node_modules/.pnpm/defu@6.1.2/node_modules/defu/dist/defu.mjs
226
+ function isObject(value) {
227
+ return value !== null && typeof value === "object";
228
+ }
229
+ function _defu(baseObject, defaults, namespace = ".", merger) {
230
+ if (!isObject(defaults)) {
231
+ return _defu(baseObject, {}, namespace, merger);
232
+ }
233
+ const object = Object.assign({}, defaults);
234
+ for (const key in baseObject) {
235
+ if (key === "__proto__" || key === "constructor") {
236
+ continue;
237
+ }
238
+ const value = baseObject[key];
239
+ if (value === null || value === void 0) {
240
+ continue;
241
+ }
242
+ if (merger && merger(object, key, value, namespace)) {
243
+ continue;
244
+ }
245
+ if (Array.isArray(value) && Array.isArray(object[key])) {
246
+ object[key] = [...value, ...object[key]];
247
+ } else if (isObject(value) && isObject(object[key])) {
248
+ object[key] = _defu(
249
+ value,
250
+ object[key],
251
+ (namespace ? `${namespace}.` : "") + key.toString(),
252
+ merger
253
+ );
254
+ } else {
255
+ object[key] = value;
256
+ }
257
+ }
258
+ return object;
259
+ }
260
+ function createDefu(merger) {
261
+ return (...arguments_) => (
262
+ // eslint-disable-next-line unicorn/no-array-reduce
263
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
264
+ );
265
+ }
266
+ var defu = createDefu();
267
+ var defuFn = createDefu((object, key, currentValue) => {
268
+ if (typeof object[key] !== "undefined" && typeof currentValue === "function") {
269
+ object[key] = currentValue(object[key]);
270
+ return true;
271
+ }
272
+ });
273
+ var defuArrayFn = createDefu((object, key, currentValue) => {
274
+ if (Array.isArray(object[key]) && typeof currentValue === "function") {
275
+ object[key] = currentValue(object[key]);
276
+ return true;
277
+ }
278
+ });
279
+
280
+ // src/core/inspector.ts
281
+ var t = __toESM(require("@babel/types"));
282
+
283
+ // src/babel/index.ts
284
+ var import_generator = __toESM(require("@babel/generator"));
285
+ var import_traverse = __toESM(require("@babel/traverse"));
286
+ var import_parser = require("@babel/parser");
287
+
288
+ // src/core/inspector.ts
289
+ function inspectProcessTailwindFeaturesReturnContext(content) {
290
+ const ast = (0, import_parser.parse)(content);
291
+ let hasPatched = false;
292
+ (0, import_traverse.default)(ast, {
293
+ FunctionDeclaration(p) {
294
+ const n = p.node;
295
+ if (n.id?.name === "processTailwindFeatures" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
296
+ const rts = n.body.body[0];
297
+ if (t.isFunctionExpression(rts.argument)) {
298
+ const body = rts.argument.body.body;
299
+ const lastStatement = body.at(-1);
300
+ hasPatched = t.isReturnStatement(lastStatement) && t.isIdentifier(lastStatement.argument) && lastStatement.argument.name === "context";
301
+ if (!hasPatched) {
302
+ const rts2 = t.returnStatement(t.identifier("context"));
303
+ body.push(rts2);
304
+ }
305
+ }
306
+ }
307
+ }
308
+ });
309
+ return {
310
+ code: hasPatched ? content : (0, import_generator.default)(ast).code,
311
+ hasPatched
312
+ };
313
+ }
314
+ function inspectPostcssPlugin(content) {
315
+ const ast = (0, import_parser.parse)(content);
316
+ const exportKey = "contextRef";
317
+ const variableName = "contextRef";
318
+ const valueKey = "value";
319
+ let hasPatched = false;
320
+ (0, import_traverse.default)(ast, {
321
+ Program(p) {
322
+ const n = p.node;
323
+ const idx = n.body.findIndex((x) => {
324
+ return t.isExpressionStatement(x) && t.isAssignmentExpression(x.expression) && t.isMemberExpression(x.expression.left) && t.isFunctionExpression(x.expression.right) && x.expression.right.id?.name === "tailwindcss";
325
+ });
326
+ if (idx > -1) {
327
+ const prevStatement = n.body[idx - 1];
328
+ const lastStatement = n.body.at(-1);
329
+ const hasPatchedCondition0 = prevStatement && t.isVariableDeclaration(prevStatement) && prevStatement.declarations.length === 1 && t.isIdentifier(prevStatement.declarations[0].id) && prevStatement.declarations[0].id.name === variableName;
330
+ const hasPatchedCondition1 = t.isExpressionStatement(lastStatement) && t.isAssignmentExpression(lastStatement.expression) && t.isIdentifier(lastStatement.expression.right) && lastStatement.expression.right.name === variableName;
331
+ hasPatched = hasPatchedCondition0 || hasPatchedCondition1;
332
+ if (!hasPatched) {
333
+ const statement = t.variableDeclaration("const", [
334
+ t.variableDeclarator(t.identifier(variableName), t.objectExpression([t.objectProperty(t.identifier(valueKey), t.arrayExpression())]))
335
+ ]);
336
+ n.body.splice(idx, 0, statement);
337
+ n.body.push(
338
+ t.expressionStatement(
339
+ t.assignmentExpression(
340
+ "=",
341
+ t.memberExpression(t.memberExpression(t.identifier("module"), t.identifier("exports")), t.identifier(exportKey)),
342
+ t.identifier(variableName)
343
+ )
344
+ )
345
+ );
346
+ }
347
+ }
348
+ },
349
+ FunctionExpression(p) {
350
+ if (hasPatched) {
351
+ return;
352
+ }
353
+ const n = p.node;
354
+ if (n.id?.name === "tailwindcss" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
355
+ const returnStatement2 = n.body.body[0];
356
+ if (t.isObjectExpression(returnStatement2.argument) && returnStatement2.argument.properties.length === 2) {
357
+ const properties = returnStatement2.argument.properties;
358
+ if (t.isObjectProperty(properties[0]) && t.isObjectProperty(properties[1])) {
359
+ const keyMatched = t.isIdentifier(properties[0].key) && properties[0].key.name === "postcssPlugin";
360
+ const pluginsMatched = t.isIdentifier(properties[1].key) && properties[1].key.name === "plugins";
361
+ if (pluginsMatched && keyMatched && t.isCallExpression(properties[1].value) && t.isMemberExpression(properties[1].value.callee) && t.isArrayExpression(properties[1].value.callee.object)) {
362
+ const pluginsCode = properties[1].value.callee.object.elements;
363
+ if (pluginsCode[1] && t.isFunctionExpression(pluginsCode[1])) {
364
+ const targetBlockStatement = pluginsCode[1].body;
365
+ const lastStatement = targetBlockStatement.body.at(-1);
366
+ if (t.isExpressionStatement(lastStatement)) {
367
+ const newExpressionStatement = t.expressionStatement(
368
+ t.callExpression(
369
+ t.memberExpression(
370
+ t.memberExpression(t.identifier(variableName), t.identifier("value")),
371
+ t.identifier("push")
372
+ ),
373
+ [lastStatement.expression]
374
+ )
375
+ );
376
+ targetBlockStatement.body[targetBlockStatement.body.length - 1] = newExpressionStatement;
377
+ }
378
+ const ifIdx = targetBlockStatement.body.findIndex((x) => t.isIfStatement(x));
379
+ if (ifIdx > -1) {
380
+ const ifRoot = targetBlockStatement.body[ifIdx];
381
+ if (t.isBlockStatement(ifRoot.consequent) && ifRoot.consequent.body[1] && t.isForOfStatement(ifRoot.consequent.body[1])) {
382
+ const forOf = ifRoot.consequent.body[1];
383
+ if (t.isBlockStatement(forOf.body) && forOf.body.body.length === 1 && t.isIfStatement(forOf.body.body[0])) {
384
+ const if2 = forOf.body.body[0];
385
+ if (t.isBlockStatement(if2.consequent) && if2.consequent.body.length === 1 && t.isExpressionStatement(if2.consequent.body[0])) {
386
+ const target = if2.consequent.body[0];
387
+ const newExpressionStatement = t.expressionStatement(
388
+ t.callExpression(t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier("value")), t.identifier("push")), [target.expression])
389
+ );
390
+ if2.consequent.body[0] = newExpressionStatement;
391
+ }
392
+ }
393
+ }
394
+ }
395
+ targetBlockStatement.body.unshift(
396
+ // contentRef.value = []
397
+ // t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression()))
398
+ // contentRef.value.length = 0
399
+ t.expressionStatement(
400
+ t.assignmentExpression(
401
+ "=",
402
+ t.memberExpression(t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.identifier("length")),
403
+ t.numericLiteral(0)
404
+ )
405
+ )
406
+ );
407
+ }
408
+ }
409
+ }
410
+ }
411
+ }
412
+ }
413
+ // BlockStatement(p) {
414
+ // const n = p.node
415
+ // if (start && p.parent.type === 'FunctionExpression' && !p.parent.id) {
416
+ // n.body.unshift(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(variableName), t.identifier(valueKey)), t.arrayExpression())))
417
+ // }
418
+ // }
419
+ });
420
+ return {
421
+ code: hasPatched ? content : (0, import_generator.default)(ast).code,
422
+ hasPatched
423
+ };
424
+ }
425
+
426
+ // src/defaults.ts
427
+ function getDefaultPatchOptions() {
428
+ return {
429
+ overwrite: true
430
+ };
431
+ }
432
+
433
+ // src/core/runtime-patcher.ts
434
+ function getInstalledPkgJsonPath(options = {}) {
435
+ try {
436
+ const tmpJsonPath = requireResolve(`tailwindcss/package.json`, {
437
+ paths: options.paths
438
+ });
439
+ return tmpJsonPath;
440
+ } catch (error) {
441
+ if (error.code === "MODULE_NOT_FOUND") {
442
+ console.warn("Can't find npm pkg: `tailwindcss`, Please ensure it has been installed!");
443
+ }
444
+ }
445
+ }
446
+ function getPatchOptions(options = {}) {
447
+ return defu(
448
+ options,
449
+ {
450
+ basedir: process.cwd()
451
+ },
452
+ getDefaultPatchOptions()
453
+ );
454
+ }
455
+ function createPatch(opt) {
456
+ return () => {
457
+ try {
458
+ const pkgJsonPath = getInstalledPkgJsonPath(opt);
459
+ return internalPatch(pkgJsonPath, opt);
460
+ } catch (error) {
461
+ console.warn(`patch tailwindcss failed:` + error.message);
462
+ }
463
+ };
464
+ }
465
+ function monkeyPatchForExposingContext(twDir, opt) {
466
+ const processTailwindFeaturesFilePath = import_node_path3.default.resolve(twDir, "lib/processTailwindFeatures.js");
467
+ const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath);
468
+ const result = {};
469
+ if (processTailwindFeaturesContent) {
470
+ const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent);
471
+ if (!hasPatched && opt.overwrite) {
472
+ import_node_fs4.default.writeFileSync(processTailwindFeaturesFilePath, code, {
473
+ encoding: "utf8"
474
+ });
475
+ console.log("patch tailwindcss processTailwindFeatures for return content successfully!");
476
+ }
477
+ result.processTailwindFeatures = code;
478
+ }
479
+ const pluginFilePath = import_node_path3.default.resolve(twDir, "lib/plugin.js");
480
+ const indexFilePath = import_node_path3.default.resolve(twDir, "lib/index.js");
481
+ const pluginContent = ensureFileContent([pluginFilePath, indexFilePath]);
482
+ if (pluginContent) {
483
+ const { code, hasPatched } = inspectPostcssPlugin(pluginContent);
484
+ if (!hasPatched && opt.overwrite) {
485
+ import_node_fs4.default.writeFileSync(pluginFilePath, code, {
486
+ encoding: "utf8"
487
+ });
488
+ console.log("patch tailwindcss for expose runtime content successfully!");
489
+ }
490
+ result.plugin = code;
491
+ }
492
+ opt.custom && typeof opt.custom === "function" && opt.custom(twDir, result);
493
+ return result;
494
+ }
495
+ function internalPatch(pkgJsonPath, options) {
496
+ if (pkgJsonPath) {
497
+ const pkgJson = require(pkgJsonPath);
498
+ const twDir = import_node_path3.default.dirname(pkgJsonPath);
499
+ if ((0, import_semver.gte)(pkgJson.version, "3.0.0")) {
500
+ options.version = pkgJson.version;
501
+ const result = monkeyPatchForExposingContext(twDir, options);
502
+ return result;
503
+ }
504
+ }
505
+ }
506
+
507
+ // src/core/postcss.ts
508
+ var import_node_path4 = __toESM(require("path"));
509
+ var import_postcss = __toESM(require("postcss"));
510
+ var import_lilconfig = require("lilconfig");
511
+ var import_jiti = __toESM(require("jiti"));
512
+ var jiti = (0, import_jiti.default)(__filename);
513
+ async function processTailwindcss(options) {
514
+ options.cwd = options.cwd ?? process.cwd();
515
+ let config = options.config;
516
+ if (!(typeof options.config === "string" && import_node_path4.default.isAbsolute(options.config))) {
517
+ const moduleName = "tailwind";
518
+ const result = await (0, import_lilconfig.lilconfig)("tailwindcss", {
519
+ searchPlaces: [`${moduleName}.config.js`, `${moduleName}.config.cjs`],
520
+ loaders: {
521
+ // 默认支持 js 和 cjs 2种格式
522
+ ".js": jiti,
523
+ ".cjs": jiti,
524
+ ".ts": jiti,
525
+ ".mjs": jiti
526
+ }
527
+ }).search(options.cwd);
528
+ if (!result) {
529
+ throw new Error(`No TailwindCSS Config found in: ${options.cwd}`);
530
+ }
531
+ config = result.filepath;
532
+ }
533
+ return await (0, import_postcss.default)([
534
+ require("tailwindcss")({
535
+ config
536
+ })
537
+ ]).process("@tailwind base;@tailwind components;@tailwind utilities;", {
538
+ from: void 0
539
+ });
540
+ }
541
+
542
+ // src/core/tw-patcher.ts
543
+ var TailwindcssPatcher = class {
544
+ rawOptions;
545
+ cacheOptions;
546
+ patchOptions;
547
+ patch;
548
+ cacheManager;
549
+ constructor(options = {}) {
550
+ this.rawOptions = options;
551
+ this.cacheOptions = getCacheOptions(options.cache);
552
+ this.patchOptions = getPatchOptions(options.patch);
553
+ this.patch = createPatch(this.patchOptions);
554
+ this.cacheManager = new CacheManager(this.cacheOptions);
555
+ }
556
+ getPkgEntry(basedir) {
557
+ return getTailwindcssEntry(basedir);
558
+ }
559
+ setCache(set) {
560
+ if (this.cacheOptions.enable) {
561
+ return this.cacheManager.write(set);
562
+ }
563
+ }
564
+ getCache() {
565
+ return this.cacheManager.read();
566
+ }
567
+ /**
568
+ * @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
569
+ * 详见 taro weapp-tailwindcss 独立分包
570
+ * @param basedir
571
+ * @returns
572
+ */
573
+ getClassSet(options) {
574
+ const { basedir, cacheStrategy = this.cacheOptions.strategy ?? "merge", removeUniversalSelector = true } = options ?? {};
575
+ const set = getClassCacheSet(basedir, {
576
+ removeUniversalSelector
577
+ });
578
+ if (cacheStrategy === "overwrite") {
579
+ set.size > 0 && this.setCache(set);
580
+ } else if (cacheStrategy === "merge") {
581
+ const cacheSet = this.getCache();
582
+ if (cacheSet) {
583
+ for (const x of cacheSet) {
584
+ set.add(x);
585
+ }
586
+ }
587
+ this.setCache(set);
588
+ }
589
+ return set;
590
+ }
591
+ getContexts(basedir) {
592
+ return getContexts(basedir);
593
+ }
594
+ async extract(options) {
595
+ const { output, tailwindcss } = options ?? {};
596
+ if (output && tailwindcss) {
597
+ const { removeUniversalSelector, filename, loose } = output;
598
+ await processTailwindcss(tailwindcss);
599
+ const set = this.getClassSet({
600
+ removeUniversalSelector
601
+ });
602
+ if (filename) {
603
+ await ensureDir((0, import_node_path5.dirname)(filename));
604
+ const classList = [...set];
605
+ await import_promises2.default.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
606
+ return filename;
607
+ }
608
+ }
609
+ }
610
+ };
611
+
612
+ // src/core/config.ts
613
+ var config_exports = {};
614
+ __reExport(config_exports, require("@tailwindcss-mangle/config"));
615
+
616
+ // src/core/index.ts
617
+ __reExport(core_exports, config_exports);
618
+
619
+ // src/cli.ts
620
+ function init() {
621
+ const cwd = process.cwd();
622
+ return (0, core_exports.initConfig)(cwd);
623
+ }
624
+ var cli = (0, import_cac.default)();
625
+ cli.command("install", "patch install").action(() => {
626
+ const opt = getPatchOptions();
627
+ const patch = createPatch(opt);
628
+ patch();
629
+ });
630
+ cli.command("init").action(async () => {
631
+ await init();
632
+ console.log(`\u2728 ${core_exports.configName}.config.ts initialized!`);
633
+ });
634
+ cli.command("extract").action(async () => {
635
+ const { config } = await (0, core_exports.getConfig)();
636
+ if (config) {
637
+ const twPatcher = new TailwindcssPatcher();
638
+ const p = await twPatcher.extract(config.patch);
639
+ console.log("\u2728 tailwindcss-patch extract success! file path:\n" + p);
640
+ }
641
+ });
642
+ cli.help();
643
+ cli.parse();
package/dist/cli.mjs CHANGED
@@ -1,24 +1,17 @@
1
- import cac from 'cac';
2
- import { getPatchOptions, TailwindcssPatcher, createPatch } from './index.mjs';
3
- import 'node:path';
4
- import 'node:fs';
5
- import 'node:fs/promises';
6
- import 'resolve';
7
- import '@babel/types';
8
- import '@babel/generator';
9
- import '@babel/traverse';
10
- import '@babel/parser';
11
- import { configName, getConfig, initConfig } from '@tailwindcss-mangle/config';
12
- import 'semver';
13
- import 'postcss';
14
- import 'lilconfig';
15
- import 'jiti';
1
+ import {
2
+ TailwindcssPatcher,
3
+ core_exports,
4
+ createPatch,
5
+ getPatchOptions
6
+ } from "./chunk-PZNRLYGX.mjs";
16
7
 
8
+ // src/cli.ts
9
+ import cac from "cac";
17
10
  function init() {
18
11
  const cwd = process.cwd();
19
- return initConfig(cwd);
12
+ return (0, core_exports.initConfig)(cwd);
20
13
  }
21
- const cli = cac();
14
+ var cli = cac();
22
15
  cli.command("install", "patch install").action(() => {
23
16
  const opt = getPatchOptions();
24
17
  const patch = createPatch(opt);
@@ -26,10 +19,10 @@ cli.command("install", "patch install").action(() => {
26
19
  });
27
20
  cli.command("init").action(async () => {
28
21
  await init();
29
- console.log(`\u2728 ${configName}.config.ts initialized!`);
22
+ console.log(`\u2728 ${core_exports.configName}.config.ts initialized!`);
30
23
  });
31
24
  cli.command("extract").action(async () => {
32
- const { config } = await getConfig();
25
+ const { config } = await (0, core_exports.getConfig)();
33
26
  if (config) {
34
27
  const twPatcher = new TailwindcssPatcher();
35
28
  const p = await twPatcher.extract(config.patch);