-
Notifications
You must be signed in to change notification settings - Fork 84
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
feat: supporting esm import #824
feat: supporting esm import #824
Conversation
Your free trial has expired. To continue using Ellipsis, sign up at https://app.ellipsis.dev for $20/developer/month. If you have any questions, reach us at [email protected] |
✅ Deploy Preview for astounding-pegasus-21c111 canceled.
|
✅ Deploy Preview for precious-marshmallow-968a81 canceled.
|
From the video, I can see that the build works; but I don't see the examples of ESM-based/CJS-based/TypeScript Node applications working properly. How did those go? |
Also just giving a reminder to target the proper version branch, as mentioned in the PR checklist. |
I have updated the video last video didn't capture the other two windows supertokens-issue-783-support-esm.mp4 |
Okay I have updated to the latest unreleased branch |
Thanks for the update! As a heads up, I probably won't be able to look at this until the weekend. There's also An optional recommendation: Your commit history might be clearer if you |
So far the new video looks promising though! |
Sure I'll do the rebase |
f1bc291
to
26270d9
Compare
Hey friend, sorry I wasn't able to get to this. The weekend actually turned out much busier than I expected. But it looks like things should calm down after today so hopefully I'll be able to review soon. (I know the code change is small. But I want to be able to test the changes as well to double-verify that no unsuspecting breaking changes would sneak in. I'm still a little surprised that we wouldn't need separate files for handling CJS/ESM exports.) |
Sure you can test the same and do let me know if you have any questions on the same Talking about the require and import in the nodejs documentation for conditional exports it's provided that we can separate the cjs and mjs if we are having two files but we can also give default key which will work for both the scenarios here's the link to documentation |
@anuragmerndev Can you link this to #459 as well to help with cleaning up the issues? Separately, I'm probably going to try to pull this branch locally to test (just to doubly make sure). My first suspicion as to why this would work is that ESM is able to import from CJS (but not the other way around). If that's the case then yeah, this should be smooth sailing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (Sorry this took so long to review. Been really busy.)
@rishabhpoddar
I only tested importing from supertokens-node/recipe/emailpassword
. (@anuragmerndev's example uses significantly more import
s.) But for that import, I can confirm that require
ing worked just fine, and import
ing worked for type=module
node apps. (import
ing does not work if the semantic exports
property is excluded from the package.json
file in supertokens-node
).
Some Brief Background
CJS applications cannot import from ESM applications. But ESM applications can import from both CJS and ESM applications. The supertokens-node
package does not identify as an ESM package (because type="module"
is not in the package.json
file); so it is a CJS package. Therefore, when an ESM application tries to import from supertokens-node
, it sees it as a CJS package, and the import works fine (because ESM can handle those). Of course, regular CJS applications have no problem importing from supertokens-node
currently.
Additional Notes
If there are any other paths that people should be allowed to import
/require
from, they need to be added to exports
in package.json
. Otherwise, consumers won't be allowed to access those paths. The exports
property in package.json
is effectively a "whitelist". If it's sufficient to only allow import
s from supertokens-node/recipe/**/*
, then I guess this PR is good to go. But if other paths need to be exposed, it would be helpful to know those.
Not sure what you wanted to do about tests. There's probably a way to write those? The test would basically involve successfully import
ing/require
ing without errors -- if you wanted to automate that.
If you guys added documentation on how to write imports for applications using ESM, then that documentation will need to be re-updated when this gets merged. (Otherwise, don't worry about it.)
@anuragmerndev
Thanks for looking into this. (And sorry again for the delay.) I'm sure this change will make a lot of people happy. Could you share a code snippet of the test file that you used in the video? That way, if anyone else wants to test locally, they'll have the ability to do so with ease.
@ITenthusiasm Thank you for the update on the issue, I have created a repository with a README outlining the test procedure for this fix. supertokens-node-import-test And thank you @ITenthusiasm for sharing your time on checking this PR, means a lot. @rishabhpoddarRegarding the exports, as @ITenthusiasm mentioned, only the files within the 'recipe' directory are currently accessible due to the whitelist configuration. If there are any additional paths that should be exposed, please let me know. During my review of the documentation for supertokens-node usage, I only encountered imports related to recipes. If there are other imports we need to include, I'll be happy to update the PR accordingly |
26270d9
to
07fce0f
Compare
Thanks @anuragmerndev and @ITenthusiasm for the work so far, really appreciate it. Someone from our team will have a look at this too soon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
First of all, sorry for the late review. We've been swamped releasing 18.0 and other projects.
As for the PR, I have some concerns:
- Please update the PR (and the target branch) to the latest
- Some entry points are missing (see my comment), so ideally, we'd like to figure out a way to "test" if this happens again. This is not necessarily a blocker for this PR, but any ideas are welcome.
- If we can test that the defined exports match some files, that'd also be awesome.
- We need to allow importing from the build output directly, as we've seen some use cases that require that. I know it's less than ideal, but we want to enable pretty extreme customizations. I was thinking of adding something like the following to the exports obj, but it seems relatively awkward, so any ideas are welcome:
"./lib/*.js": {
"types": "./lib/*.d.ts",
"default": "./lib/*.js"
},
"./lib/*": {
"types": "./lib/*.d.ts",
"default": "./lib/*.js"
}
- Can we also support existing apps that import us using a ".js" module specifier? Otherwise, this PR will be a breaking change.
add the exports in package.json to support esm import for js and ts
07fce0f
to
ae2fe2d
Compare
Hello @porcellus No worries about the late reviews, In fact, thank you for checking the PR also here's the updates on your concerns
|
I meant some way of doing this in an automated test, especially testing that we didn't miss anything.
That'd be awesome.
Sounds good
Yeah, all entrypoints should work either way, otherwise we might break some apps. |
adding the support for .js module exports and support for types, nextjs, framework and recipe module exports
Hello @porcellus, I have pushed the updated code with support for rest of modules and the .js modules for existing apps along with build folder. About the test thing I am still researching how can we automate tests for this specific PR, haven't found any till now. Kindly review the same and let me know for any updates. |
I've reviewed this and it looks good. I'll merge this into an intermediary branch, because I need to add some further changes (I've figured out a way to automatically test it) and add a changelog entry/increase the version number. |
eada4be
into
supertokens:feat/support_esm_imports
Alright thank you, Also can you please share the logic for automated tests |
Added Anurag Srivastava to contributors list as mentioned by @porcellus supertokens/supertokens-node#824 (comment)
Added Anurag Srivastava to contributors list as mentioned by @porcellus supertokens/supertokens-node#824 (comment)
Thank you for letting me contribute will do alot more of it |
* feat: supporting esm import (#824) * feat: supporting esm import add the exports in package.json to support esm import for js and ts * feat: support for .js module export adding the support for .js module exports and support for types, nextjs, framework and recipe module exports * test: add a test to check our export mapping * fix: update export mapping * chore: update version number * chore: add new related checklist item to PR template * fix: remove unnecessary/broken entry point * test: add explanation comment --------- Co-authored-by: Anurag Srivastava <[email protected]>
add the exports in package.json to support esm import for js and ts
Summary of change
This pull request addresses issue #783 by adding subpath patterns exports to the package.json file. These exports support ESM (ECMAScript Modules) imports for both JavaScript and TypeScript files, improving compatibility and ease of use for developers. Now, imports like import Session from "supertokens-node/recipe/session"; can be used without specifying the index.js file explicitly.
Related issues
Test Plan
supertokens-issue-783-support-esm.mp4
Documentation changes
No documentation changes are required for this PR.
Checklist for important updates
coreDriverInterfaceSupported.json
file has been updated (if needed)lib/ts/version.ts
frontendDriverInterfaceSupported.json
file has been updated (if needed)package.json
package-lock.json
lib/ts/version.ts
npm run build-pretty
recipe/thirdparty/providers/configUtils.ts
file,createProvider
function.git tag
) in the formatvX.Y.Z
, and then find the latest branch (git branch --all
) whoseX.Y
is greater than the latest released tag.add-ts-no-check.js
file to include thatsomeFunc: function () {..}
).Remaining TODOs for this PR
No additional tasks are required for this PR.