Prettier plugin sort imports
A prettier plugin to sort import declarations by provided RegEx order.
Input
Output
Install
npm
npm install --save-dev @trivago/prettier-plugin-sort-imports
or, using yarn
yarn add --dev @trivago/prettier-plugin-sort-imports
Usage
Add an order in prettier config file.
module.exports = {
"printWidth": 80,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"jsxBracketSameLine": true,
"semi": true,
"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderCaseInsensitive": true,
}
APIs
importOrder
A collection of regular expressions in string format. The plugin
uses new RegExp
to evaluate regular expression. E.g. node.source.value.match(new RegExp(val))
Here, val
is the string provided in import order.
importOrderCaseInsensitive
A boolean value to enable case-insensitivity in the sorting algorithm
used to order imports within each match group.
For example, when false (or not specified):
import ExampleView from './ExampleView';
import ExamplesList from './ExamplesList';
compared with "importOrderCaseInsensitive": true
:
import ExamplesList from './ExamplesList';
import ExampleView from './ExampleView';
importOrderSeparation
A boolean value to enable or disable the new line separation
between sorted import declarations. The separation takes place according to importOrder
.
sortModules
A boolean value to enable or disable sorting of the imports module declarations.
It is disabled by default
experimentalBabelParserPluginsList
A collection of parser names for babel parser. The plugin passes this list to babel parser so it can understand the syntaxes used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, you can use this field to enforce the usage of the plugins babel needs.
Since prettier options are limited to strings, you can pass plugins with options as a JSON string of the plugin array: "[\"plugin-name\", { \"pluginOption\": true }]"
.
module.exports = {
"printWidth": 80,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"jsxBracketSameLine": true,
"semi": true,
"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderCaseInsensitive": true,
"experimentalBabelParserPluginsList" : ["jsx", "typescript", "[\"decorators\", { \"decoratorsBeforeExport\": true }]"]
}
How does import sort work ?
The plugin extracts the imports which are defined in importOrder
.
These imports are local imports. The imports which are not part of the
importOrder
is considered as 3rd party imports.
After, the plugin sorts the local imports and 3rd party imports using
natural sort algorithm,
with case-insensitivity specified by importOrderCaseInsensitive
(or false by default).
In the end, the plugin returns final imports with 3rd party imports on top and
local imports at the end.
FAQ / Troubleshooting
Q. How can I add the RegEx imports in the importOrder
list ?
You can define the RegEx in the importOrder
. For
example if you want to sort the following imports:
import React from 'react';
import classnames from 'classnames';
import z from '@server/z';
import a from '@server/a';
import s from './';
import p from '@ui/p';
import q from '@ui/q';
then the importOrder
would be ["^@ui/(.*)$","^@server/(.*)$", '^[./]']
.
Now, the final output would be as follows:
import classnames from 'classnames';
import React from 'react';
import p from '@ui/p';
import q from '@ui/q';
import a from '@server/a';
import z from '@server/z';
import s from './';
Q. How can I run examples in this project ?
There is a examples directory. The examples file can be formatted by using
npm run example
command.
npm run example examples/example.tsx
Q. How to use the plugin with *.d.ts
files ?
The plugin automatically ignores the *.d.ts
files. We encourage you to declare the *.d.ts
files in .prettierignore
. Read more here.
Q. How does the plugin handle the first comment in the file.
The plugin keeps the first comment as it is in the file. The plugin also removes the new lines in between first comment and the first import.
input:
import a from 'a'
output:
import a from 'a'
Q. I'm getting error about experimental syntax.
If you are using some experimental syntax and the plugin has trouble parsing your files, you might getting errors similar to this:
SyntaxError: This experimental syntax requires enabling one of the following parser plugin(s): ...
To solve this issue, you can use the new option experimentalBabelParserPluginsList
in your .prettierrc
and pass an array of plugin names to be used.
Compatibility
Framework | Supported | Note |
---|
JS with ES Modules | ✅ Everything | - |
NodeJS with ES Modules | ✅ Everything | - |
React | ✅ Everything | - |
Angular | 🔜 Experimental features are not supported | To be supported in v3.x.x, coming soon 😉 |
Vue | ❌ Not supported | Any contribution is welcome. |
Svelte | ❌ Not supported | Any contribution is welcome. |
Contribution
For more information regarding contribution, please check the CONTRIBUTING.
Disclaimer
This plugin modifies the AST which is against the rules of prettier.
Maintainers