tailwindcss-patch 1.0.4 → 1.1.0-rc.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/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-8dfb86e3.js');
4
4
  require('path');
5
5
  require('fs');
6
6
  require('semver');
@@ -10,5 +10,6 @@ require('@babel/parser');
10
10
  require('@babel/traverse');
11
11
  require('resolve');
12
12
 
13
- const patch = patcher.createPatch();
13
+ const opt = patcher.getPatchOptions();
14
+ const patch = patcher.createPatch(opt);
14
15
  patch();
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-8dfb86e3.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,14 +52,125 @@ 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$1(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$1(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$1(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
+ function getCacheOptions(options) {
114
+ let cache;
115
+ switch (typeof options) {
116
+ case 'undefined': {
117
+ cache = {
118
+ enable: false
119
+ };
120
+ break;
121
+ }
122
+ case 'boolean': {
123
+ cache = {
124
+ enable: options
125
+ };
126
+ break;
127
+ }
128
+ case 'object': {
129
+ cache = Object.assign(Object.assign({}, options), { enable: true });
130
+ break;
131
+ }
132
+ }
133
+ return cache;
134
+ }
135
+ class TailwindcssPatcher {
136
+ constructor(options = {}) {
137
+ this.rawOptions = options;
138
+ this.cacheOptions = getCacheOptions(options.cache);
139
+ this.patchOptions = patcher.getPatchOptions(options.patch);
140
+ this.patch = patcher.createPatch(this.patchOptions);
141
+ }
142
+ getPkgEntry(basedir) {
143
+ return getTailwindcssEntry(basedir);
144
+ }
145
+ setCache(set) {
146
+ if (this.cacheOptions.enable) {
147
+ return writeCache(set, this.cacheOptions);
148
+ }
149
+ }
150
+ getCache() {
151
+ return readCache(this.cacheOptions);
152
+ }
153
+ getClassSet(basedir) {
154
+ const set = getClassCacheSet(basedir);
155
+ set.size && this.setCache(set);
156
+ return set;
157
+ }
158
+ getContexts(basedir) {
159
+ return getContexts(basedir);
160
+ }
161
+ }
162
+
55
163
  exports.createPatch = patcher.createPatch;
56
164
  exports.ensureFileContent = patcher.ensureFileContent;
57
165
  exports.getInstalledPkgJsonPath = patcher.getInstalledPkgJsonPath;
166
+ exports.getPatchOptions = patcher.getPatchOptions;
58
167
  exports.inspectPostcssPlugin = patcher.inspectPostcssPlugin;
59
168
  exports.inspectProcessTailwindFeaturesReturnContext = patcher.inspectProcessTailwindFeaturesReturnContext;
60
169
  exports.internalPatch = patcher.internalPatch;
61
170
  exports.monkeyPatchForExposingContext = patcher.monkeyPatchForExposingContext;
62
171
  exports.requireResolve = patcher.requireResolve;
172
+ exports.TailwindcssPatcher = TailwindcssPatcher;
173
+ exports.getCacheOptions = getCacheOptions;
63
174
  exports.getClassCacheSet = getClassCacheSet;
64
175
  exports.getClassCaches = getClassCaches;
65
176
  exports.getContexts = getContexts;
@@ -240,11 +240,15 @@ function getInstalledPkgJsonPath(options = {}) {
240
240
  }
241
241
  }
242
242
  }
243
- function createPatch(options = {}) {
244
- const opt = defu(options, defaultOptions);
243
+ function getPatchOptions(options = {}) {
244
+ return defu(options, {
245
+ basedir: process.cwd()
246
+ }, defaultOptions);
247
+ }
248
+ function createPatch(opt) {
245
249
  return () => {
246
250
  try {
247
- const pkgJsonPath = getInstalledPkgJsonPath(options);
251
+ const pkgJsonPath = getInstalledPkgJsonPath(opt);
248
252
  return internalPatch(pkgJsonPath, opt);
249
253
  }
250
254
  catch (error) {
@@ -296,6 +300,7 @@ function internalPatch(pkgJsonPath, options) {
296
300
  exports.createPatch = createPatch;
297
301
  exports.ensureFileContent = ensureFileContent;
298
302
  exports.getInstalledPkgJsonPath = getInstalledPkgJsonPath;
303
+ exports.getPatchOptions = getPatchOptions;
299
304
  exports.inspectPostcssPlugin = inspectPostcssPlugin;
300
305
  exports.inspectProcessTailwindFeaturesReturnContext = inspectProcessTailwindFeaturesReturnContext;
301
306
  exports.internalPatch = internalPatch;
@@ -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,18 @@
1
+ import type { CacheOptions, PatchOptions, InternalCacheOptions, InternalPatchOptions } from './type';
2
+ export interface TailwindcssPatcherOptions {
3
+ cache?: CacheOptions;
4
+ patch?: PatchOptions;
5
+ }
6
+ export declare function getCacheOptions(options?: CacheOptions): InternalCacheOptions;
7
+ export declare class TailwindcssPatcher {
8
+ rawOptions: TailwindcssPatcherOptions;
9
+ cacheOptions: InternalCacheOptions;
10
+ patchOptions: InternalPatchOptions;
11
+ patch: () => void;
12
+ constructor(options?: TailwindcssPatcherOptions);
13
+ getPkgEntry(basedir?: string): string;
14
+ setCache(set: Set<string>): string | undefined;
15
+ getCache(): Set<string> | undefined;
16
+ getClassSet(basedir?: string): Set<string>;
17
+ getContexts(basedir?: string): any[];
18
+ }
@@ -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,6 +1,7 @@
1
1
  import type { PatchOptions, InternalPatchOptions } from './type';
2
2
  export declare function getInstalledPkgJsonPath(options?: PatchOptions): string | undefined;
3
- export declare function createPatch(options?: PatchOptions): () => any;
3
+ export declare function getPatchOptions(options?: PatchOptions): InternalPatchOptions;
4
+ export declare function createPatch(opt: InternalPatchOptions): () => any;
4
5
  export declare function monkeyPatchForExposingContext(twDir: string, opt: InternalPatchOptions): {
5
6
  processTailwindFeatures?: string | undefined;
6
7
  plugin?: string | undefined;
@@ -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.4",
3
+ "version": "1.1.0-rc.1",
4
4
  "description": "patch tailwindcss for exposing context",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",