Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
62 changes: 37 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,41 @@

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).

```js
// Absolute modules with side effects (not sorted because order may matter)
import 'a';
import 'c';
import 'b';

// Relative modules with side effects (not sorted because order may matter)
import './a';
import './c';
import './b';

// Third-party modules sorted by name
import aa from 'aa';
import bb from 'bb';
import cc from 'cc';
// Modules from the Node.js "standard" library sorted by name
import { readFile, writeFile } from 'fs';
import * as path from 'path';
// First-party modules sorted by "relative depth" and then by name
import aaa from '../../aaa';
import bbb from '../../bbb';
import aaaa from '../aaaa';
import bbbb from '../bbbb';
import aaaaa from './aaaaa';
import bbbbb from './bbbbb';
## Installation

- Install with `npm i -D @leanix/import-sort-style`
- 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

## Configuration

- 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:

```
"importSort": {
".js, .ts": {
"style": "@leanix/import-sort-style",
"parser": "typescript",
"options": {
"prefixes": [
"@app",
"@lib"
]
}
}
}
```
- The list of local prefixes/aliases is optional.

## Sort order

The order of imports is:

1. Third-party modules with side effects are not sorted because order may matter, e.g. `import 'polyfills';`
2. Local, absolute modules with side-effects are not sorted because order may matter, e.g. `import '@app/polyfills';`
3. Local, relative modules with side effects are not sorted because order may matter, e.g. `import './polyfills';`
4. Third-party modules are sorted by name, e.g. `import { endOfMonth } from 'date-fns';`
5. Built-in Node.js modules are sorted by name, e.g. `import * as fs from 'fs';`
6. Local, absolute modules are sorted by the prefix-order provided and then by name `import { LIMIT } from '@app/app.constants';`
7. Local, relative modules are sorted by "relative depth" and then by name `import { LIMIT } from './app.constants';`

See the [tests](./test/main.spec.ts) for more examples.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Loading