Skip to content

Commit a5e4b5d

Browse files
authored
feat: package is now ESM (#596)
BREAKING CHANGE: package is now ESM
1 parent 5b84386 commit a5e4b5d

File tree

7 files changed

+165
-190
lines changed

7 files changed

+165
-190
lines changed

package-lock.json

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

package.json

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"publishConfig": {
44
"access": "public"
55
},
6+
"type": "module",
67
"version": "0.0.0-development",
78
"description": "Octokit plugin to paginate REST API endpoint responses",
89
"scripts": {
910
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
1011
"lint": "prettier --check '{src,test,scripts}/**/*' '!scripts/generated/*' README.md package.json",
1112
"lint:fix": "prettier --write '{src,test,scripts}/**/*' '!scripts/generated/*' README.md package.json",
1213
"pretest": "npm run -s lint",
13-
"test": "jest --coverage",
14+
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
1415
"test:ts": "npx tsc --noEmit --declaration --noUnusedLocals --allowImportingTsExtensions test/validate-typescript.ts",
1516
"update-endpoints": "npm-run-all update-endpoints:*",
1617
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json",
@@ -28,17 +29,17 @@
2829
"@octokit/types": "^12.6.0"
2930
},
3031
"peerDependencies": {
31-
"@octokit/core": "5"
32+
"@octokit/core": ">=6"
3233
},
3334
"devDependencies": {
34-
"@octokit/core": "^5.1.0",
35-
"@octokit/plugin-rest-endpoint-methods": "^10.4.0",
36-
"@octokit/tsconfig": "^2.0.0",
35+
"@octokit/core": "^6.0.1",
36+
"@octokit/plugin-rest-endpoint-methods": "^11.0.1",
37+
"@octokit/tsconfig": "^3.0.0",
3738
"@types/fetch-mock": "^7.3.1",
3839
"@types/jest": "^29.0.0",
3940
"@types/node": "^20.0.0",
4041
"esbuild": "^0.20.0",
41-
"fetch-mock": "^9.0.0",
42+
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
4243
"github-openapi-graphql-query": "^4.0.0",
4344
"glob": "^10.2.5",
4445
"jest": "^29.0.0",
@@ -49,11 +50,15 @@
4950
"typescript": "^5.0.0"
5051
},
5152
"jest": {
53+
"extensionsToTreatAsEsm": [
54+
".ts"
55+
],
5256
"transform": {
5357
"^.+\\.(ts|tsx)$": [
5458
"ts-jest",
5559
{
56-
"tsconfig": "test/tsconfig.test.json"
60+
"tsconfig": "test/tsconfig.test.json",
61+
"useESM": true
5762
}
5863
]
5964
},

scripts/build.mjs

+18-28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const sharedOptions = {
1212
minify: false,
1313
allowOverwrite: true,
1414
packages: "external",
15+
format: "esm",
16+
target: "es2022",
17+
platform: "neutral",
1518
};
1619

1720
async function main() {
@@ -22,8 +25,6 @@ async function main() {
2225
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
2326
outdir: "pkg/dist-src",
2427
bundle: false,
25-
platform: "neutral",
26-
format: "esm",
2728
...sharedOptions,
2829
sourcemap: false,
2930
});
@@ -37,29 +38,12 @@ async function main() {
3738
await rm(typeFile);
3839
}
3940

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

6448
// Copy the README, LICENSE to the pkg folder
6549
await copyFile("LICENSE", "pkg/LICENSE");
@@ -78,10 +62,16 @@ async function main() {
7862
{
7963
...pkg,
8064
files: ["dist-*/**", "bin/**"],
81-
main: "dist-node/index.js",
82-
module: "dist-web/index.js",
65+
// Tooling currently are having issues with the "exports" field, ex: TypeScript, eslint
66+
// We add a `main` and `types` field to the package.json for the time being
67+
main: "dist-bundle/index.js",
8368
types: "dist-types/index.d.ts",
84-
source: "dist-src/index.js",
69+
exports: {
70+
".": {
71+
types: "./dist-types/index.d.ts",
72+
import: "./dist-bundle/index.js",
73+
},
74+
},
8575
sideEffects: false,
8676
},
8777
null,

scripts/update-endpoints/fetch-json.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const { writeFileSync } = require("fs");
2-
const path = require("path");
1+
import { writeFileSync } from "node:fs";
32

4-
const graphql = require("github-openapi-graphql-query");
5-
const prettier = require("prettier");
3+
import graphql from "github-openapi-graphql-query";
4+
import prettier from "prettier";
65

76
if (!process.env.VERSION) {
87
throw new Error("VERSION environment variable must be set");
@@ -42,7 +41,7 @@ async function main() {
4241
});
4342

4443
writeFileSync(
45-
path.resolve(__dirname, "generated", "endpoints.json"),
44+
new URL("./generated/endpoints.json", import.meta.url),
4645
await prettier.format(JSON.stringify(endpoints), {
4746
parser: "json",
4847
}),

scripts/update-endpoints/typescript.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
* trigger notifications. So instead we automatically generate a file that
55
* only contains these paths when @octokit/openapi has a new release.
66
*/
7-
const { writeFileSync } = require("fs");
7+
import { writeFileSync } from "node:fs";
88

9-
const prettier = require("prettier");
9+
import prettier from "prettier";
1010

11-
const ENDPOINTS = require("./generated/endpoints.json");
11+
const ENDPOINTS = JSON.parse(
12+
readFileSync("./generated/endpoints.json", "utf-8"),
13+
);
1214
const endpoints = [];
1315

1416
// All of these cases have been reported to the relevant team in GitHub.
@@ -137,7 +139,7 @@ function endpointToKey(endpoint) {
137139
}
138140
async function main() {
139141
writeFileSync(
140-
"./src/generated/paginating-endpoints.ts",
142+
new URL("./src/generated/paginating-endpoints.ts", import.meta.url),
141143
await prettier.format(
142144
`import type { Endpoints } from "@octokit/types";
143145

src/paginate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
RequestParameters,
88
Route,
99
RequestInterface,
10-
} from "./types";
10+
} from "./types.js";
1111

1212
export function paginate(
1313
octokit: Octokit,

test/paginate.test.ts

+13-19
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe("pagination", () => {
288288
})
289289
.then((organizations) => {
290290
// @ts-ignore
291-
expect(organizations).toStrictEqual([{ id: 1 }, { id: 2 }]);
291+
expect(organizations).toEqual([{ id: 1 }, { id: 2 }]);
292292
});
293293
});
294294

@@ -312,7 +312,7 @@ describe("pagination", () => {
312312
[Symbol.asyncIterator]();
313313

314314
return iterator.next().then((result) => {
315-
expect(result.value.data).toStrictEqual(ORG1);
315+
expect(result.value.data).toEqual(ORG1);
316316
});
317317
});
318318

@@ -530,7 +530,7 @@ describe("pagination", () => {
530530
},
531531
})
532532
.then((results) => {
533-
expect(results).toStrictEqual([...result1.items, ...result2.items]);
533+
expect(results).toEqual([...result1.items, ...result2.items]);
534534
});
535535
});
536536

@@ -586,7 +586,7 @@ describe("pagination", () => {
586586
per_page: 1,
587587
})
588588
.then((results) => {
589-
expect(results).toStrictEqual([
589+
expect(results).toEqual([
590590
...result1.repositories,
591591
...result2.repositories,
592592
]);
@@ -642,7 +642,7 @@ describe("pagination", () => {
642642
per_page: 1,
643643
})
644644
.then((results) => {
645-
expect(results).toStrictEqual([
645+
expect(results).toEqual([
646646
...result1.repositories,
647647
...result2.repositories,
648648
]);
@@ -707,10 +707,7 @@ describe("pagination", () => {
707707
per_page: 1,
708708
})
709709
.then((results) => {
710-
expect(results).toStrictEqual([
711-
...result1.artifacts,
712-
...result2.artifacts,
713-
]);
710+
expect(results).toEqual([...result1.artifacts, ...result2.artifacts]);
714711
});
715712
});
716713

@@ -771,7 +768,7 @@ describe("pagination", () => {
771768
per_page: 1,
772769
})
773770
.then((results) => {
774-
expect(results).toStrictEqual([...result1.secrets, ...result2.secrets]);
771+
expect(results).toEqual([...result1.secrets, ...result2.secrets]);
775772
});
776773
});
777774

@@ -832,10 +829,7 @@ describe("pagination", () => {
832829
per_page: 1,
833830
})
834831
.then((results) => {
835-
expect(results).toStrictEqual([
836-
...result1.workflows,
837-
...result2.workflows,
838-
]);
832+
expect(results).toEqual([...result1.workflows, ...result2.workflows]);
839833
});
840834
});
841835
it(".paginate() with results namespace (GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs)", () => {
@@ -896,7 +890,7 @@ describe("pagination", () => {
896890
per_page: 1,
897891
})
898892
.then((results) => {
899-
expect(results).toStrictEqual([...result1.jobs, ...result2.jobs]);
893+
expect(results).toEqual([...result1.jobs, ...result2.jobs]);
900894
});
901895
});
902896
it(".paginate() with results namespace (GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs)", () => {
@@ -957,7 +951,7 @@ describe("pagination", () => {
957951
per_page: 1,
958952
})
959953
.then((results) => {
960-
expect(results).toStrictEqual([
954+
expect(results).toEqual([
961955
...result1.workflow_runs,
962956
...result2.workflow_runs,
963957
]);
@@ -1020,7 +1014,7 @@ describe("pagination", () => {
10201014
per_page: 1,
10211015
})
10221016
.then((results) => {
1023-
expect(results).toStrictEqual([
1017+
expect(results).toEqual([
10241018
...result1.workflow_runs,
10251019
...result2.workflow_runs,
10261020
]);
@@ -1056,7 +1050,7 @@ describe("pagination", () => {
10561050
per_page: 1,
10571051
})
10581052
.then((results) => {
1059-
expect(results).toStrictEqual([...result.repositories]);
1053+
expect(results).toEqual([...result.repositories]);
10601054
});
10611055
});
10621056

@@ -1155,7 +1149,7 @@ describe("pagination", () => {
11551149
per_page: 1,
11561150
})
11571151
.then((results) => {
1158-
expect(results).toStrictEqual([
1152+
expect(results).toEqual([
11591153
...result1.workflow_runs,
11601154
...result2.workflow_runs,
11611155
]);

0 commit comments

Comments
 (0)