prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

Some statements must be run before some imports but the prettier plugin puts them all in the end of the section.

Open ducqhl opened this issue 2 years ago • 1 comments

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

// prettier-ignore
dotenv.config();

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.

ducqhl avatar Mar 15 '22 15:03 ducqhl

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.

blutorange avatar Mar 16 '22 08:03 blutorange

I've stumbled upon this issue and it's very annoying..

8o8inCodes avatar Nov 12 '22 18:11 8o8inCodes

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.

import * as dotenv from 'dotenv';
dotenv.config();
// prettier-ignore

ketansp avatar May 28 '23 16:05 ketansp

After several attempts I've found perfect solution for me without any additional dependencies (less packages - less problems in the future with package-lock.json and legacy dependencies) and prettier-ignore (that ignores not only imports but other things like commas, quotes, spacing, etc).

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 importOrder as "init-(dotenv|common)$" (more flexible variants like init-(.*) also should work):

// .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 prettier . --write

// 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";

Max-Starling avatar Oct 12 '23 22:10 Max-Starling