tailwindcss-patch 1.0.0 → 1.0.2

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-e9a851ca.js');
3
+ var patcher = require('./patcher-85767405.js');
4
4
  require('path');
5
5
  require('fs');
6
6
  require('semver');
@@ -8,6 +8,7 @@ require('@babel/types');
8
8
  require('@babel/generator');
9
9
  require('@babel/parser');
10
10
  require('@babel/traverse');
11
+ require('resolve');
11
12
 
12
- const patch = patcher.createPatch({});
13
+ const patch = patcher.createPatch();
13
14
  patch();
package/dist/index.js CHANGED
@@ -4,20 +4,27 @@ 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-e9a851ca.js');
7
+ var patcher = require('./patcher-85767405.js');
8
8
  require('semver');
9
9
  require('@babel/types');
10
10
  require('@babel/generator');
11
11
  require('@babel/parser');
12
12
  require('@babel/traverse');
13
+ require('resolve');
13
14
 
14
15
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
16
 
16
17
  var path__default = /*#__PURE__*/_interopDefaultCompat(path);
17
18
  var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
18
19
 
19
- function getContexts() {
20
- const distPath = path__default["default"].dirname(require.resolve('tailwindcss/lib'));
20
+ function getTailwindcssEntry(basedir = process.cwd()) {
21
+ return patcher.requireResolve('tailwindcss', {
22
+ basedir
23
+ });
24
+ }
25
+ function getContexts(basedir) {
26
+ const twPath = getTailwindcssEntry(basedir);
27
+ const distPath = path__default["default"].dirname(twPath);
21
28
  let injectFilePath = path__default["default"].join(distPath, 'plugin.js');
22
29
  if (!fs__default["default"].existsSync(injectFilePath)) {
23
30
  injectFilePath = path__default["default"].join(distPath, 'index.js');
@@ -46,11 +53,14 @@ function getClassCacheSet() {
46
53
  }
47
54
 
48
55
  exports.createPatch = patcher.createPatch;
56
+ exports.ensureFileContent = patcher.ensureFileContent;
49
57
  exports.getInstalledPkgJsonPath = patcher.getInstalledPkgJsonPath;
50
58
  exports.inspectPostcssPlugin = patcher.inspectPostcssPlugin;
51
59
  exports.inspectProcessTailwindFeaturesReturnContext = patcher.inspectProcessTailwindFeaturesReturnContext;
52
60
  exports.internalPatch = patcher.internalPatch;
53
61
  exports.monkeyPatchForExposingContext = patcher.monkeyPatchForExposingContext;
62
+ exports.requireResolve = patcher.requireResolve;
54
63
  exports.getClassCacheSet = getClassCacheSet;
55
64
  exports.getClassCaches = getClassCaches;
56
65
  exports.getContexts = getContexts;
66
+ exports.getTailwindcssEntry = getTailwindcssEntry;
@@ -7,6 +7,7 @@ var t = require('@babel/types');
7
7
  var generate = require('@babel/generator');
8
8
  var parser = require('@babel/parser');
9
9
  var traverse = require('@babel/traverse');
10
+ var resolve = require('resolve');
10
11
 
11
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
13
 
@@ -34,6 +35,26 @@ var t__namespace = /*#__PURE__*/_interopNamespaceCompat(t);
34
35
  var generate__default = /*#__PURE__*/_interopDefaultCompat(generate);
35
36
  var traverse__default = /*#__PURE__*/_interopDefaultCompat(traverse);
36
37
 
38
+ function ensureFileContent(filepaths) {
39
+ if (typeof filepaths === 'string') {
40
+ filepaths = [filepaths];
41
+ }
42
+ let content;
43
+ for (let i = 0; i < filepaths.length; i++) {
44
+ const filepath = filepaths[i];
45
+ if (fs__default["default"].existsSync(filepath)) {
46
+ content = fs__default["default"].readFileSync(filepath, {
47
+ encoding: 'utf-8'
48
+ });
49
+ break;
50
+ }
51
+ }
52
+ return content;
53
+ }
54
+ function requireResolve(id, opts) {
55
+ return resolve.sync(id, opts);
56
+ }
57
+
37
58
  function inspectProcessTailwindFeaturesReturnContext(content) {
38
59
  const ast = parser.parse(content);
39
60
  let hasPatched = false;
@@ -204,27 +225,12 @@ const defaultOptions = {
204
225
  overwrite: true
205
226
  };
206
227
 
207
- function ensureFileContent(filepaths) {
208
- if (typeof filepaths === 'string') {
209
- filepaths = [filepaths];
210
- }
211
- let content;
212
- for (let i = 0; i < filepaths.length; i++) {
213
- const filepath = filepaths[i];
214
- if (fs__default["default"].existsSync(filepath)) {
215
- content = fs__default["default"].readFileSync(filepath, {
216
- encoding: 'utf-8'
217
- });
218
- break;
219
- }
220
- }
221
- return content;
222
- }
223
-
224
228
  function getInstalledPkgJsonPath(options) {
229
+ var _a;
225
230
  try {
226
- const tmpJsonPath = require.resolve(`tailwindcss/package.json`, {
227
- paths: options.paths
231
+ const tmpJsonPath = requireResolve(`tailwindcss/package.json`, {
232
+ paths: options.paths,
233
+ basedir: (_a = options.basedir) !== null && _a !== void 0 ? _a : process.cwd()
228
234
  });
229
235
  const pkgJson = require(tmpJsonPath);
230
236
  if (semver.gte(pkgJson.version, '3.0.0')) {
@@ -233,23 +239,24 @@ function getInstalledPkgJsonPath(options) {
233
239
  }
234
240
  catch (error) {
235
241
  if (error.code === 'MODULE_NOT_FOUND') {
236
- console.warn('没有找到`tailwindcss`包,请确认是否安装。');
242
+ console.warn('Can\'t find npm pkg: `tailwindcss`, Please ensure it has been installed!');
237
243
  }
238
244
  }
239
245
  }
240
- function createPatch(options) {
246
+ function createPatch(options = {}) {
241
247
  const opt = defu(options, defaultOptions);
242
248
  return () => {
243
249
  try {
244
- return internalPatch(getInstalledPkgJsonPath(options), opt);
250
+ const pkgJsonPath = getInstalledPkgJsonPath(options);
251
+ return internalPatch(pkgJsonPath, opt);
245
252
  }
246
253
  catch (error) {
247
254
  console.warn(`patch tailwindcss failed:` + error.message);
248
255
  }
249
256
  };
250
257
  }
251
- function monkeyPatchForExposingContext(rootDir, opt) {
252
- const processTailwindFeaturesFilePath = path__default["default"].resolve(rootDir, 'lib/processTailwindFeatures.js');
258
+ function monkeyPatchForExposingContext(twDir, opt) {
259
+ const processTailwindFeaturesFilePath = path__default["default"].resolve(twDir, 'lib/processTailwindFeatures.js');
253
260
  const processTailwindFeaturesContent = ensureFileContent(processTailwindFeaturesFilePath);
254
261
  const result = {};
255
262
  if (processTailwindFeaturesContent) {
@@ -262,8 +269,8 @@ function monkeyPatchForExposingContext(rootDir, opt) {
262
269
  }
263
270
  result.processTailwindFeatures = code;
264
271
  }
265
- const pluginFilePath = path__default["default"].resolve(rootDir, 'lib/plugin.js');
266
- const indexFilePath = path__default["default"].resolve(rootDir, 'lib/index.js');
272
+ const pluginFilePath = path__default["default"].resolve(twDir, 'lib/plugin.js');
273
+ const indexFilePath = path__default["default"].resolve(twDir, 'lib/index.js');
267
274
  const pluginContent = ensureFileContent([pluginFilePath, indexFilePath]);
268
275
  if (pluginContent) {
269
276
  const { code, hasPatched } = inspectPostcssPlugin(pluginContent);
@@ -275,19 +282,22 @@ function monkeyPatchForExposingContext(rootDir, opt) {
275
282
  }
276
283
  result.plugin = code;
277
284
  }
285
+ opt.custom && typeof opt.custom === 'function' && opt.custom(twDir, result);
278
286
  return result;
279
287
  }
280
288
  function internalPatch(pkgJsonPath, options) {
281
289
  if (pkgJsonPath) {
282
- const rootDir = path__default["default"].dirname(pkgJsonPath);
283
- const result = monkeyPatchForExposingContext(rootDir, options);
290
+ const twDir = path__default["default"].dirname(pkgJsonPath);
291
+ const result = monkeyPatchForExposingContext(twDir, options);
284
292
  return result;
285
293
  }
286
294
  }
287
295
 
288
296
  exports.createPatch = createPatch;
297
+ exports.ensureFileContent = ensureFileContent;
289
298
  exports.getInstalledPkgJsonPath = getInstalledPkgJsonPath;
290
299
  exports.inspectPostcssPlugin = inspectPostcssPlugin;
291
300
  exports.inspectProcessTailwindFeaturesReturnContext = inspectProcessTailwindFeaturesReturnContext;
292
301
  exports.internalPatch = internalPatch;
293
302
  exports.monkeyPatchForExposingContext = monkeyPatchForExposingContext;
303
+ exports.requireResolve = requireResolve;
@@ -1,5 +1,6 @@
1
1
  import type { Rule } from 'postcss';
2
- export declare function getContexts(): any[];
2
+ export declare function getTailwindcssEntry(basedir?: string): string;
3
+ export declare function getContexts(basedir?: string): any[];
3
4
  export declare function getClassCaches(): Map<string, ({
4
5
  layer: string;
5
6
  options: Record<string, any>;
@@ -1,3 +1,5 @@
1
1
  export * from './exposeContext';
2
2
  export * from './inspector';
3
3
  export * from './patcher';
4
+ export * from './utils';
5
+ export * from './type';
@@ -1,8 +1,8 @@
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;
4
- export declare function monkeyPatchForExposingContext(rootDir: string, opt: InternalPatchOptions): {
3
+ export declare function createPatch(options?: PatchOptions): () => any;
4
+ export declare function monkeyPatchForExposingContext(twDir: string, opt: InternalPatchOptions): {
5
5
  processTailwindFeatures?: string | undefined;
6
6
  plugin?: string | undefined;
7
- };
7
+ } & Record<string, any>;
8
8
  export declare function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions): any | undefined;
@@ -1,8 +1,12 @@
1
1
  export interface PatchOptions {
2
2
  overwrite?: boolean;
3
3
  paths?: string[];
4
+ basedir?: string;
5
+ custom?: (dir: string, ctx: Record<string, any>) => void;
4
6
  }
5
7
  export interface InternalPatchOptions {
6
8
  overwrite: boolean;
7
9
  paths?: string[];
10
+ basedir?: string;
11
+ custom?: (dir: string, ctx: Record<string, any>) => void;
8
12
  }
@@ -1 +1,3 @@
1
+ import { type SyncOpts } from 'resolve';
1
2
  export declare function ensureFileContent(filepaths: string | string[]): string | undefined;
3
+ export declare function requireResolve(id: string, opts?: SyncOpts): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwindcss-patch",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "patch tailwindcss for exposing context",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -25,6 +25,7 @@
25
25
  "devDependencies": {
26
26
  "@types/babel__generator": "^7.6.4",
27
27
  "@types/babel__traverse": "^7.18.3",
28
+ "@types/resolve": "^1.20.2",
28
29
  "@types/semver": "^7.3.13",
29
30
  "defu": "^6.1.2",
30
31
  "pkg-types": "^1.0.2",
@@ -36,6 +37,7 @@
36
37
  "@babel/parser": "^7.21.4",
37
38
  "@babel/traverse": "^7.21.4",
38
39
  "@babel/types": "^7.21.4",
40
+ "resolve": "^1.22.2",
39
41
  "semver": "^7.4.0"
40
42
  },
41
43
  "homepage": "https://github.com/sonofmagic/tailwindcss-mangle",