Skip to content

Commit

Permalink
feat!: better support for ESM imports (#850)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
porcellus and anuragmerndev authored Jun 11, 2024
1 parent fb3ffa7 commit 2f25f2f
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 23 deletions.
9 changes: 9 additions & 0 deletions .circleci/doUnitTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ then
fi
coreFree=$(echo $coreFree | jq .core | tr -d '"')

cd ..
./test/testExports.sh
if [[ $? -ne 0 ]]
then
echo "export test failed... exiting!"
exit 1
fi
cd .circleci

./setupAndTestWithFreeCore.sh $coreFree $coreDriverVersion
if [[ $? -ne 0 ]]
then
Expand Down
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [ ] If have added a new web framework, update the `add-ts-no-check.js` file to include that
- [ ] If added a new recipe / api interface, then make sure that the implementation of it uses NON arrow functions only (like `someFunc: function () {..}`).
- [ ] If added a new recipe, then make sure to expose it inside the recipe folder present in the root of this repo. We also need to expose its types.
- [ ] If added a new entry point, then make sure that it is importable by adding it to the `exports` in `package.json`

## Remaining TODOs for this PR

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ releasePassword
.tmp
.idea
/test_report
/temp_test_exports
/temp_*
/.nyc_output
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ examples/
add-ts-no-check.js
docs/
.prettierignore
updateLibInExample
updateLibInExample
testExports.sh
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [19.0.0] - 2024-06-10

### Breaking changes

- Defined the entry points of the library using the "exports" field in package.json to make ESM imports more comfortable. This can cause some issues for applications using directory imports from the `lib/build` directory. In those cases we recommend adding `index.js` to the import path.

## [18.0.0] - 2024-05-23

### Breaking change
Expand Down
2 changes: 1 addition & 1 deletion lib/build/version.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
export const version = "18.0.0";
export const version = "19.0.0";

export const cdiSupported = ["5.0"];

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 80 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "supertokens-node",
"version": "18.0.0",
"version": "19.0.0",
"description": "NodeJS driver for SuperTokens core",
"main": "index.js",
"scripts": {
"test": "TEST_MODE=testing npx mocha --node-option no-experimental-fetch -r test/fetch-polyfill.mjs --timeout 500000",
"test-exports": "./test/testExports.sh",
"build-check": "cd lib && npx tsc -p tsconfig.json --noEmit && cd ../test/with-typescript && npm run build",
"build": "cd lib && rm -rf build && npx tsc -p tsconfig.json && cd ../test/with-typescript && npm run build && cd ../.. && npm run post-build",
"pretty": "npx pretty-quick .",
Expand All @@ -27,6 +28,84 @@
"type": "git",
"url": "git+https://github.com/supertokens/supertokens-node.git"
},
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./index": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./index.js": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./framework/*": {
"types": "./framework/*/index.d.ts",
"default": "./framework/*/index.js"
},
"./framework/*/index": {
"types": "./framework/*/index.d.ts",
"default": "./framework/*/index.js"
},
"./framework/*/index.js": {
"types": "./framework/*/index.d.ts",
"default": "./framework/*/index.js"
},
"./nextjs": {
"types": "./nextjs/index.d.ts",
"default": "./nextjs/index.js"
},
"./nextjs/index": {
"types": "./nextjs/index.d.ts",
"default": "./nextjs/index.js"
},
"./nextjs/index.js": {
"types": "./nextjs/index.d.ts",
"default": "./nextjs/index.js"
},
"./types": {
"types": "./types/index.d.ts",
"default": "./types/index.js"
},
"./types/index": {
"types": "./types/index.d.ts",
"default": "./types/index.js"
},
"./types/index.js": {
"types": "./types/index.d.ts",
"default": "./types/index.js"
},
"./lib/*": {
"types": "./lib/*.d.ts",
"default": "./lib/*.js"
},
"./lib/*.js": {
"types": "./lib/*.d.ts",
"default": "./lib/*.js"
},
"./recipe/*": {
"types": "./recipe/*/index.d.ts",
"default": "./recipe/*/index.js"
},
"./recipe/*/index": {
"types": "./recipe/*/index.d.ts",
"default": "./recipe/*/index.js"
},
"./recipe/*/index.js": {
"types": "./recipe/*/index.d.ts",
"default": "./recipe/*/index.js"
},
"./recipe/session/claims": {
"types": "./recipe/session/claims.d.ts",
"default": "./recipe/session/claims.js"
},
"./recipe/session/claims.js": {
"types": "./recipe/session/claims.d.ts",
"default": "./recipe/session/claims.js"
}
},
"author": "rishabhpoddar",
"license": "Apache-2.0",
"bugs": {
Expand Down
10 changes: 0 additions & 10 deletions recipe/thirdparty/emaildelivery/index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions recipe/thirdparty/emaildelivery/index.js

This file was deleted.

35 changes: 35 additions & 0 deletions test/testExports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

TEST_PROJECT_DIR=./temp_test_exports

mkdir -p $TEST_PROJECT_DIR

npm pack || exit $?

mv supertokens-node-*.tgz $TEST_PROJECT_DIR
cd $TEST_PROJECT_DIR

npm init -y || exit $?

rm -f index.mjs
touch index.mjs
# We test that all JS files we publish are importable without an extension
tar -t -f ./supertokens-node* | grep \\.js$ | sed 's/^package\/\(.*\)\.js$/import \"supertokens-node\/\1\";/' >> index.mjs
# We test that all JS files we publish are importable with the JS extension
tar -t -f ./supertokens-node* | grep \\.js$ | sed 's/^package\/\(.*\)\.js$/import \"supertokens-node\/\1\.js";/' >> index.mjs
# We test that all folders that have an index.js (outside of /lib/build) are importable as folders.
tar -t -f --exclude package/lib/build ./supertokens-node* | grep package/.*/index\\.js$ | sed 's/^package\/\(.*\)\/index\.js$/import \"supertokens-node\/\1\";/' >> index.mjs
# We also test that the sdk itself is importable
echo 'import "supertokens-node";' >> index.mjs

mkdir -p node_modules/supertokens-node

tar -xvvf supertokens-node-*.tgz --strip-components=1 -C node_modules/supertokens-node package

node index.mjs
EXIT_CODE=$?

cd ..
rm -rf $TEST_PROJECT_DIR
exit $EXIT_CODE

0 comments on commit 2f25f2f

Please sign in to comment.