tailwindcss-patch 2.0.5-alpha.2 → 2.0.5-alpha.3

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