0
# Configuration Options
1
2
Comprehensive configuration options for controlling import sorting behavior through Prettier configuration files.
3
4
## Import Order Configuration
5
6
### importOrder
7
8
Defines the order of import groups using Regular Expression patterns.
9
10
```typescript { .api }
11
importOrder: string[]
12
```
13
14
**Default**: `[]` (empty array)
15
16
Array of Regular Expression patterns that define import group ordering. The plugin moves third-party imports to the top by default unless explicitly positioned using `<THIRD_PARTY_MODULES>`.
17
18
#### Basic Usage
19
20
```javascript
21
{
22
"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"]
23
}
24
```
25
26
#### Third Party Module Positioning
27
28
Use `<THIRD_PARTY_MODULES>` to explicitly position third-party imports:
29
30
```javascript
31
{
32
"importOrder": [
33
"^@core/(.*)$",
34
"<THIRD_PARTY_MODULES>",
35
"^@server/(.*)$",
36
"^@ui/(.*)$",
37
"^[./]"
38
]
39
}
40
```
41
42
#### Type Import Positioning
43
44
Use special keywords for TypeScript type imports:
45
46
- `<THIRD_PARTY_TS_TYPES>`: Third-party type imports
47
- `<TS_TYPES>`: Local type imports
48
49
```javascript
50
{
51
"importOrder": [
52
"<THIRD_PARTY_TS_TYPES>",
53
"<THIRD_PARTY_MODULES>",
54
"<TS_TYPES>",
55
"^[./]"
56
]
57
}
58
```
59
60
### importOrderSeparation
61
62
Controls whether import groups are separated by blank lines.
63
64
```typescript { .api }
65
importOrderSeparation?: boolean
66
```
67
68
**Default**: `false`
69
70
When `true`, adds blank lines between import groups defined by `importOrder` patterns.
71
72
#### Example
73
74
```javascript
75
// Configuration
76
{
77
"importOrder": ["^@core/(.*)$", "^@ui/(.*)$", "^[./]"],
78
"importOrderSeparation": true
79
}
80
81
// Result
82
import { logger } from '@core/logger';
83
84
import { Alert } from '@ui/Alert';
85
86
import { utils } from './utils';
87
```
88
89
### importOrderCaseInsensitive
90
91
Enables case-insensitive sorting within import groups.
92
93
```typescript { .api }
94
importOrderCaseInsensitive?: boolean
95
```
96
97
**Default**: `false`
98
99
When `true`, sorts imports within each group case-insensitively.
100
101
#### Example
102
103
```javascript
104
// With importOrderCaseInsensitive: false
105
import ExampleView from './ExampleView';
106
import ExamplesList from './ExamplesList';
107
108
// With importOrderCaseInsensitive: true
109
import ExamplesList from './ExamplesList';
110
import ExampleView from './ExampleView';
111
```
112
113
## Specifier Configuration
114
115
### importOrderSortSpecifiers
116
117
Controls sorting of named imports within individual import statements.
118
119
```typescript { .api }
120
importOrderSortSpecifiers?: boolean
121
```
122
123
**Default**: `false`
124
125
When `true`, sorts named imports alphabetically within each import statement.
126
127
#### Example
128
129
```javascript
130
// Input
131
import { useEffect, useState, useCallback } from 'react';
132
133
// With importOrderSortSpecifiers: true
134
import { useCallback, useEffect, useState } from 'react';
135
```
136
137
### importOrderGroupNamespaceSpecifiers
138
139
Controls positioning of namespace imports within their group.
140
141
```typescript { .api }
142
importOrderGroupNamespaceSpecifiers?: boolean
143
```
144
145
**Default**: `false`
146
147
When `true`, groups namespace specifiers (import * as name) at the top of their import group.
148
149
#### Example
150
151
```javascript
152
// Input
153
import { Component } from 'react';
154
import * as React from 'react';
155
import { render } from 'react-dom';
156
157
// With importOrderGroupNamespaceSpecifiers: true
158
import * as React from 'react';
159
import { Component } from 'react';
160
import { render } from 'react-dom';
161
```
162
163
## Parser Configuration
164
165
### importOrderParserPlugins
166
167
Specifies Babel parser plugins for handling special syntax.
168
169
```typescript { .api }
170
importOrderParserPlugins?: ImportOrderParserPlugin[]
171
172
type ImportOrderParserPlugin =
173
| Extract<ParserPlugin, string>
174
| `[${string},${string}]`
175
```
176
177
**Default**: `["typescript", "jsx"]`
178
179
Array of Babel parser plugins to enable parsing of specific syntax features. Can include plugin options as JSON strings.
180
181
#### Basic Plugins
182
183
```javascript
184
{
185
"importOrderParserPlugins": ["typescript", "jsx", "classProperties"]
186
}
187
```
188
189
#### Plugins with Options
190
191
```javascript
192
{
193
"importOrderParserPlugins": [
194
"typescript",
195
"jsx",
196
"[\"decorators\", { \"decoratorsBeforeExport\": true }]"
197
]
198
}
199
```
200
201
#### Disable Default Plugins
202
203
```javascript
204
{
205
"importOrderParserPlugins": []
206
}
207
```
208
209
## Advanced Configuration
210
211
### importOrderSideEffects
212
213
Controls handling of side-effect imports (imports without specifiers).
214
215
```typescript { .api }
216
importOrderSideEffects?: boolean
217
```
218
219
**Default**: `true`
220
221
When `true`, includes side-effect imports in sorting process. When `false`, leaves side-effect imports in their original positions.
222
223
#### Example
224
225
```javascript
226
// Side-effect imports
227
import 'polyfills';
228
import './styles.css';
229
import { Component } from 'react';
230
```
231
232
### importOrderImportAttributesKeyword
233
234
Specifies the import attributes/assertions syntax style.
235
236
```typescript { .api }
237
importOrderImportAttributesKeyword?: 'assert' | 'with' | 'with-legacy'
238
```
239
240
**Default**: `'with'`
241
242
Controls the syntax for import attributes:
243
244
- `'with'`: `import data from './data.json' with { type: 'json' }`
245
- `'assert'`: `import data from './data.json' assert { type: 'json' }`
246
- `'with-legacy'`: `import data from './data.json' with type: 'json'`
247
248
When unspecified, Babel generator matches the input code style.
249
250
## Complete Configuration Example
251
252
```javascript
253
// prettier.config.js
254
module.exports = {
255
// Standard Prettier options
256
printWidth: 80,
257
tabWidth: 2,
258
semi: true,
259
singleQuote: true,
260
261
// Plugin configuration
262
plugins: ["@trivago/prettier-plugin-sort-imports"],
263
264
// Import sorting configuration
265
importOrder: [
266
"^@core/(.*)$",
267
"<THIRD_PARTY_MODULES>",
268
"^@server/(.*)$",
269
"^@ui/(.*)$",
270
"^[./]"
271
],
272
importOrderSeparation: true,
273
importOrderSortSpecifiers: true,
274
importOrderCaseInsensitive: false,
275
importOrderGroupNamespaceSpecifiers: true,
276
importOrderSideEffects: true,
277
importOrderParserPlugins: ["typescript", "jsx", "classProperties"],
278
importOrderImportAttributesKeyword: "with"
279
};
280
```