CodeMod to sort ES6 imports by type
Use this codemod to sort ES6 imports by type in this order:
- internal Node.js modules before
- external module imports before
- local imports from parent folders before
- local imports from sibling folders.
Imports of the same type are sorted alphabetically.
Install
yarn global add codemod-imports-sort
Use
codemod-imports-sort path/to/file.js
Example
Before:
;;;;;;;;;
After:
;;;;;;;;;
Options
--sortConfig FILE.json
Optionally you can pass the path to a JSON file with a config to define the desired order of imports. The config should resemble the config for the import/order
plugin of eslint. groups
must be an array of string
or [string
]. The only allowed string
s are: "builtin"
, "external"
, "scoped-external"
, "internal"
, "parent"
, "sibling"
, "index"
.
For example to define to sort index imports first, then internal and external modules in a alphabetically sorted group and then sibling, parent and builtin modules together in a group, use this configuration:
Omitted types are implicitly grouped together as the last element
Built with jscodeshift.