tailwindcss-patch 2.0.0-alpha.0 → 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.
package/README.md CHANGED
@@ -7,9 +7,11 @@ get tailwindcss context at runtime ! extract all class to json file!
7
7
  - [Usage](#usage)
8
8
  - [Cli](#cli)
9
9
  - [Init Config File](#init-config-file)
10
- - [extract all class into a json](#extract-all-class-into-a-json)
10
+ - [Extract all class into a json](#extract-all-class-into-a-json)
11
11
  - [Nodejs](#nodejs)
12
12
  - [Migration form v1 to v2](#migration-form-v1-to-v2)
13
+ - [2. cli command change](#2-cli-command-change)
14
+ - [1. default remove `*` in json array result](#1-default-remove--in-json-array-result)
13
15
  - [Notice](#notice)
14
16
 
15
17
  ## Setup
@@ -51,7 +53,7 @@ tw-patch init
51
53
 
52
54
  Then there will be a ts file called `tailwindcss-patch.config.ts` exist in your `cwd`.
53
55
 
54
- ### extract all class into a json
56
+ ### Extract all class into a json
55
57
 
56
58
  ```sh
57
59
  tw-patch extract
@@ -70,11 +72,13 @@ const twPatcher = new TailwindcssPatcher(/* options */)
70
72
  // get all contexts at runtime
71
73
  twPatcher.getContexts()
72
74
  // get all class generated by tailwindcss utilities
73
- twPatcher.getClassCacheSet()
75
+ twPatcher.getClassSet()
74
76
  ```
75
77
 
76
78
  ## Migration form v1 to v2
77
79
 
80
+ ### 2. cli command change
81
+
78
82
  ```diff
79
83
  {
80
84
  - "tw-patch"
@@ -82,6 +86,16 @@ twPatcher.getClassCacheSet()
82
86
  }
83
87
  ```
84
88
 
89
+ ### 1. default remove `*` in json array result
90
+
91
+ ```diff
92
+ [
93
+ - "*",
94
+ "text-[99px]",
95
+ "text-[100px]"
96
+ ]
97
+ ```
98
+
85
99
  ## Notice
86
100
 
87
101
  `getContexts`,`getClassCacheSet` should be invoked after `postcss-loader`'s activation.
package/bin/tw-patch.js CHANGED
@@ -1,7 +1,7 @@
1
- #!/usr/bin/env node
2
- const fs = require('node:fs')
3
- const path = require('node:path')
4
- const cliPath = path.resolve(__dirname, '../dist/cli.cjs')
5
- if (fs.existsSync(cliPath)) {
6
- require(cliPath)
7
- }
1
+ #!/usr/bin/env node
2
+ const fs = require('node:fs')
3
+ const path = require('node:path')
4
+ const cliPath = path.resolve(__dirname, '../dist/cli.cjs')
5
+ if (fs.existsSync(cliPath)) {
6
+ require(cliPath)
7
+ }
package/dist/cli.cjs CHANGED
@@ -105,13 +105,10 @@ cli.command("init").action(async () => {
105
105
  });
106
106
  cli.command("extract").action(async () => {
107
107
  const { config } = await index.getConfig();
108
- if (config?.output?.filename) {
108
+ if (config) {
109
109
  const twPatcher = new index.TailwindcssPatcher();
110
- await twPatcher.extract({
111
- filename: config.output.filename,
112
- configDir: config.postcss?.configDir,
113
- loose: config.postcss?.loose
114
- });
110
+ const p = await twPatcher.extract(config);
111
+ console.log("\u2728 tailwindcss-patch extract success! file path:\n" + p);
115
112
  }
116
113
  });
117
114
  cli.help();
package/dist/cli.mjs CHANGED
@@ -97,13 +97,10 @@ cli.command("init").action(async () => {
97
97
  });
98
98
  cli.command("extract").action(async () => {
99
99
  const { config } = await getConfig();
100
- if (config?.output?.filename) {
100
+ if (config) {
101
101
  const twPatcher = new TailwindcssPatcher();
102
- await twPatcher.extract({
103
- filename: config.output.filename,
104
- configDir: config.postcss?.configDir,
105
- loose: config.postcss?.loose
106
- });
102
+ const p = await twPatcher.extract(config);
103
+ console.log("\u2728 tailwindcss-patch extract success! file path:\n" + p);
107
104
  }
108
105
  });
109
106
  cli.help();
package/dist/index.cjs CHANGED
@@ -86,12 +86,15 @@ function getClassCaches(basedir) {
86
86
  const contexts = getContexts(basedir);
87
87
  return contexts.map((x) => x.classCache);
88
88
  }
89
- function getClassCacheSet(basedir) {
89
+ function getClassCacheSet(basedir, options) {
90
90
  const classCaches = getClassCaches(basedir);
91
91
  const classSet = /* @__PURE__ */ new Set();
92
92
  for (const classCacheMap of classCaches) {
93
93
  const keys = classCacheMap.keys();
94
94
  for (const key of keys) {
95
+ if (options?.removeUniversalSelector && key.toString() === "*") {
96
+ continue;
97
+ }
95
98
  classSet.add(key.toString());
96
99
  }
97
100
  }
@@ -362,6 +365,18 @@ const defu = createDefu();
362
365
  const defaultOptions = {
363
366
  overwrite: true
364
367
  };
368
+ function getDefaultUserConfig() {
369
+ return {
370
+ output: {
371
+ filename: ".tw-patch/tw-class-list.json",
372
+ removeUniversalSelector: true,
373
+ loose: true
374
+ },
375
+ postcss: {
376
+ configDir: process.cwd()
377
+ }
378
+ };
379
+ }
365
380
 
366
381
  function getInstalledPkgJsonPath(options = {}) {
367
382
  try {
@@ -470,11 +485,11 @@ class TailwindcssPatcher {
470
485
  * @param basedir
471
486
  * @returns
472
487
  */
473
- getClassSet(options = {
474
- cacheStrategy: this.cacheOptions.strategy ?? "merge"
475
- }) {
476
- const { basedir, cacheStrategy } = options;
477
- const set = getClassCacheSet(basedir);
488
+ getClassSet(options) {
489
+ const { basedir, cacheStrategy = this.cacheOptions.strategy ?? "merge", removeUniversalSelector = true } = options ?? {};
490
+ const set = getClassCacheSet(basedir, {
491
+ removeUniversalSelector
492
+ });
478
493
  if (cacheStrategy === "overwrite") {
479
494
  set.size > 0 && this.setCache(set);
480
495
  } else if (cacheStrategy === "merge") {
@@ -492,13 +507,19 @@ class TailwindcssPatcher {
492
507
  return getContexts(basedir);
493
508
  }
494
509
  async extract(options) {
495
- const { configDir, filename, loose } = options;
496
- await getCss(configDir);
497
- const set = this.getClassSet();
498
- await ensureDir(path.dirname(filename));
499
- const classList = [...set];
500
- await fs__default$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
501
- return classList;
510
+ const { output, postcss } = options;
511
+ if (output && postcss) {
512
+ const { removeUniversalSelector, filename, loose } = output;
513
+ const { configDir } = postcss;
514
+ await getCss(configDir);
515
+ const set = this.getClassSet({
516
+ removeUniversalSelector
517
+ });
518
+ await ensureDir(path.dirname(filename));
519
+ const classList = [...set];
520
+ await fs__default$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
521
+ return filename;
522
+ }
502
523
  }
503
524
  }
504
525
 
@@ -506,13 +527,7 @@ function getConfig() {
506
527
  return c12.loadConfig({
507
528
  name: "tailwindcss-patch",
508
529
  defaults: {
509
- output: {
510
- filename: ".tw-patch/tw-class-list.json"
511
- },
512
- postcss: {
513
- configDir: process.cwd(),
514
- loose: true
515
- }
530
+ ...getDefaultUserConfig()
516
531
  }
517
532
  });
518
533
  }
package/dist/index.d.ts CHANGED
@@ -76,13 +76,20 @@ type TailwindcssRuntimeContext = {
76
76
  interface UserConfig {
77
77
  output?: {
78
78
  filename?: string;
79
+ loose?: boolean;
80
+ /**
81
+ * @description remove * in output json
82
+ */
83
+ removeUniversalSelector?: boolean;
79
84
  };
80
85
  postcss?: {
81
86
  configDir?: string;
82
- loose?: boolean;
83
87
  };
84
88
  tailwindcss?: {};
85
89
  }
90
+ type DeepRequired<T> = {
91
+ [K in keyof T]: Required<DeepRequired<T[K]>>;
92
+ };
86
93
 
87
94
  declare function getCacheOptions(options?: CacheOptions | boolean): InternalCacheOptions;
88
95
  declare class CacheManager {
@@ -117,19 +124,18 @@ declare class TailwindcssPatcher {
117
124
  getClassSet(options?: {
118
125
  basedir?: string;
119
126
  cacheStrategy?: CacheStrategy;
127
+ removeUniversalSelector?: boolean;
120
128
  }): Set<string>;
121
129
  getContexts(basedir?: string): TailwindcssRuntimeContext[];
122
- extract(options: {
123
- configDir: string;
124
- filename: string;
125
- loose: boolean;
126
- }): Promise<string[]>;
130
+ extract(options: DeepRequired<UserConfig>): Promise<string | undefined>;
127
131
  }
128
132
 
129
133
  declare function getTailwindcssEntry(basedir?: string): string;
130
134
  declare function getContexts(basedir?: string): TailwindcssRuntimeContext[];
131
135
  declare function getClassCaches(basedir?: string): TailwindcssClassCache[];
132
- declare function getClassCacheSet(basedir?: string): Set<string>;
136
+ declare function getClassCacheSet(basedir?: string, options?: {
137
+ removeUniversalSelector?: boolean;
138
+ }): Set<string>;
133
139
 
134
140
  declare function inspectProcessTailwindFeaturesReturnContext(content: string): {
135
141
  code: string;
@@ -149,11 +155,11 @@ declare function monkeyPatchForExposingContext(twDir: string, opt: InternalPatch
149
155
  } & Record<string, any>;
150
156
  declare function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions): any | undefined;
151
157
 
152
- declare function getConfig(): Promise<c12.ResolvedConfig<UserConfig, c12.ConfigLayerMeta>>;
158
+ declare function getConfig(): Promise<c12.ResolvedConfig<DeepRequired<UserConfig>, c12.ConfigLayerMeta>>;
153
159
  declare const defineConfig: c12.DefineConfig<UserConfig, c12.ConfigLayerMeta>;
154
160
 
155
161
  declare function ensureFileContent(filepaths: string | string[]): string | undefined;
156
162
  declare function requireResolve(id: string, opts?: SyncOpts): string;
157
163
  declare function ensureDir(p: string): Promise<void>;
158
164
 
159
- export { CacheManager, CacheOptions, CacheStrategy, 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 };
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 CHANGED
@@ -69,12 +69,15 @@ function getClassCaches(basedir) {
69
69
  const contexts = getContexts(basedir);
70
70
  return contexts.map((x) => x.classCache);
71
71
  }
72
- function getClassCacheSet(basedir) {
72
+ function getClassCacheSet(basedir, options) {
73
73
  const classCaches = getClassCaches(basedir);
74
74
  const classSet = /* @__PURE__ */ new Set();
75
75
  for (const classCacheMap of classCaches) {
76
76
  const keys = classCacheMap.keys();
77
77
  for (const key of keys) {
78
+ if (options?.removeUniversalSelector && key.toString() === "*") {
79
+ continue;
80
+ }
78
81
  classSet.add(key.toString());
79
82
  }
80
83
  }
@@ -345,6 +348,18 @@ const defu = createDefu();
345
348
  const defaultOptions = {
346
349
  overwrite: true
347
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
+ }
348
363
 
349
364
  function getInstalledPkgJsonPath(options = {}) {
350
365
  try {
@@ -453,11 +468,11 @@ class TailwindcssPatcher {
453
468
  * @param basedir
454
469
  * @returns
455
470
  */
456
- getClassSet(options = {
457
- cacheStrategy: this.cacheOptions.strategy ?? "merge"
458
- }) {
459
- const { basedir, cacheStrategy } = options;
460
- const set = getClassCacheSet(basedir);
471
+ getClassSet(options) {
472
+ const { basedir, cacheStrategy = this.cacheOptions.strategy ?? "merge", removeUniversalSelector = true } = options ?? {};
473
+ const set = getClassCacheSet(basedir, {
474
+ removeUniversalSelector
475
+ });
461
476
  if (cacheStrategy === "overwrite") {
462
477
  set.size > 0 && this.setCache(set);
463
478
  } else if (cacheStrategy === "merge") {
@@ -475,13 +490,19 @@ class TailwindcssPatcher {
475
490
  return getContexts(basedir);
476
491
  }
477
492
  async extract(options) {
478
- const { configDir, filename, loose } = options;
479
- await getCss(configDir);
480
- const set = this.getClassSet();
481
- await ensureDir(dirname(filename));
482
- const classList = [...set];
483
- await fs$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
484
- return classList;
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
+ }
485
506
  }
486
507
  }
487
508
 
@@ -489,13 +510,7 @@ function getConfig() {
489
510
  return loadConfig({
490
511
  name: "tailwindcss-patch",
491
512
  defaults: {
492
- output: {
493
- filename: ".tw-patch/tw-class-list.json"
494
- },
495
- postcss: {
496
- configDir: process.cwd(),
497
- loose: true
498
- }
513
+ ...getDefaultUserConfig()
499
514
  }
500
515
  });
501
516
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwindcss-patch",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0",
4
4
  "description": "patch tailwindcss for exposing context",
5
5
  "exports": {
6
6
  ".": {
@@ -75,6 +75,6 @@
75
75
  "build": "unbuild",
76
76
  "test": "vitest run --coverage.enabled",
77
77
  "test:dev": "vitest",
78
- "cli": "node bin/tw-patch.js"
78
+ "cli": "node bin/tw-patch.js install"
79
79
  }
80
80
  }