tailwindcss-patch 1.0.3 → 1.1.0-rc.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/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var patcher = require('./patcher-90d636ff.js');
3
+ var patcher = require('./patcher-75f59039.js');
4
4
  require('path');
5
5
  require('fs');
6
6
  require('semver');
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
- var patcher = require('./patcher-90d636ff.js');
7
+ var patcher = require('./patcher-75f59039.js');
8
8
  require('semver');
9
9
  require('@babel/types');
10
10
  require('@babel/generator');
@@ -35,12 +35,12 @@ function getContexts(basedir) {
35
35
  }
36
36
  return [];
37
37
  }
38
- function getClassCaches() {
39
- const contexts = getContexts();
38
+ function getClassCaches(basedir) {
39
+ const contexts = getContexts(basedir);
40
40
  return contexts.map((x) => x.classCache);
41
41
  }
42
- function getClassCacheSet() {
43
- const classCaches = getClassCaches();
42
+ function getClassCacheSet(basedir) {
43
+ const classCaches = getClassCaches(basedir);
44
44
  const classSet = new Set();
45
45
  for (let i = 0; i < classCaches.length; i++) {
46
46
  const classCacheMap = classCaches[i];
@@ -52,6 +52,111 @@ function getClassCacheSet() {
52
52
  return classSet;
53
53
  }
54
54
 
55
+ const pkgName = 'tailwindcss-patch';
56
+
57
+ function log(message, ...optionalParams) {
58
+ return console.log(`[${pkgName}]:` + message, ...optionalParams);
59
+ }
60
+
61
+ function mkCacheDirectory(cacheDirectory) {
62
+ const exists = fs__default["default"].existsSync(cacheDirectory);
63
+ if (!exists) {
64
+ fs__default["default"].mkdirSync(cacheDirectory, {
65
+ recursive: true
66
+ });
67
+ }
68
+ return cacheDirectory;
69
+ }
70
+ function getCacheOptions(options = {}) {
71
+ var _a, _b, _c;
72
+ const cwd = (_a = options.cwd) !== null && _a !== void 0 ? _a : process.cwd();
73
+ const dir = (_b = options.dir) !== null && _b !== void 0 ? _b : path__default["default"].resolve(cwd, 'node_modules/.cache', pkgName);
74
+ const file = (_c = options.file) !== null && _c !== void 0 ? _c : 'index.json';
75
+ const filename = path__default["default"].resolve(dir, file);
76
+ return {
77
+ cwd,
78
+ dir,
79
+ file,
80
+ filename
81
+ };
82
+ }
83
+ function writeCache(data, options = {}) {
84
+ try {
85
+ const { dir, filename } = getCacheOptions(options);
86
+ mkCacheDirectory(dir);
87
+ fs__default["default"].writeFileSync(filename, JSON.stringify(Array.from(data), null, 2), 'utf-8');
88
+ return filename;
89
+ }
90
+ catch (error) {
91
+ log('write cache file fail!');
92
+ }
93
+ }
94
+ function readCache(options = {}) {
95
+ const { filename } = getCacheOptions(options);
96
+ try {
97
+ if (fs__default["default"].existsSync(filename)) {
98
+ const data = fs__default["default"].readFileSync(filename, 'utf-8');
99
+ return new Set(JSON.parse(data));
100
+ }
101
+ }
102
+ catch (error) {
103
+ log('parse cache content fail! path:' + filename);
104
+ try {
105
+ fs__default["default"].unlinkSync(filename);
106
+ }
107
+ catch (error) {
108
+ log('delete cache file fail! path:' + filename);
109
+ }
110
+ }
111
+ }
112
+
113
+ class TailwindcssPatcher {
114
+ constructor(options = {}) {
115
+ this.rawOptions = options;
116
+ let cache;
117
+ switch (typeof options.cache) {
118
+ case 'undefined': {
119
+ cache = {
120
+ enable: false
121
+ };
122
+ break;
123
+ }
124
+ case 'boolean': {
125
+ cache = {
126
+ enable: options.cache
127
+ };
128
+ break;
129
+ }
130
+ case 'object': {
131
+ cache = Object.assign(Object.assign({}, options.cache), { enable: true });
132
+ break;
133
+ }
134
+ }
135
+ this.cacheOptions = cache;
136
+ this.patchOptions = options.patch;
137
+ this.patch = patcher.createPatch(options.patch);
138
+ }
139
+ getPkgEntry(basedir) {
140
+ return getTailwindcssEntry(basedir);
141
+ }
142
+ setCache(set) {
143
+ if (this.cacheOptions.enable) {
144
+ return writeCache(set, this.cacheOptions);
145
+ }
146
+ }
147
+ getCache() {
148
+ return readCache(this.cacheOptions);
149
+ }
150
+ getClassSet(basedir) {
151
+ const set = getClassCacheSet(basedir);
152
+ set.size && this.setCache(set);
153
+ return set;
154
+ }
155
+ getContexts(basedir) {
156
+ return getContexts(basedir);
157
+ }
158
+ }
159
+
55
160
  exports.createPatch = patcher.createPatch;
56
161
  exports.ensureFileContent = patcher.ensureFileContent;
57
162
  exports.getInstalledPkgJsonPath = patcher.getInstalledPkgJsonPath;
@@ -60,6 +165,7 @@ exports.inspectProcessTailwindFeaturesReturnContext = patcher.inspectProcessTail
60
165
  exports.internalPatch = patcher.internalPatch;
61
166
  exports.monkeyPatchForExposingContext = patcher.monkeyPatchForExposingContext;
62
167
  exports.requireResolve = patcher.requireResolve;
168
+ exports.TailwindcssPatcher = TailwindcssPatcher;
63
169
  exports.getClassCacheSet = getClassCacheSet;
64
170
  exports.getClassCaches = getClassCaches;
65
171
  exports.getContexts = getContexts;
@@ -241,10 +241,12 @@ function getInstalledPkgJsonPath(options = {}) {
241
241
  }
242
242
  }
243
243
  function createPatch(options = {}) {
244
- const opt = defu(options, defaultOptions);
244
+ const opt = defu(options, {
245
+ basedir: process.cwd()
246
+ }, defaultOptions);
245
247
  return () => {
246
248
  try {
247
- const pkgJsonPath = getInstalledPkgJsonPath(options);
249
+ const pkgJsonPath = getInstalledPkgJsonPath(opt);
248
250
  return internalPatch(pkgJsonPath, opt);
249
251
  }
250
252
  catch (error) {
@@ -0,0 +1,7 @@
1
+ import type { CacheOptions } from './type';
2
+ export declare function mkCacheDirectory(cacheDirectory: string): string;
3
+ export declare function getCacheOptions(options?: CacheOptions): Required<CacheOptions> & {
4
+ filename: string;
5
+ };
6
+ export declare function writeCache(data: Set<string>, options?: CacheOptions): string | undefined;
7
+ export declare function readCache(options?: CacheOptions): Set<string> | undefined;
@@ -0,0 +1,17 @@
1
+ import type { InternalCacheOptions, PatchOptions } from './type';
2
+ export interface TailwindcssPatcherOptions {
3
+ cache?: InternalCacheOptions;
4
+ patch?: PatchOptions;
5
+ }
6
+ export declare class TailwindcssPatcher {
7
+ rawOptions: TailwindcssPatcherOptions;
8
+ cacheOptions: InternalCacheOptions;
9
+ patchOptions?: PatchOptions;
10
+ patch: () => void;
11
+ constructor(options?: TailwindcssPatcherOptions);
12
+ getPkgEntry(basedir?: string): string;
13
+ setCache(set: Set<string>): string | undefined;
14
+ getCache(): Set<string> | undefined;
15
+ getClassSet(basedir?: string): Set<string>;
16
+ getContexts(basedir?: string): any[];
17
+ }
@@ -0,0 +1 @@
1
+ export declare const pkgName = "tailwindcss-patch";
@@ -1,9 +1,9 @@
1
1
  import type { Rule } from 'postcss';
2
2
  export declare function getTailwindcssEntry(basedir?: string): string;
3
3
  export declare function getContexts(basedir?: string): any[];
4
- export declare function getClassCaches(): Map<string, ({
4
+ export declare function getClassCaches(basedir?: string): Map<string, ({
5
5
  layer: string;
6
6
  options: Record<string, any>;
7
7
  sort: Record<string, any>;
8
8
  } | Rule)[]>[];
9
- export declare function getClassCacheSet(): Set<string>;
9
+ export declare function getClassCacheSet(basedir?: string): Set<string>;
@@ -1,3 +1,4 @@
1
+ export * from './class';
1
2
  export * from './exposeContext';
2
3
  export * from './inspector';
3
4
  export * from './patcher';
@@ -0,0 +1 @@
1
+ export declare function log(message?: any, ...optionalParams: any[]): void;
@@ -1,3 +1,11 @@
1
+ export interface CacheOptions {
2
+ dir?: string;
3
+ cwd?: string;
4
+ file?: string;
5
+ }
6
+ export type InternalCacheOptions = CacheOptions & {
7
+ enable?: boolean;
8
+ };
1
9
  export interface PatchOptions {
2
10
  overwrite?: boolean;
3
11
  paths?: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwindcss-patch",
3
- "version": "1.0.3",
3
+ "version": "1.1.0-rc.0",
4
4
  "description": "patch tailwindcss for exposing context",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/babel__generator": "^7.6.4",
27
- "@types/babel__traverse": "^7.18.4",
27
+ "@types/babel__traverse": "^7.18.5",
28
28
  "@types/resolve": "^1.20.2",
29
29
  "@types/semver": "^7.3.13",
30
30
  "defu": "^6.1.2",
@@ -51,8 +51,6 @@
51
51
  "dev:tsc": "tsc -p tsconfig.json --sourceMap",
52
52
  "build:tsc": "tsc -p tsconfig.json",
53
53
  "test": "npm run patch && jest",
54
- "postinstall": "echo postinstall",
55
- "preinstall": "npx only-allow pnpm",
56
54
  "patch": "ts-node src/cli.ts"
57
55
  }
58
56
  }