Skip to content

Commit 4986fb0

Browse files
authoredFeb 25, 2024··
feat: package is now ESM (#675)
BREAKING CHANGE: package is now ESM
1 parent 0404bc6 commit 4986fb0

15 files changed

+123
-133
lines changed
 

‎package-lock.json

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

‎package.json

+19-11
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@
44
"publishConfig": {
55
"access": "public"
66
},
7+
"type": "module",
78
"description": "Octokit plugin for GitHub's recommended request throttling",
89
"scripts": {
910
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
1011
"lint": "prettier --check '{src,scripts,test}/**/*' '!*/generated/**' README.md package.json",
1112
"lint:fix": "prettier --write '{src,scripts,test}/**/*' '!*/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
"update-endpoints": "npm-run-all update-endpoints:*",
15-
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json",
16-
"update-endpoints:code": "node scripts/update-endpoints/code",
17-
"validate:ts": "npm run build && tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node test/typescript-validate.ts"
16+
"update-endpoints:fetch-json": "node scripts/update-endpoints/fetch-json.js",
17+
"update-endpoints:code": "node scripts/update-endpoints/code.js",
18+
"validate:ts": "tsc --noEmit --noImplicitAny --target es2022 --esModuleInterop --moduleResolution node16 --module node16 test/typescript-validate.ts"
1819
},
1920
"repository": "github:octokit/plugin-throttling.js",
2021
"author": "Simon Grondin (http://github.com/SGrondin)",
2122
"license": "MIT",
2223
"dependencies": {
23-
"@octokit/types": "^12.2.0",
24+
"@octokit/types": "^12.6.0",
2425
"bottleneck": "^2.15.3"
2526
},
2627
"peerDependencies": {
27-
"@octokit/core": "^5.0.0"
28+
"@octokit/core": "^6.0.0"
2829
},
2930
"devDependencies": {
30-
"@octokit/core": "^5.0.0",
31-
"@octokit/request-error": "^5.0.0",
32-
"@octokit/tsconfig": "^2.0.0",
31+
"@octokit/core": "^6.0.0",
32+
"@octokit/request-error": "^6.0.1",
33+
"@octokit/tsconfig": "^3.0.0",
3334
"@types/fetch-mock": "^7.3.1",
3435
"@types/jest": "^29.0.0",
3536
"@types/node": "^20.0.0",
3637
"esbuild": "^0.20.0",
37-
"fetch-mock": "^9.0.0",
38+
"fetch-mock": "npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1",
3839
"github-openapi-graphql-query": "^4.0.0",
3940
"glob": "^10.2.6",
4041
"jest": "^29.0.0",
@@ -45,11 +46,15 @@
4546
"typescript": "^5.0.0"
4647
},
4748
"jest": {
49+
"extensionsToTreatAsEsm": [
50+
".ts"
51+
],
4852
"transform": {
4953
"^.+\\.(ts|tsx)$": [
5054
"ts-jest",
5155
{
52-
"tsconfig": "test/tsconfig.test.json"
56+
"tsconfig": "test/tsconfig.test.json",
57+
"useESM": true
5358
}
5459
]
5560
},
@@ -60,6 +65,9 @@
6065
"functions": 100,
6166
"lines": 100
6267
}
68+
},
69+
"moduleNameMapper": {
70+
"^(.+)\\.jsx?$": "$1"
6371
}
6472
},
6573
"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: "node18",
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,

‎scripts/update-endpoints/code.js

+5-3
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, readFileSync } 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(new URL("./generated/endpoints.json", import.meta.url), "utf-8"),
13+
);
1214
const paths = [];
1315

1416
for (const endpoint of ENDPOINTS) {

‎scripts/update-endpoints/fetch-json.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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");
3+
import graphql from "github-openapi-graphql-query";
54

65
if (!process.env.VERSION) {
76
throw new Error("VERSION environment variable must be set");
@@ -21,14 +20,11 @@ const QUERY = `
2120
main();
2221

2322
async function main() {
24-
const {
25-
data: { endpoints },
26-
} = await graphql(QUERY, {
27-
version,
28-
});
23+
const res = await graphql(QUERY, { version });
24+
const endpoints = res.data.endpoints;
2925

3026
writeFileSync(
31-
path.resolve(__dirname, "generated", "endpoints.json"),
27+
new URL("./generated/endpoints.json", import.meta.url),
3228
JSON.stringify(endpoints, null, 2) + "\n",
3329
);
3430
}

‎src/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import BottleneckLight from "bottleneck/light";
33
import type TBottleneck from "bottleneck";
44
import { Octokit } from "@octokit/core";
5-
import type { OctokitOptions } from "@octokit/core/dist-types/types.d";
6-
import type { Groups, State, ThrottlingOptions } from "./types";
7-
import { VERSION } from "./version";
5+
import type { OctokitOptions } from "@octokit/core";
6+
import type { Groups, State, ThrottlingOptions } from "./types.js";
7+
import { VERSION } from "./version.js";
88

9-
import { wrapRequest } from "./wrap-request";
10-
import triggersNotificationPaths from "./generated/triggers-notification-paths";
11-
import { routeMatcher } from "./route-matcher";
9+
import { wrapRequest } from "./wrap-request.js";
10+
import triggersNotificationPaths from "./generated/triggers-notification-paths.js";
11+
import { routeMatcher } from "./route-matcher.js";
1212
import type { EndpointDefaults, OctokitResponse } from "@octokit/types";
1313

1414
// Workaround to allow tests to directly access the triggersNotification function.
@@ -191,15 +191,15 @@ export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) {
191191
// the types expect that the function can return either a Promise of the response, or directly return the response.
192192
// This is due to the fact that `@octokit/request` uses aysnc functions
193193
// Also, since we add the custom `retryCount` property to the request argument, the types are not compatible.
194-
// @ts-expect-error
194+
// @ts-ignore We use the ignore instead of expect-error because TypeScript cannot make up it's mind if there is an error or not.
195195
octokit.hook.wrap("request", wrapRequest.bind(null, state));
196196

197197
return {};
198198
}
199199
throttling.VERSION = VERSION;
200200
throttling.triggersNotification = triggersNotification;
201201

202-
declare module "@octokit/core/dist-types/types.d" {
202+
declare module "@octokit/core" {
203203
interface OctokitOptions {
204204
throttle?: ThrottlingOptions;
205205
}

‎src/wrap-request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { EndpointDefaults, OctokitResponse } from "@octokit/types";
2-
import type { State } from "./types";
2+
import type { State } from "./types.js";
33

44
const noop = () => Promise.resolve();
55

‎test/events.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { TestOctokit } from "./octokit";
1+
import { TestOctokit } from "./octokit.ts";
2+
import { jest } from "@jest/globals";
23

34
describe("Events", function () {
45
it("Should support non-limit 403s", async function () {

‎test/exports.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ThrottlingOptions } from "../src";
1+
import type { ThrottlingOptions } from "../src/index.ts";
22
import type { Octokit } from "@octokit/core";
33

44
describe("Exports", function () {

‎test/octokit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Octokit } from "@octokit/core";
22
import { RequestError } from "@octokit/request-error";
3-
import { throttling } from "../src";
3+
import { throttling } from "../src/index.ts";
44

55
function testPlugin(octokit: Octokit) {
66
const t0 = Date.now();

‎test/plugin.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Bottleneck from "bottleneck";
2-
import { TestOctokit } from "./octokit";
3-
import { throttling } from "../src";
2+
import { TestOctokit } from "./octokit.ts";
3+
import { throttling } from "../src/index.ts";
44

55
describe("General", function () {
66
it("Should be possible to disable the plugin", async function () {

‎test/retry.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import Bottleneck from "bottleneck";
2-
import { TestOctokit } from "./octokit";
2+
import { TestOctokit } from "./octokit.ts";
33
import { Octokit } from "@octokit/core";
4-
import { throttling } from "../src";
5-
import { AddressInfo } from "net";
6-
import { createServer } from "http";
4+
import { throttling } from "../src/index.ts";
5+
import type { AddressInfo } from "node:net";
6+
import { createServer } from "node:http";
7+
import { jest } from "@jest/globals";
78

89
jest.setTimeout(20000);
910

‎test/smoke.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { throttling } from "../src";
1+
import { throttling } from "../src/index.ts";
22

33
describe("Smoke test", () => {
44
it("is a function", () => {

‎test/tsconfig.test.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"compilerOptions": {
44
"emitDeclarationOnly": false,
55
"noEmit": true,
6-
"verbatimModuleSyntax": false
6+
"allowImportingTsExtensions": true
77
},
88
"include": ["src/**/*"]
99
}

‎test/typescript-validate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Octokit } from "@octokit/core";
2-
import { throttling } from "../pkg";
2+
import { throttling } from "../src/index.js";
33
// ************************************************************
44
// THIS CODE IS NOT EXECUTED. IT IS FOR TYPECHECKING ONLY
55
// ************************************************************

0 commit comments

Comments
 (0)
Please sign in to comment.