What is @trivago/prettier-plugin-sort-imports?
@trivago/prettier-plugin-sort-imports is a Prettier plugin that automatically sorts your import statements in a consistent and customizable manner. It helps maintain a clean and organized codebase by ensuring that import statements are ordered according to specified rules.
What are @trivago/prettier-plugin-sort-imports's main functionalities?
Sort imports alphabetically
This feature sorts the import statements alphabetically by module name.
import React from 'react';
import { useState } from 'react';
import { render } from 'react-dom';
Group imports by type
This feature groups import statements by their type, such as external libraries, internal modules, and styles.
import React from 'react';
import { useState } from 'react';
import { render } from 'react-dom';
import './styles.css';
import utils from './utils';
Customizable sorting order
This feature allows you to define a custom sorting order for your import statements, such as placing React imports at the top, followed by other external libraries, and then internal modules.
import React from 'react';
import { useState } from 'react';
import { render } from 'react-dom';
import utils from './utils';
import './styles.css';
Other packages similar to @trivago/prettier-plugin-sort-imports
eslint-plugin-import
eslint-plugin-import is an ESLint plugin that helps validate proper imports and enforce import order. It provides a wide range of rules for managing import statements, including sorting them. Unlike @trivago/prettier-plugin-sort-imports, which is a Prettier plugin, eslint-plugin-import is used with ESLint.
import-sort
import-sort is a tool and library for sorting ES6 imports. It can be used as a standalone tool or integrated with various editors and build tools. It offers customizable sorting configurations similar to @trivago/prettier-plugin-sort-imports but is not tied to Prettier.
prettier-plugin-organize-imports
prettier-plugin-organize-imports is a Prettier plugin that uses the TypeScript language service to organize imports. It automatically removes unused imports and sorts the remaining ones. It is similar to @trivago/prettier-plugin-sort-imports but focuses on TypeScript projects.
Prettier plugin sort imports
A prettier plugin to sort import declarations by provided RegEx order.
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,
}
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.
importOrderSeparation
A boolean value to enable or disable the new line separation
between sorted import declarations. The separation takes place according to importOrder
.
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.
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
Maintainers