prettier-plugin-sort 0.2.0 → 1.0.0
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 +97 -259
- package/README.zh.md +91 -258
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1742 -636
- package/dist/options.d.ts +13 -16
- package/dist/parser-ast.d.ts +78 -0
- package/dist/sort-exports.d.ts +6 -9
- package/dist/sort-imports.d.ts +8 -2
- package/dist/sort-package.d.ts +6 -2
- package/dist/sort-typescript.d.ts +6 -0
- package/dist/utils/package-rules.d.ts +12 -0
- package/dist/utils/source-text.d.ts +9 -0
- package/package.json +12 -8
- package/dist/order-package.d.ts +0 -10
- package/dist/utils.d.ts +0 -8
package/README.zh.md
CHANGED
|
@@ -2,20 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
一个专注于排序的 [Prettier](https://prettier.io/) 插件。
|
|
4
4
|
|
|
5
|
-
- 排序
|
|
6
|
-
- 排序 `
|
|
7
|
-
- 排序 package.json 的顶层字段、字符串数组和依赖表
|
|
8
|
-
- 零运行时依赖
|
|
5
|
+
- 排序 JavaScript、TypeScript 和 Flow 中的顶层 `import` 和 `export { ... }`
|
|
6
|
+
- 排序 `package.json` 字段
|
|
9
7
|
|
|
10
8
|
使用其他语言阅读:[English](./README.md) | 中文
|
|
11
9
|
|
|
12
10
|
## 安装
|
|
13
11
|
|
|
12
|
+
需要 Prettier 3.9 或更高版本。
|
|
13
|
+
|
|
14
14
|
```shell
|
|
15
15
|
npm i -D prettier prettier-plugin-sort
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
在 Prettier
|
|
18
|
+
在 Prettier 配置中启用插件:
|
|
19
19
|
|
|
20
20
|
```json
|
|
21
21
|
{
|
|
@@ -23,24 +23,56 @@ npm i -D prettier prettier-plugin-sort
|
|
|
23
23
|
}
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
## 作用范围
|
|
27
|
+
|
|
28
|
+
ES 模块排序支持以下 Prettier 解析器:
|
|
29
|
+
|
|
30
|
+
- `babel`
|
|
31
|
+
- `babel-flow`
|
|
32
|
+
- `babel-ts`
|
|
33
|
+
- `typescript`
|
|
34
|
+
- `flow`
|
|
35
|
+
- `acorn`
|
|
36
|
+
- `espree`
|
|
37
|
+
- `meriyah`
|
|
38
|
+
|
|
39
|
+
上述排序同样适用于这些解析器处理的**嵌入式**代码,例如 Vue 或 Markdown 中的 JavaScript 或 TypeScript 内容。
|
|
40
|
+
|
|
41
|
+
插件只处理 ES 模块语法。CommonJS 的 `require()`、`module.exports` 和 TypeScript `export =` 会保持不变。
|
|
42
|
+
|
|
43
|
+
`package.json` 排序支持以下 Prettier 解析器:
|
|
44
|
+
|
|
45
|
+
- `json`
|
|
46
|
+
- `json-stringify`
|
|
47
|
+
|
|
48
|
+
仅当文件名为 `package.json` 时,才会启用排序。
|
|
27
49
|
|
|
28
|
-
##
|
|
50
|
+
## 配置项
|
|
51
|
+
|
|
52
|
+
| 配置项 | 类型 | 默认值 | 作用 |
|
|
53
|
+
| ------------------------ | ----------------- | ------------------------------------------------------- | --------------------------------------------------- |
|
|
54
|
+
| `esmImportSort` | `boolean` | `true` | 分组并排序顶层静态 `import`,同时排列其中的具名导入 |
|
|
55
|
+
| `esmImportGroups` | `ImportGroup[]` | `["builtin", "external", "parent", "sibling", "index"]` | 指定 `import` 分组顺序 |
|
|
56
|
+
| `esmImportSeparation` | `boolean` | `true` | 在不同分组之间及副作用 `import` 的上下两侧留出空行 |
|
|
57
|
+
| `esmImportTypeStyle` | `TypeImportStyle` | `"separate"` | 控制仅类型 `import` 的写法与顺序 |
|
|
58
|
+
| `esmImportMerge` | `boolean` | `true` | 安全合并来自同一模块的 `import` |
|
|
59
|
+
| `esmExportSpecifierSort` | `boolean` | `true` | 按名称排列 `export { ... }` 形式的导出列表 |
|
|
60
|
+
| `packageSort` | `boolean` | `true` | 排序 `package.json` 字段 |
|
|
29
61
|
|
|
30
|
-
|
|
62
|
+
## `import` 排序
|
|
31
63
|
|
|
32
|
-
|
|
64
|
+
插件会分组并排序文件顶层的静态 `import`,同时排列其中的具名导入。带 `as` 的具名导入按本地名称排序。
|
|
33
65
|
|
|
34
|
-
|
|
66
|
+
插件不会处理动态 `import()`,也不会处理字符串和注释中类似 `import` 的内容。将 `esmImportSort` 设为 `false`,可以关闭所有 `import` 排序功能。
|
|
67
|
+
|
|
68
|
+
插件会把分散在其他顶层语句之间的静态 `import` 集中到第一条 `import` 所在位置,再进行排序。
|
|
35
69
|
|
|
36
70
|
排序前:
|
|
37
71
|
|
|
38
72
|
<!-- prettier-ignore -->
|
|
39
73
|
```typescript
|
|
40
|
-
import App from './App
|
|
74
|
+
import App from './App';
|
|
41
75
|
import fs from 'node:fs';
|
|
42
|
-
import lodash from 'lodash';
|
|
43
|
-
import path from 'node:path';
|
|
44
76
|
import react from 'react';
|
|
45
77
|
```
|
|
46
78
|
|
|
@@ -48,51 +80,35 @@ import react from 'react';
|
|
|
48
80
|
|
|
49
81
|
```typescript
|
|
50
82
|
import fs from 'node:fs';
|
|
51
|
-
import path from 'node:path';
|
|
52
83
|
|
|
53
|
-
import lodash from 'lodash';
|
|
54
84
|
import react from 'react';
|
|
55
85
|
|
|
56
|
-
import App from './App
|
|
86
|
+
import App from './App';
|
|
57
87
|
```
|
|
58
88
|
|
|
59
|
-
|
|
89
|
+
### 分组
|
|
60
90
|
|
|
61
|
-
|
|
91
|
+
`esmImportGroups` 支持以下分组:
|
|
62
92
|
|
|
63
|
-
|
|
93
|
+
| 分组 | 匹配范围 |
|
|
94
|
+
| ---------- | ------------------------------------------- |
|
|
95
|
+
| `builtin` | 以 `node:`、`bun:` 开头的模块,以及 `bun` |
|
|
96
|
+
| `external` | 第三方包,以及没有匹配其他分组的模块路径 |
|
|
97
|
+
| `internal` | 以 `/`、`~`、`@/` 或 `#` 开头的模块路径 |
|
|
98
|
+
| `parent` | `../utils` 这类指向上级目录的相对路径 |
|
|
99
|
+
| `sibling` | `./Button` 这类指向同级目录的相对路径 |
|
|
100
|
+
| `index` | `.`、`./`、`./index` 及带扩展名的 `./index` |
|
|
64
101
|
|
|
65
|
-
|
|
102
|
+
现代 Node.js 代码应使用 [`node:` URL](https://nodejs.org/api/esm.html#node-imports) 显式引用内置模块。本插件也只把带 `node:` 前缀的 Node.js 内置模块归入 `builtin`。`fs`、`path` 等未带前缀的内置模块名称归入 `external`。
|
|
66
103
|
|
|
67
|
-
|
|
68
|
-
{
|
|
69
|
-
"plugins": ["prettier-plugin-sort"],
|
|
70
|
-
"importOrderGroups": ["builtin", "external", "parent", "sibling", "index"],
|
|
71
|
-
"importOrderSeparation": true,
|
|
72
|
-
"importOrderTypeImports": "separate",
|
|
73
|
-
"importOrderMergeDuplicates": true
|
|
74
|
-
}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
各分组的匹配规则如下:
|
|
78
|
-
|
|
79
|
-
| 分组 | 匹配内容 | 示例 |
|
|
80
|
-
| ---------- | ---------------------------------------------------- | ---------------------------------- |
|
|
81
|
-
| `builtin` | `node:*`、`bun:*`、`bun`,以及无前缀的 Node 内置模块 | `node:fs`、`path`、`bun` |
|
|
82
|
-
| `external` | npm 包,以及不属于其他分组的模块 | `react`、`@scope/pkg` |
|
|
83
|
-
| `internal` | 项目绝对路径与别名 | `/utils`、`~/app`、`@/shared` |
|
|
84
|
-
| `parent` | 向上跳级的相对路径 | `../Button` |
|
|
85
|
-
| `sibling` | 同级相对路径(不包含 index) | `./Icon` |
|
|
86
|
-
| `index` | 当前目录的 index 模块 | `.`、`./`、`./index`、`./index.ts` |
|
|
104
|
+
插件会移除配置数组中的重复项,并按默认顺序追加未列出的默认分组。默认配置不包含 `internal`,因此它在未显式加入时排在最后。
|
|
87
105
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
你可以通过 `importOrderGroups` 调整分组顺序,或者删掉不需要的分组,比如把 `internal` 显式加入分组:
|
|
106
|
+
项目使用上述路径别名时,可以手动加入 `internal`:
|
|
91
107
|
|
|
92
108
|
```json
|
|
93
109
|
{
|
|
94
110
|
"plugins": ["prettier-plugin-sort"],
|
|
95
|
-
"
|
|
111
|
+
"esmImportGroups": [
|
|
96
112
|
"builtin",
|
|
97
113
|
"external",
|
|
98
114
|
"internal",
|
|
@@ -103,129 +119,40 @@ import 的模块一般可以按其来源分为不同的类别,例如上面的
|
|
|
103
119
|
}
|
|
104
120
|
```
|
|
105
121
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
<!-- prettier-ignore -->
|
|
109
|
-
```typescript
|
|
110
|
-
import App from './App.tsx';
|
|
111
|
-
import react from 'react';
|
|
112
|
-
import shared from '@/shared';
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
排序后:
|
|
116
|
-
|
|
117
|
-
<!-- prettier-ignore -->
|
|
118
|
-
```typescript
|
|
119
|
-
import react from 'react';
|
|
120
|
-
|
|
121
|
-
import shared from '@/shared';
|
|
122
|
-
|
|
123
|
-
import App from './App.tsx';
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
如果不想在分组之间插入空行,把 `importOrderSeparation` 设为 `false` 即可。
|
|
127
|
-
|
|
128
|
-
#### 类型导入
|
|
129
|
-
|
|
130
|
-
默认配置下,插件会把 `type` import 拆成独立的语句。
|
|
122
|
+
插件不会读取 `compilerOptions.paths`,也不会解析构建工具的别名配置。
|
|
131
123
|
|
|
132
|
-
|
|
124
|
+
如果不希望不同分组之间及副作用 `import` 的上下两侧留出空行,可以将 `esmImportSeparation` 设为 `false`。
|
|
133
125
|
|
|
134
|
-
|
|
135
|
-
```typescript
|
|
136
|
-
import { useState, type FC } from 'react';
|
|
137
|
-
```
|
|
126
|
+
### 仅类型 `import` 写法
|
|
138
127
|
|
|
139
|
-
|
|
128
|
+
`esmImportTypeStyle` 用来控制仅类型 `import` 的写法,共有四种取值:
|
|
140
129
|
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
import type { FC } from 'react';
|
|
144
|
-
import { useState } from 'react';
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
`importOrderTypeImports` 的几种风格则参考了 ESLint 生态里对 type import 的常见约定,尤其是
|
|
148
|
-
[@typescript-eslint/consistent-type-imports](https://typescript-eslint.io/rules/consistent-type-imports) 的 `fixStyle` 设计。
|
|
149
|
-
|
|
150
|
-
以 `import { c, type B, a } from 'mod';` 为例,各模式的效果:
|
|
151
|
-
|
|
152
|
-
| 模式 | 结果 |
|
|
130
|
+
| 配置值 | 输入 `import { c, type B, a } from 'mod'` 后的结果 |
|
|
153
131
|
| -------------- | ---------------------------------------------------------------- |
|
|
154
132
|
| `separate` | `import type { B } from 'mod';`<br>`import { a, c } from 'mod';` |
|
|
155
133
|
| `inline-first` | `import { type B, a, c } from 'mod';` |
|
|
156
134
|
| `inline-last` | `import { a, c, type B } from 'mod';` |
|
|
157
135
|
| `mixed` | `import { a, type B, c } from 'mod';` |
|
|
158
136
|
|
|
159
|
-
`separate
|
|
160
|
-
|
|
161
|
-
#### 合并同源导入
|
|
162
|
-
|
|
163
|
-
默认情况下,来自同一来源的多条 import 语句会被合并成一条,便于阅读。
|
|
164
|
-
|
|
165
|
-
排序前:
|
|
166
|
-
|
|
167
|
-
<!-- prettier-ignore -->
|
|
168
|
-
```typescript
|
|
169
|
-
import { useState } from 'react';
|
|
170
|
-
import { useEffect } from 'react';
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
排序后:
|
|
174
|
-
|
|
175
|
-
```typescript
|
|
176
|
-
import { useEffect, useState } from 'react';
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
`importOrderMergeDuplicates` 只负责合并这一步,花括号内的排列方式完全由 `importOrderTypeImports` 决定。比如把 `import { useState } from 'react';` 和 `import { type FC, useEffect } from 'react';` 合并后,不同模式的结果:
|
|
180
|
-
|
|
181
|
-
- `separate`(默认):合并后这一阶段又会被拆回两条,最终保持独立的 `import type` 语句
|
|
182
|
-
- `inline-first`:`import { type FC, useEffect, useState } from 'react';`
|
|
183
|
-
- `inline-last`:`import { useEffect, useState, type FC } from 'react';`
|
|
184
|
-
- `mixed`:`import { type FC, useEffect, useState } from 'react';`
|
|
185
|
-
|
|
186
|
-
如果你希望保留原本分离的两条语句,把 `importOrderMergeDuplicates` 设为 `false` 即可。副作用导入(`import 'mod';`)因为顺序有语义,永远不会被合并。
|
|
187
|
-
|
|
188
|
-
#### 副作用导入
|
|
189
|
-
|
|
190
|
-
副作用导入(`import 'mod'`)的顺序通常有运行时语义,例如 CSS 的层叠顺序、polyfill 必须在框架之前加载等。插件不会跨越副作用导入移动其他 import 语句:
|
|
191
|
-
|
|
192
|
-
排序前:
|
|
137
|
+
默认值为 `separate`。包含默认导入或命名空间导入的仅类型 `import` 会保留原有形式。
|
|
193
138
|
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
import Button from './Button';
|
|
197
|
-
import App from './App';
|
|
198
|
-
import 'normalize.css';
|
|
199
|
-
import theme from './theme';
|
|
200
|
-
import Icon from './Icon';
|
|
201
|
-
```
|
|
139
|
+
### 合并与排序边界
|
|
202
140
|
|
|
203
|
-
|
|
141
|
+
启用 `esmImportMerge` 后,来自同一模块的 `import` 会在安全的情况下合并。
|
|
204
142
|
|
|
205
|
-
|
|
206
|
-
import App from './App';
|
|
207
|
-
import Button from './Button';
|
|
143
|
+
当 `import` 属性不同、注释无法安全移动,或者默认导入和命名空间导入存在冲突时,插件不会合并这些 `import`。副作用 `import` 也不会合并。
|
|
208
144
|
|
|
209
|
-
|
|
145
|
+
排序不会跨越以下边界:
|
|
210
146
|
|
|
211
|
-
import
|
|
212
|
-
|
|
213
|
-
|
|
147
|
+
- 副作用 `import` 的顺序可能影响 CSS 层叠或兼容性补丁的加载,因此不会参与排序。它们会保持相对位置,并分隔前后的排序片段。
|
|
148
|
+
- 带有 `prettier-ignore` 的声明保持不变,其前后的 `import` 分别排序。
|
|
149
|
+
- 独立注释会分隔前后的 `import`。紧跟某条 `import` 的注释会和它一起移动。
|
|
150
|
+
- 插件会保留文件开头的 `#!` 指令、Prettier 文件级指令和位置敏感的 ESLint 指令。
|
|
151
|
+
- `import source`、`import defer`、Flow `import typeof` 等特殊声明可以整体参与排序。插件不会改写或合并这些声明。
|
|
214
152
|
|
|
215
|
-
|
|
153
|
+
## `export` 排序
|
|
216
154
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
- import 按分组分类,分组内按字母序排列
|
|
220
|
-
- 默认分组顺序:`builtin` → `external` → `parent` → `sibling` → `index`
|
|
221
|
-
- 分组之间默认插入空行,可通过 `importOrderSeparation` 关闭
|
|
222
|
-
- `type` import 默认拆成独立语句,可通过 `importOrderTypeImports` 调整为内联
|
|
223
|
-
- 同一来源的多条 import 默认合并为一条,可通过 `importOrderMergeDuplicates` 关闭
|
|
224
|
-
- 副作用导入(`import 'mod'`)其顺序有语义,不会被移动,两侧的 import 各自独立排序
|
|
225
|
-
|
|
226
|
-
### export
|
|
227
|
-
|
|
228
|
-
默认情况下,`export { … }` 花括号内的命名导出会按字母序排列。
|
|
155
|
+
`esmExportSpecifierSort` 会按名称排列顶层 `export { ... }` 和 `export type { ... }`。使用 `as` 时,按导出后的名称排序。
|
|
229
156
|
|
|
230
157
|
排序前:
|
|
231
158
|
|
|
@@ -240,17 +167,11 @@ export { useState, useEffect, type FC } from 'react';
|
|
|
240
167
|
export { type FC, useEffect, useState } from 'react';
|
|
241
168
|
```
|
|
242
169
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
排序规则:
|
|
170
|
+
插件不会移动或合并 `export` 声明。花括号内含有注释时,整条声明会保持原样,避免改变注释归属。
|
|
246
171
|
|
|
247
|
-
|
|
248
|
-
- 不改变整条 export 语句在文件中的位置
|
|
249
|
-
- 不合并同来源的多条 export 语句
|
|
172
|
+
## `package.json` 排序
|
|
250
173
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
默认配置下,效果大致如下。
|
|
174
|
+
`package.json` 的字段顺序遵循 [sort-package-json 4.0.0 的默认规则](https://github.com/keithamus/sort-package-json/blob/v4.0.0/defaultRules.md)。`scripts`、`exports` 和依赖字段等内部内容也按该版本的规则排序。
|
|
254
175
|
|
|
255
176
|
排序前:
|
|
256
177
|
|
|
@@ -258,11 +179,10 @@ export { type FC, useEffect, useState } from 'react';
|
|
|
258
179
|
```json
|
|
259
180
|
{
|
|
260
181
|
"version": "1.0.0",
|
|
261
|
-
"
|
|
262
|
-
"name": "demo",
|
|
182
|
+
"name": "example",
|
|
263
183
|
"dependencies": {
|
|
264
|
-
"typescript": "^
|
|
265
|
-
"prettier": "^3.
|
|
184
|
+
"typescript": "^7.0.0",
|
|
185
|
+
"prettier": "^3.9.0"
|
|
266
186
|
}
|
|
267
187
|
}
|
|
268
188
|
```
|
|
@@ -271,66 +191,20 @@ export { type FC, useEffect, useState } from 'react';
|
|
|
271
191
|
|
|
272
192
|
```json
|
|
273
193
|
{
|
|
274
|
-
"name": "
|
|
194
|
+
"name": "example",
|
|
275
195
|
"version": "1.0.0",
|
|
276
|
-
"keywords": ["plugin", "prettier", "sort"],
|
|
277
196
|
"dependencies": {
|
|
278
|
-
"prettier": "^3.
|
|
279
|
-
"typescript": "^
|
|
197
|
+
"prettier": "^3.9.0",
|
|
198
|
+
"typescript": "^7.0.0"
|
|
280
199
|
}
|
|
281
200
|
}
|
|
282
201
|
```
|
|
283
202
|
|
|
284
|
-
`
|
|
285
|
-
|
|
286
|
-
排序规则:
|
|
287
|
-
|
|
288
|
-
- 顶层字段按常用顺序排列(`name` → `version` → ... → `dependencies`)
|
|
289
|
-
- `scripts` 和 `betterScripts` 的键名按 pre/post 生命周期包围和 `:` 命名空间分组排序
|
|
290
|
-
- `exports` 键按路径优先、条件字母序、`default` 置底排序,嵌套的 export 对象递归处理
|
|
291
|
-
- `dependencies`、`devDependencies`、`peerDependencies` 等依赖表永远按字母序排列,即使 `packageJsonOrder` 设为 `false` 也不例外。因为 `npm install` 每次都会按照字母序写回
|
|
292
|
-
- 字符串数组按字段分类处理:`keywords`、`files`、`activationEvents` 只去重不排序,保留原始语义顺序;`bundledDependencies`、`bundleDependencies`、`extensionPack`、`extensionDependencies` 去重后按字母序排列;`workspaces` 数组不排序;其余字符串数组按字母序排列
|
|
293
|
-
- 想让某些顶层字段完全跳过排序,可以在 `packageJsonOrderExcludeKeys` 里列出
|
|
294
|
-
|
|
295
|
-
## 配置项
|
|
296
|
-
|
|
297
|
-
Prettier 的插件选项是扁平的,所以这些配置都以 `importOrder`、`exportOrder` 或 `packageJsonOrder` 开头。
|
|
298
|
-
|
|
299
|
-
| 配置项 | 说明 | 默认值 |
|
|
300
|
-
| ----------------------------- | ------------------------------------------------------------------------------ | ------------------------------------------------------- |
|
|
301
|
-
| `importOrder` | 是否排序 JS / TS 里的 import | `true` |
|
|
302
|
-
| `importOrderGroups` | 分组顺序,支持 `builtin`、`external`、`internal`、`parent`、`sibling`、`index` | `["builtin", "external", "parent", "sibling", "index"]` |
|
|
303
|
-
| `importOrderSeparation` | 分组之间是否插入空行 | `true` |
|
|
304
|
-
| `importOrderTypeImports` | `type` import 的处理方式:`separate`、`inline-first`、`inline-last`、`mixed` | `"separate"` |
|
|
305
|
-
| `importOrderMergeDuplicates` | 是否合并同来源的多条 import 语句(副作用导入除外) | `true` |
|
|
306
|
-
| `exportOrder` | 是否按字母序排列 `export { … }` 花括号内的命名导出 | `true` |
|
|
307
|
-
| `packageJsonOrder` | 是否排序 package.json 的顶层字段、scripts/exports 子键和字符串数组 | `true` |
|
|
308
|
-
| `packageJsonOrderExcludeKeys` | 不参与 package.json 排序的顶层字段 | `[]` |
|
|
309
|
-
|
|
310
|
-
## 示例
|
|
311
|
-
|
|
312
|
-
```json
|
|
313
|
-
{
|
|
314
|
-
"plugins": ["prettier-plugin-sort"],
|
|
315
|
-
"importOrderGroups": [
|
|
316
|
-
"builtin",
|
|
317
|
-
"external",
|
|
318
|
-
"internal",
|
|
319
|
-
"parent",
|
|
320
|
-
"sibling",
|
|
321
|
-
"index"
|
|
322
|
-
],
|
|
323
|
-
"importOrderTypeImports": "inline-last",
|
|
324
|
-
"packageJsonOrderExcludeKeys": ["contributes"]
|
|
325
|
-
}
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
## 类型提示
|
|
203
|
+
将 `packageSort` 设为 `false` 只会关闭字段排序,Prettier 仍会照常排版该文件。
|
|
329
204
|
|
|
330
|
-
|
|
331
|
-
`SortOptions` 类型,这样写配置时会有补全和校验。
|
|
205
|
+
## TypeScript 配置
|
|
332
206
|
|
|
333
|
-
|
|
207
|
+
插件同时导出 `SortOptions`、`ImportGroup` 和 `TypeImportStyle`,可用于 TypeScript 配置文件:
|
|
334
208
|
|
|
335
209
|
```typescript
|
|
336
210
|
import { type Config } from 'prettier';
|
|
@@ -338,7 +212,7 @@ import { type SortOptions } from 'prettier-plugin-sort';
|
|
|
338
212
|
|
|
339
213
|
export default {
|
|
340
214
|
plugins: ['prettier-plugin-sort'],
|
|
341
|
-
|
|
215
|
+
esmImportGroups: [
|
|
342
216
|
'builtin',
|
|
343
217
|
'external',
|
|
344
218
|
'internal',
|
|
@@ -346,47 +220,6 @@ export default {
|
|
|
346
220
|
'sibling',
|
|
347
221
|
'index',
|
|
348
222
|
],
|
|
349
|
-
|
|
350
|
-
packageJsonOrderExcludeKeys: ['contributes'],
|
|
223
|
+
esmImportTypeStyle: 'inline-last',
|
|
351
224
|
} satisfies Config & SortOptions;
|
|
352
225
|
```
|
|
353
|
-
|
|
354
|
-
### 在 `.js` 文件里使用
|
|
355
|
-
|
|
356
|
-
```js
|
|
357
|
-
/** @type {import('prettier').Config & import('prettier-plugin-sort').SortOptions} */
|
|
358
|
-
const config = {
|
|
359
|
-
plugins: ['prettier-plugin-sort'],
|
|
360
|
-
importOrderGroups: [
|
|
361
|
-
'builtin',
|
|
362
|
-
'external',
|
|
363
|
-
'internal',
|
|
364
|
-
'parent',
|
|
365
|
-
'sibling',
|
|
366
|
-
'index',
|
|
367
|
-
],
|
|
368
|
-
importOrderTypeImports: 'inline-last',
|
|
369
|
-
packageJsonOrderExcludeKeys: ['contributes'],
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
export default config;
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
如果你只想单独复用字面量类型,也可以使用插件导出的 `ImportGroup` 和
|
|
376
|
-
`TypeImportsStyle`。
|
|
377
|
-
|
|
378
|
-
## 项目由来
|
|
379
|
-
|
|
380
|
-
在接触 Prettier 之前,我一直都在使用 IDE 的自定义代码排序。因为后面开始尝试各种不同的 IDE,所以便有了统一配置管理的需求,于是将 ESLint + Prettier 引入到了项目中。
|
|
381
|
-
|
|
382
|
-
可是 Prettier 没有提供 sort 配置,我想要格式化 import 排序,就必须安装 `prettier-plugin-organize-imports` 插件,想要格式化 package.json 排序,就必须安装 `prettier-plugin-packagejson` 插件,导致体验十分割裂。
|
|
383
|
-
|
|
384
|
-
我在很长的一段时间里都没有去在意这些细枝末节,主要的精力放在了开发上。但在最近,我有调整 import type 内联排版的需求,发现 `prettier-plugin-organize-imports` 并不支持。再加上基于 `sort-package-json` 开发的 `prettier-plugin-packagejson` 有很多对于插件而言冗余的依赖项,所以便有了自己开发的打算。
|
|
385
|
-
|
|
386
|
-
`prettier-plugin-sort` 的目的不是为了替代谁,而是让开发者有着更多的选择。Prettier 绝大多数的用途都是格式化 JS/TS 代码,而所有的 JS 项目都有着 package.json,所以 `prettier-plugin-sort` 只实现了这两种基础的排序,初衷是让 JS 开发者能够以最小的心智负担做到开箱即用(未来可能还会添加对 tsconfig.json 的排序支持)。如果你有着其它代码的排序要求,那么依然可以选择安装 `prettier-plugin-css-order` 之类的插件,它们之间并不冲突。
|
|
387
|
-
|
|
388
|
-
## 鸣谢
|
|
389
|
-
|
|
390
|
-
- `eslint-plugin-import`: https://github.com/import-js/eslint-plugin-import
|
|
391
|
-
- `typescript-eslint`: https://github.com/typescript-eslint/typescript-eslint
|
|
392
|
-
- `sort-package-json`: https://github.com/keithamus/sort-package-json
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Plugin } from 'prettier';
|
|
2
|
-
declare const
|
|
3
|
-
export default
|
|
2
|
+
declare const sortPlugin: Plugin;
|
|
3
|
+
export default sortPlugin;
|
|
4
4
|
export { options } from './options';
|
|
5
|
-
export type { ImportGroup, SortOptions,
|
|
5
|
+
export type { ImportGroup, SortOptions, TypeImportStyle } from './options';
|