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

Added back CommonsJS output under /cjs #1956

Closed
wants to merge 7 commits into from

Conversation

arthurfiorette
Copy link
Collaborator

@arthurfiorette arthurfiorette commented May 16, 2024

#1922 (comment) removed support for CommonsJS entirely without warnings and a major release. Since a lot of libraries depends on this package, adding a cjs bundle back should be considered.

You may argue that their libraries should migrate to ESM, however, one of the uses cases of this library is to generate json schemas inside a Typescript Plugin which is loaded by the TypeScript CJS source itself and will not be ported to ESM soon (if ever).

@arthurfiorette
Copy link
Collaborator Author

The main ts-json-schema-generator.ts file imported package.json to get its version, I was required to do that step as a post-process operation because I couldn't get the same ts source code transpiled into (cjs/esm) to work.

https://github.com/vega/ts-json-schema-generator/actions/runs/9105923115/job/25032310451#step:7:997

package.json Show resolved Hide resolved
import { mkdirSync, writeFileSync } from "fs";

// This constant gets replaced by the build script
const pkgVersion = "0.0.0"; /* __VERSION__ */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this as it makes it harder to watch build. The cjs version used to work so can you try to make the import version work here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a lot but ran out of choices.

  • import pkg from 'package.json' (or * as pkg) works in cjs but not in ESM as it requires a with {type: 'json'} statement

  • require('package.json') works but not in ESM mode.

  • using const _require = typeof require === 'undefined' ? createRequire(import.meta.url) : require should work but breaks in CJS even if createRequire(import.meta.url) does not gets executed.

  • using fs.readFileSync requires __dirname (which can be created using import.meta.url in esm but breaks just like the above try) and a second build step to manually copy package.json to dist/cjs folders.

  • Trying to bypass import.meta.url using `eval('import.meta.url') does not work.

I'm not sure if there's any other way to fix it without replacing the dist code. Also, as it gets built on CI before publishing, all official releases should work just as fine.

@arthurfiorette
Copy link
Collaborator Author

After all, the only thing I can say is:

Welcome to the hell of maintaining ESM/CJS modularity in modern JavaScript.

package.json Show resolved Hide resolved
@domoritz
Copy link
Member

True. I've wasted too many days with bundling and packaging stuff.

@arthurfiorette
Copy link
Collaborator Author

is there anything left?

@domoritz
Copy link
Member

replaced by #1964

@domoritz domoritz closed this May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants