Skip to content

Commit

Permalink
feat: package is now ESM (#393)
Browse files Browse the repository at this point in the history
* feat: package is now ESM

BREAKING CHANGE: package is now ESM

* build: add dom libs to tsconfig so it can build
  • Loading branch information
wolfy1339 authored Feb 25, 2024
1 parent fd8e6fa commit 54aa965
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 103 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Node
Install with <code>npm install @octokit/auth-token</code>

```js
const { createTokenAuth } = require("@octokit/auth-token");
// or: import { createTokenAuth } from "@octokit/auth-token";
import { createTokenAuth } from "@octokit/auth-token";
```

</td></tr>
Expand Down
105 changes: 51 additions & 54 deletions package-lock.json

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

24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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/[email protected]",
"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
}
]
},
Expand All @@ -52,6 +57,9 @@
"functions": 100,
"lines": 100
}
},
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
},
"release": {
Expand Down
39 changes: 14 additions & 25 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
@@ -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_/;
Expand Down
4 changes: 2 additions & 2 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as OctokitTypes from "@octokit/types";
import type * as OctokitTypes from "@octokit/types";

export type AnyResponse = OctokitTypes.OctokitResponse<any>;
export type StrategyInterface = OctokitTypes.StrategyInterface<
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 });
});
5 changes: 2 additions & 3 deletions test/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false
"noEmit": true
},
"include": ["src/**/*"]
"include": ["test/**/*"]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@octokit/tsconfig",
"compilerOptions": {
"lib": ["es2023", "dom", "dom.iterable"],
"esModuleInterop": true,
"declaration": true,
"outDir": "pkg/dist-types",
Expand Down

0 comments on commit 54aa965

Please sign in to comment.