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

build: add esm exports #3407

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

build: add esm exports #3407

wants to merge 4 commits into from

Conversation

mesqueeb
Copy link

closes #3060

The implementation should work for enabling ESM compatibility without changing the core library code. Each package has:

  • A properly structured ESM wrapper that:
    • Imports the CommonJS module
    • Re-exports both named exports and the default export
  • An updated package.json with an appropriate exports field providing both import and require paths
  • The esm directory added to the files array so it's included when published
  • Proper handling of TypeScript type definitions where applicable

No further changes are needed. The solution is non-invasive and should work for all the packages in the repository.

@mesqueeb
Copy link
Author

mesqueeb commented Mar 19, 2025

i'm done, I think this will be OK for most cases, here's the full report

Node Postgres Package Type Analysis Report

pg

This package does not contain types.
Details: {
  "packageName": "pg",
  "packageVersion": "8.14.1",
  "types": false
}

pg-pool

This package does not contain types.
Details: {
  "packageName": "pg-pool",
  "packageVersion": "3.8.0",
  "types": false
}

pg-protocol

pg-protocol v1.8.0

Build tools:

  • typescript@^4.0.3

🐛 Import resolved to types through a conditional package.json export, but only after failing to resolve through an earlier condition. This behavior is a TypeScript bug (microsoft/TypeScript#50762). It may misrepresent the runtime behavior of this import and should not be relied upon. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FallbackCondition.md

🚭 Syntax detected in the module is incompatible with the module kind according to the package.json or file extension. This is an error in Node and may cause problems in some bundlers. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UnexpectedModuleSyntax.md

┌───────────────────┬─────────────────────────────┐
│                   │ "pg-protocol"               │
├───────────────────┼─────────────────────────────┤
│ node10            │ 🟢                          │
├───────────────────┼─────────────────────────────┤
│ node16 (from CJS) │ 🟢 (CJS)                    │
├───────────────────┼─────────────────────────────┤
│ node16 (from ESM) │ 🐛 Used fallback condition  │
│                   │ 🚭 Unexpected module syntax │
├───────────────────┼─────────────────────────────┤
│ bundler           │ 🐛 Used fallback condition  │
└───────────────────┴─────────────────────────────┘

pg-query-stream

pg-query-stream v4.8.1

Build tools:

  • typescript@^4.0.3

🐛 Import resolved to types through a conditional package.json export, but only after failing to resolve through an earlier condition. This behavior is a TypeScript bug (microsoft/TypeScript#50762). It may misrepresent the runtime behavior of this import and should not be relied upon. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FallbackCondition.md

🚭 Syntax detected in the module is incompatible with the module kind according to the package.json or file extension. This is an error in Node and may cause problems in some bundlers. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UnexpectedModuleSyntax.md

┌───────────────────┬─────────────────────────────┐
│                   │ "pg-query-stream"           │
├───────────────────┼─────────────────────────────┤
│ node10            │ 🟢                          │
├───────────────────┼─────────────────────────────┤
│ node16 (from CJS) │ 🟢 (CJS)                    │
├───────────────────┼─────────────────────────────┤
│ node16 (from ESM) │ 🐛 Used fallback condition  │
│                   │ 🚭 Unexpected module syntax │
├───────────────────┼─────────────────────────────┤
│ bundler           │ 🐛 Used fallback condition  │
└───────────────────┴─────────────────────────────┘

pg-cursor

This package does not contain types.
Details: {
  "packageName": "pg-cursor",
  "packageVersion": "2.13.1",
  "types": false
}

pg-native

This package does not contain types.
Details: {
  "packageName": "pg-native",
  "packageVersion": "3.3.0",
  "types": false
}

pg-connection-string

pg-connection-string v2.7.0

🚭 Syntax detected in the module is incompatible with the module kind according to the package.json or file extension. This is an error in Node and may cause problems in some bundlers. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UnexpectedModuleSyntax.md

┌───────────────────┬─────────────────────────────┐
│                   │ "pg-connection-string"      │
├───────────────────┼─────────────────────────────┤
│ node10            │ 🟢                          │
├───────────────────┼─────────────────────────────┤
│ node16 (from CJS) │ 🟢 (CJS)                    │
├───────────────────┼─────────────────────────────┤
│ node16 (from ESM) │ 🚭 Unexpected module syntax │
├───────────────────┼─────────────────────────────┤
│ bundler           │ 🟢                          │
└───────────────────┴─────────────────────────────┘

pg-cloudflare

pg-cloudflare v1.1.1

Build tools:

  • typescript@^4.0.3

🚭 Syntax detected in the module is incompatible with the module kind according to the package.json or file extension. This is an error in Node and may cause problems in some bundlers. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/UnexpectedModuleSyntax.md

🐛 Import resolved to types through a conditional package.json export, but only after failing to resolve through an earlier condition. This behavior is a TypeScript bug (microsoft/TypeScript#50762). It may misrepresent the runtime behavior of this import and should not be relied upon. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FallbackCondition.md

💀 Import failed to resolve to type declarations or JavaScript files. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/NoResolution.md

┌───────────────────┬─────────────────────────────┬─────────────────────────────┐
│                   │ "pg-cloudflare"             │ "pg-cloudflare/worker"      │
├───────────────────┼─────────────────────────────┼─────────────────────────────┤
│ node10            │ 🟢                          │ 💀 Resolution failed        │
├───────────────────┼─────────────────────────────┼─────────────────────────────┤
│ node16 (from CJS) │ 🚭 Unexpected module syntax │ 🚭 Unexpected module syntax │
├───────────────────┼─────────────────────────────┼─────────────────────────────┤
│ node16 (from ESM) │ 🐛 Used fallback condition  │ 🐛 Used fallback condition  │
│                   │ 🚭 Unexpected module syntax │ 🚭 Unexpected module syntax │
├───────────────────┼─────────────────────────────┼─────────────────────────────┤
│ bundler           │ 🐛 Used fallback condition  │ 🐛 Used fallback condition  │
└───────────────────┴─────────────────────────────┴─────────────────────────────┘

@brianc
Copy link
Owner

brianc commented Mar 20, 2025

This is pretty fantastic, ngl! Couple issues

  1. lint is all off. Can you run yarn lint --fix? Should take care of it
  2. any suggestions on how to test this and/or write tests for it? I have a few ideas but wanna see if you have any since you seem to have experience here. :)

@mesqueeb
Copy link
Author

@brianc I would love some ideas on how to test this. I haven't done much testing yet myself exactly because I didn't have good ideas how to easily test. : P

We used node-postgres in a big project and I had just stumbled upon the esm issues, but I'm personally not too familiar with this library myself.

I've just ran lint btw! Thanks for pointing that out.

@brianc
Copy link
Owner

brianc commented Mar 20, 2025

sorry looks like some tests are failing in CI now?

@brianc
Copy link
Owner

brianc commented Mar 20, 2025

I would love some ideas on how to test this. I haven't done much testing yet myself exactly because I didn't have good ideas how to easily test. : P

since this is a mono-repo my idea was to make another package that isn't actually published but is just an esm package/module that imports pg as an esm module. The test would just be then all the exported things are importable in the esm module. If you're not familiar w/ this codebase and so on, this is something I'm able to do...just didn't know if you had other ideas.

As far as the existing test failing if you have time to check that out that'd be great! I can do the new tests.

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.

support esm
2 participants