From eada4be95a3b3ac87d0b08e8c292700441e4ba9b Mon Sep 17 00:00:00 2001 From: Anurag Srivastava <144275260+anuragmerndev@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:05:57 +0530 Subject: [PATCH 1/7] 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 --- package.json | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/package.json b/package.json index 2d99ff715..5d5c3df8d 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,52 @@ "type": "git", "url": "git+https://github.com/supertokens/supertokens-node.git" }, + "exports": { + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "./framework/*": { + "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.js": { + "types": "./nextjs/index.d.ts", + "default": "./nextjs/index.js" + }, + "./types": { + "types": "./types/index.d.ts", + "default": "./types/index.js" + }, + "./types/index.js": { + "types": "./types/index.d.ts", + "default": "./types/index.js" + }, + "./lib/*": { + "types": "./lib/*/index.d.ts", + "default": "./lib/*/index.js" + }, + "./lib/*.js": { + "types": "./lib/*.d.ts", + "default": "./lib/*.js" + }, + "./recipe/*": { + "types": "./recipe/*/index.d.ts", + "default": "./recipe/*/index.js" + }, + "./recipe/*/index.js": { + "types": "./recipe/*/index.d.ts", + "default": "./recipe/*/index.js" + } + }, "author": "rishabhpoddar", "license": "Apache-2.0", "bugs": { From d01e03d7f85694e4ce6f774e00a7f38f75d28dfc Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Fri, 7 Jun 2024 19:34:21 +0200 Subject: [PATCH 2/7] test: add a test to check our export mapping --- .circleci/doUnitTests.sh | 9 +++++++++ .gitignore | 2 ++ .npmignore | 3 ++- test/testExports.sh | 31 +++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100755 test/testExports.sh diff --git a/.circleci/doUnitTests.sh b/.circleci/doUnitTests.sh index e880670e1..a59e4a916 100755 --- a/.circleci/doUnitTests.sh +++ b/.circleci/doUnitTests.sh @@ -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 diff --git a/.gitignore b/.gitignore index d13b6071e..a5e771d2c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ releasePassword .tmp .idea /test_report +/temp_test_exports +/temp_* /.nyc_output \ No newline at end of file diff --git a/.npmignore b/.npmignore index 683ee5888..c87e7731c 100644 --- a/.npmignore +++ b/.npmignore @@ -18,4 +18,5 @@ examples/ add-ts-no-check.js docs/ .prettierignore -updateLibInExample \ No newline at end of file +updateLibInExample +testExports.sh \ No newline at end of file diff --git a/test/testExports.sh b/test/testExports.sh new file mode 100755 index 000000000..02a38ef18 --- /dev/null +++ b/test/testExports.sh @@ -0,0 +1,31 @@ +#!/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 +tar -t -f ./supertokens-node* | grep \\.js$ | sed 's/^package\/\(.*\)\.js$/import \"supertokens-node\/\1\";/' >> index.mjs +tar -t -f ./supertokens-node* | grep \\.js$ | sed 's/^package\/\(.*\)\.js$/import \"supertokens-node\/\1\.js";/' >> index.mjs +tar -t -f --exclude package/lib/build ./supertokens-node* | grep package/.*/index\\.js$ | sed 's/^package\/\(.*\)\/index\.js$/import \"supertokens-node\/\1\";/' >> index.mjs +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 + From 756dea39559a451de4762430e933edf5142b36bf Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Fri, 7 Jun 2024 19:36:06 +0200 Subject: [PATCH 3/7] fix: update export mapping --- package.json | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5d5c3df8d..d308a5577 100644 --- a/package.json +++ b/package.json @@ -32,10 +32,22 @@ "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" @@ -44,6 +56,10 @@ "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" @@ -52,13 +68,17 @@ "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/*/index.d.ts", - "default": "./lib/*/index.js" + "types": "./lib/*.d.ts", + "default": "./lib/*.js" }, "./lib/*.js": { "types": "./lib/*.d.ts", @@ -68,9 +88,21 @@ "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", From 7326cbbfa1509f82ed4876a635dfe77654e61574 Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Fri, 7 Jun 2024 19:37:12 +0200 Subject: [PATCH 4/7] chore: update version number --- CHANGELOG.md | 6 ++++++ lib/build/version.d.ts | 2 +- lib/build/version.js | 2 +- lib/ts/version.ts | 2 +- package-lock.json | 4 ++-- package.json | 3 ++- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5bd65564..76a1adc0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/build/version.d.ts b/lib/build/version.d.ts index a9d18f5e9..09065b34b 100644 --- a/lib/build/version.d.ts +++ b/lib/build/version.d.ts @@ -1,4 +1,4 @@ // @ts-nocheck -export declare const version = "18.0.0"; +export declare const version = "19.0.0"; export declare const cdiSupported: string[]; export declare const dashboardVersion = "0.11"; diff --git a/lib/build/version.js b/lib/build/version.js index 2c6882e5d..c922e6bb9 100644 --- a/lib/build/version.js +++ b/lib/build/version.js @@ -15,7 +15,7 @@ exports.dashboardVersion = exports.cdiSupported = exports.version = void 0; * License for the specific language governing permissions and limitations * under the License. */ -exports.version = "18.0.0"; +exports.version = "19.0.0"; exports.cdiSupported = ["5.0"]; // Note: The actual script import for dashboard uses v{DASHBOARD_VERSION} exports.dashboardVersion = "0.11"; diff --git a/lib/ts/version.ts b/lib/ts/version.ts index 92e4d0f9a..e2c1d5d52 100644 --- a/lib/ts/version.ts +++ b/lib/ts/version.ts @@ -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"]; diff --git a/package-lock.json b/package-lock.json index 98af715e6..d4f1b4611 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "supertokens-node", - "version": "18.0.0", + "version": "19.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "supertokens-node", - "version": "18.0.0", + "version": "19.0.0", "license": "Apache-2.0", "dependencies": { "content-type": "^1.0.5", diff --git a/package.json b/package.json index d308a5577..8f5377af5 100644 --- a/package.json +++ b/package.json @@ -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 .", From 4239c5b22a1cc9edefb687d36615f444174e9bbd Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Fri, 7 Jun 2024 19:41:22 +0200 Subject: [PATCH 5/7] chore: add new related checklist item to PR template --- .github/PULL_REQUEST_TEMPLATE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index da1c80b1c..314efe946 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -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 From 78beef5f84edd38aeb3413194b346bb47c9c5405 Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Fri, 7 Jun 2024 19:44:09 +0200 Subject: [PATCH 6/7] fix: remove unnecessary/broken entry point --- recipe/thirdparty/emaildelivery/index.d.ts | 10 ---------- recipe/thirdparty/emaildelivery/index.js | 6 ------ 2 files changed, 16 deletions(-) delete mode 100644 recipe/thirdparty/emaildelivery/index.d.ts delete mode 100644 recipe/thirdparty/emaildelivery/index.js diff --git a/recipe/thirdparty/emaildelivery/index.d.ts b/recipe/thirdparty/emaildelivery/index.d.ts deleted file mode 100644 index 30bed2519..000000000 --- a/recipe/thirdparty/emaildelivery/index.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -export * from "../../../lib/build/recipe/thirdparty/emaildelivery/services"; -/** - * 'export *' does not re-export a default. - * import NextJS from "supertokens-node/nextjs"; - * the above import statement won't be possible unless either - * - user add "esModuleInterop": true in their tsconfig.json file - * - we do the following change: - */ -import * as _default from "../../../lib/build/recipe/thirdparty/emaildelivery/services"; -export default _default; diff --git a/recipe/thirdparty/emaildelivery/index.js b/recipe/thirdparty/emaildelivery/index.js deleted file mode 100644 index e0c46c07f..000000000 --- a/recipe/thirdparty/emaildelivery/index.js +++ /dev/null @@ -1,6 +0,0 @@ -"use strict"; -function __export(m) { - for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; -} -exports.__esModule = true; -__export(require("../../../lib/build/recipe/thirdparty/emaildelivery/services")); From 069cef8fed27f5a9350015a8085cdc9256e8de03 Mon Sep 17 00:00:00 2001 From: Mihaly Lengyel Date: Tue, 11 Jun 2024 13:08:18 +0200 Subject: [PATCH 7/7] test: add explanation comment --- test/testExports.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/testExports.sh b/test/testExports.sh index 02a38ef18..67196888c 100755 --- a/test/testExports.sh +++ b/test/testExports.sh @@ -13,9 +13,13 @@ 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