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

docs: add detailed guide for shared configs #16495

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

Lehoczky
Copy link

Description

This PR moves the the sharing-configurations paragraph from the Configuration File page to its own subpage and guides readers through the creation of a shared config in more details.

Motivation

After looking for a way to add plugins to a shared config, I found #15667 and I wanted to add this information to the docs, because plugins are getting used more and more with for example prettier-plugin-tailwindcss and prettier-plugin-svelte.

Why a new page?

I believe adding this information to the existing "Configuration File" page would just clutter up that page, so I extracted it to a new one. This is also how the ESLint's documentation is organized: there is a Configuration Files and a Share Configurations page.

Detailed explanation for creating a shared config

The existing docs very briefly says how to create a shared config, it is mostly about how to use them:

Sharing a Prettier configuration is simple: just publish a module that exports a configuration object, say @company/prettier-config, and reference it in your package.json

This can be confusing for people who never created an npm package before, and can also make them feel dumb by not knowing how to "just publish a module that exports a configuration object".

Now that the shared configuration section had its own page, I wanted to make it more friendly for newcomers and write a more detailed guide for sharing a configuration. The structure was also inspired by ESLint, where there are these sections for the Share Configurations:

  • Creating a Shareable Config
  • Publishing a Shareable Config
  • Using a Shareable Config

Notes

I wanted the docs to be detailed, but not going into too deep about how npm packages work. This is why there are multiple links to the npm docs. I hope I found a good balance between detailed steps for the reader, and linking to other docs when it is needed.

Of course I'm not a native speaker, so there might be mistakes in the text.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

"name": "@username/prettier-config",
"version": "1.0.0",
"description": "My personal Prettier config",
"main": "index.mjs", // <-- change index.json to index.mjs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use exports here.

Copy link
Author

@Lehoczky Lehoczky Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though main works as well, I'm happy to change it to a modern alternative! What do you think about updating it everywhere in the examples, even for the json configuration?

Otherwise only changing it for the JavaScript example might confuse people.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this might still be confusing, because the provided example form Alexander uses main too:

https://github.com/kachkaev/routine-npm-packages/blob/bc3e658f88c0b41beb118c7a1b9b91ec647f8478/packages/prettier-config/package.json#L16

Copy link
Author

@Lehoczky Lehoczky Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in fd8d1b5

@Lehoczky Lehoczky requested a review from fisker July 26, 2024 08:40

## Other examples

### Sharing a JavaScript Configuration File
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just one ESM version?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to rewrite the first part of the guide to ESM instead of a json export? Sounds good to me!

I will create an example repository then, because https://github.com/azz/prettier-config is using a json file. And the new example will also use the exports field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will create an example repository then, because azz/prettier-config is using a json file. And the new example will also use the exports field.

@azz Will you accept PR change your example to ESM?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍 Now the example uses an ESM file, and I've also added the exports and type fields to the example package.json.

const config = {
singleQuote: true,
plugins: ["prettier-plugin-xml"],
};
Copy link
Member

@fisker fisker Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommended way to use plugin should be

import * as prettierPluginXml ...

const config = {
  singleQuote: true,
  plugins: [prettierPluginXml],
};

But vscode-prettier cannot work since they transfer config to worker.

My suggestion is to use an absolute path/url instead, but import.meta.resolve is under --experimental-import-meta-resolve flag. Not sure what's the best way here.

Copy link
Author

@Lehoczky Lehoczky Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately import.meta.resolve by itself is not under a flag, only if someone wants to its second parameter:

image

My issue is now that I've got an error from the vscode-prettier extension that it cannot find the module @prettier/plugin-xml/src/plugin.js. I'm gonna open a separate issue there.

What do you think about merging this PR without this section about sharing configurations with plugins? It could be added later in a follow up PR easily.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna open a separate issue there.

The issue is here in Prettier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fortunately import.meta.resolve by itself is not under a flag,

We still support Node.js v14.

Copy link
Member

@fisker fisker Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about merging this PR without this section about sharing configurations with plugins? It could be added later in a follow up PR easily.

The example should work for everyone, give me a while to think about it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! Take as much time as needed, I'm gonna update the PR when necessary^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for leaving this for such a long time. Let's merge with current example.

@fisker
Copy link
Member

fisker commented Sep 29, 2024

Can you fix the spellcheck?

@Lehoczky
Copy link
Author

Lehoczky commented Oct 1, 2024

Can you fix the spellcheck?

✅ lint command is passing now^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants