@1adybug/prettier-plugin-sort-imports 0.0.4 → 0.0.5

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
@@ -198,6 +198,43 @@ export default {
198
198
  }
199
199
  ```
200
200
 
201
+ ### Method 3: Config File Path
202
+
203
+ Use `sortImportsConfigPath` option to load configuration from an external file:
204
+
205
+ ```javascript
206
+ // prettier.config.mjs
207
+ export default {
208
+ plugins: ["prettier-plugin-import-sorts"],
209
+ sortImportsConfigPath: "./import-sort.config.js",
210
+ }
211
+ ```
212
+
213
+ ```javascript
214
+ // import-sort.config.js (or import-sort.config.cjs)
215
+ module.exports = {
216
+ getGroup: importStatement => {
217
+ const path = importStatement.path
218
+ if (path.startsWith("react")) return "react"
219
+ if (path.startsWith("@/")) return "internal"
220
+ if (path.startsWith(".")) return "relative"
221
+ return "external"
222
+ },
223
+ sortGroup: (a, b) => {
224
+ const order = ["react", "external", "internal", "relative"]
225
+ return order.indexOf(a.name) - order.indexOf(b.name)
226
+ },
227
+ separator: "\n",
228
+ }
229
+ ```
230
+
231
+ **Important Notes**:
232
+
233
+ - Config file must use **CommonJS format** (`module.exports`), ESM format (`export default`) is not supported
234
+ - If your project has `"type": "module"` in `package.json`, use `.cjs` extension (e.g., `import-sort.config.cjs`)
235
+ - Config file path is resolved relative to the project root (`process.cwd()`)
236
+ - Configuration priority: `createPlugin` parameters > `sortImportsConfigPath` loaded config > Prettier config options
237
+
201
238
  ### importSortRemoveUnused
202
239
 
203
240
  Whether to remove unused imports, defaults to `false`.
package/README.zh-CN.md CHANGED
@@ -198,6 +198,43 @@ export default {
198
198
  }
199
199
  ```
200
200
 
201
+ ### 方式 3:配置文件路径
202
+
203
+ 使用 `sortImportsConfigPath` 选项从外部文件加载配置:
204
+
205
+ ```javascript
206
+ // prettier.config.mjs
207
+ export default {
208
+ plugins: ["prettier-plugin-import-sorts"],
209
+ sortImportsConfigPath: "./import-sort.config.js",
210
+ }
211
+ ```
212
+
213
+ ```javascript
214
+ // import-sort.config.js (或 import-sort.config.cjs)
215
+ module.exports = {
216
+ getGroup: importStatement => {
217
+ const path = importStatement.path
218
+ if (path.startsWith("react")) return "react"
219
+ if (path.startsWith("@/")) return "internal"
220
+ if (path.startsWith(".")) return "relative"
221
+ return "external"
222
+ },
223
+ sortGroup: (a, b) => {
224
+ const order = ["react", "external", "internal", "relative"]
225
+ return order.indexOf(a.name) - order.indexOf(b.name)
226
+ },
227
+ separator: "\n",
228
+ }
229
+ ```
230
+
231
+ **重要提示**:
232
+
233
+ - 配置文件必须使用 **CommonJS 格式**(`module.exports`),不支持 ESM 格式(`export default`)
234
+ - 如果你的项目在 `package.json` 中设置了 `"type": "module"`,请使用 `.cjs` 扩展名(如 `import-sort.config.cjs`)
235
+ - 配置文件路径相对于项目根目录(`process.cwd()`)解析
236
+ - 配置优先级:`createPlugin` 参数 > `sortImportsConfigPath` 加载的配置 > Prettier 配置选项
237
+
201
238
  ### importSortRemoveUnused
202
239
 
203
240
  是否删除未使用的导入,默认为 `false`。
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Plugin } from "prettier";
2
2
  import { PluginConfig } from "./types";
3
+ export * from "./types";
3
4
  /** 默认插件实例(用于简单使用) */
4
5
  declare const plugin: Plugin;
5
6
  /** 创建自定义配置的插件(工厂函数) */
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createRequire } from "module";
2
+ import { resolve } from "path";
2
3
  import { parse } from "@babel/parser";
3
4
  import traverse from "@babel/traverse";
4
5
  function analyzeUsedIdentifiers(code) {
@@ -526,18 +527,43 @@ function mergeImports(imports) {
526
527
  }
527
528
  const src_require = createRequire(import.meta.url);
528
529
  let src_userConfig = {};
530
+ const configCache = new Map();
531
+ function loadConfigFromPath(configPath) {
532
+ if (configCache.has(configPath)) return configCache.get(configPath);
533
+ try {
534
+ const absolutePath = resolve(process.cwd(), configPath);
535
+ let config = {};
536
+ try {
537
+ delete src_require.cache[absolutePath];
538
+ const module = src_require(absolutePath);
539
+ config = module.default || module || {};
540
+ } catch (requireError) {
541
+ throw new Error(`Failed to load config file: ${configPath}. Please ensure the config file uses CommonJS format (module.exports) or has a .cjs extension. ESM format (.mjs or "type": "module") is not supported in synchronous loading context.\nOriginal error: ${requireError}`);
542
+ }
543
+ configCache.set(configPath, config);
544
+ return config;
545
+ } catch (error) {
546
+ console.error(`Failed to load config from ${configPath}:`, error);
547
+ const emptyConfig = {};
548
+ configCache.set(configPath, emptyConfig);
549
+ return emptyConfig;
550
+ }
551
+ }
529
552
  function preprocessImports(text, options) {
530
553
  try {
531
554
  const imports = parseImports(text);
532
555
  if (0 === imports.length) return text;
556
+ const configPath = options.sortImportsConfigPath;
557
+ let fileConfig = {};
558
+ if (configPath && "string" == typeof configPath) fileConfig = loadConfigFromPath(configPath);
533
559
  const config = {
534
- getGroup: src_userConfig.getGroup ?? options.getGroup,
535
- sortGroup: src_userConfig.sortGroup ?? options.sortGroup,
536
- sortImportStatement: src_userConfig.sortImportStatement ?? options.sortImportStatement,
537
- sortImportContent: src_userConfig.sortImportContent ?? options.sortImportContent,
538
- separator: src_userConfig.separator ?? options.importSortSeparator ?? options.separator,
539
- sortSideEffect: src_userConfig.sortSideEffect ?? options.importSortSideEffect ?? false,
540
- removeUnusedImports: src_userConfig.removeUnusedImports ?? options.importSortRemoveUnused ?? false
560
+ getGroup: src_userConfig.getGroup ?? fileConfig.getGroup ?? options.getGroup,
561
+ sortGroup: src_userConfig.sortGroup ?? fileConfig.sortGroup ?? options.sortGroup,
562
+ sortImportStatement: src_userConfig.sortImportStatement ?? fileConfig.sortImportStatement ?? options.sortImportStatement,
563
+ sortImportContent: src_userConfig.sortImportContent ?? fileConfig.sortImportContent ?? options.sortImportContent,
564
+ separator: src_userConfig.separator ?? fileConfig.separator ?? options.importSortSeparator ?? options.separator,
565
+ sortSideEffect: src_userConfig.sortSideEffect ?? fileConfig.sortSideEffect ?? options.importSortSideEffect ?? false,
566
+ removeUnusedImports: src_userConfig.removeUnusedImports ?? fileConfig.removeUnusedImports ?? options.importSortRemoveUnused ?? false
541
567
  };
542
568
  let processedImports = imports;
543
569
  if (config.removeUnusedImports) {
@@ -587,6 +613,11 @@ function createPluginInstance() {
587
613
  }
588
614
  },
589
615
  options: {
616
+ sortImportsConfigPath: {
617
+ type: "string",
618
+ category: "Import Sort",
619
+ description: "配置文件路径,用于加载自定义排序配置"
620
+ },
590
621
  importSortSeparator: {
591
622
  type: "string",
592
623
  category: "Import Sort",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@1adybug/prettier-plugin-sort-imports",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
6
6
  "keywords": [
7
7
  "prettier",