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

Switch to new style imports. #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tomprince
Copy link

@tomprince tomprince commented Mar 10, 2023

What kind of change does this PR introduce?

This adds support for importing postgrest-js as an ESM module in node.

What is the current behavior?

Currently, postgrest-js can only be imported as a CommonJS module on node (fixes #411).

Additional context

As written, this prevents imports like require("@supabase/postgrest-js/dist/main/PostgrestBuilder.js"). Node's docs suggest that this is a breaking change:

Existing packages introducing the "exports" field will prevent consumers of the package from using any entry points that are not defined, including the package.json (e.g. require('your-package/package.json'). This will likely be a breaking change.

However, I think that is only true if you consider that kind of import to be part of your public interface. The documentation does suggest a way to continue to support that kind of import if desired.

Looking at the code, the only things that could be accessed that way that aren't available from the top-level import are in src/types.ts and src/constants.ts.


This seems to work when I test locally, but I don't know enough about how this package is used in the wild to know if this might cause issues for other consumers.

@tomprince tomprince force-pushed the module-imports branch 4 times, most recently from bbad37a to f8d690f Compare March 10, 2023 04:16
@tomprince tomprince marked this pull request as ready for review March 10, 2023 06:54
@@ -0,0 +1,3 @@
import * as fs from 'fs'

fs.writeFileSync('./dist/main/package.json', '{"type": "commonjs"}')
Copy link
Author

Choose a reason for hiding this comment

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

Node looks at type in the nearest package.json to tell if a .jsfile should be interpreted as an ESM or commonjs module. You can use .mjs or .cjs to specify this on a per-file basis, but typescript doesn't have any support for that. See https://github.com/azu/tsconfig-to-dual-package#motivation for more details and links to discussion about this.

I elected to put type: module in the top-level package, since my impression is that the ecosystem is slowly moving towards ESM style modules.

preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
Copy link
Author

Choose a reason for hiding this comment

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

I copied this from https://kulshekhar.github.io/ts-jest/docs/guides/esm-support, but it isn't clear to me why this isn't included in any of the presets.

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.

Cannot import postgres-js as an ESM module in node.
1 participant