Skip to content
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

Support ESM usage of module #551

Open
julrich opened this issue Sep 6, 2023 · 3 comments
Open

Support ESM usage of module #551

julrich opened this issue Sep 6, 2023 · 3 comments

Comments

@julrich
Copy link

julrich commented Sep 6, 2023

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.

@julrich
Copy link
Author

julrich commented Sep 10, 2023

Added some initial work for this here: #552

@bcherny
Copy link
Owner

bcherny commented Sep 23, 2023

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?

@julrich
Copy link
Author

julrich commented Sep 23, 2023

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants