Skip to content

Commit 54aa965

Browse files
authored
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
1 parent fd8e6fa commit 54aa965

11 files changed

+96
-103
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ Node
4949
Install with <code>npm install @octokit/auth-token</code>
5050

5151
```js
52-
const { createTokenAuth } = require("@octokit/auth-token");
53-
// or: import { createTokenAuth } from "@octokit/auth-token";
52+
import { createTokenAuth } from "@octokit/auth-token";
5453
```
5554

5655
</td></tr>

package-lock.json

+51-54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
"publishConfig": {
44
"access": "public"
55
},
6+
"type": "module",
67
"version": "0.0.0-development",
78
"description": "GitHub API token authentication for browsers and Node.js",
89
"scripts": {
910
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
10-
"test": "jest --coverage",
11+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
1112
"pretest": "npm run -s lint",
1213
"lint": "prettier --check '{src,test}/**/*.{ts,md}' '*.md' package.json",
1314
"lint:fix": "prettier --write '{src,test}/**/*.{ts,md}' '*.md' package.json"
@@ -22,26 +23,30 @@
2223
"author": "Gregor Martynus (https://github.com/gr2m)",
2324
"license": "MIT",
2425
"devDependencies": {
25-
"@octokit/request": "^8.0.1",
26-
"@octokit/tsconfig": "^2.0.0",
26+
"@octokit/request": "^9.0.0",
27+
"@octokit/tsconfig": "^3.0.0",
2728
"@octokit/types": "^12.0.0",
28-
"@types/fetch-mock": "^7.3.1",
29+
"@types/fetch-mock": "^7.3.8",
2930
"@types/jest": "^29.0.0",
3031
"esbuild": "^0.20.0",
31-
"fetch-mock": "^9.0.0",
32+
"fetch-mock": "npm:@gr2m/[email protected]",
3233
"glob": "^10.2.6",
3334
"jest": "^29.0.0",
3435
"prettier": "3.2.5",
3536
"semantic-release": "^23.0.0",
36-
"ts-jest": "^29.0.0",
37-
"typescript": "^5.0.0"
37+
"ts-jest": "^29.1.0",
38+
"typescript": "^5.3.0"
3839
},
3940
"jest": {
41+
"extensionsToTreatAsEsm": [
42+
".ts"
43+
],
4044
"transform": {
4145
"^.+\\.(ts|tsx)$": [
4246
"ts-jest",
4347
{
44-
"tsconfig": "test/tsconfig.test.json"
48+
"tsconfig": "test/tsconfig.test.json",
49+
"useESM": true
4550
}
4651
]
4752
},
@@ -52,6 +57,9 @@
5257
"functions": 100,
5358
"lines": 100
5459
}
60+
},
61+
"moduleNameMapper": {
62+
"^(.+)\\.jsx?$": "$1"
5563
}
5664
},
5765
"release": {

scripts/build.mjs

+14-25
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,14 @@ async function main() {
3535

3636
const entryPoints = ["./pkg/dist-src/index.js"];
3737

38-
await Promise.all([
39-
// Build the a CJS Node.js bundle
40-
esbuild.build({
41-
entryPoints,
42-
outdir: "pkg/dist-node",
43-
bundle: true,
44-
platform: "node",
45-
target: "node14",
46-
format: "cjs",
47-
...sharedOptions,
48-
}),
49-
// Build an ESM browser bundle
50-
esbuild.build({
51-
entryPoints,
52-
outdir: "pkg/dist-web",
53-
bundle: true,
54-
platform: "browser",
55-
format: "esm",
56-
...sharedOptions,
57-
}),
58-
]);
38+
await esbuild.build({
39+
entryPoints,
40+
outdir: "pkg/dist-bundle",
41+
bundle: true,
42+
platform: "neutral",
43+
format: "esm",
44+
...sharedOptions,
45+
});
5946

6047
// Copy the README, LICENSE to the pkg folder
6148
await copyFile("LICENSE", "pkg/LICENSE");
@@ -74,10 +61,12 @@ async function main() {
7461
{
7562
...pkg,
7663
files: ["dist-*/**", "bin/**"],
77-
main: "dist-node/index.js",
78-
browser: "dist-web/index.js",
79-
types: "dist-types/index.d.ts",
80-
module: "dist-src/index.js",
64+
exports: {
65+
".": {
66+
types: "./dist-types/index.d.ts",
67+
import: "./dist-bundle/index.js",
68+
},
69+
},
8170
sideEffects: false,
8271
},
8372
null,

src/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Token, Authentication } from "./types";
1+
import type { Token, Authentication } from "./types.js";
22

33
const REGEX_IS_INSTALLATION_LEGACY = /^v1\./;
44
const REGEX_IS_INSTALLATION = /^ghs_/;

src/hook.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import type {
66
RequestParameters,
77
Route,
88
Token,
9-
} from "./types";
9+
} from "./types.js";
1010

11-
import { withAuthorizationPrefix } from "./with-authorization-prefix";
11+
import { withAuthorizationPrefix } from "./with-authorization-prefix.js";
1212

1313
export async function hook(
1414
token: Token,

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { auth } from "./auth";
2-
import { hook } from "./hook";
3-
import type { StrategyInterface, Token, Authentication } from "./types";
1+
import { auth } from "./auth.js";
2+
import { hook } from "./hook.js";
3+
import type { StrategyInterface, Token, Authentication } from "./types.js";
44

55
export type Types = {
66
StrategyOptions: Token;

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as OctokitTypes from "@octokit/types";
1+
import type * as OctokitTypes from "@octokit/types";
22

33
export type AnyResponse = OctokitTypes.OctokitResponse<any>;
44
export type StrategyInterface = OctokitTypes.StrategyInterface<

test/index.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { request } from "@octokit/request";
2-
import fetchMock, { MockMatcherFunction } from "fetch-mock";
2+
import fetchMock, { type MockMatcherFunction } from "fetch-mock";
33

4-
import { createTokenAuth } from "../src/index";
4+
import { createTokenAuth } from "../src/index.js";
55

66
test("README example", async () => {
77
const auth = createTokenAuth("ghp_PersonalAccessToken01245678900000000");
@@ -173,7 +173,7 @@ test('auth.hook(request, "GET /user")', async () => {
173173
const { hook } = createTokenAuth("ghp_PersonalAccessToken01245678900000000");
174174
const { data } = await hook(requestMock, "GET /user");
175175

176-
expect(data).toStrictEqual({ id: 123 });
176+
expect({ ...data }).toStrictEqual({ id: 123 });
177177
});
178178

179179
test("auth.hook() with JWT", async () => {
@@ -204,5 +204,5 @@ test("auth.hook() with JWT", async () => {
204204
);
205205
const { data } = await hook(requestMock, "GET /user");
206206

207-
expect(data).toStrictEqual({ id: 123 });
207+
expect({ ...data }).toStrictEqual({ id: 123 });
208208
});

test/tsconfig.test.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
44
"emitDeclarationOnly": false,
5-
"noEmit": true,
6-
"verbatimModuleSyntax": false
5+
"noEmit": true
76
},
8-
"include": ["src/**/*"]
7+
"include": ["test/**/*"]
98
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "@octokit/tsconfig",
33
"compilerOptions": {
4+
"lib": ["es2023", "dom", "dom.iterable"],
45
"esModuleInterop": true,
56
"declaration": true,
67
"outDir": "pkg/dist-types",

0 commit comments

Comments
 (0)