Skip to content
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

Closed
ducqhl opened this issue Mar 15, 2022 · 4 comments
Labels
feature request A new feature request

Comments

@ducqhl
Copy link

ducqhl commented Mar 15, 2022

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 ducqhl added the feature request A new feature request label Mar 15, 2022
@blutorange
Copy link
Contributor

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.

@8o8inCodes
Copy link

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

@ketansp
Copy link

ketansp commented May 28, 2023

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

@Max-Starling
Copy link

Max-Starling commented Oct 12, 2023

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

@ducqhl ducqhl closed this as completed Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A new feature request
Projects
None yet
Development

No branches or pull requests

5 participants