I came across this GitHub issue, which helped me resolve the problem.
phongpt156 posted:
"importOrder": ["^[./].*(?<!\\.(c|le|sc)ss)"]
The use of the negative lookahead solved it. Hopefully it helps someone
Final
// .prettierrc
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always",
"importOrder": ["^[./].*(?<!\\.(c|le|sc)ss)$", "\\.(c|le|sc)ss$"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
Answer from nop on Stack Overflow
» npm install @ianvs/prettier-plugin-sort-imports
Prettier @trivago/prettier-plugin-sort-imports fails to place .css imports at the bottom
prettier-plugin-sort-imports with prettier >=3
Need your opinion on the preferred way of sorting TS Imports
node.js - prettier-plugin-sort-imports ignores import order - Stack Overflow
I came across this GitHub issue, which helped me resolve the problem.
phongpt156 posted:
"importOrder": ["^[./].*(?<!\\.(c|le|sc)ss)"]
The use of the negative lookahead solved it. Hopefully it helps someone
Final
// .prettierrc
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "always",
"importOrder": ["^[./].*(?<!\\.(c|le|sc)ss)$", "\\.(c|le|sc)ss$"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
Improvement suggestions:
- Ensure that the escape characters in the regular expression are
correct, for example,
\. should be .
Adjust the order of the rules to ensure that .css and .scss files are excluded first and then matched.
When testing, you can use some regular expression testing tools (such as regex101) to ensure that the expression correctly matches the expected file path.
A correct case
Copy"^./$)", // not .css or .scss file
".*\\.(css|scss)$" // .css and .scss file
» npm install @trivago/prettier-plugin-sort-imports
Which of these options is the best for sorting imports? What cons and pros, maybe in your experience you rated them and can say what is better in terms of ease of use or configurations.
Prettier:
trivago/prettier-plugin-sort-imports
IanVS/prettier-plugin-sort-imports
ESLint:
build-in "sort-imports"
eslint-plugin-import (import/order)
eslint-plugin-simple-import-sort
EDIT: Now I stack on the question of what is better to use ESLINT or Prettier for imports sorting? It seems to me that sorting imports is more a specialization of the formatter and not of the linter?
» npm install @plasmohq/prettier-plugin-sort-imports