tailwindcss-patch 2.0.0 → 2.0.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
@@ -1,6 +1,6 @@
1
1
  # tailwindcss-patch
2
2
 
3
- get tailwindcss context at runtime ! extract all class to json file!
3
+ get tailwindcss context at runtime ! extract all classes into file!
4
4
 
5
5
  - [tailwindcss-patch](#tailwindcss-patch)
6
6
  - [Setup](#setup)
@@ -10,9 +10,10 @@ get tailwindcss context at runtime ! extract all class to json file!
10
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)
13
+ - [0. cli command change](#0-cli-command-change)
14
14
  - [1. default remove `*` in json array result](#1-default-remove--in-json-array-result)
15
- - [Notice](#notice)
15
+
16
+ > Nodejs version should >= `16.6.0`
16
17
 
17
18
  ## Setup
18
19
 
@@ -77,7 +78,7 @@ twPatcher.getClassSet()
77
78
 
78
79
  ## Migration form v1 to v2
79
80
 
80
- ### 2. cli command change
81
+ ### 0. cli command change
81
82
 
82
83
  ```diff
83
84
  {
@@ -95,11 +96,3 @@ twPatcher.getClassSet()
95
96
  "text-[100px]"
96
97
  ]
97
98
  ```
98
-
99
- ## Notice
100
-
101
- `getContexts`,`getClassCacheSet` should be invoked after `postcss-loader`'s activation.
102
-
103
- which means you may not get tailwindcss contexts at build start time.
104
-
105
- you may call them at `generateBundle` lifetime hook in `vite/webpack plugin`.
package/dist/cli.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  const index = require('./index.cjs');
4
- const path = require('node:path');
4
+ require('node:path');
5
5
  require('node:fs');
6
- const fs = require('node:fs/promises');
6
+ require('node:fs/promises');
7
7
  require('resolve');
8
8
  require('@babel/types');
9
9
  require('@babel/generator');
@@ -12,86 +12,15 @@ require('@babel/parser');
12
12
  const cac = require('cac');
13
13
  require('semver');
14
14
  require('postcss');
15
- require('postcss-load-config');
16
15
  require('c12');
17
16
 
18
17
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
19
18
 
20
- const path__default = /*#__PURE__*/_interopDefaultCompat(path);
21
- const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
22
19
  const cac__default = /*#__PURE__*/_interopDefaultCompat(cac);
23
20
 
24
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
25
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
26
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
28
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
29
- const dedent = createDedent({});
30
- function createDedent(options) {
31
- dedent.withOptions = newOptions => createDedent(_objectSpread(_objectSpread({}, options), newOptions));
32
- return dedent;
33
- function dedent(strings, ...values) {
34
- const raw = typeof strings === "string" ? [strings] : strings.raw;
35
- const {
36
- escapeSpecialCharacters = Array.isArray(strings)
37
- } = options;
38
-
39
- // first, perform interpolation
40
- let result = "";
41
- for (let i = 0; i < raw.length; i++) {
42
- let next = raw[i];
43
- if (escapeSpecialCharacters) {
44
- // handle escaped newlines, backticks, and interpolation characters
45
- next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\{/g, "{");
46
- }
47
- result += next;
48
- if (i < values.length) {
49
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
50
- result += values[i];
51
- }
52
- }
53
-
54
- // now strip indentation
55
- const lines = result.split("\n");
56
- let mindent = null;
57
- for (const l of lines) {
58
- const m = l.match(/^(\s+)\S+/);
59
- if (m) {
60
- const indent = m[1].length;
61
- if (!mindent) {
62
- // this is the first indented line
63
- mindent = indent;
64
- } else {
65
- mindent = Math.min(mindent, indent);
66
- }
67
- }
68
- }
69
- if (mindent !== null) {
70
- const m = mindent; // appease TypeScript
71
- result = lines
72
- // https://github.com/typescript-eslint/typescript-eslint/issues/7140
73
- // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
74
- .map(l => l[0] === " " || l[0] === "\t" ? l.slice(m) : l).join("\n");
75
- }
76
- return result
77
- // dedent eats leading and trailing whitespace too
78
- .trim()
79
- // handle escaped newlines at the end to ensure they don't get stripped too
80
- .replace(/\\n/g, "\n");
81
- }
82
- }
83
-
84
21
  function init() {
85
22
  const cwd = process.cwd();
86
- return fs__default.writeFile(
87
- path__default.resolve(cwd, "tailwindcss-patch.config.ts"),
88
- dedent`
89
- import { defineConfig } from 'tailwindcss-patch'
90
-
91
- export default defineConfig({})
92
- `,
93
- "utf8"
94
- );
23
+ return index.initConfig(cwd);
95
24
  }
96
25
  const cli = cac__default();
97
26
  cli.command("install", "patch install").action(() => {
package/dist/cli.mjs CHANGED
@@ -1,7 +1,7 @@
1
- import { getPatchOptions, getConfig, TailwindcssPatcher, createPatch } from './index.mjs';
2
- import path from 'node:path';
1
+ import { getPatchOptions, getConfig, TailwindcssPatcher, initConfig, createPatch } from './index.mjs';
2
+ import 'node:path';
3
3
  import 'node:fs';
4
- import fs from 'node:fs/promises';
4
+ import 'node:fs/promises';
5
5
  import 'resolve';
6
6
  import '@babel/types';
7
7
  import '@babel/generator';
@@ -10,80 +10,11 @@ import '@babel/parser';
10
10
  import cac from 'cac';
11
11
  import 'semver';
12
12
  import 'postcss';
13
- import 'postcss-load-config';
14
13
  import 'c12';
15
14
 
16
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
17
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
18
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
20
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
21
- const dedent = createDedent({});
22
- function createDedent(options) {
23
- dedent.withOptions = newOptions => createDedent(_objectSpread(_objectSpread({}, options), newOptions));
24
- return dedent;
25
- function dedent(strings, ...values) {
26
- const raw = typeof strings === "string" ? [strings] : strings.raw;
27
- const {
28
- escapeSpecialCharacters = Array.isArray(strings)
29
- } = options;
30
-
31
- // first, perform interpolation
32
- let result = "";
33
- for (let i = 0; i < raw.length; i++) {
34
- let next = raw[i];
35
- if (escapeSpecialCharacters) {
36
- // handle escaped newlines, backticks, and interpolation characters
37
- next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\{/g, "{");
38
- }
39
- result += next;
40
- if (i < values.length) {
41
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
42
- result += values[i];
43
- }
44
- }
45
-
46
- // now strip indentation
47
- const lines = result.split("\n");
48
- let mindent = null;
49
- for (const l of lines) {
50
- const m = l.match(/^(\s+)\S+/);
51
- if (m) {
52
- const indent = m[1].length;
53
- if (!mindent) {
54
- // this is the first indented line
55
- mindent = indent;
56
- } else {
57
- mindent = Math.min(mindent, indent);
58
- }
59
- }
60
- }
61
- if (mindent !== null) {
62
- const m = mindent; // appease TypeScript
63
- result = lines
64
- // https://github.com/typescript-eslint/typescript-eslint/issues/7140
65
- // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
66
- .map(l => l[0] === " " || l[0] === "\t" ? l.slice(m) : l).join("\n");
67
- }
68
- return result
69
- // dedent eats leading and trailing whitespace too
70
- .trim()
71
- // handle escaped newlines at the end to ensure they don't get stripped too
72
- .replace(/\\n/g, "\n");
73
- }
74
- }
75
-
76
15
  function init() {
77
16
  const cwd = process.cwd();
78
- return fs.writeFile(
79
- path.resolve(cwd, "tailwindcss-patch.config.ts"),
80
- dedent`
81
- import { defineConfig } from 'tailwindcss-patch'
82
-
83
- export default defineConfig({})
84
- `,
85
- "utf8"
86
- );
17
+ return initConfig(cwd);
87
18
  }
88
19
  const cli = cac();
89
20
  cli.command("install", "patch install").action(() => {
package/dist/index.cjs CHANGED
@@ -10,7 +10,6 @@ const generate = require('@babel/generator');
10
10
  const traverse = require('@babel/traverse');
11
11
  const parser = require('@babel/parser');
12
12
  const postcss = require('postcss');
13
- const postcssrc = require('postcss-load-config');
14
13
  const c12 = require('c12');
15
14
 
16
15
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
@@ -35,7 +34,6 @@ const t__namespace = /*#__PURE__*/_interopNamespaceCompat(t);
35
34
  const generate__default = /*#__PURE__*/_interopDefaultCompat(generate);
36
35
  const traverse__default = /*#__PURE__*/_interopDefaultCompat(traverse);
37
36
  const postcss__default = /*#__PURE__*/_interopDefaultCompat(postcss);
38
- const postcssrc__default = /*#__PURE__*/_interopDefaultCompat(postcssrc);
39
37
 
40
38
  const { sync } = pkg__default;
41
39
  function ensureFileContent(filepaths) {
@@ -92,10 +90,11 @@ function getClassCacheSet(basedir, options) {
92
90
  for (const classCacheMap of classCaches) {
93
91
  const keys = classCacheMap.keys();
94
92
  for (const key of keys) {
95
- if (options?.removeUniversalSelector && key.toString() === "*") {
93
+ const v = key.toString();
94
+ if (options?.removeUniversalSelector && v === "*") {
96
95
  continue;
97
96
  }
98
- classSet.add(key.toString());
97
+ classSet.add(v);
99
98
  }
100
99
  }
101
100
  return classSet;
@@ -362,9 +361,11 @@ function createDefu(merger) {
362
361
  }
363
362
  const defu = createDefu();
364
363
 
365
- const defaultOptions = {
366
- overwrite: true
367
- };
364
+ function getDefaultPatchOptions() {
365
+ return {
366
+ overwrite: true
367
+ };
368
+ }
368
369
  function getDefaultUserConfig() {
369
370
  return {
370
371
  output: {
@@ -372,8 +373,8 @@ function getDefaultUserConfig() {
372
373
  removeUniversalSelector: true,
373
374
  loose: true
374
375
  },
375
- postcss: {
376
- configDir: process.cwd()
376
+ tailwindcss: {
377
+ cwd: process.cwd()
377
378
  }
378
379
  };
379
380
  }
@@ -396,7 +397,7 @@ function getPatchOptions(options = {}) {
396
397
  {
397
398
  basedir: process.cwd()
398
399
  },
399
- defaultOptions
400
+ getDefaultPatchOptions()
400
401
  );
401
402
  }
402
403
  function createPatch(opt) {
@@ -451,11 +452,15 @@ function internalPatch(pkgJsonPath, options) {
451
452
  }
452
453
  }
453
454
 
454
- async function getCss(p) {
455
- const { options, plugins } = await postcssrc__default(void 0, p);
456
- const res = await postcss__default(plugins).process("@tailwind base;@tailwind components;@tailwind utilities;", {
457
- from: void 0,
458
- ...options
455
+ async function processTailwindcss(options) {
456
+ options.cwd = options.cwd ?? process.cwd();
457
+ const config = options.config && path__default.isAbsolute(options.config) ? options.config : path__default.resolve(options.cwd, "tailwind.config.js");
458
+ const res = await postcss__default([
459
+ require("tailwindcss")({
460
+ config
461
+ })
462
+ ]).process("@tailwind base;@tailwind components;@tailwind utilities;", {
463
+ from: void 0
459
464
  });
460
465
  return res;
461
466
  }
@@ -507,31 +512,104 @@ class TailwindcssPatcher {
507
512
  return getContexts(basedir);
508
513
  }
509
514
  async extract(options) {
510
- const { output, postcss } = options;
511
- if (output && postcss) {
515
+ const { output, tailwindcss } = options;
516
+ if (output && tailwindcss) {
512
517
  const { removeUniversalSelector, filename, loose } = output;
513
- const { configDir } = postcss;
514
- await getCss(configDir);
518
+ await processTailwindcss(tailwindcss);
515
519
  const set = this.getClassSet({
516
520
  removeUniversalSelector
517
521
  });
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
+ if (filename) {
523
+ await ensureDir(path.dirname(filename));
524
+ const classList = [...set];
525
+ await fs__default$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
526
+ return filename;
527
+ }
528
+ }
529
+ }
530
+ }
531
+
532
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
533
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
534
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
535
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
536
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
537
+ const dedent = createDedent({});
538
+ function createDedent(options) {
539
+ dedent.withOptions = newOptions => createDedent(_objectSpread(_objectSpread({}, options), newOptions));
540
+ return dedent;
541
+ function dedent(strings, ...values) {
542
+ const raw = typeof strings === "string" ? [strings] : strings.raw;
543
+ const {
544
+ escapeSpecialCharacters = Array.isArray(strings)
545
+ } = options;
546
+
547
+ // first, perform interpolation
548
+ let result = "";
549
+ for (let i = 0; i < raw.length; i++) {
550
+ let next = raw[i];
551
+ if (escapeSpecialCharacters) {
552
+ // handle escaped newlines, backticks, and interpolation characters
553
+ next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\{/g, "{");
554
+ }
555
+ result += next;
556
+ if (i < values.length) {
557
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
558
+ result += values[i];
559
+ }
560
+ }
561
+
562
+ // now strip indentation
563
+ const lines = result.split("\n");
564
+ let mindent = null;
565
+ for (const l of lines) {
566
+ const m = l.match(/^(\s+)\S+/);
567
+ if (m) {
568
+ const indent = m[1].length;
569
+ if (!mindent) {
570
+ // this is the first indented line
571
+ mindent = indent;
572
+ } else {
573
+ mindent = Math.min(mindent, indent);
574
+ }
575
+ }
576
+ }
577
+ if (mindent !== null) {
578
+ const m = mindent; // appease TypeScript
579
+ result = lines
580
+ // https://github.com/typescript-eslint/typescript-eslint/issues/7140
581
+ // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
582
+ .map(l => l[0] === " " || l[0] === "\t" ? l.slice(m) : l).join("\n");
522
583
  }
584
+ return result
585
+ // dedent eats leading and trailing whitespace too
586
+ .trim()
587
+ // handle escaped newlines at the end to ensure they don't get stripped too
588
+ .replace(/\\n/g, "\n");
523
589
  }
524
590
  }
525
591
 
526
- function getConfig() {
592
+ function getConfig(cwd) {
527
593
  return c12.loadConfig({
528
594
  name: "tailwindcss-patch",
529
595
  defaults: {
530
596
  ...getDefaultUserConfig()
531
- }
597
+ },
598
+ cwd
532
599
  });
533
600
  }
534
601
  const defineConfig = c12.createDefineConfig();
602
+ function initConfig(cwd) {
603
+ return fs__default$1.writeFile(
604
+ path__default.resolve(cwd, "tailwindcss-patch.config.ts"),
605
+ dedent`
606
+ import { defineConfig } from 'tailwindcss-patch'
607
+
608
+ export default defineConfig({})
609
+ `,
610
+ "utf8"
611
+ );
612
+ }
535
613
 
536
614
  exports.CacheManager = CacheManager;
537
615
  exports.TailwindcssPatcher = TailwindcssPatcher;
@@ -547,6 +625,7 @@ exports.getContexts = getContexts;
547
625
  exports.getInstalledPkgJsonPath = getInstalledPkgJsonPath;
548
626
  exports.getPatchOptions = getPatchOptions;
549
627
  exports.getTailwindcssEntry = getTailwindcssEntry;
628
+ exports.initConfig = initConfig;
550
629
  exports.inspectPostcssPlugin = inspectPostcssPlugin;
551
630
  exports.inspectProcessTailwindFeaturesReturnContext = inspectProcessTailwindFeaturesReturnContext;
552
631
  exports.internalPatch = internalPatch;
package/dist/index.d.ts CHANGED
@@ -82,10 +82,10 @@ interface UserConfig {
82
82
  */
83
83
  removeUniversalSelector?: boolean;
84
84
  };
85
- postcss?: {
86
- configDir?: string;
85
+ tailwindcss?: {
86
+ cwd?: string;
87
+ config?: string;
87
88
  };
88
- tailwindcss?: {};
89
89
  }
90
90
  type DeepRequired<T> = {
91
91
  [K in keyof T]: Required<DeepRequired<T[K]>>;
@@ -127,7 +127,7 @@ declare class TailwindcssPatcher {
127
127
  removeUniversalSelector?: boolean;
128
128
  }): Set<string>;
129
129
  getContexts(basedir?: string): TailwindcssRuntimeContext[];
130
- extract(options: DeepRequired<UserConfig>): Promise<string | undefined>;
130
+ extract(options: UserConfig): Promise<string | undefined>;
131
131
  }
132
132
 
133
133
  declare function getTailwindcssEntry(basedir?: string): string;
@@ -155,11 +155,12 @@ declare function monkeyPatchForExposingContext(twDir: string, opt: InternalPatch
155
155
  } & Record<string, any>;
156
156
  declare function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions): any | undefined;
157
157
 
158
- declare function getConfig(): Promise<c12.ResolvedConfig<DeepRequired<UserConfig>, c12.ConfigLayerMeta>>;
158
+ declare function getConfig(cwd?: string): Promise<c12.ResolvedConfig<UserConfig, c12.ConfigLayerMeta>>;
159
159
  declare const defineConfig: c12.DefineConfig<UserConfig, c12.ConfigLayerMeta>;
160
+ declare function initConfig(cwd: string): Promise<void>;
160
161
 
161
162
  declare function ensureFileContent(filepaths: string | string[]): string | undefined;
162
163
  declare function requireResolve(id: string, opts?: SyncOpts): string;
163
164
  declare function ensureDir(p: string): Promise<void>;
164
165
 
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 };
166
+ 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, initConfig, inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext, internalPatch, monkeyPatchForExposingContext, requireResolve };
package/dist/index.mjs CHANGED
@@ -8,18 +8,8 @@ import generate from '@babel/generator';
8
8
  import traverse from '@babel/traverse';
9
9
  import { parse } from '@babel/parser';
10
10
  import postcss from 'postcss';
11
- import postcssrc from 'postcss-load-config';
12
11
  import { createDefineConfig, loadConfig } from 'c12';
13
12
 
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
13
  const { sync } = pkg;
24
14
  function ensureFileContent(filepaths) {
25
15
  if (typeof filepaths === "string") {
@@ -75,10 +65,11 @@ function getClassCacheSet(basedir, options) {
75
65
  for (const classCacheMap of classCaches) {
76
66
  const keys = classCacheMap.keys();
77
67
  for (const key of keys) {
78
- if (options?.removeUniversalSelector && key.toString() === "*") {
68
+ const v = key.toString();
69
+ if (options?.removeUniversalSelector && v === "*") {
79
70
  continue;
80
71
  }
81
- classSet.add(key.toString());
72
+ classSet.add(v);
82
73
  }
83
74
  }
84
75
  return classSet;
@@ -345,9 +336,11 @@ function createDefu(merger) {
345
336
  }
346
337
  const defu = createDefu();
347
338
 
348
- const defaultOptions = {
349
- overwrite: true
350
- };
339
+ function getDefaultPatchOptions() {
340
+ return {
341
+ overwrite: true
342
+ };
343
+ }
351
344
  function getDefaultUserConfig() {
352
345
  return {
353
346
  output: {
@@ -355,8 +348,8 @@ function getDefaultUserConfig() {
355
348
  removeUniversalSelector: true,
356
349
  loose: true
357
350
  },
358
- postcss: {
359
- configDir: process.cwd()
351
+ tailwindcss: {
352
+ cwd: process.cwd()
360
353
  }
361
354
  };
362
355
  }
@@ -379,7 +372,7 @@ function getPatchOptions(options = {}) {
379
372
  {
380
373
  basedir: process.cwd()
381
374
  },
382
- defaultOptions
375
+ getDefaultPatchOptions()
383
376
  );
384
377
  }
385
378
  function createPatch(opt) {
@@ -434,11 +427,15 @@ function internalPatch(pkgJsonPath, options) {
434
427
  }
435
428
  }
436
429
 
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
430
+ async function processTailwindcss(options) {
431
+ options.cwd = options.cwd ?? process.cwd();
432
+ const config = options.config && path.isAbsolute(options.config) ? options.config : path.resolve(options.cwd, "tailwind.config.js");
433
+ const res = await postcss([
434
+ require("tailwindcss")({
435
+ config
436
+ })
437
+ ]).process("@tailwind base;@tailwind components;@tailwind utilities;", {
438
+ from: void 0
442
439
  });
443
440
  return res;
444
441
  }
@@ -490,30 +487,112 @@ class TailwindcssPatcher {
490
487
  return getContexts(basedir);
491
488
  }
492
489
  async extract(options) {
493
- const { output, postcss } = options;
494
- if (output && postcss) {
490
+ const { output, tailwindcss } = options;
491
+ if (output && tailwindcss) {
495
492
  const { removeUniversalSelector, filename, loose } = output;
496
- const { configDir } = postcss;
497
- await getCss(configDir);
493
+ await processTailwindcss(tailwindcss);
498
494
  const set = this.getClassSet({
499
495
  removeUniversalSelector
500
496
  });
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;
497
+ if (filename) {
498
+ await ensureDir(dirname(filename));
499
+ const classList = [...set];
500
+ await fs$1.writeFile(filename, JSON.stringify(classList, null, loose ? 2 : void 0), "utf8");
501
+ return filename;
502
+ }
505
503
  }
506
504
  }
507
505
  }
508
506
 
509
- function getConfig() {
507
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
508
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
509
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
510
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
511
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
512
+ const dedent = createDedent({});
513
+ function createDedent(options) {
514
+ dedent.withOptions = newOptions => createDedent(_objectSpread(_objectSpread({}, options), newOptions));
515
+ return dedent;
516
+ function dedent(strings, ...values) {
517
+ const raw = typeof strings === "string" ? [strings] : strings.raw;
518
+ const {
519
+ escapeSpecialCharacters = Array.isArray(strings)
520
+ } = options;
521
+
522
+ // first, perform interpolation
523
+ let result = "";
524
+ for (let i = 0; i < raw.length; i++) {
525
+ let next = raw[i];
526
+ if (escapeSpecialCharacters) {
527
+ // handle escaped newlines, backticks, and interpolation characters
528
+ next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\{/g, "{");
529
+ }
530
+ result += next;
531
+ if (i < values.length) {
532
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
533
+ result += values[i];
534
+ }
535
+ }
536
+
537
+ // now strip indentation
538
+ const lines = result.split("\n");
539
+ let mindent = null;
540
+ for (const l of lines) {
541
+ const m = l.match(/^(\s+)\S+/);
542
+ if (m) {
543
+ const indent = m[1].length;
544
+ if (!mindent) {
545
+ // this is the first indented line
546
+ mindent = indent;
547
+ } else {
548
+ mindent = Math.min(mindent, indent);
549
+ }
550
+ }
551
+ }
552
+ if (mindent !== null) {
553
+ const m = mindent; // appease TypeScript
554
+ result = lines
555
+ // https://github.com/typescript-eslint/typescript-eslint/issues/7140
556
+ // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
557
+ .map(l => l[0] === " " || l[0] === "\t" ? l.slice(m) : l).join("\n");
558
+ }
559
+ return result
560
+ // dedent eats leading and trailing whitespace too
561
+ .trim()
562
+ // handle escaped newlines at the end to ensure they don't get stripped too
563
+ .replace(/\\n/g, "\n");
564
+ }
565
+ }
566
+
567
+ function getConfig(cwd) {
510
568
  return loadConfig({
511
569
  name: "tailwindcss-patch",
512
570
  defaults: {
513
571
  ...getDefaultUserConfig()
514
- }
572
+ },
573
+ cwd
515
574
  });
516
575
  }
517
576
  const defineConfig = createDefineConfig();
577
+ function initConfig(cwd) {
578
+ return fs$1.writeFile(
579
+ path.resolve(cwd, "tailwindcss-patch.config.ts"),
580
+ dedent`
581
+ import { defineConfig } from 'tailwindcss-patch'
582
+
583
+
584
+
585
+ // -- Unbuild CommonJS Shims --
586
+ import __cjs_url__ from 'url';
587
+ import __cjs_path__ from 'path';
588
+ import __cjs_mod__ from 'module';
589
+ const __filename = __cjs_url__.fileURLToPath(import.meta.url);
590
+ const __dirname = __cjs_path__.dirname(__filename);
591
+ const require = __cjs_mod__.createRequire(import.meta.url);
592
+ export default defineConfig({})
593
+ `,
594
+ "utf8"
595
+ );
596
+ }
518
597
 
519
- export { CacheManager, TailwindcssPatcher, createPatch, defineConfig, ensureDir, ensureFileContent, getCacheOptions, getClassCacheSet, getClassCaches, getConfig, getContexts, getInstalledPkgJsonPath, getPatchOptions, getTailwindcssEntry, inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext, internalPatch, monkeyPatchForExposingContext, requireResolve };
598
+ export { CacheManager, TailwindcssPatcher, createPatch, defineConfig, ensureDir, ensureFileContent, getCacheOptions, getClassCacheSet, getClassCaches, getConfig, getContexts, getInstalledPkgJsonPath, getPatchOptions, getTailwindcssEntry, initConfig, inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext, internalPatch, monkeyPatchForExposingContext, requireResolve };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tailwindcss-patch",
3
- "version": "2.0.0",
4
- "description": "patch tailwindcss for exposing context",
3
+ "version": "2.0.1",
4
+ "description": "patch tailwindcss for exposing context and extract classes",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
@@ -30,7 +30,9 @@
30
30
  ],
31
31
  "keywords": [
32
32
  "tailwindcss",
33
- "patch"
33
+ "patch",
34
+ "extract",
35
+ "class"
34
36
  ],
35
37
  "author": "SonOfMagic <qq1324318532@gmail.com>",
36
38
  "license": "MIT",
@@ -38,13 +40,19 @@
38
40
  "access": "public",
39
41
  "registry": "https://registry.npmjs.org/"
40
42
  },
43
+ "peerDependencies": {
44
+ "tailwindcss": ">=3.0.0"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "tailwindcss": {
48
+ "optional": false
49
+ }
50
+ },
41
51
  "devDependencies": {
42
52
  "@types/babel__generator": "^7.6.4",
43
53
  "@types/babel__traverse": "^7.20.1",
44
54
  "@types/resolve": "^1.20.2",
45
55
  "@types/semver": "^7.5.0",
46
- "dedent": "^1.5.1",
47
- "defu": "^6.1.2",
48
56
  "pkg-types": "^1.0.3",
49
57
  "tailwindcss": "^3.3.3"
50
58
  },
@@ -56,7 +64,6 @@
56
64
  "c12": "^1.4.2",
57
65
  "cac": "^6.7.14",
58
66
  "postcss": "^8.4.27",
59
- "postcss-load-config": "^4.0.1",
60
67
  "resolve": "^1.22.4",
61
68
  "semver": "^7.5.4"
62
69
  },