You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Closes#188
This adds a new option, `importOrderSafeSideEffects`, which is an array
of regex patterns (similar to `importOrder`) specifying side-effect-only
imports which are considered "safe" to reorder along with the rest of
imports.
Copy file name to clipboardExpand all lines: README.md
+14-1Lines changed: 14 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ A prettier plugin to sort import declarations by provided Regular Expression ord
4
4
5
5
This project is based on [@trivago/prettier-plugin-sort-imports](https://github.com/trivago/prettier-plugin-sort-imports), but adds additional features:
6
6
7
-
- Does not re-order across side-effect imports
7
+
- Does not re-order across side-effect imports by default
8
8
- Combines imports from the same source
9
9
- Combines type and value imports (if `importOrderTypeScriptVersion` is set to `"4.5.0"` or higher)
10
10
- Groups type imports with `<TYPES>` keyword
@@ -33,6 +33,7 @@ This project is based on [@trivago/prettier-plugin-sort-imports](https://github.
33
33
-[5. Group aliases with local imports](#5-group-aliases-with-local-imports)
34
34
-[6. Enforce a blank line after top of file comments](#6-enforce-a-blank-line-after-top-of-file-comments)
35
35
-[7. Enable/disable plugin or use different order in certain folders or files](#7-enabledisable-plugin-or-use-different-order-in-certain-folders-or-files)
@@ -360,6 +361,18 @@ This can also be beneficial for large projects wishing to gradually adopt a sort
360
361
361
362
You can also do this in reverse, where the plugin is enabled globally, but disabled for a set of files or directories in the overrides configuration. It is also useful for setting a different sort order to use in certain files or directories instead of the global sort order.
362
363
364
+
#### `importOrderSafeSideEffects`
365
+
366
+
**type**: `Array<string>`
367
+
368
+
**default value:**`[]`
369
+
370
+
In general, it is not safe to reorder imports that do not actually import anything (side-effect-only imports), because these imports are affecting the global scope, the order in which they occur can be important.
371
+
372
+
However, in some cases, you may know that some of your side-effect imports can be sorted along with normal imports. For example, `import "server-only"` can be used in some React applications to ensure some code only runs on the server. For these cases, this option is an escape hatch.
373
+
374
+
This option accepts an array of regex patterns which will be compared against side-effect-only imports to determine if they are safe to reorder along with the rest of your imports. By default, no such imports are considered safe. You can opt-in to sorting them by adding them to this option. We recommend using `^` at the start and `$` at the end of your pattern, to be sure they match exactly.
0 commit comments