Closed vnphanquang closed 6 months ago
Message from #66:
I really appreciate your effort with the PR (#67), but I've just switched over from js to ts a little while ago and would like to keep it that way. I think a bundler is the best way forward for now, but I'm also always open for feedback and suggestions :)
Preface
This is a big change (not breaking) and I am sorry if i crossed any line here. I understand both the benefits and arguments against vanilla JS so I will respect whatever eventual decision made by code owner and other maintainers. Discussions are welcome. Thanks.
Changes
The PR is split into multiple commits, each with its own purpose. I recommend reviewing one by one.
jest
tovitest
. The rationale behind this is thatvitest
provides better ergonomics in general, especially when working with ESM and Typesript. After previous refactor (1),jest
complains about ESM import syntax, whilevitest
works out of the box. Added also are Vitest UI, and coverage report, runnpm run test:ui
to try it out.Context
Following https://github.com/tsndr/cloudflare-worker-jwt/issues/66, the current output of
tsc
is not compatible with Node's ESM algorithm, which requires explicit.js
extension in relative import statements. This PR addresses this issue by migrating the current source code to vanilla JS (with JSDocs), removing a build step completely and allowing explicit.js
from source code.Alternatives
Other ways to get around #66:
utils.ts
intoindex.ts
: quick & dirty, but lose code organizationrollup
,esbuild
, or tsup for example, to squash all source code to a single output file: add to dependency and complexity of the projectAbout Vanilla JS
The most significant benefit of using vanilla JS in a lib is that there is no build step. In combination with declaration map (setting
declarationMap
totrue
intsconfig
), this greatly reduces development friction by letting us inspect and ticker directly with the lib source code from user land.That being said, this package is targeted to run in cloudflare environment, and "wrangler runs esbuild by default as part of the dev and publish commands, and bundles your Worker project into a single Worker script". So, the aforementioned benefit is certainly there but arguably not that significant.
On the other hand, working with vanilla requires, unsurprisingly, JSDocs. I personally don't mind this given I've published a dozen of libs with vanilla JS, some of quite complex typing. During the refactor, I saw that most things in
index.ts
have some level of JSDocs already, so all left to do was removing TS-specific syntax and moving some more complex typing to a separatetypes.ts
file for better ergonomics. All in all the development experience won't change that much, but i'll leave that for you to judge.Change to Publishing Process
The
build
script has been changed todts
for generating type only. This means source code insrc
are shipped as is.A
prepublishOnly
is added to runtsc
, so theoreticallynpm publish
is the only command needed for publication.