-
Notifications
You must be signed in to change notification settings - Fork 237
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
Output both CJS & ESM #380
Conversation
Thanks for your effort here @acidoxee
We're not looking to make a breaking change on this library currently and are happy with the current status of it, so apologies - we wont be proceeding with this PR. Happy to consider a much simpler PR that adds ESM support and does not require a major |
My apologies for assuming you'd be OK with a breaking change, I did not ask prior to writing the PR as I should have. Although to reassure you, the breaking change I'm suggesting is actually a tiny fraction of the PR (quite literally 3 lines of code removed), since the only reason for it was to remove the "default" export of the lib, which is that factory: Lines 8 to 10 in 6b89c6b
Since the - const jwksClient = require('jwks-rsa');
+ const { JwksClient } = require('jwks-rsa');
- const client = jwksClient({
+ const client = new JwksClient({
jwksUri: 'https://sandrino.auth0.com/.well-known/jwks.json',
requestHeaders: {}, // Optional
timeout: 30000 // Defaults to 30s
}); That seemed quite acceptable to me since this small change made the setup of dual CJS/ESM simpler, but I'm sorry I've assumed that. To be honest I'm not quite sure why that factory exists in the first place, but that's none of my business if you want to keep it that way. Would you be open to keeping the PR and going further if I managed to remove the breaking change? |
Hi @acidoxee - sure ok, could you remove the breaking change We have a history of accepting PRs that attempt to add ESM support and end up breaking in various bundlers that we haven't checked, so you might have to be patient while I find the time to test this out on some |
Hey @acidoxee , is there any contribute I can make to get the ball rolling on this again? |
Hi @d3c0d3dpt 👋 Thanks for taking an interest, I haven't had time to return to this PR until last time, but if I remember the remaining things were:
|
Hi everyone, it's been a while, and unfortunately our bundles are still quite heavily bloated with this lib and the CJS version of its dependencies (
See also this blog post from the author of the original PR. Support is experimental and only present in v22 (I reckon it's unlikely this feature will be backported to other versions given its complexity), so shipping ESM-only isn't viable at this very moment if you wish to support LTS releases and CJS. But v22 will become the active LTS in October, which is quite soon. @adamjmcgrath do you think preparing to ship native ESM (and ESM only) around this period would be acceptable? It would be a huge win for us to benefit from native ESM for this package and its dependencies, and it seems the rest of the ecosystem and Node itself are finally making decent progress regarding ESM migration. I'm still willing to assist you towards this path if you're interested, both for the library's code itself and for the tools around it. Edit: I was wrong, Node did just add experimental support to |
Given the lack of interest, I'll close my PR and the related issue, and delete my fork. I've personally "cut the middle-man" and went with using Still, thanks for having taken a peek at my issue and PR 👋 |
Description
In response to #378, I've started modernizing this package and writing ESM by default. I'm using
tsc
to output both CJS & ESM entrypoints, along with type definitions.I don't believe using a full-fledged bundler is necessary since the package is quite small. Also, since the package was authored in JS, I've kept it that way instead of migrating to a full TS codebase. I've removed the
index.d.ts
manual type definition file though, leveraging just asrc/types.d.ts
file to host source types (it's handier than editing and sharing types in JS files). This file is copied manually during thebuild:*
npm scripts sincetsc
doesn't do it, but "real" type definitions are now entirely handled bytsc
, ensuring accuracy regarding sources and module resolution.Build artifacts are now contained in a classic
dist
folder, where there arecjs
andesm
entrypoints. The CJS one has an additionalpackage.json
file with{ "type": "commonjs" }
inside, since we're now in an ESM package by default at the root andtsc
didn't output.cjs
/.d.cts
extensions. Those entrypoints are properly exposed through subpath exports with theexports
field in the rootpackage.json
.The previous TS config wasn't checking source files, only the manual declaration file and the single test written in TS, so I believe a LOT of existing errors were ignored. So I've introduced a few different configs, each aimed at a different outcome:
tsconfig.tests.json
for tests, which is more or less what the previous config did. It's still more extensive since it also checks JS files, whereas the previous config only checked the singletests/ts-definitions.tests.ts
filetsconfig.build-*.json
for each build outputtsconfig.json
for source, which contains almost all files and serves CI and IDE checksThe TS config for sources doesn't emit anything, it's just here for typechecking. All configs extend a single
tsconfig.base.json
for brevity, which itself extends@tsconfig/node14
. I've used this version since this seemed to be the lowest Node version you're aiming to support.At the time of writing this, there's still some work to do:
// @ts-ignore
comments. I've typed what was easy to infer for me, but for the rest I believe existing maintainers will have a way easier time than me, who's discovering the codebase. I think a lot of errors will disappear once typings of "original" sources will be completed, since it ripples through the codebaseexamples
folder hasn't been touched for now, nor is it included in typechecking right now. Since the source codebase is now ESM, examples in CJS that imported relative source files (likeconst xxx = require('../../src');
) won't work anymore. I suggest either rewriting examples in ESM to leverage "real" source imports, or faking the package imports withrequire('jwks-rsa')
like we were not in the package's codebase. Also, I'm pretty sure some existing examples don't work, but I've not taken the time to dive in there yetDo note that I've removed the "default" export from
src/index.js
, because those really don't play well between bundlers/runtimes. I've followed the recommendations from AreTheTypesWrong, and I've updated the README to show a named import andnew
instantiation of the client class instead. This is a breaking change and should warrant a new major version bump.References
Related to #378.
Testing
I've added CircleCI and GitHub Action jobs for typechecking and verifying that built outputs have proper types and resolution, thanks to AreTheTypesWrong's CLI. At this point I've just copied what was already there and added the new scripts, I've no idea where/whether we'll want those checks here.
Those checks can be performed locally as well with the new
types:check
andbuild:check
npm scripts.Checklist