numtide / treefmt-nix

treefmt nix configuration
https://numtide.github.io/treefmt/
MIT License
240 stars 75 forks source link

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

Open srid opened 1 year ago

srid commented 1 year ago

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
gkze commented 1 year ago

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

Mic92 commented 1 year ago

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 commented 1 year ago

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

gkze commented 1 year ago

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

multivac61 commented 4 months ago

@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

jukremer commented 2 months ago

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?

Mic92 commented 2 months ago

@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.

gkze commented 2 months ago

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

jukremer commented 2 months ago

Does pluginSearchDirs has any effect at all?

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

Toby222 commented 2 months ago

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.