|
2 | 2 |
|
3 | 3 | This [import-sort](https://github.com/renke/import-sort) is based on [import-sort-style-module](https://github.com/renke/import-sort/tree/master/packages/import-sort-style-module). |
4 | 4 |
|
5 | | -```js |
6 | | -// Absolute modules with side effects (not sorted because order may matter) |
7 | | -import 'a'; |
8 | | -import 'c'; |
9 | | -import 'b'; |
10 | | - |
11 | | -// Relative modules with side effects (not sorted because order may matter) |
12 | | -import './a'; |
13 | | -import './c'; |
14 | | -import './b'; |
15 | | - |
16 | | -// Third-party modules sorted by name |
17 | | -import aa from 'aa'; |
18 | | -import bb from 'bb'; |
19 | | -import cc from 'cc'; |
20 | | -// Modules from the Node.js "standard" library sorted by name |
21 | | -import { readFile, writeFile } from 'fs'; |
22 | | -import * as path from 'path'; |
23 | | -// First-party modules sorted by "relative depth" and then by name |
24 | | -import aaa from '../../aaa'; |
25 | | -import bbb from '../../bbb'; |
26 | | -import aaaa from '../aaaa'; |
27 | | -import bbbb from '../bbbb'; |
28 | | -import aaaaa from './aaaaa'; |
29 | | -import bbbbb from './bbbbb'; |
| 5 | +## Installation |
| 6 | + |
| 7 | +- Install with `npm i -D @leanix/import-sort-style` |
| 8 | +- If used with Prettier, install `npm i -D prettier-plugin-import-sort@0.0.3` for Prettier@v1 and `npm i -D prettier-plugin-import-sort` for Prettier@v2 |
| 9 | + |
| 10 | +## Configuration |
| 11 | + |
| 12 | +- Add a configuration like the following to your package.json to specify the parser (which may need to be installed separately) and the corresponding extensions: |
| 13 | + |
| 14 | +``` |
| 15 | +"importSort": { |
| 16 | + ".js, .ts": { |
| 17 | + "style": "@leanix/import-sort-style", |
| 18 | + "parser": "typescript", |
| 19 | + "options": { |
| 20 | + "prefixes": [ |
| 21 | + "@app", |
| 22 | + "@lib" |
| 23 | + ] |
| 24 | + } |
| 25 | + } |
| 26 | +} |
30 | 27 | ``` |
| 28 | +- The list of local prefixes/aliases is optional. |
| 29 | + |
| 30 | +## Sort order |
| 31 | + |
| 32 | +The order of imports is: |
| 33 | + |
| 34 | +1. Third-party modules with side effects are not sorted because order may matter, e.g. `import 'polyfills';` |
| 35 | +2. Local, absolute modules with side-effects are not sorted because order may matter, e.g. `import '@app/polyfills';` |
| 36 | +3. Local, relative modules with side effects are not sorted because order may matter, e.g. `import './polyfills';` |
| 37 | +4. Third-party modules are sorted by name, e.g. `import { endOfMonth } from 'date-fns';` |
| 38 | +5. Built-in Node.js modules are sorted by name, e.g. `import * as fs from 'fs';` |
| 39 | +6. Local, absolute modules are sorted by the prefix-order provided and then by name `import { LIMIT } from '@app/app.constants';` |
| 40 | +7. Local, relative modules are sorted by "relative depth" and then by name `import { LIMIT } from './app.constants';` |
| 41 | + |
| 42 | +See the [tests](./test/main.spec.ts) for more examples. |
0 commit comments