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 './';
You can define the <THIRD_PARTY_MODULES>
special word in the importOrder
. For example above, the importOrder
would be like ["^@ui/(.*)$", "^@server/(.*)$", "<THIRD_PARTY_MODULES>", '^[./]']
.
Now, the final output would be as follows:
import p from '@ui/p';
import q from '@ui/q';
import a from '@server/a';
import z from '@server/z';
import classnames from 'classnames';
import React from 'react';
import s from './';
The plugin automatically ignores the *.d.ts
files. We encourage you to declare the *.d.ts
files in .prettierignore
. Read more here.
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:
// comment
import a from 'a';
output:
// comment
import a from 'a';
If you are using some experimental syntax and the plugin has trouble parsing your files, you might get 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 importOrderParserPlugins
in your .prettierrc
(prettier config) and pass
an array of plugin names to be used.
Q. Why the plugin does not work with pnpm ? or Why do I see the [warn] Ignored unknown option
?
Due to the package handling of the pnpm, sometimes, the plugin is not automatically loaded. You can load the plugin via prettier config.
module.exports = {
plugins: [require('@ianvs/prettier-plugin-sort-imports')],
};
Be sure that you have @vue/compiler-sfc
installed in your project, (run npm ls @vue/compiler-sfc
to double-check). If it's not there, install it and try formatting again.