docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

feat: export CreateSitemapItemsOption type

Open johnnyreilly opened this issue 1 year ago • 2 comments

Pre-flight checklist

  • [x] I have read the Contributing Guidelines on pull requests.
  • [x] If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • [ ] If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

This is a follow on from https://github.com/facebook/docusaurus/pull/10083

I realised when plugging it into my blog that I hadn't exported the type.

It's possible to do some type gymnastics to get to it: https://github.com/johnnyreilly/blog.johnnyreilly.com/blob/e38e4615e8afb9d66a59e607de04f46ec927a96d/blog-website/createSitemapItems.mjs#L6C1-L10C49

But it would be nicer if it was just exposed. This PR exposes the type explicity.

Test Plan

I tried to import in docusaurus.config.ts and it worked!

image

Test links

Deploy preview: https://deploy-preview-_____--docusaurus-2.netlify.app/

Related issues/PRs

https://github.com/facebook/docusaurus/pull/10083

johnnyreilly avatar May 04 '24 18:05 johnnyreilly

[V2]

Built without sensitive environment variables

Name Link
Latest commit bf5d8858d341b6cdde1922730dabb929a13ca4fa
Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/66367cf1884dfb000840cbfd
Deploy Preview https://deploy-preview-10105--docusaurus-2.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar May 04 '24 18:05 netlify[bot]

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 77 🟢 98 🟢 96 🟢 100 🟠 88 Report
/docs/installation 🟠 58 🟢 96 🟢 100 🟢 100 🟠 88 Report
/docs/category/getting-started 🟠 76 🟢 100 🟢 100 🟢 90 🟠 88 Report
/blog 🟠 69 🟢 100 🟢 100 🟢 90 🟠 88 Report
/blog/preparing-your-site-for-docusaurus-v3 🟠 63 🟢 96 🟢 100 🟢 100 🟠 88 Report
/blog/tags/release 🟠 70 🟢 100 🟢 100 🟠 80 🟠 88 Report
/blog/tags 🟠 75 🟢 100 🟢 100 🟢 90 🟠 88 Report

github-actions[bot] avatar May 04 '24 18:05 github-actions[bot]

I'd prefer to not export it. Options are easily accessible and many other plugins to not export types. Once we export them, we must have a good name and maintain retrocompatibility.

The type gymnastic is not super hard, particularly when not using JSDoc but using plain TS

const createSitemapItems: Options['createSitemapItems'] = async (params) => {
  return params.defaultCreateSitemapItems(params);
};

If we expose this, then this means we'll also probably have users finding convenient to expose many other option types as well. And we end up with many exported types whose naming convention should be consistent, and for which we need to remain retrocompatible over time.

slorber avatar May 09 '24 15:05 slorber

It sounds like you've made up your mind already, but I thought it worth mentioning that not exporting the type doesn't align with the createFeedItems hook which does export the type, and so can be used like so: (a straightforward import - no gymnastics)

import('@docusaurus/plugin-content-blog').CreateFeedItemsFn

Example usage of exported type here: https://github.com/johnnyreilly/blog.johnnyreilly.com/blob/e38e4615e8afb9d66a59e607de04f46ec927a96d/blog-website/createFeedItems.mjs#L6

Given we exported the type for that (comparable) functionality, why differ this time?

See: https://github.com/facebook/docusaurus/blob/ab9a4e751f3dfd1c4cd5ab70688e4b482d11103b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts#L289

johnnyreilly avatar May 09 '24 15:05 johnnyreilly

We export many things from docs, blog and pages plugin. I'm not sure it's the right decision actually to export everything by default like we do. If we can avoid exporting more things in the future, I'd prefer to not export them (at least until we have a clear plan on how to handle exported types), particularly when there is an easy workaround to get the type.

Also those plugins have a .d.ts file so the public TS API surface is more explicit. If typedefs gets generated from implementation code and we just re-export types, it is more likely that a TS breaking change will sneak in without us noticing it.

slorber avatar May 09 '24 16:05 slorber

Should we then look at removing the CreateFeedItemsFn type export for Docusaurus 4? We'd then be being more consistent which is always nice from a consumers perspective.

I guess I'm a little confused as to what the rationale is for when types should and should not be exported. Is it documented somewhere?

johnnyreilly avatar May 09 '24 16:05 johnnyreilly

I don't know 🙈

I guess I'm a little confused as to what the rationale is for when types should and should not be exported. Is it documented somewhere?

I am too, the way we handle types atm is messy and not super consistent. Until we have a clear plan (which we haven't), I'd rather not export more types unless it's really necessary.

slorber avatar May 10 '24 08:05 slorber

Fair enough!

johnnyreilly avatar May 10 '24 11:05 johnnyreilly