From 90e12d027530cfa95544c054094b664a9c5e9cc5 Mon Sep 17 00:00:00 2001 From: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:51:48 -0500 Subject: [PATCH] feat: package is now ESM (#235) BREAKING CHANGE: package is now ESM --- README.md | 4 +- package-lock.json | 150 +++++++++++++++++++++++++++------- package.json | 18 ++-- scripts/build.mjs | 44 ++++------ src/auth.ts | 4 +- src/get-oauth-access-token.ts | 2 +- src/hook.ts | 4 +- src/index.ts | 10 +-- test/index.test.ts | 3 +- test/smoke.test.ts | 2 +- test/tsconfig.test.json | 3 +- 11 files changed, 163 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index c188eb4..53a7d29 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Node Install with `npm install @octokit/core @octokit/auth-oauth-device` ```js -const { createOAuthDeviceAuth } = require("@octokit/auth-oauth-device"); +import { createOAuthDeviceAuth } from "@octokit/auth-oauth-device"; ``` @@ -224,7 +224,7 @@ Must be either `oauth-app` or `github-app`. Defaults to `oauth-app`. You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the API root endpoint. Example: ```js -const { request } = require("@octokit/request"); +import { request } from "@octokit/request"; createOAuthDeviceAuth({ clientId: "1234567890abcdef1234", clientSecret: "secret", diff --git a/package-lock.json b/package-lock.json index b52d847..789e6c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,10 @@ "version": "0.0.0-development", "license": "MIT", "dependencies": { - "@octokit/oauth-methods": "^4.0.0", - "@octokit/request": "^8.0.0", + "@octokit/oauth-methods": "^5.0.0", + "@octokit/request": "^9.0.0", "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.0" }, "devDependencies": { "@octokit/tsconfig": "^3.0.0", @@ -1779,11 +1779,27 @@ "node": ">= 18" } }, - "node_modules/@octokit/endpoint": { + "node_modules/@octokit/core/node_modules/@octokit/endpoint": { "version": "9.0.4", "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-9.0.4.tgz", "integrity": "sha512-DWPLtr1Kz3tv8L0UvXTDP1fNwM0S+z6EJpRcvH66orY6Eld4XBMCSYsaWp4xIm61jTWxK68BrR7ibO+vSDnZqw==", + "dev": true, + "dependencies": { + "@octokit/types": "^12.0.0", + "universal-user-agent": "^6.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/core/node_modules/@octokit/request": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.2.0.tgz", + "integrity": "sha512-exPif6x5uwLqv1N1irkLG1zZNJkOtj8bZxuVHd71U5Ftuxf2wGNvAJyNBcPbPC+EBzwYEbBDdSFb8EPcjpYxPQ==", + "dev": true, "dependencies": { + "@octokit/endpoint": "^9.0.0", + "@octokit/request-error": "^5.0.0", "@octokit/types": "^12.0.0", "universal-user-agent": "^6.0.0" }, @@ -1791,6 +1807,24 @@ "node": ">= 18" } }, + "node_modules/@octokit/core/node_modules/universal-user-agent": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.1.tgz", + "integrity": "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==", + "dev": true + }, + "node_modules/@octokit/endpoint": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-10.0.0.tgz", + "integrity": "sha512-emBcNDxBdC1y3+knJonS5zhUB/CG6TihubxM2U1/pG/Z1y3a4oV0Gzz3lmkCvWWQI6h3tqBAX9MgCBFp+M68Jw==", + "dependencies": { + "@octokit/types": "^12.0.0", + "universal-user-agent": "^7.0.2" + }, + "engines": { + "node": ">= 18" + } + }, "node_modules/@octokit/graphql": { "version": "7.0.2", "resolved": "https://registry.npmjs.org/@octokit/graphql/-/graphql-7.0.2.tgz", @@ -1805,24 +1839,68 @@ "node": ">= 18" } }, - "node_modules/@octokit/oauth-authorization-url": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/@octokit/oauth-authorization-url/-/oauth-authorization-url-6.0.2.tgz", - "integrity": "sha512-CdoJukjXXxqLNK4y/VOiVzQVjibqoj/xHgInekviUJV73y/BSIcwvJ/4aNHPBPKcPWFnd4/lO9uqRV65jXhcLA==", + "node_modules/@octokit/graphql/node_modules/@octokit/endpoint": { + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-9.0.4.tgz", + "integrity": "sha512-DWPLtr1Kz3tv8L0UvXTDP1fNwM0S+z6EJpRcvH66orY6Eld4XBMCSYsaWp4xIm61jTWxK68BrR7ibO+vSDnZqw==", + "dev": true, + "dependencies": { + "@octokit/types": "^12.0.0", + "universal-user-agent": "^6.0.0" + }, "engines": { "node": ">= 18" } }, - "node_modules/@octokit/oauth-methods": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@octokit/oauth-methods/-/oauth-methods-4.0.1.tgz", - "integrity": "sha512-1NdTGCoBHyD6J0n2WGXg9+yDLZrRNZ0moTEex/LSPr49m530WNKcCfXDghofYptr3st3eTii+EHoG5k/o+vbtw==", + "node_modules/@octokit/graphql/node_modules/@octokit/request": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.2.0.tgz", + "integrity": "sha512-exPif6x5uwLqv1N1irkLG1zZNJkOtj8bZxuVHd71U5Ftuxf2wGNvAJyNBcPbPC+EBzwYEbBDdSFb8EPcjpYxPQ==", + "dev": true, "dependencies": { - "@octokit/oauth-authorization-url": "^6.0.2", - "@octokit/request": "^8.0.2", + "@octokit/endpoint": "^9.0.0", "@octokit/request-error": "^5.0.0", "@octokit/types": "^12.0.0", - "btoa-lite": "^1.0.0" + "universal-user-agent": "^6.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/graphql/node_modules/universal-user-agent": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.1.tgz", + "integrity": "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==", + "dev": true + }, + "node_modules/@octokit/oauth-authorization-url": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/@octokit/oauth-authorization-url/-/oauth-authorization-url-7.0.0.tgz", + "integrity": "sha512-/x3GvKiAD0ntPvVlbuObuF+sP36CKDvXzwwyQG4VIf6TjqXFXFQ/BsmbZF+mD+rTSImmRPwH+Vr6HndyR+5qIQ==", + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/oauth-methods": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@octokit/oauth-methods/-/oauth-methods-5.0.0.tgz", + "integrity": "sha512-atKKkzwR0ke0z0mFFTMHg8PEmoFDWa5sxdzX5b6wy8poMfAKny2dh5/KtsEbIqAWh8gfYVqNRwwUDMiw6cZ7pg==", + "dependencies": { + "@octokit/oauth-authorization-url": "^7.0.0", + "@octokit/request": "^9.0.0", + "@octokit/request-error": "^6.0.0", + "@octokit/types": "^12.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/oauth-methods/node_modules/@octokit/request-error": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-6.0.1.tgz", + "integrity": "sha512-EPeILOAEGqwmEG6g7dYlbvqkjJ5nWNcZvQpgeLEn1MieIcJ0xK0CJtvVcIMXqzIM8DOWzvI6zL5FQAwjTvk/Tg==", + "dependencies": { + "@octokit/types": "^12.0.0" }, "engines": { "node": ">= 18" @@ -1882,14 +1960,14 @@ } }, "node_modules/@octokit/request": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.2.0.tgz", - "integrity": "sha512-exPif6x5uwLqv1N1irkLG1zZNJkOtj8bZxuVHd71U5Ftuxf2wGNvAJyNBcPbPC+EBzwYEbBDdSFb8EPcjpYxPQ==", + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/@octokit/request/-/request-9.0.0.tgz", + "integrity": "sha512-hShsrZ40W8dz3TkyD5ifHNcrYfYpYF7Sh2x+ZKafA+eO0zNeKDTsbF1E0xrUf0mVYGeKL+NLMO5SnfAP22l05Q==", "dependencies": { - "@octokit/endpoint": "^9.0.0", - "@octokit/request-error": "^5.0.0", + "@octokit/endpoint": "^10.0.0", + "@octokit/request-error": "^6.0.1", "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.2" }, "engines": { "node": ">= 18" @@ -1899,6 +1977,7 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.0.1.tgz", "integrity": "sha512-X7pnyTMV7MgtGmiXBwmO6M5kIPrntOXdyKZLigNfQWSEQzVxR4a4vo49vJjTWX70mPndj8KhfT4Dx+2Ng3vnBQ==", + "dev": true, "dependencies": { "@octokit/types": "^12.0.0", "deprecation": "^2.0.0", @@ -1908,6 +1987,17 @@ "node": ">= 18" } }, + "node_modules/@octokit/request/node_modules/@octokit/request-error": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-6.0.1.tgz", + "integrity": "sha512-EPeILOAEGqwmEG6g7dYlbvqkjJ5nWNcZvQpgeLEn1MieIcJ0xK0CJtvVcIMXqzIM8DOWzvI6zL5FQAwjTvk/Tg==", + "dependencies": { + "@octokit/types": "^12.0.0" + }, + "engines": { + "node": ">= 18" + } + }, "node_modules/@octokit/tsconfig": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@octokit/tsconfig/-/tsconfig-3.0.0.tgz", @@ -2795,11 +2885,6 @@ "node-int64": "^0.4.0" } }, - "node_modules/btoa-lite": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/btoa-lite/-/btoa-lite-1.0.0.tgz", - "integrity": "sha512-gvW7InbIyF8AicrqWoptdW08pUxuhq8BEgowNajy9RhiE86fmGAGl+bLKo6oB8QP0CkqHLowfN0oJdKC/J6LbA==" - }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -3663,7 +3748,8 @@ "node_modules/deprecation": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/deprecation/-/deprecation-2.3.1.tgz", - "integrity": "sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==" + "integrity": "sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==", + "dev": true }, "node_modules/detect-newline": { "version": "3.1.0", @@ -9998,6 +10084,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "dev": true, "dependencies": { "wrappy": "1" } @@ -11842,9 +11929,9 @@ } }, "node_modules/universal-user-agent": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.1.tgz", - "integrity": "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==" + "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==" }, "node_modules/universalify": { "version": "2.0.1", @@ -12095,7 +12182,8 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "dev": true }, "node_modules/write-file-atomic": { "version": "4.0.2", diff --git a/package.json b/package.json index eeefda2..5f2fe7e 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,13 @@ "name": "@octokit/auth-oauth-device", "version": "0.0.0-development", "description": "GitHub OAuth Device authentication strategy for JavaScript", + "type": "module", "scripts": { "build": "node scripts/build.mjs && tsc -p tsconfig.json", "lint": "prettier --check '{src,test,scripts}/**/*' README.md package.json", "lint:fix": "prettier --write '{src,test,scripts}/**/*' README.md package.json", "pretest": "npm run -s lint", - "test": "jest --coverage" + "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage" }, "repository": "github:octokit/auth-oauth-device.js", "keywords": [ @@ -19,10 +20,10 @@ "author": "Gregor Martynus (https://dev.to/gr2m)", "license": "MIT", "dependencies": { - "@octokit/oauth-methods": "^4.0.0", - "@octokit/request": "^8.0.0", + "@octokit/oauth-methods": "^5.0.0", + "@octokit/request": "^9.0.0", "@octokit/types": "^12.0.0", - "universal-user-agent": "^6.0.0" + "universal-user-agent": "^7.0.0" }, "devDependencies": { "@octokit/tsconfig": "^3.0.0", @@ -39,11 +40,15 @@ "typescript": "^5.0.0" }, "jest": { + "extensionsToTreatAsEsm": [ + ".ts" + ], "transform": { "^.+\\.(ts|tsx)$": [ "ts-jest", { - "tsconfig": "test/tsconfig.test.json" + "tsconfig": "test/tsconfig.test.json", + "useESM": true } ] }, @@ -54,6 +59,9 @@ "functions": 100, "lines": 100 } + }, + "moduleNameMapper": { + "^(.+)\\.jsx?$": "$1" } }, "release": { diff --git a/scripts/build.mjs b/scripts/build.mjs index f040b38..a5ccb1b 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -8,6 +8,9 @@ const sharedOptions = { minify: false, allowOverwrite: true, packages: "external", + target: "es2022", + format: "esm", + platform: "neutral", }; async function main() { @@ -18,8 +21,6 @@ async function main() { entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]), outdir: "pkg/dist-src", bundle: false, - platform: "neutral", - format: "esm", ...sharedOptions, sourcemap: false, }); @@ -33,29 +34,12 @@ async function main() { await rm(typeFile); } - 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: ["./pkg/dist-src/index.js"], + outdir: "pkg/dist-bundle", + bundle: true, + ...sharedOptions, + }); // Copy the README, LICENSE to the pkg folder await copyFile("LICENSE", "pkg/LICENSE"); @@ -74,10 +58,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 68cb945..2072b9a 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,4 +1,4 @@ -import { getOAuthAccessToken } from "./get-oauth-access-token"; +import { getOAuthAccessToken } from "./get-oauth-access-token.js"; import type { OAuthAppAuthOptions, GitHubAppAuthOptions, @@ -6,7 +6,7 @@ import type { GitHubAppAuthentication, OAuthAppState, GitHubAppState, -} from "./types"; +} from "./types.js"; export async function auth( state: OAuthAppState | GitHubAppState, diff --git a/src/get-oauth-access-token.ts b/src/get-oauth-access-token.ts index 3cf060a..6b75d44 100644 --- a/src/get-oauth-access-token.ts +++ b/src/get-oauth-access-token.ts @@ -11,7 +11,7 @@ import type { ClientType, GitHubAppAuthenticationWithExpiration, Verification, -} from "./types"; +} from "./types.js"; export async function getOAuthAccessToken( state: OAuthAppState | GitHubAppState, diff --git a/src/hook.ts b/src/hook.ts index 1bd94d5..126eae3 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -6,8 +6,8 @@ import type { Route, } from "@octokit/types"; -import { getOAuthAccessToken } from "./get-oauth-access-token"; -import type { OAuthAppState, GitHubAppState } from "./types"; +import { getOAuthAccessToken } from "./get-oauth-access-token.js"; +import type { OAuthAppState, GitHubAppState } from "./types.js"; import type { EndpointDefaults } from "@octokit/types"; export async function hook( diff --git a/src/index.ts b/src/index.ts index 9aadb13..a2729f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import { getUserAgent } from "universal-user-agent"; import { request as octokitRequest } from "@octokit/request"; -import { auth } from "./auth"; -import { hook } from "./hook"; +import { auth } from "./auth.js"; +import { hook } from "./hook.js"; import type { GitHubAppAuthInterface, GitHubAppState, @@ -10,8 +10,8 @@ import type { OAuthAppAuthInterface, OAuthAppState, OAuthAppStrategyOptions, -} from "./types"; -import { VERSION } from "./version"; +} from "./types.js"; +import { VERSION } from "./version.js"; // Remember to update README.md#Types when changing exports export type { @@ -22,7 +22,7 @@ export type { GitHubAppAuthOptions, GitHubAppAuthentication, GitHubAppAuthenticationWithExpiration, -} from "./types"; +} from "./types.js"; export function createOAuthDeviceAuth( options: OAuthAppStrategyOptions, diff --git a/test/index.test.ts b/test/index.test.ts index 68d1a9c..64f8684 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,7 +1,8 @@ import fetchMock from "fetch-mock"; import { request } from "@octokit/request"; +import { jest } from "@jest/globals"; -import { createOAuthDeviceAuth } from "../src/index"; +import { createOAuthDeviceAuth } from "../src/index.js"; test("README example", async () => { const mock = fetchMock diff --git a/test/smoke.test.ts b/test/smoke.test.ts index 4bd0f81..6d295e3 100644 --- a/test/smoke.test.ts +++ b/test/smoke.test.ts @@ -1,4 +1,4 @@ -import { createOAuthDeviceAuth } from "../src"; +import { createOAuthDeviceAuth } from "../src/index.js"; describe("Smoke test", () => { it("is a function", () => { diff --git a/test/tsconfig.test.json b/test/tsconfig.test.json index b0961e1..fe99a4e 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/**/*"] }