I came across this GitHub issue, which helped me resolve the problem.
phongpt156 posted:
"importOrder": ["^[./].*(?<!\\.(c|le|sc)ss)$", "\\.(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 OverflowI came across this GitHub issue, which helped me resolve the problem.
phongpt156 posted:
"importOrder": ["^[./].*(?<!\\.(c|le|sc)ss)$", "\\.(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
"^./$)", // not .css or .scss file
".*\\.(css|scss)$" // .css and .scss file
» npm install @trivago/prettier-plugin-sort-imports
» npm install @ianvs/prettier-plugin-sort-imports
You need to load the plugin on your Prettier config:
{
...
plugins: ['@trivago/prettier-plugin-sort-imports'],
}
For reference, see the Using Plugin section on the Prettier docs.
After installing the plugin, I had to uninstall the Prettier extension from VS Code, restart VS Code and then reinstall the extension before the plugin would work.
For reference, I am using the following versions...
- prettier v3.2.5
- Prettier VS Code Exension v10.1.0
- @trivago/prettier-plugin-sort-imports v4.3.0