@1adybug/prettier 0.0.5 → 0.0.6

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.
Files changed (2) hide show
  1. package/dist/index.js +50 -14
  2. package/package.json +5 -4
package/dist/index.js CHANGED
@@ -1,10 +1,42 @@
1
- import { readFileSync } from "fs";
1
+ import { existsSync, readFileSync, statSync } from "fs";
2
2
  import { builtinModules } from "module";
3
+ import { join, parse, resolve } from "path";
3
4
  import prettier_plugin_block_padding from "@1adybug/prettier-plugin-block-padding";
4
5
  import prettier_plugin_remove_braces from "@1adybug/prettier-plugin-remove-braces";
5
6
  import { createPlugin } from "@1adybug/prettier-plugin-sort-imports";
6
7
  import json5 from "json5";
8
+ import { createMatchPath, loadConfig } from "tsconfig-paths";
7
9
  import * as __rspack_external_prettier_plugin_tailwindcss_a3404f7e from "prettier-plugin-tailwindcss";
10
+ const extensions = [
11
+ ".ts",
12
+ ".tsx",
13
+ ".js",
14
+ ".jsx",
15
+ ".mjs",
16
+ ".cjs"
17
+ ];
18
+ function getResolveAlias(filepath) {
19
+ try {
20
+ filepath = resolve(filepath);
21
+ let tsconfigPath;
22
+ while(true){
23
+ const { root, dir } = parse(filepath);
24
+ tsconfigPath = join(dir, "tsconfig.json");
25
+ if (existsSync(tsconfigPath)) break;
26
+ if (dir === root) break;
27
+ filepath = dir;
28
+ }
29
+ if (!tsconfigPath) return;
30
+ const tsconfig = loadConfig(tsconfigPath);
31
+ if ("failed" === tsconfig.resultType) return;
32
+ const matchPath = createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
33
+ return function(importPath) {
34
+ return matchPath(importPath, void 0, void 0, extensions);
35
+ };
36
+ } catch (error) {
37
+ return;
38
+ }
39
+ }
8
40
  const packageJson = json5.parse(readFileSync("package.json", "utf-8"));
9
41
  function hasDependency(dependency) {
10
42
  const dependencies = packageJson.dependencies ?? {};
@@ -17,20 +49,14 @@ function hasDependency(dependency) {
17
49
  });
18
50
  return total.some((item)=>"string" == typeof dependency ? item === dependency : dependency.test(item));
19
51
  }
20
- const hasReact = hasDependency("react");
21
52
  function isReact(path) {
22
- return hasReact && /^@?react\b/.test(path);
53
+ return /^(npm:)?react(-dom|-native)?(\/|$)/.test(path);
23
54
  }
24
55
  function isBuiltin(path) {
25
56
  return path.startsWith("node:") || builtinModules.includes(path);
26
57
  }
27
- let pathAlias = [];
28
- try {
29
- const tsConfig = json5.parse(readFileSync("tsconfig.json", "utf-8"));
30
- pathAlias = Object.keys(tsConfig.compilerOptions?.paths ?? {}).map((item)=>item.match(/^((@|~).*\/)\*/)).filter(Boolean).map((item)=>item[1]);
31
- } catch {}
32
58
  function isAbsolute(path) {
33
- return pathAlias.some((item)=>path.startsWith(item));
59
+ return !!src_resolveAlias?.(path);
34
60
  }
35
61
  function isRelative(path) {
36
62
  return path.startsWith("./") || path.startsWith("../");
@@ -47,9 +73,6 @@ function compareGroupName(a, b) {
47
73
  const bInfo = JSON.parse(b);
48
74
  return orders.indexOf(aInfo.type) - orders.indexOf(bInfo.type) || aInfo.dir.localeCompare(bInfo.dir);
49
75
  }
50
- function getDir(path) {
51
- return path.match(/^(((@|~)\/?|\.{1,2}\/)([^./]+))\//)?.[1] ?? "";
52
- }
53
76
  function getModuleType(path) {
54
77
  if (isReact(path)) return "react";
55
78
  if (isBuiltin(path)) return "builtin";
@@ -66,12 +89,25 @@ const otherPlugins = hasTailwindcss ? [
66
89
  prettier_plugin_block_padding,
67
90
  prettier_plugin_remove_braces
68
91
  ];
92
+ let src_resolveAlias;
93
+ function getResolvedPathDir(resolvedPath) {
94
+ if (existsSync(resolvedPath) && statSync(resolvedPath).isFile()) return parse(resolvedPath).dir;
95
+ for (const extension of extensions){
96
+ const importPath = resolvedPath + extension;
97
+ if (existsSync(importPath)) return parse(importPath).dir;
98
+ }
99
+ return resolvedPath;
100
+ }
69
101
  const src_plugin = createPlugin({
70
- getGroup ({ path }) {
102
+ getGroup ({ path, filepath }) {
103
+ if (filepath) src_resolveAlias ??= getResolveAlias(filepath);
71
104
  const type = getModuleType(path);
105
+ let dir = "";
106
+ if ("absolute" === type) dir = getResolvedPathDir(src_resolveAlias(path));
107
+ if ("relative" === type && !!filepath) dir = getResolvedPathDir(resolve(filepath, path));
72
108
  const info = {
73
109
  type,
74
- dir: "absolute" === type || "relative" === type ? getDir(path) : ""
110
+ dir
75
111
  };
76
112
  return JSON.stringify(info);
77
113
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1adybug/prettier",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "推荐的 Prettier 配置",
5
5
  "keywords": [
6
6
  "prettier",
@@ -36,9 +36,10 @@
36
36
  "dependencies": {
37
37
  "json5": "^2.2.3",
38
38
  "prettier-plugin-tailwindcss": "^0.7.1",
39
- "@1adybug/prettier-plugin-remove-braces": "0.0.4",
40
- "@1adybug/prettier-plugin-block-padding": "0.0.11",
41
- "@1adybug/prettier-plugin-sort-imports": "0.0.18"
39
+ "tsconfig-paths": "^4.2.0",
40
+ "@1adybug/prettier-plugin-sort-imports": "0.0.19",
41
+ "@1adybug/prettier-plugin-remove-braces": "0.0.5",
42
+ "@1adybug/prettier-plugin-block-padding": "0.0.11"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@rslib/core": "^0.18.2",