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.md
CHANGED
|
@@ -2,20 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
A [Prettier](https://prettier.io/) plugin focused on sorting.
|
|
4
4
|
|
|
5
|
-
- Sort `import`
|
|
6
|
-
- Sort
|
|
7
|
-
- Sort top-level keys, string arrays, and dependency maps in `package.json`
|
|
8
|
-
- Zero runtime dependencies
|
|
5
|
+
- Sort top-level `import` and `export { ... }` in JavaScript, TypeScript, and Flow
|
|
6
|
+
- Sort fields in `package.json`
|
|
9
7
|
|
|
10
8
|
Read this in other languages: English | [中文](./README.zh.md)
|
|
11
9
|
|
|
12
10
|
## Install
|
|
13
11
|
|
|
12
|
+
Requires Prettier 3.9 or newer.
|
|
13
|
+
|
|
14
14
|
```shell
|
|
15
15
|
npm i -D prettier prettier-plugin-sort
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
Add the plugin to your Prettier config:
|
|
19
19
|
|
|
20
20
|
```json
|
|
21
21
|
{
|
|
@@ -23,74 +23,92 @@ Enable it in your Prettier config:
|
|
|
23
23
|
}
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
## Scope
|
|
27
|
+
|
|
28
|
+
ES module sorting supports these Prettier parsers:
|
|
29
|
+
|
|
30
|
+
- `babel`
|
|
31
|
+
- `babel-flow`
|
|
32
|
+
- `babel-ts`
|
|
33
|
+
- `typescript`
|
|
34
|
+
- `flow`
|
|
35
|
+
- `acorn`
|
|
36
|
+
- `espree`
|
|
37
|
+
- `meriyah`
|
|
38
|
+
|
|
39
|
+
The same sorting also applies to **embedded** JavaScript and TypeScript in Vue or Markdown.
|
|
27
40
|
|
|
28
|
-
|
|
41
|
+
The plugin only handles ES module syntax. CommonJS `require()` and `module.exports`, along with TypeScript `export =`, remain unchanged.
|
|
29
42
|
|
|
30
|
-
|
|
43
|
+
`package.json` sorting is enabled for these Prettier parsers:
|
|
44
|
+
|
|
45
|
+
- `json`
|
|
46
|
+
- `json-stringify`
|
|
47
|
+
|
|
48
|
+
Sorting is enabled only when the file is named `package.json`.
|
|
49
|
+
|
|
50
|
+
## Options
|
|
31
51
|
|
|
32
|
-
|
|
52
|
+
| Option | Type | Default | Description |
|
|
53
|
+
| ------------------------ | ----------------- | ------------------------------------------------------- | ------------------------------------------------------------------------ |
|
|
54
|
+
| `esmImportSort` | `boolean` | `true` | Group and sort top-level static imports and the specifiers inside braces |
|
|
55
|
+
| `esmImportGroups` | `ImportGroup[]` | `["builtin", "external", "parent", "sibling", "index"]` | Set the order of import groups |
|
|
56
|
+
| `esmImportSeparation` | `boolean` | `true` | Add blank lines between groups and above and below side-effect imports |
|
|
57
|
+
| `esmImportTypeStyle` | `TypeImportStyle` | `"separate"` | Control the form and order of type-only imports |
|
|
58
|
+
| `esmImportMerge` | `boolean` | `true` | Safely merge imports from the same module |
|
|
59
|
+
| `esmExportSpecifierSort` | `boolean` | `true` | Sort specifiers inside `export { ... }` |
|
|
60
|
+
| `packageSort` | `boolean` | `true` | Sort fields in `package.json` |
|
|
33
61
|
|
|
34
|
-
|
|
62
|
+
## Import sorting
|
|
35
63
|
|
|
36
|
-
|
|
64
|
+
The plugin groups and sorts top-level static imports. It also sorts the specifiers inside braces. Import specifiers that use `as` are sorted by their local names.
|
|
65
|
+
|
|
66
|
+
The plugin leaves dynamic `import()` calls and import-like text in strings or comments unchanged. Set `esmImportSort` to `false` to disable all import sorting.
|
|
67
|
+
|
|
68
|
+
The plugin collects static imports separated by other top-level statements at the first import position, then sorts them.
|
|
69
|
+
|
|
70
|
+
Before sorting:
|
|
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
|
|
|
47
|
-
After:
|
|
79
|
+
After sorting:
|
|
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
|
+
### Grouping
|
|
60
90
|
|
|
61
|
-
|
|
91
|
+
`esmImportGroups` accepts these values:
|
|
62
92
|
|
|
63
|
-
|
|
93
|
+
| Group | Matches |
|
|
94
|
+
| ---------- | -------------------------------------------------------------- |
|
|
95
|
+
| `builtin` | Modules beginning with `node:` or `bun:`, plus `bun` |
|
|
96
|
+
| `external` | Packages and module paths not matched by any other group |
|
|
97
|
+
| `internal` | Module paths beginning with `/`, `~`, `@/`, or `#` |
|
|
98
|
+
| `parent` | Parent-relative paths such as `../utils` |
|
|
99
|
+
| `sibling` | Sibling paths such as `./Button` |
|
|
100
|
+
| `index` | `.`, `./`, `./index`, and `./index` with an optional extension |
|
|
64
101
|
|
|
65
|
-
|
|
66
|
-
{
|
|
67
|
-
"plugins": ["prettier-plugin-sort"],
|
|
68
|
-
"importOrderGroups": ["builtin", "external", "parent", "sibling", "index"],
|
|
69
|
-
"importOrderSeparation": true,
|
|
70
|
-
"importOrderTypeImports": "separate",
|
|
71
|
-
"importOrderMergeDuplicates": true
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
Group matchers:
|
|
102
|
+
Modern Node.js code should reference built-ins explicitly with [`node:` URLs](https://nodejs.org/api/esm.html#node-imports). The plugin also classifies only `node:`-prefixed Node.js built-ins as `builtin`. Module specifiers without the prefix, such as `fs` and `path`, belong to `external`.
|
|
76
103
|
|
|
77
|
-
|
|
78
|
-
| ---------- | ----------------------------------------------------------- | ---------------------------------- |
|
|
79
|
-
| `builtin` | `node:*`, `bun:*`, `bun`, and unprefixed Node built-ins | `node:fs`, `path`, `bun` |
|
|
80
|
-
| `external` | npm packages, and anything that doesn't match another group | `react`, `@scope/pkg` |
|
|
81
|
-
| `internal` | Project absolute paths and aliases | `/utils`, `~/app`, `@/shared` |
|
|
82
|
-
| `parent` | Parent-relative paths | `../Button` |
|
|
83
|
-
| `sibling` | Sibling paths (excluding index) | `./Icon` |
|
|
84
|
-
| `index` | Current directory index | `.`, `./`, `./index`, `./index.ts` |
|
|
104
|
+
The plugin removes duplicate entries and appends omitted default groups in their default order. The default configuration does not include `internal`, so it sorts last unless explicitly added.
|
|
85
105
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
Reorder or drop groups through `importOrderGroups`. For example, adding `internal` explicitly:
|
|
106
|
+
Projects that use one of these path aliases can add `internal`:
|
|
89
107
|
|
|
90
108
|
```json
|
|
91
109
|
{
|
|
92
110
|
"plugins": ["prettier-plugin-sort"],
|
|
93
|
-
"
|
|
111
|
+
"esmImportGroups": [
|
|
94
112
|
"builtin",
|
|
95
113
|
"external",
|
|
96
114
|
"internal",
|
|
@@ -101,232 +119,92 @@ Reorder or drop groups through `importOrderGroups`. For example, adding `interna
|
|
|
101
119
|
}
|
|
102
120
|
```
|
|
103
121
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
<!-- prettier-ignore -->
|
|
107
|
-
```typescript
|
|
108
|
-
import App from './App.tsx';
|
|
109
|
-
import react from 'react';
|
|
110
|
-
import shared from '@/shared';
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
After:
|
|
114
|
-
|
|
115
|
-
<!-- prettier-ignore -->
|
|
116
|
-
```typescript
|
|
117
|
-
import react from 'react';
|
|
118
|
-
|
|
119
|
-
import shared from '@/shared';
|
|
120
|
-
|
|
121
|
-
import App from './App.tsx';
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
Set `importOrderSeparation` to `false` if you don't want blank lines between groups.
|
|
125
|
-
|
|
126
|
-
#### Type imports
|
|
122
|
+
The plugin does not read `compilerOptions.paths` or resolve aliases from bundler configuration.
|
|
127
123
|
|
|
128
|
-
|
|
124
|
+
Set `esmImportSeparation` to `false` to remove blank lines between groups and above and below side-effect imports.
|
|
129
125
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
<!-- prettier-ignore -->
|
|
133
|
-
```typescript
|
|
134
|
-
import { useState, type FC } from 'react';
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
After:
|
|
138
|
-
|
|
139
|
-
<!-- prettier-ignore -->
|
|
140
|
-
```typescript
|
|
141
|
-
import type { FC } from 'react';
|
|
142
|
-
import { useState } from 'react';
|
|
143
|
-
```
|
|
126
|
+
### Type-only import style
|
|
144
127
|
|
|
145
|
-
|
|
128
|
+
`esmImportTypeStyle` controls how type-only imports are written and accepts four values:
|
|
146
129
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
| Mode | Result |
|
|
130
|
+
| Value | Input: `import { c, type B, a } from 'mod'` |
|
|
150
131
|
| -------------- | ---------------------------------------------------------------- |
|
|
151
132
|
| `separate` | `import type { B } from 'mod';`<br>`import { a, c } from 'mod';` |
|
|
152
133
|
| `inline-first` | `import { type B, a, c } from 'mod';` |
|
|
153
134
|
| `inline-last` | `import { a, c, type B } from 'mod';` |
|
|
154
135
|
| `mixed` | `import { a, type B, c } from 'mod';` |
|
|
155
136
|
|
|
156
|
-
`separate
|
|
157
|
-
|
|
158
|
-
#### Merging same-source imports
|
|
159
|
-
|
|
160
|
-
By default, multiple `import` statements from the same source are merged into one.
|
|
161
|
-
|
|
162
|
-
Before:
|
|
163
|
-
|
|
164
|
-
<!-- prettier-ignore -->
|
|
165
|
-
```typescript
|
|
166
|
-
import { useState } from 'react';
|
|
167
|
-
import { useEffect } from 'react';
|
|
168
|
-
```
|
|
137
|
+
`separate` is the default. Type-only default and namespace imports keep their original form.
|
|
169
138
|
|
|
170
|
-
|
|
139
|
+
### Merging and sorting boundaries
|
|
171
140
|
|
|
172
|
-
|
|
173
|
-
import { useEffect, useState } from 'react';
|
|
174
|
-
```
|
|
141
|
+
With `esmImportMerge` enabled, imports from the same module are merged when it is safe to do so.
|
|
175
142
|
|
|
176
|
-
|
|
143
|
+
The plugin does not merge imports with different import attributes, comments that cannot move safely, or conflicting default or namespace bindings. Side-effect imports also remain separate.
|
|
177
144
|
|
|
178
|
-
|
|
179
|
-
- `inline-first`: `import { type FC, useEffect, useState } from 'react';`
|
|
180
|
-
- `inline-last`: `import { useEffect, useState, type FC } from 'react';`
|
|
181
|
-
- `mixed`: `import { type FC, useEffect, useState } from 'react';`
|
|
145
|
+
Sorting never crosses these boundaries:
|
|
182
146
|
|
|
183
|
-
|
|
147
|
+
- Side-effect import order can affect CSS cascading or polyfill loading, so these imports are not sorted. They keep their relative positions and separate the sorting segments around them.
|
|
148
|
+
- A declaration marked with `prettier-ignore` stays unchanged. Imports before and after it are sorted separately.
|
|
149
|
+
- A standalone comment separates the imports before and after it. A comment attached to an import moves with that declaration.
|
|
150
|
+
- The plugin preserves shebangs, Prettier file pragmas, and position-sensitive ESLint directives.
|
|
151
|
+
- Specialized declarations such as `import source`, `import defer`, and Flow `import typeof` may be reordered as a whole. The plugin never rewrites or merges them.
|
|
184
152
|
|
|
185
|
-
|
|
153
|
+
## Export sorting
|
|
186
154
|
|
|
187
|
-
|
|
155
|
+
`esmExportSpecifierSort` sorts top-level `export { ... }` and `export type { ... }` entries by name. Aliased entries are sorted by their exported names.
|
|
188
156
|
|
|
189
|
-
Before:
|
|
190
|
-
|
|
191
|
-
<!-- prettier-ignore -->
|
|
192
|
-
```typescript
|
|
193
|
-
import Button from './Button';
|
|
194
|
-
import App from './App';
|
|
195
|
-
import 'normalize.css';
|
|
196
|
-
import theme from './theme';
|
|
197
|
-
import Icon from './Icon';
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
After:
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
import App from './App';
|
|
204
|
-
import Button from './Button';
|
|
205
|
-
|
|
206
|
-
import 'normalize.css';
|
|
207
|
-
|
|
208
|
-
import Icon from './Icon';
|
|
209
|
-
import theme from './theme';
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
Imports on each side are sorted independently. The side-effect import itself stays in place.
|
|
213
|
-
|
|
214
|
-
Sorting rules:
|
|
215
|
-
|
|
216
|
-
- Imports are classified into groups. Within each group they are sorted alphabetically
|
|
217
|
-
- Default group order: `builtin` → `external` → `parent` → `sibling` → `index`
|
|
218
|
-
- A blank line is inserted between groups by default. Disable with `importOrderSeparation`
|
|
219
|
-
- `type` imports are split into their own statement by default. Use `importOrderTypeImports` to inline them instead
|
|
220
|
-
- Multiple imports from the same source are merged into one by default. Disable with `importOrderMergeDuplicates`
|
|
221
|
-
- Side-effect imports (`import 'mod'`) carry semantic order and are never moved. Imports on either side are sorted independently
|
|
222
|
-
|
|
223
|
-
### Exports
|
|
224
|
-
|
|
225
|
-
By default, named specifiers inside `export { … }` are sorted alphabetically.
|
|
226
|
-
|
|
227
|
-
Before:
|
|
157
|
+
Before sorting:
|
|
228
158
|
|
|
229
159
|
<!-- prettier-ignore -->
|
|
230
160
|
```typescript
|
|
231
161
|
export { useState, useEffect, type FC } from 'react';
|
|
232
162
|
```
|
|
233
163
|
|
|
234
|
-
After:
|
|
164
|
+
After sorting:
|
|
235
165
|
|
|
236
166
|
```typescript
|
|
237
167
|
export { type FC, useEffect, useState } from 'react';
|
|
238
168
|
```
|
|
239
169
|
|
|
240
|
-
The plugin
|
|
241
|
-
|
|
242
|
-
Sorting rules:
|
|
170
|
+
The plugin does not move or merge export declarations. If an export list contains comments, the declaration remains unchanged to preserve comment placement.
|
|
243
171
|
|
|
244
|
-
|
|
245
|
-
- The position of the export statement in the file is not changed
|
|
246
|
-
- Multiple export statements from the same source are not merged
|
|
172
|
+
## `package.json` sorting
|
|
247
173
|
|
|
248
|
-
|
|
174
|
+
`package.json` field order follows [the default rules from sort-package-json 4.0.0](https://github.com/keithamus/sort-package-json/blob/v4.0.0/defaultRules.md). Nested content such as `scripts`, `exports`, and dependency fields follows the same version's rules.
|
|
249
175
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
Before:
|
|
176
|
+
Before sorting:
|
|
253
177
|
|
|
254
178
|
<!-- prettier-ignore -->
|
|
255
179
|
```json
|
|
256
180
|
{
|
|
257
181
|
"version": "1.0.0",
|
|
258
|
-
"
|
|
259
|
-
"name": "demo",
|
|
182
|
+
"name": "example",
|
|
260
183
|
"dependencies": {
|
|
261
|
-
"typescript": "^
|
|
262
|
-
"prettier": "^3.
|
|
184
|
+
"typescript": "^7.0.0",
|
|
185
|
+
"prettier": "^3.9.0"
|
|
263
186
|
}
|
|
264
187
|
}
|
|
265
188
|
```
|
|
266
189
|
|
|
267
|
-
After:
|
|
190
|
+
After sorting:
|
|
268
191
|
|
|
269
192
|
```json
|
|
270
193
|
{
|
|
271
|
-
"name": "
|
|
194
|
+
"name": "example",
|
|
272
195
|
"version": "1.0.0",
|
|
273
|
-
"keywords": ["plugin", "prettier", "sort"],
|
|
274
196
|
"dependencies": {
|
|
275
|
-
"prettier": "^3.
|
|
276
|
-
"typescript": "^
|
|
197
|
+
"prettier": "^3.9.0",
|
|
198
|
+
"typescript": "^7.0.0"
|
|
277
199
|
}
|
|
278
200
|
}
|
|
279
201
|
```
|
|
280
202
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
Rules the plugin follows:
|
|
203
|
+
Setting `packageSort` to `false` disables key sorting only. Prettier still formats the file as usual.
|
|
284
204
|
|
|
285
|
-
|
|
286
|
-
- `scripts` and `betterScripts` keys are sorted with pre/post lifecycle grouping and `:` namespace ordering
|
|
287
|
-
- `exports` keys are sorted: path subkeys first, conditions alphabetically with `default` moved to the end; nested export maps are recursively sorted
|
|
288
|
-
- `dependencies`, `devDependencies`, `peerDependencies` and other dependency maps are always sorted alphabetically, even if `packageJsonOrder` is set to `false`, because `npm install` rewrites them in alphabetical order every time
|
|
289
|
-
- String arrays are handled by field: `keywords`, `files`, `activationEvents` are deduplicated preserving original order; `bundledDependencies`, `bundleDependencies`, `extensionPack`, `extensionDependencies` are deduplicated then sorted; `workspaces` arrays are left untouched; all other string arrays are sorted alphabetically
|
|
290
|
-
- Use `packageJsonOrderExcludeKeys` to opt specific top-level keys out of sorting entirely
|
|
291
|
-
|
|
292
|
-
## Options
|
|
205
|
+
## TypeScript configuration
|
|
293
206
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
| Option | Description | Default |
|
|
297
|
-
| ----------------------------- | ------------------------------------------------------------------------------------------ | ------------------------------------------------------- |
|
|
298
|
-
| `importOrder` | Sort `import` declarations in JS / TS | `true` |
|
|
299
|
-
| `importOrderGroups` | Group order. Valid values: `builtin`, `external`, `internal`, `parent`, `sibling`, `index` | `["builtin", "external", "parent", "sibling", "index"]` |
|
|
300
|
-
| `importOrderSeparation` | Insert a blank line between adjacent groups | `true` |
|
|
301
|
-
| `importOrderTypeImports` | How to place `type` imports: `separate`, `inline-first`, `inline-last`, `mixed` | `"separate"` |
|
|
302
|
-
| `importOrderMergeDuplicates` | Merge multiple `import` statements from the same source (except side-effect imports) | `true` |
|
|
303
|
-
| `exportOrder` | Sort named specifiers inside `export { … }` alphabetically | `true` |
|
|
304
|
-
| `packageJsonOrder` | Sort top-level keys, scripts/exports subkeys, and string arrays in `package.json` | `true` |
|
|
305
|
-
| `packageJsonOrderExcludeKeys` | Top-level `package.json` keys to leave untouched | `[]` |
|
|
306
|
-
|
|
307
|
-
## Example
|
|
308
|
-
|
|
309
|
-
```json
|
|
310
|
-
{
|
|
311
|
-
"plugins": ["prettier-plugin-sort"],
|
|
312
|
-
"importOrderGroups": [
|
|
313
|
-
"builtin",
|
|
314
|
-
"external",
|
|
315
|
-
"internal",
|
|
316
|
-
"parent",
|
|
317
|
-
"sibling",
|
|
318
|
-
"index"
|
|
319
|
-
],
|
|
320
|
-
"importOrderTypeImports": "inline-last",
|
|
321
|
-
"packageJsonOrderExcludeKeys": ["contributes"]
|
|
322
|
-
}
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
## Type hints
|
|
326
|
-
|
|
327
|
-
If you write your Prettier config in a `.ts` or `.js` file, you can reuse the `SortOptions` type exported by the plugin to get completion and validation.
|
|
328
|
-
|
|
329
|
-
### In a `.ts` file
|
|
207
|
+
The package also exports `SortOptions`, `ImportGroup`, and `TypeImportStyle` for use in TypeScript configuration files:
|
|
330
208
|
|
|
331
209
|
```typescript
|
|
332
210
|
import { type Config } from 'prettier';
|
|
@@ -334,7 +212,7 @@ import { type SortOptions } from 'prettier-plugin-sort';
|
|
|
334
212
|
|
|
335
213
|
export default {
|
|
336
214
|
plugins: ['prettier-plugin-sort'],
|
|
337
|
-
|
|
215
|
+
esmImportGroups: [
|
|
338
216
|
'builtin',
|
|
339
217
|
'external',
|
|
340
218
|
'internal',
|
|
@@ -342,46 +220,6 @@ export default {
|
|
|
342
220
|
'sibling',
|
|
343
221
|
'index',
|
|
344
222
|
],
|
|
345
|
-
|
|
346
|
-
packageJsonOrderExcludeKeys: ['contributes'],
|
|
223
|
+
esmImportTypeStyle: 'inline-last',
|
|
347
224
|
} satisfies Config & SortOptions;
|
|
348
225
|
```
|
|
349
|
-
|
|
350
|
-
### In a `.js` file
|
|
351
|
-
|
|
352
|
-
```js
|
|
353
|
-
/** @type {import('prettier').Config & import('prettier-plugin-sort').SortOptions} */
|
|
354
|
-
const config = {
|
|
355
|
-
plugins: ['prettier-plugin-sort'],
|
|
356
|
-
importOrderGroups: [
|
|
357
|
-
'builtin',
|
|
358
|
-
'external',
|
|
359
|
-
'internal',
|
|
360
|
-
'parent',
|
|
361
|
-
'sibling',
|
|
362
|
-
'index',
|
|
363
|
-
],
|
|
364
|
-
importOrderTypeImports: 'inline-last',
|
|
365
|
-
packageJsonOrderExcludeKeys: ['contributes'],
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
export default config;
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
The `ImportGroup` and `TypeImportsStyle` literal types are also exported if you only need those.
|
|
372
|
-
|
|
373
|
-
## Motivation
|
|
374
|
-
|
|
375
|
-
Before adopting Prettier, I relied on IDE-native sorting features to keep my code organized. As I started switching between different IDEs, I wanted a portable, unified configuration, so I brought ESLint and Prettier into my projects.
|
|
376
|
-
|
|
377
|
-
Prettier doesn't provide sorting out of the box. To sort imports I had to install `prettier-plugin-organize-imports`. To sort `package.json` I had to install `prettier-plugin-packagejson`. The fragmented experience bothered me.
|
|
378
|
-
|
|
379
|
-
I ignored this for a long time while focusing on actual development. Recently, though, I needed to control how `import type` was inlined and found that `prettier-plugin-organize-imports` didn't support it. On top of that, `prettier-plugin-packagejson`, built on `sort-package-json`, carries many dependencies that are redundant for a plugin. That's when I decided to build my own.
|
|
380
|
-
|
|
381
|
-
`prettier-plugin-sort` isn't meant to replace anything. It's about giving developers more options. Prettier is used almost entirely for JS/TS code, and every JS project has a `package.json`, so the plugin covers these two fundamental sorting tasks. The goal is to let JS developers work out of the box with minimal mental overhead (support for `tsconfig.json` sorting may be added in a future release). If you have other sorting needs, you can still install something like `prettier-plugin-css-order` alongside it. There's no conflict.
|
|
382
|
-
|
|
383
|
-
## Credits
|
|
384
|
-
|
|
385
|
-
- `eslint-plugin-import`: https://github.com/import-js/eslint-plugin-import
|
|
386
|
-
- `typescript-eslint`: https://github.com/typescript-eslint/typescript-eslint
|
|
387
|
-
- `sort-package-json`: https://github.com/keithamus/sort-package-json
|