treefmt-nix icon indicating copy to clipboard operation
treefmt-nix copied to clipboard

How to use `prettier`'s `plugins` option?

Open srid opened this issue 2 years ago • 14 comments

Follow-up to #103

When using the prettier formatter, how do we actually use the plugins option?

With

programs.prettier.settings = {
  plugins = [ "prettier-plugin-tailwindcss" ];
};

(To use https://tailwindcss.com/blog/automatic-class-sorting-with-prettier)

I get:

[INFO]: Error using formatter #prettier:
• [STDOUT]:

• [STDERR]:
[error] Cannot find package 'prettier-plugin-tailwindcss' imported from /Users/srid/code/nix-browser/noop.js

srid avatar Aug 15 '23 17:08 srid

You'd need to install the NPM module yourself. Right now accessing/managing NPM modules from Nix/Nixpkgs isn't straightforward

gkze avatar Aug 15 '23 21:08 gkze

Does pluginSearchDirs has any effect at all? I couldn't find it in the upstream documentations. I packaged the plugin with npm2nix but it's not found by prettier:

    treefmt.programs.prettier.settings.plugins = [ "prettier-plugin-tailwindcss" ];
    treefmt.programs.prettier.settings.pluginSearchDirs = [
      "${self'.packages.prettier-plugin-tailwindcss}/lib"
    ];

Mic92 avatar Aug 24 '23 11:08 Mic92

I got it working by specifying an absolute plugin path: https://git.clan.lol/clan/clan-core/src/commit/dbc0ae08c09a20b3921c663bee3356ff9d5ad7eb/formatter.nix#L15

Mic92 avatar Aug 24 '23 12:08 Mic92

Yeah it' not pretty (no pun intended lol) but that works... hopefully once NPM <=> Nix improves it will be more straightforward

gkze avatar Sep 13 '23 00:09 gkze

@Mic92 Is this still the recommended way? Looks like the absolutely plugin path hack has been removed from HEAD: https://git.clan.lol/clan/clan-core/src/branch/main/formatter.nix

multivac61 avatar May 22 '24 14:05 multivac61

What is currently the best method to use a plugin that's in nixpkgs?

prettier = {
  enable = true;
  settings = {
    plugins = [ "${pkgs.prettier-plugin-go-template}" ];
    overrides = {
      files = [ "*.html" ];
      options.parser = "go-template";
    };
  };
};

Would this work? Do I need to specify the path further? Do I have to add the package separately?

jukremer avatar Jul 18 '24 08:07 jukremer

@Mic92 Is this still the recommended way? Looks like the absolutely plugin path hack has been removed from HEAD: https://git.clan.lol/clan/clan-core/src/branch/main/formatter.nix

I don't actually don't know, we currently don't need prettier.

Mic92 avatar Jul 18 '24 09:07 Mic92

When I got this working I installed the plugins via the devShell's packages and specified their names in the prettier config

gkze avatar Jul 18 '24 17:07 gkze

Does pluginSearchDirs has any effect at all?

Prettier removed the pluginSearchDirs option so that is probably why it's not working.

jukremer avatar Jul 20 '24 08:07 jukremer

How do you set plugin options? e.g. @prettier/plugin-xml has the option xmlWhitespaceSensitivity, but you can't set it in settings because it does not exist.

Toby222 avatar Jul 26 '24 11:07 Toby222

Finally got it working, had to update the plugin which is in nixpkgs.

{ pkgs, ... }:
{
  projectRootFile = "flake.nix";
  programs = {
    nixfmt.enable = true;
    gofmt.enable = true;
    taplo.enable = true;
    prettier = {
      enable = true;
      settings = {
        plugins = [
          "${pkgs.prettier-plugin-go-template}/lib/node_modules/prettier-plugin-go-template/lib/index.js"
        ];
        overrides = [
          {
            files = [ "*.html" ];
            options.parser = "go-template";
          }
        ];
      };
    };
  };
  settings.global.excludes = [
    "public/**"
    "static/**"
  ];
}

jukremer avatar Oct 17 '24 15:10 jukremer

@jukremer interestingly your approach did not work for me with prettier-plugin-sort-imports; which in fact has broken prettier on 24.11 ; I get an error like:

[error] Cannot find package '@trivago/prettier-plugin-sort-imports' imported from .../noop.js

no matter how I configure the plugin.

(I have:

  programs.prettier.enable = true;
  settings.formatter.prettier = {
    plugins = [
      "${prettier-plugin-sort-imports}/lib/node_modules/@trivago/prettier-plugin-sort-imports/lib/src/index.js"
    ];
  ...
```)

silky avatar Dec 19 '24 12:12 silky

I think this approach only works for packages that are in nixpkgs.

jukremer avatar Dec 29 '24 09:12 jukremer

@jukremer why? I defined the package myself above; so it should be no different.

silky avatar Dec 29 '24 17:12 silky