From 54aa9656bf1603382df5ff03d729a9124b6595d9 Mon Sep 17 00:00:00 2001 From: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> Date: Sun, 25 Feb 2024 08:45:52 -0500 Subject: [PATCH] feat: package is now ESM (#393) * feat: package is now ESM BREAKING CHANGE: package is now ESM * build: add dom libs to tsconfig so it can build --- README.md | 3 +- package-lock.json | 105 +++++++++++++++++++--------------------- package.json | 24 ++++++--- scripts/build.mjs | 39 ++++++--------- src/auth.ts | 2 +- src/hook.ts | 4 +- src/index.ts | 6 +-- src/types.ts | 2 +- test/index.test.ts | 8 +-- test/tsconfig.test.json | 5 +- tsconfig.json | 1 + 11 files changed, 96 insertions(+), 103 deletions(-) diff --git a/README.md b/README.md index 78910895c..f94e2f288 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,7 @@ Node Install with npm install @octokit/auth-token ```js -const { createTokenAuth } = require("@octokit/auth-token"); -// or: import { createTokenAuth } from "@octokit/auth-token"; +import { createTokenAuth } from "@octokit/auth-token"; ``` diff --git a/package-lock.json b/package-lock.json index bdeff1a58..5b3abc5e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,19 +9,19 @@ "version": "0.0.0-development", "license": "MIT", "devDependencies": { - "@octokit/request": "^8.0.1", - "@octokit/tsconfig": "^2.0.0", + "@octokit/request": "^9.0.0", + "@octokit/tsconfig": "^3.0.0", "@octokit/types": "^12.0.0", - "@types/fetch-mock": "^7.3.1", + "@types/fetch-mock": "^7.3.8", "@types/jest": "^29.0.0", "esbuild": "^0.20.0", - "fetch-mock": "^9.0.0", + "fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1", "glob": "^10.2.6", "jest": "^29.0.0", "prettier": "3.2.5", "semantic-release": "^23.0.0", - "ts-jest": "^29.0.0", - "typescript": "^5.0.0" + "ts-jest": "^29.1.0", + "typescript": "^5.3.0" }, "engines": { "node": ">= 18" @@ -1844,27 +1844,23 @@ } }, "node_modules/@octokit/endpoint": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-9.0.0.tgz", - "integrity": "sha512-szrQhiqJ88gghWY2Htt8MqUDO6++E/EIXqJ2ZEp5ma3uGS46o7LZAzSLt49myB7rT+Hfw5Y6gO3LmOxGzHijAQ==", + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-10.0.0.tgz", + "integrity": "sha512-emBcNDxBdC1y3+knJonS5zhUB/CG6TihubxM2U1/pG/Z1y3a4oV0Gzz3lmkCvWWQI6h3tqBAX9MgCBFp+M68Jw==", "dev": true, "dependencies": { - "@octokit/types": "^11.0.0", - "is-plain-object": "^5.0.0", - "universal-user-agent": "^6.0.0" + "@octokit/types": "^12.0.0", + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" } }, - "node_modules/@octokit/endpoint/node_modules/@octokit/types": { - "version": "11.1.0", - "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.1.0.tgz", - "integrity": "sha512-Fz0+7GyLm/bHt8fwEqgvRBWwIV1S6wRRyq+V6exRKLVWaKGsuy6H9QFYeBVDV7rK6fO3XwHgQOPxv+cLj2zpXQ==", - "dev": true, - "dependencies": { - "@octokit/openapi-types": "^18.0.0" - } + "node_modules/@octokit/endpoint/node_modules/universal-user-agent": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-7.0.2.tgz", + "integrity": "sha512-0JCqzSKnStlRRQfCdowvqy3cy0Dvtlb8xecj/H8JFZuCze4rwjPZQOgvFvn0Ws/usCHQFGpyr+pB9adaGwXn4Q==", + "dev": true }, "node_modules/@octokit/graphql": { "version": "5.0.6", @@ -1956,6 +1952,12 @@ "@octokit/core": ">=4" } }, + "node_modules/@octokit/plugin-paginate-rest/node_modules/@octokit/tsconfig": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-2.0.0.tgz", + "integrity": "sha512-tWnrai3quGt8+gRN2edzo9fmraWekeryXPeXDomMw2oFSpu/lH3VSWGn/q4V+rwjTRMeeXk/ci623/01Zet4VQ==", + "dev": true + }, "node_modules/@octokit/plugin-paginate-rest/node_modules/@octokit/types": { "version": "9.3.2", "resolved": "https://registry.npmjs.org/@octokit/types/-/types-9.3.2.tgz", @@ -2017,16 +2019,15 @@ } }, "node_modules/@octokit/request": { - "version": "8.0.1", - "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.0.1.tgz", - "integrity": "sha512-jgqMljk72c1uE2YmKLjtWmRvEVVBKn1WWLZ1WLoCk/n3w83UNUGY9RcK6ALT7zkRU2NBJprx3GsCNHQvGrM1sg==", + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-9.0.0.tgz", + "integrity": "sha512-hShsrZ40W8dz3TkyD5ifHNcrYfYpYF7Sh2x+ZKafA+eO0zNeKDTsbF1E0xrUf0mVYGeKL+NLMO5SnfAP22l05Q==", "dev": true, "dependencies": { - "@octokit/endpoint": "^9.0.0", - "@octokit/request-error": "^5.0.0", - "@octokit/types": "^11.0.0", - "is-plain-object": "^5.0.0", - "universal-user-agent": "^6.0.0" + "@octokit/endpoint": "^10.0.0", + "@octokit/request-error": "^6.0.1", + "@octokit/types": "^12.0.0", + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" @@ -2056,32 +2057,27 @@ } }, "node_modules/@octokit/request/node_modules/@octokit/request-error": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.0.0.tgz", - "integrity": "sha512-1ue0DH0Lif5iEqT52+Rf/hf0RmGO9NWFjrzmrkArpG9trFfDM/efx00BJHdLGuro4BR/gECxCU2Twf5OKrRFsQ==", + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-6.0.1.tgz", + "integrity": "sha512-EPeILOAEGqwmEG6g7dYlbvqkjJ5nWNcZvQpgeLEn1MieIcJ0xK0CJtvVcIMXqzIM8DOWzvI6zL5FQAwjTvk/Tg==", "dev": true, "dependencies": { - "@octokit/types": "^11.0.0", - "deprecation": "^2.0.0", - "once": "^1.4.0" + "@octokit/types": "^12.0.0" }, "engines": { "node": ">= 18" } }, - "node_modules/@octokit/request/node_modules/@octokit/types": { - "version": "11.1.0", - "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.1.0.tgz", - "integrity": "sha512-Fz0+7GyLm/bHt8fwEqgvRBWwIV1S6wRRyq+V6exRKLVWaKGsuy6H9QFYeBVDV7rK6fO3XwHgQOPxv+cLj2zpXQ==", - "dev": true, - "dependencies": { - "@octokit/openapi-types": "^18.0.0" - } + "node_modules/@octokit/request/node_modules/universal-user-agent": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-7.0.2.tgz", + "integrity": "sha512-0JCqzSKnStlRRQfCdowvqy3cy0Dvtlb8xecj/H8JFZuCze4rwjPZQOgvFvn0Ws/usCHQFGpyr+pB9adaGwXn4Q==", + "dev": true }, "node_modules/@octokit/tsconfig": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-2.0.0.tgz", - "integrity": "sha512-tWnrai3quGt8+gRN2edzo9fmraWekeryXPeXDomMw2oFSpu/lH3VSWGn/q4V+rwjTRMeeXk/ci623/01Zet4VQ==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-3.0.0.tgz", + "integrity": "sha512-tQLwgXYfBq9iUbOq26kWCzsJL6DY7qjOLzqcg5tCFQ4ob48H47iX98NudHW7S5OQ/fpSKYJhb3eQehyBNzYjfA==", "dev": true }, "node_modules/@octokit/types": { @@ -2549,9 +2545,9 @@ } }, "node_modules/@types/fetch-mock": { - "version": "7.3.5", - "resolved": "https://registry.npmjs.org/@types/fetch-mock/-/fetch-mock-7.3.5.tgz", - "integrity": "sha512-sLecm9ohBdGIpYUP9rWk5/XIKY2xHMYTBJIcJuBBM8IJWnYoQ1DAj8F4OVjnfD0API1drlkWEV0LPNk+ACuhsg==", + "version": "7.3.8", + "resolved": "https://registry.npmjs.org/@types/fetch-mock/-/fetch-mock-7.3.8.tgz", + "integrity": "sha512-ztsIGiyUvD0GaqPc9/hb8k20gnr6lupqA6SFtqt+8v2mtHhNO/Ebb6/b7N6af/7x0A7s1C8nxrEGzajMBqz8qA==", "dev": true }, "node_modules/@types/graceful-fs": { @@ -3993,9 +3989,10 @@ } }, "node_modules/fetch-mock": { - "version": "9.11.0", - "resolved": "https://registry.npmjs.org/fetch-mock/-/fetch-mock-9.11.0.tgz", - "integrity": "sha512-PG1XUv+x7iag5p/iNHD4/jdpxL9FtVSqRMUQhPab4hVDt80T1MH5ehzVrL2IdXO9Q2iBggArFvPqjUbHFuI58Q==", + "name": "@gr2m/fetch-mock", + "version": "9.11.0-pull-request-644.1", + "resolved": "https://registry.npmjs.org/@gr2m/fetch-mock/-/fetch-mock-9.11.0-pull-request-644.1.tgz", + "integrity": "sha512-gTp6RCHzlOXS1qRb0APfuyz48Lw/JFPa4uiar+kEgL1STsDwth75HJZ4x30tBlXMJXV8XDTDzJ2Hz9w3RWiHJA==", "dev": true, "dependencies": { "@babel/core": "^7.0.0", @@ -12255,9 +12252,9 @@ } }, "node_modules/typescript": { - "version": "5.1.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz", - "integrity": "sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==", + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", "dev": true, "bin": { "tsc": "bin/tsc", diff --git a/package.json b/package.json index 139507cf0..913993bb2 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,12 @@ "publishConfig": { "access": "public" }, + "type": "module", "version": "0.0.0-development", "description": "GitHub API token authentication for browsers and Node.js", "scripts": { "build": "node scripts/build.mjs && tsc -p tsconfig.json", - "test": "jest --coverage", + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage", "pretest": "npm run -s lint", "lint": "prettier --check '{src,test}/**/*.{ts,md}' '*.md' package.json", "lint:fix": "prettier --write '{src,test}/**/*.{ts,md}' '*.md' package.json" @@ -22,26 +23,30 @@ "author": "Gregor Martynus (https://github.com/gr2m)", "license": "MIT", "devDependencies": { - "@octokit/request": "^8.0.1", - "@octokit/tsconfig": "^2.0.0", + "@octokit/request": "^9.0.0", + "@octokit/tsconfig": "^3.0.0", "@octokit/types": "^12.0.0", - "@types/fetch-mock": "^7.3.1", + "@types/fetch-mock": "^7.3.8", "@types/jest": "^29.0.0", "esbuild": "^0.20.0", - "fetch-mock": "^9.0.0", + "fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1", "glob": "^10.2.6", "jest": "^29.0.0", "prettier": "3.2.5", "semantic-release": "^23.0.0", - "ts-jest": "^29.0.0", - "typescript": "^5.0.0" + "ts-jest": "^29.1.0", + "typescript": "^5.3.0" }, "jest": { + "extensionsToTreatAsEsm": [ + ".ts" + ], "transform": { "^.+\\.(ts|tsx)$": [ "ts-jest", { - "tsconfig": "test/tsconfig.test.json" + "tsconfig": "test/tsconfig.test.json", + "useESM": true } ] }, @@ -52,6 +57,9 @@ "functions": 100, "lines": 100 } + }, + "moduleNameMapper": { + "^(.+)\\.jsx?$": "$1" } }, "release": { diff --git a/scripts/build.mjs b/scripts/build.mjs index c573f6340..0f4437e90 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -35,27 +35,14 @@ async function main() { const entryPoints = ["./pkg/dist-src/index.js"]; - await Promise.all([ - // Build the a CJS Node.js bundle - esbuild.build({ - entryPoints, - outdir: "pkg/dist-node", - bundle: true, - platform: "node", - target: "node14", - format: "cjs", - ...sharedOptions, - }), - // Build an ESM browser bundle - esbuild.build({ - entryPoints, - outdir: "pkg/dist-web", - bundle: true, - platform: "browser", - format: "esm", - ...sharedOptions, - }), - ]); + await esbuild.build({ + entryPoints, + outdir: "pkg/dist-bundle", + bundle: true, + platform: "neutral", + format: "esm", + ...sharedOptions, + }); // Copy the README, LICENSE to the pkg folder await copyFile("LICENSE", "pkg/LICENSE"); @@ -74,10 +61,12 @@ async function main() { { ...pkg, files: ["dist-*/**", "bin/**"], - main: "dist-node/index.js", - browser: "dist-web/index.js", - types: "dist-types/index.d.ts", - module: "dist-src/index.js", + exports: { + ".": { + types: "./dist-types/index.d.ts", + import: "./dist-bundle/index.js", + }, + }, sideEffects: false, }, null, diff --git a/src/auth.ts b/src/auth.ts index 3c2068256..0cbf9dc13 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,4 +1,4 @@ -import type { Token, Authentication } from "./types"; +import type { Token, Authentication } from "./types.js"; const REGEX_IS_INSTALLATION_LEGACY = /^v1\./; const REGEX_IS_INSTALLATION = /^ghs_/; diff --git a/src/hook.ts b/src/hook.ts index 1f0415ba5..3260a2ac3 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -6,9 +6,9 @@ import type { RequestParameters, Route, Token, -} from "./types"; +} from "./types.js"; -import { withAuthorizationPrefix } from "./with-authorization-prefix"; +import { withAuthorizationPrefix } from "./with-authorization-prefix.js"; export async function hook( token: Token, diff --git a/src/index.ts b/src/index.ts index eaed9dffe..93368d93a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -import { auth } from "./auth"; -import { hook } from "./hook"; -import type { StrategyInterface, Token, Authentication } from "./types"; +import { auth } from "./auth.js"; +import { hook } from "./hook.js"; +import type { StrategyInterface, Token, Authentication } from "./types.js"; export type Types = { StrategyOptions: Token; diff --git a/src/types.ts b/src/types.ts index bffa9b1eb..4c57445ea 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import * as OctokitTypes from "@octokit/types"; +import type * as OctokitTypes from "@octokit/types"; export type AnyResponse = OctokitTypes.OctokitResponse; export type StrategyInterface = OctokitTypes.StrategyInterface< diff --git a/test/index.test.ts b/test/index.test.ts index e2d52c971..d01e0900f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,7 +1,7 @@ import { request } from "@octokit/request"; -import fetchMock, { MockMatcherFunction } from "fetch-mock"; +import fetchMock, { type MockMatcherFunction } from "fetch-mock"; -import { createTokenAuth } from "../src/index"; +import { createTokenAuth } from "../src/index.js"; test("README example", async () => { const auth = createTokenAuth("ghp_PersonalAccessToken01245678900000000"); @@ -173,7 +173,7 @@ test('auth.hook(request, "GET /user")', async () => { const { hook } = createTokenAuth("ghp_PersonalAccessToken01245678900000000"); const { data } = await hook(requestMock, "GET /user"); - expect(data).toStrictEqual({ id: 123 }); + expect({ ...data }).toStrictEqual({ id: 123 }); }); test("auth.hook() with JWT", async () => { @@ -204,5 +204,5 @@ test("auth.hook() with JWT", async () => { ); const { data } = await hook(requestMock, "GET /user"); - expect(data).toStrictEqual({ id: 123 }); + expect({ ...data }).toStrictEqual({ id: 123 }); }); diff --git a/test/tsconfig.test.json b/test/tsconfig.test.json index b0961e18c..169709c73 100644 --- a/test/tsconfig.test.json +++ b/test/tsconfig.test.json @@ -2,8 +2,7 @@ "extends": "../tsconfig.json", "compilerOptions": { "emitDeclarationOnly": false, - "noEmit": true, - "verbatimModuleSyntax": false + "noEmit": true }, - "include": ["src/**/*"] + "include": ["test/**/*"] } diff --git a/tsconfig.json b/tsconfig.json index c684ab8d2..60e928878 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "@octokit/tsconfig", "compilerOptions": { + "lib": ["es2023", "dom", "dom.iterable"], "esModuleInterop": true, "declaration": true, "outDir": "pkg/dist-types",