Skip to content

Commit 89894c1

Browse files
jasikparkIanVS
andauthored
Add note for how to specify subpath import regex to README (#243)
Figured this should go with the existing local alias examples, it's a simpler approach, and presumably the new state-of-art for local aliases, so maybe it should go higher in the examples? Followup to #242 --------- Co-authored-by: Ian VanSchooten <[email protected]>
1 parent 43c7e29 commit 89894c1

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,29 @@ import { createBrowserHistory } from 'history';
297297
import App from './App';
298298
```
299299

300-
##### 5. Group aliases with local imports
300+
##### 5. Group subpath import style local aliases
301301

302-
If you define non-relative aliases to refer to local files without long chains of `"../../../"`, you can include those aliases in your `importOrder` to keep them grouped with your local code.
302+
If you are using [subpath imports](https://nodejs.org/api/packages.html#subpath-imports) (local imports starting with "#"), you can include those in your `importOrder` to keep them grouped with your local code, perhaps just above your relative imports, as shown below.
303+
304+
```json
305+
"importOrder": [
306+
"<THIRD_PARTY_MODULES>",
307+
"^#.+",
308+
"^[.]"]
309+
```
310+
311+
e.g.:
312+
313+
```ts
314+
import { debounce, reduce } from 'lodash';
315+
import { Users } from '#api';
316+
import icon from '#assets/icon';
317+
import App from './App';
318+
```
319+
320+
##### 6. Group aliases with local imports
321+
322+
If you use some other method to define non-relative aliases to refer to local files without long chains of `"../../../"`, you can include those aliases in your `importOrder` to keep them grouped with your local code. If you use the common `@` symbol for these aliases, you may want some way to group them separately from scoped npm packages, which can be done like this:
303323

304324
```json
305325
"importOrder": [
@@ -317,7 +337,7 @@ import icon from '@assets/icon';
317337
import App from './App';
318338
```
319339

320-
##### 6. Enforce a blank line after top of file comments
340+
##### 7. Enforce a blank line after top of file comments
321341

322342
If you have pragma-comments at the top of file, or you have boilerplate copyright announcements, you may be interested in separating that content from your code imports, you can add that separator first.
323343

@@ -341,7 +361,7 @@ import icon from '@assets/icon';
341361
import App from './App';
342362
```
343363

344-
##### 7. Enable/disable plugin or use different order in certain folders or files
364+
##### 8. Enable/disable plugin or use different order in certain folders or files
345365

346366
If you'd like to sort the imports only in a specific set of files or directories, you can disable the plugin by setting `importOrder` to an empty array, and then use Prettier's [Configuration Overrides](https://prettier.io/docs/en/configuration#configuration-overrides) to set the order for files matching a glob pattern.
347367

0 commit comments

Comments
 (0)