Skip to content

Export CJS bundle #1263

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

Merged
merged 1 commit into from
Jul 31, 2023
Merged

Export CJS bundle #1263

merged 1 commit into from
Jul 31, 2023

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Jul 31, 2023

Changes

Addresses #888. Exports a CJS bundle for openapi-typescript. Does so via package.json exports so it requires a fairly-modern version of Node (but all current supported versions do).

Bundles via esbuild to gloss over complexities of CJS vs ESM deep imports for dependencies. Since all the dependencies are tiny, the bundle isn’t too bad

How to Review

Tests should pass

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Jul 31, 2023

🦋 Changeset detected

Latest commit: ebb44e8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 31, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: ebb44e8
Status: ✅  Deploy successful!
Preview URL: https://cdbd5aa2.openapi-ts.pages.dev
Branch Preview URL: https://cjs-export.openapi-ts.pages.dev

View logs

"build": "del dist && tsc -p tsconfig.build.json",
"build": "run-s -s build:*",
"build:esm": "tsc -p tsconfig.build.json",
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs src/index.ts --footer:js=\"module.exports = module.exports.default;\"",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With dist/index.cjs, the hope is that this will just shadow TypeScript types generated for dist/index.js. But we’ll see.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also the --footer:js is a hack to get rid of require(…).default without affecting the ESM package.

I spent some time trying to bundle with Rollup, but it turned out to be far more complex, and the end result still required similar hacks (just more in number)

"build": "del dist && tsc -p tsconfig.build.json",
"build": "run-s -s build:*",
"build:esm": "tsc -p tsconfig.build.json",
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:js-yaml --external:undici src/index.ts --footer:js=\"module.exports = module.exports.default;\"",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

js-yaml and undici are still external because they seemed to pose no problems. Should reduce weirdness without being difficult.

The other deps are ESM, but are tiny, so inlining feels right

Copy link
Contributor Author

@drwpow drwpow Jul 31, 2023

Choose a reason for hiding this comment

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

🤔 Probably also need to remove undici, but that will wait until we drop Node 16 support on Sep 11, 2023, as removing undici would require Node 18+. Though there are probably a few Node 16 bugs already that make upgrading worth it, I don’t want to draw a hard line in the sand right here and now

@drwpow drwpow merged commit 1bf2d4d into main Jul 31, 2023
@drwpow drwpow deleted the cjs-export branch July 31, 2023 18:33
@github-actions github-actions bot mentioned this pull request Jul 31, 2023
@djMax
Copy link

djMax commented Aug 3, 2023

I think some situations (I can't isolate yet) do not pick up the types with this change.

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