kwhitley / itty-router

A little router.
MIT License
1.75k stars 78 forks source link

COMMUNITY REQUEST: Hybrid export PR or discussion #81

Closed kwhitley closed 2 years ago

kwhitley commented 2 years ago

Hey all! To help with a few issues people have found regarding import into different scenarios (Deno, Jest, etc), I'm proposing a hybrid module export from the build process:

  1. A CommonJS export (currently used for compatibility)
  2. An ES Modules export (e.g. export const Router = ...)

Further investigation into this (and what the underlying issue is) would also be helpful, as I have used itty using esbuild (Cloudflare ES module syntax), as well as traditional CommonJS into webpack builds (or even just using require without w bundler for things like itty's own Jest tests). It always seems to work for me, so I'm not sure what the issue is!

I'll be out of the country (without my computer) for the next few weeks, so any help before I return mid-January would be greatly appreciated!

Cheers and Happy Holidays!

markusahlstrand commented 2 years ago

Would it be an option to just convert the project to a Typescript project? Then I'm thinking that it would be pretty easy to export both esmodules and cjs.. If it's of interest I could whip together a PR for this?

kwhitley commented 2 years ago

You can certainly try @markusahlstrand! While I'm happy for typings to go alongside itty, i'm far less concerned that itty itself is typed for a few reasons:

  1. It's a largely code-golfed concept - bytes matter here, so reforming some of the wilder internal bits to play nice with TS may not fly with our vision!
  2. Itty was designed with utter flexibility in mind. For instance, we specifically do NOT want a rigid type around Request, as we want anything that somewhat looks like a Request to pass. All we care about is a method and a url. The rest, including any additional params, is completely up to the user (or downstream abstraction authors) to tailor to their needs.

On the flipside, a much better candidate for a full TS conversion is itty-router-extras!

kwhitley commented 2 years ago

Pending release via #98