-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some statements must be run before some imports but the prettier plugin puts them all in the end of the section. #143
Comments
Not sure if it's the best solution, but right now, you could perhaps extract that statement into a separate file and import it via a side-effect import: // setup-environment.js
dotenv.config();
// other-file.js
import "./setup-environment.js";
import {} from ...;
// more code Then if you use this fork https://www.npmjs.com/package/@ianvs/prettier-plugin-sort-imports which include the PR (#111) I made for not sorting side-effect imports, it should stay at the top. |
I've stumbled upon this issue and it's very annoying.. |
I have a temporary work-around like this. Do mind the line at the end. Basically prettier ignore is applied on an empty line here. Import statements after that do get sorted properly.
|
After several attempts I've found perfect solution for me without any additional dependencies (less packages - less problems in the future with I wraped "pre-import" logic into modules (as suggested above): // init-common.ts
import moduleAlias from "module-alias";
moduleAlias.addAliases({
"@common": `${__dirname}/../../../common`
});
import initCommonLibrary from "@common/register";
initCommonLibrary(); // init-dotenv.ts
import dotenv from "dotenv";
import path from "path";
dotenv.config({
path: path.resolve(__dirname, `../../.${process.env.NODE_ENV}.env`)
}); Then I added them to // .prettierrc.json
"importOrder": [
"init-(dotenv|common)$",
"^(@[^common][a-z]+|[a-z]+)",
"^[a-z]+",
"^@common/(.*)$",
"^[./]"
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["@trivago/prettier-plugin-sort-imports"] And this is what I get after running // result.ts
import "./src/modules/init-common";
import "./src/modules/init-dotenv";
import * as sentry from "@sentry/node";
import express from "express";
import { ContentType } from "@common/enums/http.enums";
import loggerMiddleware from "./src/middleware/logger.middleware";
import swagger from "./swagger"; |
Is your feature request related to a problem?
I used a library name dotenv and it required to run before any custom defined module in my project, but the sorter bring the line
dotenv.config()
to the end of the import section.Describe the solution you'd like
I wish to have some disable sort comment like lint rules
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: