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

gh-43: Full support for ESM modules. #56

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

Conversation

iSuslov
Copy link

@iSuslov iSuslov commented May 12, 2023

Fixes #43 in v1.3

Problem:
In typescript when using more strict moduleResolution such as nodenext or node16, typescript can't find type declarations of the esm module which has .mjs extension. This is because TypeScript ignores .js when there is an identically named .d.ts file beside it. Same happens with .mjs and .d.mts but if no such pair found, typescript thinks there are no type declarations.
More info in my comment here.

Solution:
Assuming that weaviate team doesn't want to change package module type from commonjs to module, keeping commonjs as default option, the smallest change is to generate d.mts declaration file. Since we don't have actual .mts files, it's not easy to emit one using standard bundlers and builders, see. The best workaround I see is to clone original type definitions after build. This will make this PR small and if one day weaviate team decides to implement a build pipeline it will be straightforward to implement this step using other tools.

@@ -7,9 +7,9 @@
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
Copy link
Author

Choose a reason for hiding this comment

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

types should always go first. See typescript doc

],
"scripts": {
"test": "tsc -noEmit -p tsconfig-test.json && jest --useStderr --runInBand --detectOpenHandles",
"build": "npm run lint && tsup src/index.ts --format cjs,esm --dts --clean --platform neutral --minify",
"build": "npm run lint && tsup src/index.ts --format cjs,esm --dts --clean --platform neutral --minify && cp ./dist/index.d.ts ./dist/index.d.mts",
Copy link
Author

Choose a reason for hiding this comment

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

Simply duplicate declaration file with d.mts extension. This will make typescript compiler happy because for .mjs module types will be visible.

@parkerduckworth
Copy link
Member

@iSuslov the intention of making this package a CommonJS library is to keep backwards compatibility with the projects who previously used the CommonJS JavaScript client which this client was migrated from.

Does changing this package type to module break this compatibility?

@weaviate-git-bot
Copy link

To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Slack channel?

@tsmith023
Copy link
Contributor

tsmith023 commented Aug 17, 2023

Hi @iSuslov 😁 what is the status of this PR? Are you in a position to reply to #56 (comment)?

@erikvullings
Copy link

@iSuslov the intention of making this package a CommonJS library is to keep backwards compatibility with the projects who previously used the CommonJS JavaScript client which this client was migrated from.

Does changing this package type to module break this compatibility?

Although I am not the submitter of this PR, I can reply to your comment: if you change the type to module in the package.json, this will be a breaking change. Although you can still import an ESM module into a CommonJs application, it has to be done using the dynamic import function, since ESM modules have asynchronous execution. See here for more information.

Still, all things considered, I would rather have you move to ESM than using the approach suggested here using an additional build step. Many npm packages have already migrated to ESM, and although it is a bit of a pain to move your code to ESM, it is not rocket science, and you get other benefits too.

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.

Misconfiguration for typescript projects using module node16/nodenext
5 participants