bcherny/json-schema-to-typescript

Support ESM usage of module

julrich opened this issue · 3 comments

We're currently working on some ESM-based tooling (we're going pure ESM there), and in the process of migrating existing code into that cleaned-up code base we've stumbled upon some challenges with json-schema-to-typescript.

Currently the project only targets CJS. If there's interest in supporting both ESM and CJS, I'd be open to do a PR for that. We'd need to work on that anyways (less preferably as a fork).

One main thing that would be interesting to me in that context is the use of the json-schema-ref-parser fork. There seems to be some instability as a consequence of maintainer changes over in that code base. Initially ESM-support was added there (APIDevTools/json-schema-ref-parser@96b805a), but the latest rewrite seems to have completely removed that again (APIDevTools/json-schema-ref-parser@a5b3946#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519).

For the use in json-schema-to-typescript the fork (https://github.com/bcherny/json-schema-ref-parser) seems to be exactly in between those last big rewrite changes. Is this just coincidence? My impression was that the main fix on the fork was about the node version being wrong in some in between-releases of json-schema-ref-parser.

If the intention would be to go back to a non-forked version of json-schema-ref-parser as soon as possible, this might be a blocker. Because those last rewrite changes over there removed everything about ESM apparently. If continuing to work of the already existing fork, ESM there should be a pretty easy fix. It's just line 3 over here that needs to be changed to "export default $RefParser" in my first initial, local testing.

Added some initial work for this here: #552

Can you please better explain the issue you are running into when consuming JSTT in your ESM project? My understanding is most bundlers have an interop layer, so if something isn't working, it's more likely a misconfiguration on your end than something we should fix in JSTT.

If I'm missing something, a good start would be a quick demo of the issue you're running into. Could you throw a demo in a new repo and post a link?

I'll give it a try: We're not actually using a bundler, only TypeScript for compilation. Not sure how you'd arrive at a miconfigured bundler.

When adding json-schema-to-typescript to the project, importing (e.g.) compile and building the application nets the following error:

[typescript] common/temp/node_modules/.pnpm/json-schema-to-typescript@13.1.1/node_modules/json-schema-to-typescript/dist/src/index.d.ts:2:40 - (TS1479) The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@bcherny/json-schema-ref-parser")' call instead.

The line marked by the error contains import { Options as $RefOptions } from '@bcherny/json-schema-ref-parser';

Been a few days now, so not that deep into this any longer... but mainly correct package.json definitions (like "type": "module") were needed at least... and I'm not aware that you can really be compatible without providing ESM and CJS output as split code + definitions.

I linked the post by Anthony Fu in the closed PR, which (I think) outlines the whole ordeal quite well:
https://antfu.me/posts/publish-esm-and-cjs

Tried my hand at a small reproduction, but not sure I'll get that ready in a reasonable timeframe. The error comes from a bigger monorepo-based setup, which makes it a bit hard to isolate. If I'm simply missing something here, I'm happy to learn! :)