-
-
Notifications
You must be signed in to change notification settings - Fork 322
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
[altair-fastify-plugin] Add nodenext compatible typings and exports #2667
base: master
Are you sure you want to change the base?
[altair-fastify-plugin] Add nodenext compatible typings and exports #2667
Conversation
Reviewer's Guide by SourceryThis pull request introduces changes to the altair-fastify-plugin to add Node.js "nodenext" compatible typings and exports. The main goal is to make the plugin compatible with both CommonJS (CJS) and ECMAScript Modules (ESM) by implementing the Fastify "infamous triplet" pattern for module exports. Sequence DiagramsequenceDiagram
participant Consumer
participant Index.js
participant Dist
Consumer->>Index.js: Import module
Index.js->>Dist: Require './dist/index.js'
Dist-->>Index.js: Return default export
Index.js-->>Consumer: Provide CJS and ESM compatible exports
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @kingston - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This looks interesting. Happy to give this approach a try. One question though, why do we need to explicitly create the index.d.ts file instead of relying on dist/index.d.ts instead? |
I attempted to make src/index.ts compile to the appropriate Typescript typings but I couldn't find a clean way to accomplish it since the standard Fastify approach relies on namespace to augment the function declaration and the actual export in the Altair source code is a const from fp(...) which cannot be augmented by namespace. Therefore, it was significantly easier to write manually write the index.d.ts which just re-exports the exports from dist/index.d.ts in the correct format. This article goes into the trickiness in more detail: https://blog.andrewbran.ch/default-exports-in-commonjs-libraries/ (but the solution offered doesn't work since we are exporting a const instead of a root-level function even though the const is a function) |
Thanks for sharing that article! It was an interesting read. One of the points mentioned there though was that we could use the |
Ah it wasn't the transpiled Typescript code but the compiled types that were proving challenging so I'm not sure verbatimModuleSyntax would help. In the types, we're exporting a function and using namespace declaration to merge an additional interface into it. But in code, we're exporting a const from fp() which means we can't use namespace declaration to merge it so it can't compile to the same types. To avoid this normally, I export it as a dual CJS-ESM package using a tool like tsup (https://github.com/egoist/tsup?tab=readme-ov-file) but that's a larger change so didn't propose that option but it could work as well. |
Ah it wasn't the transpiled Typescript code but the compiled types that were proving challenging so I'm not sure verbatimModuleSyntax would help. In the types, we're exporting a function and using namespace declaration to merge an additional interface into it. But in code, we're exporting a const from fp() which means we can't use namespace declaration to merge it so it can't compile to the same types. To avoid this normally, I export it as a dual CJS-ESM package using a tool like tsup (https://github.com/egoist/tsup?tab=readme-ov-file) but that's a larger change so I didn't propose it initially. However, long-term that might be a nice option since it provides the right exports for each usage scenario. |
Right yeah it's the types. In a current PR, I'm replacing the default export with a named export, which should completely resolve this issue right? We really don't need to use default export for this, and if it's causing type confusion between systems, we might as well just get rid of it |
Yup that also works! It'd be a breaking change though so I assume it'd be a major release? |
Yeah I'll cut a major release for that change. It also updates to using fastify v5. |
We can mark this as closed now? |
Fixes #2666
Checks
yarn test-build
Changes proposed in this pull request:
This introduces a new index.js set of exports that are compatible with both CJS and ESM modules using the Fastify "infamous triplet" refactor for making exports support all the various import types. (fastify/fastify#4349)
I tried to do it with pure Typescript but I could not find a way to have Typescript compile into the necessary format so I created a separate set of index.js/index.d.ts which works when tested in both ESM and CJS environments.
Summary by Sourcery
Add nodenext compatible typings and exports to the altair-fastify-plugin to support both CommonJS and ESM modules, using a separate set of index.js and index.d.ts files.
New Features: