@1adybug/prettier-plugin-sort-imports 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.
- package/README.md +150 -37
- package/README.zh-CN.md +136 -29
- package/dist/index.d.ts +3 -3
- package/dist/index.js +81 -82
- package/dist/parser.d.ts +2 -1
- package/dist/sorter.d.ts +1 -1
- package/dist/types.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ A powerful Prettier plugin for intelligently grouping and sorting import stateme
|
|
|
19
19
|
### Installation
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
npm install prettier-plugin-
|
|
22
|
+
npm install @1adybug/prettier-plugin-sort-imports --save-dev
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### Basic Configuration
|
|
@@ -28,7 +28,7 @@ Add the plugin to your `prettier.config.mjs`:
|
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
30
|
export default {
|
|
31
|
-
plugins: ["prettier-plugin-
|
|
31
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
@@ -50,7 +50,7 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
50
50
|
|
|
51
51
|
```javascript
|
|
52
52
|
// prettier.config.mjs
|
|
53
|
-
import { createPlugin } from "prettier-plugin-
|
|
53
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
54
54
|
|
|
55
55
|
export default {
|
|
56
56
|
plugins: [
|
|
@@ -157,40 +157,46 @@ interface PluginConfig {
|
|
|
157
157
|
|
|
158
158
|
### Method 1: Simple Configuration
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
Use the default plugin with basic options:
|
|
161
161
|
|
|
162
162
|
```javascript
|
|
163
163
|
export default {
|
|
164
|
-
plugins: ["prettier-plugin-
|
|
164
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
165
165
|
importSortSideEffect: false, // Whether to sort side effect imports
|
|
166
166
|
importSortSeparator: "", // Group separator
|
|
167
167
|
importSortRemoveUnused: false, // Whether to remove unused imports
|
|
168
168
|
}
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
-
### Method 2: Advanced Configuration
|
|
171
|
+
### Method 2: Advanced Configuration
|
|
172
172
|
|
|
173
|
-
Use `createPlugin` function
|
|
173
|
+
Use `createPlugin` function for full control and plugin compatibility:
|
|
174
174
|
|
|
175
175
|
```javascript
|
|
176
|
-
import { createPlugin } from "prettier-plugin-
|
|
176
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
177
177
|
|
|
178
178
|
export default {
|
|
179
179
|
plugins: [
|
|
180
180
|
createPlugin({
|
|
181
|
+
// Custom sorting functions
|
|
181
182
|
getGroup: statement => {
|
|
182
|
-
|
|
183
|
+
if (statement.path.startsWith("react")) return "react"
|
|
184
|
+
if (!statement.path.startsWith(".")) return "external"
|
|
185
|
+
return "local"
|
|
183
186
|
},
|
|
184
187
|
sortGroup: (a, b) => {
|
|
185
|
-
|
|
188
|
+
const order = ["react", "external", "local"]
|
|
189
|
+
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
186
190
|
},
|
|
187
191
|
sortImportStatement: (a, b) => {
|
|
188
|
-
|
|
192
|
+
return a.path.localeCompare(b.path)
|
|
189
193
|
},
|
|
190
194
|
sortImportContent: (a, b) => {
|
|
191
|
-
|
|
195
|
+
return a.name.localeCompare(b.name)
|
|
192
196
|
},
|
|
193
|
-
|
|
197
|
+
|
|
198
|
+
// Configuration
|
|
199
|
+
separator: "\n",
|
|
194
200
|
sortSideEffect: true,
|
|
195
201
|
removeUnusedImports: false,
|
|
196
202
|
}),
|
|
@@ -198,42 +204,149 @@ export default {
|
|
|
198
204
|
}
|
|
199
205
|
```
|
|
200
206
|
|
|
201
|
-
### Method 3:
|
|
207
|
+
### Method 3: Custom Plugin Module
|
|
202
208
|
|
|
203
|
-
|
|
209
|
+
Create a custom plugin module for better organization and reusability:
|
|
204
210
|
|
|
205
|
-
|
|
206
|
-
// prettier.config.mjs
|
|
207
|
-
export default {
|
|
208
|
-
plugins: ["prettier-plugin-import-sorts"],
|
|
209
|
-
sortImportsConfigPath: "./import-sort.config.js",
|
|
210
|
-
}
|
|
211
|
-
```
|
|
211
|
+
**Step 1**: Create a custom plugin file `prettier-plugin-sort-imports.mjs`:
|
|
212
212
|
|
|
213
213
|
```javascript
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
214
|
+
// prettier-plugin-sort-imports.mjs
|
|
215
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
216
|
+
|
|
217
|
+
export default createPlugin({
|
|
218
|
+
// Custom grouping logic
|
|
219
|
+
getGroup: statement => {
|
|
220
|
+
const path = statement.path
|
|
221
|
+
|
|
222
|
+
// React and related libraries
|
|
223
|
+
if (path.startsWith("react") || path.startsWith("@react")) {
|
|
224
|
+
return "react"
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// UI libraries
|
|
228
|
+
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra")) {
|
|
229
|
+
return "ui"
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Utility libraries
|
|
233
|
+
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns")) {
|
|
234
|
+
return "utils"
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// External packages (node_modules)
|
|
238
|
+
if (!path.startsWith(".") && !path.startsWith("@/")) {
|
|
239
|
+
return "external"
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Internal aliases (@/)
|
|
243
|
+
if (path.startsWith("@/")) {
|
|
244
|
+
return "internal"
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Relative imports
|
|
248
|
+
return "relative"
|
|
222
249
|
},
|
|
250
|
+
|
|
251
|
+
// Define group order
|
|
223
252
|
sortGroup: (a, b) => {
|
|
224
|
-
const order = ["react", "external", "internal", "relative"]
|
|
253
|
+
const order = ["react", "external", "ui", "utils", "internal", "relative"]
|
|
225
254
|
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
226
255
|
},
|
|
256
|
+
|
|
257
|
+
// Custom import content sorting
|
|
258
|
+
sortImportContent: (a, b) => {
|
|
259
|
+
// Types first, then variables
|
|
260
|
+
if (a.type !== b.type) {
|
|
261
|
+
return a.type === "type" ? -1 : 1
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Alphabetical order within same type
|
|
265
|
+
const aName = a.alias ?? a.name
|
|
266
|
+
const bName = b.alias ?? b.name
|
|
267
|
+
return aName.localeCompare(bName)
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
// Add blank lines between groups
|
|
227
271
|
separator: "\n",
|
|
272
|
+
|
|
273
|
+
// Sort side effects
|
|
274
|
+
sortSideEffect: true,
|
|
275
|
+
})
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
**Step 2**: Use the custom plugin in your `prettier.config.mjs`:
|
|
279
|
+
|
|
280
|
+
```javascript
|
|
281
|
+
// prettier.config.mjs
|
|
282
|
+
export default {
|
|
283
|
+
plugins: ["./prettier-plugin-sort-imports.mjs"],
|
|
284
|
+
// Other prettier options...
|
|
285
|
+
semi: false,
|
|
286
|
+
tabWidth: 4,
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
**Benefits of this approach**:
|
|
291
|
+
- ✅ **Reusable**: Share the same configuration across multiple projects
|
|
292
|
+
- ✅ **Version Control**: Track your import sorting rules in git
|
|
293
|
+
- ✅ **Maintainable**: Keep complex logic separate from prettier config
|
|
294
|
+
- ✅ **Team Collaboration**: Consistent import sorting rules across team members
|
|
295
|
+
|
|
296
|
+
### Method 4: Plugin Compatibility
|
|
297
|
+
|
|
298
|
+
Use `createPlugin` with `otherPlugins` to merge with other Prettier plugins and avoid conflicts:
|
|
299
|
+
|
|
300
|
+
```javascript
|
|
301
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
302
|
+
import * as tailwindPlugin from "prettier-plugin-tailwindcss"
|
|
303
|
+
|
|
304
|
+
export default {
|
|
305
|
+
plugins: [
|
|
306
|
+
createPlugin({
|
|
307
|
+
// Your import sorting configuration
|
|
308
|
+
getGroup: statement => {
|
|
309
|
+
if (statement.path.startsWith("react")) return "react"
|
|
310
|
+
if (!statement.path.startsWith(".")) return "external"
|
|
311
|
+
return "local"
|
|
312
|
+
},
|
|
313
|
+
separator: "\n",
|
|
314
|
+
|
|
315
|
+
// Other Prettier plugins to combine with (Plugin objects only)
|
|
316
|
+
otherPlugins: [
|
|
317
|
+
tailwindPlugin, // Import the plugin directly
|
|
318
|
+
// Add more plugins as needed...
|
|
319
|
+
],
|
|
320
|
+
|
|
321
|
+
// Configuration options for other plugins
|
|
322
|
+
prettierOptions: {
|
|
323
|
+
// TailwindCSS plugin options
|
|
324
|
+
tailwindConfig: "./tailwind.config.js",
|
|
325
|
+
tailwindFunctions: ["clsx", "cn", "cva"],
|
|
326
|
+
tailwindAttributes: ["class", "className", "ngClass", ":class"],
|
|
327
|
+
|
|
328
|
+
// Other plugin options can go here...
|
|
329
|
+
},
|
|
330
|
+
}),
|
|
331
|
+
],
|
|
228
332
|
}
|
|
229
333
|
```
|
|
230
334
|
|
|
231
|
-
**Important Notes
|
|
335
|
+
**Important Notes:**
|
|
336
|
+
|
|
337
|
+
- `otherPlugins` only accepts imported Plugin objects, not string plugin names
|
|
338
|
+
- You must import the plugins yourself to ensure proper module resolution
|
|
339
|
+
- This approach avoids complex module loading issues and gives you full control
|
|
340
|
+
|
|
341
|
+
**Plugin Execution Order:**
|
|
342
|
+
|
|
343
|
+
- Other plugins are executed in the order they appear in the `otherPlugins` array
|
|
344
|
+
- Import sorting is always executed last to ensure compatibility
|
|
345
|
+
|
|
346
|
+
**Configuration Passing:**
|
|
232
347
|
|
|
233
|
-
-
|
|
234
|
-
-
|
|
235
|
-
- Config file path is resolved relative to the project root (`process.cwd()`)
|
|
236
|
-
- Configuration priority: `createPlugin` parameters > `sortImportsConfigPath` loaded config > Prettier config options
|
|
348
|
+
- Options in `prettierOptions` are passed to all other plugins
|
|
349
|
+
- This allows other plugins to receive their configuration even when merged
|
|
237
350
|
|
|
238
351
|
### importSortRemoveUnused
|
|
239
352
|
|
|
@@ -427,7 +540,7 @@ Prettier natively cannot accept functions as configuration parameters (because c
|
|
|
427
540
|
|
|
428
541
|
```javascript
|
|
429
542
|
// Factory function is called in config file, returning a plugin instance
|
|
430
|
-
import { createPlugin } from "prettier-plugin-
|
|
543
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
431
544
|
|
|
432
545
|
export default {
|
|
433
546
|
plugins: [
|
package/README.zh-CN.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
### 安装
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
npm install prettier-plugin-
|
|
22
|
+
npm install @1adybug/prettier-plugin-sort-imports --save-dev
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### 基础配置
|
|
@@ -28,7 +28,7 @@ npm install prettier-plugin-import-sorts --save-dev
|
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
30
|
export default {
|
|
31
|
-
plugins: ["prettier-plugin-
|
|
31
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
@@ -50,7 +50,7 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
50
50
|
|
|
51
51
|
```javascript
|
|
52
52
|
// prettier.config.mjs
|
|
53
|
-
import { createPlugin } from "prettier-plugin-
|
|
53
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
54
54
|
|
|
55
55
|
export default {
|
|
56
56
|
plugins: [
|
|
@@ -161,7 +161,7 @@ interface PluginConfig {
|
|
|
161
161
|
|
|
162
162
|
```javascript
|
|
163
163
|
export default {
|
|
164
|
-
plugins: ["prettier-plugin-
|
|
164
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
165
165
|
importSortSideEffect: false, // 是否对副作用导入排序
|
|
166
166
|
importSortSeparator: "", // 分组分隔符
|
|
167
167
|
importSortRemoveUnused: false, // 是否删除未使用的导入
|
|
@@ -173,7 +173,7 @@ export default {
|
|
|
173
173
|
使用 `createPlugin` 函数可以传递自定义函数:
|
|
174
174
|
|
|
175
175
|
```javascript
|
|
176
|
-
import { createPlugin } from "prettier-plugin-
|
|
176
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
177
177
|
|
|
178
178
|
export default {
|
|
179
179
|
plugins: [
|
|
@@ -198,42 +198,149 @@ export default {
|
|
|
198
198
|
}
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
-
### 方式 3
|
|
201
|
+
### 方式 3:自定义插件模块
|
|
202
202
|
|
|
203
|
-
|
|
203
|
+
创建自定义插件模块以获得更好的组织性和可复用性:
|
|
204
204
|
|
|
205
|
-
|
|
206
|
-
// prettier.config.mjs
|
|
207
|
-
export default {
|
|
208
|
-
plugins: ["prettier-plugin-import-sorts"],
|
|
209
|
-
sortImportsConfigPath: "./import-sort.config.js",
|
|
210
|
-
}
|
|
211
|
-
```
|
|
205
|
+
**步骤 1**:创建自定义插件文件 `prettier-plugin-sort-imports.mjs`:
|
|
212
206
|
|
|
213
207
|
```javascript
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
208
|
+
// prettier-plugin-sort-imports.mjs
|
|
209
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
210
|
+
|
|
211
|
+
export default createPlugin({
|
|
212
|
+
// 自定义分组逻辑
|
|
213
|
+
getGroup: statement => {
|
|
214
|
+
const path = statement.path
|
|
215
|
+
|
|
216
|
+
// React 及相关库
|
|
217
|
+
if (path.startsWith("react") || path.startsWith("@react")) {
|
|
218
|
+
return "react"
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// UI 库
|
|
222
|
+
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra")) {
|
|
223
|
+
return "ui"
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// 工具库
|
|
227
|
+
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns")) {
|
|
228
|
+
return "utils"
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 外部包 (node_modules)
|
|
232
|
+
if (!path.startsWith(".") && !path.startsWith("@/")) {
|
|
233
|
+
return "external"
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 内部别名 (@/)
|
|
237
|
+
if (path.startsWith("@/")) {
|
|
238
|
+
return "internal"
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 相对导入
|
|
242
|
+
return "relative"
|
|
222
243
|
},
|
|
244
|
+
|
|
245
|
+
// 定义分组顺序
|
|
223
246
|
sortGroup: (a, b) => {
|
|
224
|
-
const order = ["react", "external", "internal", "relative"]
|
|
247
|
+
const order = ["react", "external", "ui", "utils", "internal", "relative"]
|
|
225
248
|
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
226
249
|
},
|
|
250
|
+
|
|
251
|
+
// 自定义导入内容排序
|
|
252
|
+
sortImportContent: (a, b) => {
|
|
253
|
+
// 类型在前,变量在后
|
|
254
|
+
if (a.type !== b.type) {
|
|
255
|
+
return a.type === "type" ? -1 : 1
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 同类型内按字母顺序
|
|
259
|
+
const aName = a.alias ?? a.name
|
|
260
|
+
const bName = b.alias ?? b.name
|
|
261
|
+
return aName.localeCompare(bName)
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
// 在分组间添加空行
|
|
227
265
|
separator: "\n",
|
|
266
|
+
|
|
267
|
+
// 排序副作用导入
|
|
268
|
+
sortSideEffect: true,
|
|
269
|
+
})
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**步骤 2**:在 `prettier.config.mjs` 中使用自定义插件:
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
// prettier.config.mjs
|
|
276
|
+
export default {
|
|
277
|
+
plugins: ["./prettier-plugin-sort-imports.mjs"],
|
|
278
|
+
// 其他 prettier 选项...
|
|
279
|
+
semi: false,
|
|
280
|
+
tabWidth: 4,
|
|
228
281
|
}
|
|
229
282
|
```
|
|
230
283
|
|
|
231
|
-
|
|
284
|
+
**此方法的优点**:
|
|
285
|
+
- ✅ **可复用**:在多个项目间共享相同配置
|
|
286
|
+
- ✅ **版本控制**:在 git 中跟踪你的导入排序规则
|
|
287
|
+
- ✅ **易维护**:将复杂逻辑从 prettier 配置中分离
|
|
288
|
+
- ✅ **团队协作**:团队成员间保持一致的导入排序规则
|
|
289
|
+
|
|
290
|
+
### 方式 4:插件兼容性
|
|
291
|
+
|
|
292
|
+
使用 `createPlugin` 的 `otherPlugins` 参数与其他 Prettier 插件合并,避免冲突:
|
|
293
|
+
|
|
294
|
+
```javascript
|
|
295
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
296
|
+
import * as tailwindPlugin from "prettier-plugin-tailwindcss"
|
|
297
|
+
|
|
298
|
+
export default {
|
|
299
|
+
plugins: [
|
|
300
|
+
createPlugin({
|
|
301
|
+
// 你的导入排序配置
|
|
302
|
+
getGroup: statement => {
|
|
303
|
+
if (statement.path.startsWith("react")) return "react"
|
|
304
|
+
if (!statement.path.startsWith(".")) return "external"
|
|
305
|
+
return "local"
|
|
306
|
+
},
|
|
307
|
+
separator: "\n",
|
|
308
|
+
|
|
309
|
+
// 要合并的其他 Prettier 插件(仅支持 Plugin 对象)
|
|
310
|
+
otherPlugins: [
|
|
311
|
+
tailwindPlugin, // 直接导入插件
|
|
312
|
+
// 根据需要添加更多插件...
|
|
313
|
+
],
|
|
314
|
+
|
|
315
|
+
// 其他插件的配置选项
|
|
316
|
+
prettierOptions: {
|
|
317
|
+
// TailwindCSS 插件选项
|
|
318
|
+
tailwindConfig: "./tailwind.config.js",
|
|
319
|
+
tailwindFunctions: ["clsx", "cn", "cva"],
|
|
320
|
+
tailwindAttributes: ["class", "className", "ngClass", ":class"],
|
|
321
|
+
|
|
322
|
+
// 其他插件选项可以在这里配置...
|
|
323
|
+
},
|
|
324
|
+
}),
|
|
325
|
+
],
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
**重要说明**:
|
|
330
|
+
|
|
331
|
+
- `otherPlugins` 只接受导入的 Plugin 对象,不支持字符串插件名称
|
|
332
|
+
- 你必须自己导入插件以确保正确的模块解析
|
|
333
|
+
- 这种方法避免了复杂的模块加载问题,给你完全的控制权
|
|
334
|
+
|
|
335
|
+
**插件执行顺序**:
|
|
336
|
+
|
|
337
|
+
- 其他插件按照在 `otherPlugins` 数组中出现的顺序执行
|
|
338
|
+
- 导入排序始终最后执行以确保兼容性
|
|
339
|
+
|
|
340
|
+
**配置传递**:
|
|
232
341
|
|
|
233
|
-
-
|
|
234
|
-
-
|
|
235
|
-
- 配置文件路径相对于项目根目录(`process.cwd()`)解析
|
|
236
|
-
- 配置优先级:`createPlugin` 参数 > `sortImportsConfigPath` 加载的配置 > Prettier 配置选项
|
|
342
|
+
- `prettierOptions` 中的选项会传递给所有其他插件
|
|
343
|
+
- 这允许其他插件即使在合并时也能接收到它们的配置
|
|
237
344
|
|
|
238
345
|
### importSortRemoveUnused
|
|
239
346
|
|
|
@@ -427,7 +534,7 @@ Prettier 原生无法接受函数作为配置参数(因为配置需要序列
|
|
|
427
534
|
|
|
428
535
|
```javascript
|
|
429
536
|
// 工厂函数在配置文件中被调用,返回一个插件实例
|
|
430
|
-
import { createPlugin } from "prettier-plugin-
|
|
537
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
431
538
|
|
|
432
539
|
export default {
|
|
433
540
|
plugins: [
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Plugin } from "prettier";
|
|
2
|
-
import { PluginConfig } from "./types";
|
|
2
|
+
import type { PluginConfig } from "./types";
|
|
3
3
|
export * from "./types";
|
|
4
|
-
/** 默认插件实例(用于简单使用) */
|
|
5
|
-
declare const plugin: Plugin;
|
|
6
4
|
/** 创建自定义配置的插件(工厂函数) */
|
|
7
5
|
export declare function createPlugin(config?: PluginConfig): Plugin;
|
|
6
|
+
/** 默认插件实例(用于简单使用) */
|
|
7
|
+
declare const plugin: Plugin;
|
|
8
8
|
export default plugin;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
|
-
import { resolve } from "path";
|
|
3
2
|
import { parse } from "@babel/parser";
|
|
4
3
|
import traverse from "@babel/traverse";
|
|
5
4
|
function analyzeUsedIdentifiers(code) {
|
|
@@ -236,7 +235,8 @@ function parseImportNode(node, comments, usedComments) {
|
|
|
236
235
|
start,
|
|
237
236
|
end
|
|
238
237
|
};
|
|
239
|
-
const
|
|
238
|
+
const isTypeOnlyExport = "type" === node.exportKind;
|
|
239
|
+
const importContents = parseExportSpecifiers(node, isTypeOnlyExport);
|
|
240
240
|
return {
|
|
241
241
|
path: source,
|
|
242
242
|
isExport: true,
|
|
@@ -290,7 +290,7 @@ function parseImportSpecifiers(node, isTypeOnlyImport = false) {
|
|
|
290
290
|
}
|
|
291
291
|
return contents;
|
|
292
292
|
}
|
|
293
|
-
function parseExportSpecifiers(node) {
|
|
293
|
+
function parseExportSpecifiers(node, isTypeOnlyExport = false) {
|
|
294
294
|
const contents = [];
|
|
295
295
|
if (!node.specifiers) return contents;
|
|
296
296
|
for (const specifier of node.specifiers)if ("ExportSpecifier" === specifier.type) {
|
|
@@ -306,7 +306,7 @@ function parseExportSpecifiers(node) {
|
|
|
306
306
|
}
|
|
307
307
|
const localName = "Identifier" === specifier.local.type ? specifier.local.name : specifier.local.value;
|
|
308
308
|
const exportedName = "Identifier" === specifier.exported.type ? specifier.exported.name : specifier.exported.value;
|
|
309
|
-
const isTypeExport = "type" === specifier.exportKind;
|
|
309
|
+
const isTypeExport = isTypeOnlyExport || "type" === specifier.exportKind;
|
|
310
310
|
contents.push({
|
|
311
311
|
name: localName,
|
|
312
312
|
alias: localName !== exportedName ? exportedName : void 0,
|
|
@@ -526,58 +526,33 @@ function mergeImports(imports) {
|
|
|
526
526
|
return Array.from(mergedMap.values());
|
|
527
527
|
}
|
|
528
528
|
const src_require = createRequire(import.meta.url);
|
|
529
|
-
|
|
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
|
-
}
|
|
552
|
-
function preprocessImports(text, options) {
|
|
529
|
+
function preprocessImports(text, options, config = {}) {
|
|
553
530
|
try {
|
|
554
531
|
const imports = parseImports(text);
|
|
555
532
|
if (0 === imports.length) return text;
|
|
556
|
-
const
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
sortSideEffect: src_userConfig.sortSideEffect ?? fileConfig.sortSideEffect ?? options.importSortSideEffect ?? false,
|
|
566
|
-
removeUnusedImports: src_userConfig.removeUnusedImports ?? fileConfig.removeUnusedImports ?? options.importSortRemoveUnused ?? false
|
|
533
|
+
const optionsConfig = options;
|
|
534
|
+
const finalConfig = {
|
|
535
|
+
getGroup: config.getGroup ?? optionsConfig.getGroup,
|
|
536
|
+
sortGroup: config.sortGroup ?? optionsConfig.sortGroup,
|
|
537
|
+
sortImportStatement: config.sortImportStatement ?? optionsConfig.sortImportStatement,
|
|
538
|
+
sortImportContent: config.sortImportContent ?? optionsConfig.sortImportContent,
|
|
539
|
+
separator: config.separator ?? optionsConfig.importSortSeparator ?? optionsConfig.separator,
|
|
540
|
+
sortSideEffect: config.sortSideEffect ?? optionsConfig.importSortSideEffect ?? false,
|
|
541
|
+
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.importSortRemoveUnused ?? false
|
|
567
542
|
};
|
|
568
543
|
let processedImports = imports;
|
|
569
|
-
if (
|
|
544
|
+
if (finalConfig.removeUnusedImports) {
|
|
570
545
|
const lastImport = imports[imports.length - 1];
|
|
571
546
|
const codeAfterImports = text.slice(lastImport.end ?? 0);
|
|
572
547
|
processedImports = removeUnusedImportsFromStatements(imports, codeAfterImports);
|
|
573
548
|
}
|
|
574
|
-
const sortedImports = sortImports(processedImports,
|
|
549
|
+
const sortedImports = sortImports(processedImports, finalConfig);
|
|
575
550
|
const mergedImports = mergeImports(sortedImports);
|
|
576
551
|
let formattedImports;
|
|
577
|
-
if (
|
|
578
|
-
const groups = groupImports(mergedImports,
|
|
579
|
-
const sortedGroups = sortGroups(groups,
|
|
580
|
-
formattedImports = formatGroups(sortedGroups,
|
|
552
|
+
if (finalConfig.getGroup) {
|
|
553
|
+
const groups = groupImports(mergedImports, finalConfig);
|
|
554
|
+
const sortedGroups = sortGroups(groups, finalConfig);
|
|
555
|
+
formattedImports = formatGroups(sortedGroups, finalConfig);
|
|
581
556
|
} else formattedImports = formatImportStatements(mergedImports);
|
|
582
557
|
const firstImport = imports[0];
|
|
583
558
|
const lastImport = imports[imports.length - 1];
|
|
@@ -593,55 +568,79 @@ function preprocessImports(text, options) {
|
|
|
593
568
|
return text;
|
|
594
569
|
}
|
|
595
570
|
}
|
|
596
|
-
const
|
|
597
|
-
const
|
|
598
|
-
const
|
|
599
|
-
function
|
|
571
|
+
const { parsers: { babel } } = src_require("prettier/parser-babel");
|
|
572
|
+
const { parsers: { typescript } } = src_require("prettier/parser-typescript");
|
|
573
|
+
const { parsers: { "babel-ts": babelTs } } = src_require("prettier/parser-babel");
|
|
574
|
+
function createCombinedPreprocess(parserName, config) {
|
|
575
|
+
return function(text, options) {
|
|
576
|
+
const otherPlugins = config.otherPlugins || [];
|
|
577
|
+
if (0 === otherPlugins.length) return preprocessImports(text, options, config);
|
|
578
|
+
const prettierOptions = config.prettierOptions || {};
|
|
579
|
+
const mergedOptions = {
|
|
580
|
+
...options,
|
|
581
|
+
...prettierOptions
|
|
582
|
+
};
|
|
583
|
+
const preprocessFunctions = [];
|
|
584
|
+
for (const plugin of otherPlugins){
|
|
585
|
+
const parser = plugin?.parsers?.[parserName];
|
|
586
|
+
if (parser?.preprocess && "function" == typeof parser.preprocess) preprocessFunctions.push(parser.preprocess);
|
|
587
|
+
}
|
|
588
|
+
preprocessFunctions.push((text, options)=>preprocessImports(text, options, config));
|
|
589
|
+
let processedText = text;
|
|
590
|
+
for (const preprocess of preprocessFunctions)try {
|
|
591
|
+
processedText = preprocess(processedText, mergedOptions);
|
|
592
|
+
} catch (error) {
|
|
593
|
+
console.warn("Plugin preprocess failed:", error instanceof Error ? error.message : String(error));
|
|
594
|
+
}
|
|
595
|
+
return processedText;
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
function createPluginInstance(config = {}) {
|
|
599
|
+
const baseOptions = {
|
|
600
|
+
importSortSeparator: {
|
|
601
|
+
type: "string",
|
|
602
|
+
category: "Import Sort",
|
|
603
|
+
description: "分组之间的分隔符"
|
|
604
|
+
},
|
|
605
|
+
importSortSideEffect: {
|
|
606
|
+
type: "boolean",
|
|
607
|
+
category: "Import Sort",
|
|
608
|
+
description: "是否对副作用导入进行排序",
|
|
609
|
+
default: false
|
|
610
|
+
},
|
|
611
|
+
importSortRemoveUnused: {
|
|
612
|
+
type: "boolean",
|
|
613
|
+
category: "Import Sort",
|
|
614
|
+
description: "是否删除未使用的导入",
|
|
615
|
+
default: false
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
const otherPlugins = config.otherPlugins || [];
|
|
619
|
+
const mergedOptions = {
|
|
620
|
+
...baseOptions
|
|
621
|
+
};
|
|
622
|
+
for (const plugin of otherPlugins)if (plugin?.options) Object.assign(mergedOptions, plugin.options);
|
|
600
623
|
return {
|
|
601
624
|
parsers: {
|
|
602
625
|
babel: {
|
|
603
|
-
...
|
|
604
|
-
preprocess:
|
|
626
|
+
...babel,
|
|
627
|
+
preprocess: createCombinedPreprocess("babel", config)
|
|
605
628
|
},
|
|
606
629
|
typescript: {
|
|
607
|
-
...
|
|
608
|
-
preprocess:
|
|
630
|
+
...typescript,
|
|
631
|
+
preprocess: createCombinedPreprocess("typescript", config)
|
|
609
632
|
},
|
|
610
633
|
"babel-ts": {
|
|
611
|
-
...
|
|
612
|
-
preprocess:
|
|
634
|
+
...babelTs,
|
|
635
|
+
preprocess: createCombinedPreprocess("babel-ts", config)
|
|
613
636
|
}
|
|
614
637
|
},
|
|
615
|
-
options:
|
|
616
|
-
sortImportsConfigPath: {
|
|
617
|
-
type: "string",
|
|
618
|
-
category: "Import Sort",
|
|
619
|
-
description: "配置文件路径,用于加载自定义排序配置"
|
|
620
|
-
},
|
|
621
|
-
importSortSeparator: {
|
|
622
|
-
type: "string",
|
|
623
|
-
category: "Import Sort",
|
|
624
|
-
description: "分组之间的分隔符"
|
|
625
|
-
},
|
|
626
|
-
importSortSideEffect: {
|
|
627
|
-
type: "boolean",
|
|
628
|
-
category: "Import Sort",
|
|
629
|
-
description: "是否对副作用导入进行排序",
|
|
630
|
-
default: false
|
|
631
|
-
},
|
|
632
|
-
importSortRemoveUnused: {
|
|
633
|
-
type: "boolean",
|
|
634
|
-
category: "Import Sort",
|
|
635
|
-
description: "是否删除未使用的导入",
|
|
636
|
-
default: false
|
|
637
|
-
}
|
|
638
|
-
}
|
|
638
|
+
options: mergedOptions
|
|
639
639
|
};
|
|
640
640
|
}
|
|
641
|
-
const src_plugin = createPluginInstance();
|
|
642
641
|
function createPlugin(config = {}) {
|
|
643
|
-
|
|
644
|
-
return createPluginInstance();
|
|
642
|
+
return createPluginInstance(config);
|
|
645
643
|
}
|
|
644
|
+
const src_plugin = createPluginInstance();
|
|
646
645
|
const src = src_plugin;
|
|
647
646
|
export { createPlugin, src as default };
|
package/dist/parser.d.ts
CHANGED
package/dist/sorter.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Group, ImportContent, ImportStatement, PluginConfig } from "./types"; /** 导入类型 */
|
|
2
2
|
/** 合并后的配置 */
|
|
3
|
-
export interface MergedConfig extends Omit<Required<PluginConfig>, "separator" | "removeUnusedImports"> {
|
|
3
|
+
export interface MergedConfig extends Omit<Required<PluginConfig>, "separator" | "removeUnusedImports" | "otherPlugins" | "prettierOptions"> {
|
|
4
4
|
separator: PluginConfig["separator"];
|
|
5
5
|
removeUnusedImports: boolean;
|
|
6
6
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Plugin } from "prettier";
|
|
1
2
|
/** 导入内容 */
|
|
2
3
|
export interface ImportContent {
|
|
3
4
|
/** 导入的内容的名称 */
|
|
@@ -67,4 +68,8 @@ export interface PluginConfig {
|
|
|
67
68
|
sortSideEffect?: boolean;
|
|
68
69
|
/** 是否删除未使用的导入,默认为 false */
|
|
69
70
|
removeUnusedImports?: boolean;
|
|
71
|
+
/** 要合并的其他 Prettier 插件,按传入顺序执行 */
|
|
72
|
+
otherPlugins?: Plugin[];
|
|
73
|
+
/** 传递给其他插件的 Prettier 配置选项 */
|
|
74
|
+
prettierOptions?: Record<string, any>;
|
|
70
75
|
}
|