tailwindcss-patch 2.0.4 → 2.0.5-alpha.1

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 CHANGED
@@ -52,7 +52,7 @@ npx tw-patch install
52
52
  tw-patch init
53
53
  ```
54
54
 
55
- Then there will be a ts file called `tailwindcss-patch.config.ts` exist in your `cwd`.
55
+ Then there will be a ts file called `tailwindcss-mangle.config.ts` exist in your `cwd`.
56
56
 
57
57
  ### Extract all class into a json
58
58
 
@@ -62,7 +62,7 @@ tw-patch extract
62
62
 
63
63
  default there will be a json in `.tw-patch/tw-class-list.json` in your project.
64
64
 
65
- you can custom this behavior by config `tailwindcss-patch.config.ts`
65
+ you can custom this behavior by config `tailwindcss-mangle.config.ts`
66
66
 
67
67
  ### Nodejs
68
68
 
@@ -1,35 +1,75 @@
1
- import fs$1 from 'node:fs/promises';
2
- import path, { dirname } from 'node:path';
3
- import fs from 'node:fs';
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 { lilconfig } from 'lilconfig';
12
- import createJiti from 'jiti';
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"));
13
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
+ });
14
51
 
15
- // -- Unbuild CommonJS Shims --
16
- import __cjs_url__ from 'url';
17
- import __cjs_path__ from 'path';
18
- import __cjs_mod__ from 'module';
19
- const __filename = __cjs_url__.fileURLToPath(import.meta.url);
20
- const __dirname = __cjs_path__.dirname(__filename);
21
- const require = __cjs_mod__.createRequire(import.meta.url);
22
- export * from '@tailwindcss-mangle/config';
52
+ // src/core/tw-patcher.ts
53
+ var import_promises2 = __toESM(require("fs/promises"));
54
+ var import_node_path5 = require("path");
23
55
 
24
- const { sync } = pkg;
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;
25
65
  function ensureFileContent(filepaths) {
26
66
  if (typeof filepaths === "string") {
27
67
  filepaths = [filepaths];
28
68
  }
29
69
  let content;
30
70
  for (const filepath of filepaths) {
31
- if (fs.existsSync(filepath)) {
32
- content = fs.readFileSync(filepath, {
71
+ if (import_node_fs.default.existsSync(filepath)) {
72
+ content = import_node_fs.default.readFileSync(filepath, {
33
73
  encoding: "utf8"
34
74
  });
35
75
  break;
@@ -42,23 +82,24 @@ function requireResolve(id, opts) {
42
82
  }
43
83
  async function ensureDir(p) {
44
84
  try {
45
- await fs$1.access(p);
85
+ await import_promises.default.access(p);
46
86
  } catch {
47
- await fs$1.mkdir(p, {
87
+ await import_promises.default.mkdir(p, {
48
88
  recursive: true
49
89
  });
50
90
  }
51
91
  }
52
92
 
93
+ // src/core/exposeContext.ts
53
94
  function getTailwindcssEntry(basedir = process.cwd()) {
54
95
  return requireResolve("tailwindcss");
55
96
  }
56
97
  function getContexts(basedir) {
57
98
  const twPath = getTailwindcssEntry(basedir);
58
- const distPath = path.dirname(twPath);
59
- let injectFilePath = path.join(distPath, "plugin.js");
60
- if (!fs.existsSync(injectFilePath)) {
61
- injectFilePath = path.join(distPath, "index.js");
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");
62
103
  }
63
104
  const mo = require(injectFilePath);
64
105
  if (mo.contextRef) {
@@ -86,12 +127,19 @@ function getClassCacheSet(basedir, options) {
86
127
  return classSet;
87
128
  }
88
129
 
89
- const pkgName = "tailwindcss-patch";
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";
90
136
 
137
+ // src/logger.ts
91
138
  function log(message, ...optionalParams) {
92
139
  return console.log(`[${pkgName}]:` + message, ...optionalParams);
93
140
  }
94
141
 
142
+ // src/core/cache.ts
95
143
  function getCacheOptions(options) {
96
144
  let cache;
97
145
  switch (typeof options) {
@@ -114,14 +162,15 @@ function getCacheOptions(options) {
114
162
  }
115
163
  return cache;
116
164
  }
117
- class CacheManager {
165
+ var CacheManager = class {
166
+ options;
118
167
  constructor(options = {}) {
119
168
  this.options = this.getOptions(options);
120
169
  }
121
170
  mkdir(cacheDirectory) {
122
- const exists = fs.existsSync(cacheDirectory);
171
+ const exists = import_node_fs3.default.existsSync(cacheDirectory);
123
172
  if (!exists) {
124
- fs.mkdirSync(cacheDirectory, {
173
+ import_node_fs3.default.mkdirSync(cacheDirectory, {
125
174
  recursive: true
126
175
  });
127
176
  }
@@ -129,9 +178,9 @@ class CacheManager {
129
178
  }
130
179
  getOptions(options = {}) {
131
180
  const cwd = options.cwd ?? process.cwd();
132
- const dir = options.dir ?? path.resolve(cwd, "node_modules/.cache", pkgName);
181
+ const dir = options.dir ?? import_node_path2.default.resolve(cwd, "node_modules/.cache", pkgName);
133
182
  const file = options.file ?? "index.json";
134
- const filename = path.resolve(dir, file);
183
+ const filename = import_node_path2.default.resolve(dir, file);
135
184
  return {
136
185
  cwd,
137
186
  dir,
@@ -144,7 +193,7 @@ class CacheManager {
144
193
  try {
145
194
  const { dir, filename } = this.options;
146
195
  this.mkdir(dir);
147
- fs.writeFileSync(filename, JSON.stringify([...data], void 0, 2), "utf8");
196
+ import_node_fs3.default.writeFileSync(filename, JSON.stringify([...data], void 0, 2), "utf8");
148
197
  return filename;
149
198
  } catch {
150
199
  log("write cache file fail!");
@@ -153,21 +202,27 @@ class CacheManager {
153
202
  read() {
154
203
  const { filename } = this.options;
155
204
  try {
156
- if (fs.existsSync(filename)) {
157
- const data = fs.readFileSync(filename, "utf8");
205
+ if (import_node_fs3.default.existsSync(filename)) {
206
+ const data = import_node_fs3.default.readFileSync(filename, "utf8");
158
207
  return new Set(JSON.parse(data));
159
208
  }
160
209
  } catch {
161
210
  log("parse cache content fail! path:" + filename);
162
211
  try {
163
- fs.unlinkSync(filename);
212
+ import_node_fs3.default.unlinkSync(filename);
164
213
  } catch {
165
214
  log("delete cache file fail! path:" + filename);
166
215
  }
167
216
  }
168
217
  }
169
- }
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");
170
224
 
225
+ // ../../node_modules/.pnpm/defu@6.1.2/node_modules/defu/dist/defu.mjs
171
226
  function isObject(value) {
172
227
  return value !== null && typeof value === "object";
173
228
  }
@@ -208,12 +263,33 @@ function createDefu(merger) {
208
263
  arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
209
264
  );
210
265
  }
211
- const defu = createDefu();
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
+ });
212
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
213
289
  function inspectProcessTailwindFeaturesReturnContext(content) {
214
- const ast = parse(content);
290
+ const ast = (0, import_parser.parse)(content);
215
291
  let hasPatched = false;
216
- traverse(ast, {
292
+ (0, import_traverse.default)(ast, {
217
293
  FunctionDeclaration(p) {
218
294
  const n = p.node;
219
295
  if (n.id?.name === "processTailwindFeatures" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
@@ -231,17 +307,17 @@ function inspectProcessTailwindFeaturesReturnContext(content) {
231
307
  }
232
308
  });
233
309
  return {
234
- code: hasPatched ? content : generate(ast).code,
310
+ code: hasPatched ? content : (0, import_generator.default)(ast).code,
235
311
  hasPatched
236
312
  };
237
313
  }
238
314
  function inspectPostcssPlugin(content) {
239
- const ast = parse(content);
315
+ const ast = (0, import_parser.parse)(content);
240
316
  const exportKey = "contextRef";
241
317
  const variableName = "contextRef";
242
318
  const valueKey = "value";
243
319
  let hasPatched = false;
244
- traverse(ast, {
320
+ (0, import_traverse.default)(ast, {
245
321
  Program(p) {
246
322
  const n = p.node;
247
323
  const idx = n.body.findIndex((x) => {
@@ -276,9 +352,9 @@ function inspectPostcssPlugin(content) {
276
352
  }
277
353
  const n = p.node;
278
354
  if (n.id?.name === "tailwindcss" && n.body.body.length === 1 && t.isReturnStatement(n.body.body[0])) {
279
- const returnStatement = n.body.body[0];
280
- if (t.isObjectExpression(returnStatement.argument) && returnStatement.argument.properties.length === 2) {
281
- const properties = returnStatement.argument.properties;
355
+ const returnStatement2 = n.body.body[0];
356
+ if (t.isObjectExpression(returnStatement2.argument) && returnStatement2.argument.properties.length === 2) {
357
+ const properties = returnStatement2.argument.properties;
282
358
  if (t.isObjectProperty(properties[0]) && t.isObjectProperty(properties[1])) {
283
359
  const keyMatched = t.isIdentifier(properties[0].key) && properties[0].key.name === "postcssPlugin";
284
360
  const pluginsMatched = t.isIdentifier(properties[1].key) && properties[1].key.name === "plugins";
@@ -342,17 +418,19 @@ function inspectPostcssPlugin(content) {
342
418
  // }
343
419
  });
344
420
  return {
345
- code: hasPatched ? content : generate(ast).code,
421
+ code: hasPatched ? content : (0, import_generator.default)(ast).code,
346
422
  hasPatched
347
423
  };
348
424
  }
349
425
 
426
+ // src/defaults.ts
350
427
  function getDefaultPatchOptions() {
351
428
  return {
352
429
  overwrite: true
353
430
  };
354
431
  }
355
432
 
433
+ // src/core/runtime-patcher.ts
356
434
  function getInstalledPkgJsonPath(options = {}) {
357
435
  try {
358
436
  const tmpJsonPath = requireResolve(`tailwindcss/package.json`, {
@@ -385,26 +463,26 @@ function createPatch(opt) {
385
463
  };
386
464
  }
387
465
  function monkeyPatchForExposingContext(twDir, opt) {
388
- const processTailwindFeaturesFilePath = path.resolve(twDir, "lib/processTailwindFeatures.js");
466
+ const processTailwindFeaturesFilePath = import_node_path3.default.resolve(twDir, "lib/processTailwindFeatures.js");
389
467
  const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath);
390
468
  const result = {};
391
469
  if (processTailwindFeaturesContent) {
392
470
  const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext(processTailwindFeaturesContent);
393
471
  if (!hasPatched && opt.overwrite) {
394
- fs.writeFileSync(processTailwindFeaturesFilePath, code, {
472
+ import_node_fs4.default.writeFileSync(processTailwindFeaturesFilePath, code, {
395
473
  encoding: "utf8"
396
474
  });
397
475
  console.log("patch tailwindcss processTailwindFeatures for return content successfully!");
398
476
  }
399
477
  result.processTailwindFeatures = code;
400
478
  }
401
- const pluginFilePath = path.resolve(twDir, "lib/plugin.js");
402
- const indexFilePath = path.resolve(twDir, "lib/index.js");
479
+ const pluginFilePath = import_node_path3.default.resolve(twDir, "lib/plugin.js");
480
+ const indexFilePath = import_node_path3.default.resolve(twDir, "lib/index.js");
403
481
  const pluginContent = ensureFileContent([pluginFilePath, indexFilePath]);
404
482
  if (pluginContent) {
405
483
  const { code, hasPatched } = inspectPostcssPlugin(pluginContent);
406
484
  if (!hasPatched && opt.overwrite) {
407
- fs.writeFileSync(pluginFilePath, code, {
485
+ import_node_fs4.default.writeFileSync(pluginFilePath, code, {
408
486
  encoding: "utf8"
409
487
  });
410
488
  console.log("patch tailwindcss for expose runtime content successfully!");
@@ -417,8 +495,8 @@ function monkeyPatchForExposingContext(twDir, opt) {
417
495
  function internalPatch(pkgJsonPath, options) {
418
496
  if (pkgJsonPath) {
419
497
  const pkgJson = require(pkgJsonPath);
420
- const twDir = path.dirname(pkgJsonPath);
421
- if (gte(pkgJson.version, "3.0.0")) {
498
+ const twDir = import_node_path3.default.dirname(pkgJsonPath);
499
+ if ((0, import_semver.gte)(pkgJson.version, "3.0.0")) {
422
500
  options.version = pkgJson.version;
423
501
  const result = monkeyPatchForExposingContext(twDir, options);
424
502
  return result;
@@ -426,13 +504,18 @@ function internalPatch(pkgJsonPath, options) {
426
504
  }
427
505
  }
428
506
 
429
- const jiti = createJiti(__filename);
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);
430
513
  async function processTailwindcss(options) {
431
514
  options.cwd = options.cwd ?? process.cwd();
432
515
  let config = options.config;
433
- if (!(typeof options.config === "string" && path.isAbsolute(options.config))) {
516
+ if (!(typeof options.config === "string" && import_node_path4.default.isAbsolute(options.config))) {
434
517
  const moduleName = "tailwind";
435
- const result = await lilconfig("tailwindcss", {
518
+ const result = await (0, import_lilconfig.lilconfig)("tailwindcss", {
436
519
  searchPlaces: [`${moduleName}.config.js`, `${moduleName}.config.cjs`],
437
520
  loaders: {
438
521
  // 默认支持 js 和 cjs 2种格式
@@ -447,7 +530,7 @@ async function processTailwindcss(options) {
447
530
  }
448
531
  config = result.filepath;
449
532
  }
450
- return await postcss([
533
+ return await (0, import_postcss.default)([
451
534
  require("tailwindcss")({
452
535
  config
453
536
  })
@@ -456,7 +539,13 @@ async function processTailwindcss(options) {
456
539
  });
457
540
  }
458
541
 
459
- class TailwindcssPatcher {
542
+ // src/core/tw-patcher.ts
543
+ var TailwindcssPatcher = class {
544
+ rawOptions;
545
+ cacheOptions;
546
+ patchOptions;
547
+ patch;
548
+ cacheManager;
460
549
  constructor(options = {}) {
461
550
  this.rawOptions = options;
462
551
  this.cacheOptions = getCacheOptions(options.cache);
@@ -511,13 +600,44 @@ class TailwindcssPatcher {
511
600
  removeUniversalSelector
512
601
  });
513
602
  if (filename) {
514
- await ensureDir(dirname(filename));
603
+ await ensureDir((0, import_node_path5.dirname)(filename));
515
604
  const classList = [...set];
516
- await fs$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
605
+ await import_promises2.default.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
517
606
  return filename;
518
607
  }
519
608
  }
520
609
  }
521
- }
610
+ };
611
+
612
+ // src/core/config.ts
613
+ var config_exports = {};
614
+ __reExport(config_exports, require("@tailwindcss-mangle/config"));
522
615
 
523
- export { CacheManager, TailwindcssPatcher, createPatch, ensureDir, ensureFileContent, getCacheOptions, getClassCacheSet, getClassCaches, getContexts, getInstalledPkgJsonPath, getPatchOptions, getTailwindcssEntry, inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext, internalPatch, monkeyPatchForExposingContext, requireResolve };
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();