Skip to content

Commit 52f74d8

Browse files
kfcampbellwolfy1339
andauthoredJun 7, 2023
fix(build): switch to esbuild and tsc instead of pika (#603)
Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>
1 parent 43b12d3 commit 52f74d8

9 files changed

+861
-2610
lines changed
 

‎package-lock.json

+732-2,575
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+12-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"description": "Octokit plugin for GitHub's recommended request throttling",
88
"scripts": {
9-
"build": "pika-pack build",
9+
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
1010
"lint": "prettier --check '{src,scripts,test}/**/*' '!*/generated/**' README.md package.json",
1111
"lint:fix": "prettier --write '{src,scripts,test}/**/*' '!*/generated/**' README.md package.json",
1212
"pretest": "npm run -s lint",
@@ -29,15 +29,14 @@
2929
"devDependencies": {
3030
"@octokit/core": "^4.0.0",
3131
"@octokit/request-error": "^3.0.0",
32-
"@pika/pack": "^0.3.7",
33-
"@pika/plugin-build-node": "^0.9.1",
34-
"@pika/plugin-build-web": "^0.9.1",
35-
"@pika/plugin-ts-standard-pkg": "^0.9.1",
32+
"@octokit/tsconfig": "^2.0.0",
3633
"@types/fetch-mock": "^7.3.1",
3734
"@types/jest": "^29.0.0",
3835
"@types/node": "^18.0.0",
36+
"esbuild": "^0.17.19",
3937
"fetch-mock": "^9.0.0",
4038
"github-openapi-graphql-query": "^4.0.0",
39+
"glob": "^10.2.6",
4140
"jest": "^29.0.0",
4241
"npm-run-all": "^4.1.5",
4342
"prettier": "2.8.8",
@@ -46,7 +45,14 @@
4645
"typescript": "^5.0.0"
4746
},
4847
"jest": {
49-
"preset": "ts-jest",
48+
"transform": {
49+
"^.+\\.(ts|tsx)$": [
50+
"ts-jest",
51+
{
52+
"tsconfig": "test/tsconfig.test.json"
53+
}
54+
]
55+
},
5056
"coverageThreshold": {
5157
"global": {
5258
"statements": 100,
@@ -56,22 +62,6 @@
5662
}
5763
}
5864
},
59-
"@pika/pack": {
60-
"pipeline": [
61-
[
62-
"@pika/plugin-ts-standard-pkg"
63-
],
64-
[
65-
"@pika/plugin-build-node",
66-
{
67-
"minNodeVersion": 18
68-
}
69-
],
70-
[
71-
"@pika/plugin-build-web"
72-
]
73-
]
74-
},
7565
"release": {
7666
"branches": [
7767
"+([0-9]).x",

‎scripts/build.mjs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import esbuild from "esbuild";
2+
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
3+
import { glob } from "glob";
4+
5+
const sharedOptions = {
6+
sourcemap: "external",
7+
sourcesContent: true,
8+
minify: false,
9+
allowOverwrite: true,
10+
packages: "external",
11+
};
12+
13+
async function main() {
14+
// Start with a clean slate
15+
await rm("pkg", { recursive: true, force: true });
16+
// Build the source code for a neutral platform as ESM
17+
await esbuild.build({
18+
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
19+
outdir: "pkg/dist-src",
20+
bundle: false,
21+
platform: "neutral",
22+
format: "esm",
23+
...sharedOptions,
24+
sourcemap: false,
25+
});
26+
27+
// Remove the types file from the dist-src folder
28+
const typeFiles = await glob([
29+
"./pkg/dist-src/**/types.js.map",
30+
"./pkg/dist-src/**/types.js",
31+
]);
32+
for (const typeFile of typeFiles) {
33+
await rm(typeFile);
34+
}
35+
36+
const entryPoints = ["./pkg/dist-src/index.js"];
37+
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+
]);
59+
60+
// Copy the README, LICENSE to the pkg folder
61+
await copyFile("LICENSE", "pkg/LICENSE");
62+
await copyFile("README.md", "pkg/README.md");
63+
64+
// Handle the package.json
65+
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
66+
// Remove unnecessary fields from the package.json
67+
delete pkg.scripts;
68+
delete pkg.prettier;
69+
delete pkg.release;
70+
delete pkg.jest;
71+
await writeFile(
72+
"pkg/package.json",
73+
JSON.stringify(
74+
{
75+
...pkg,
76+
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",
81+
sideEffects: false,
82+
},
83+
null,
84+
2
85+
)
86+
);
87+
}
88+
main();

‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @ts-expect-error
22
import BottleneckLight from "bottleneck/light";
33
import { Octokit } from "@octokit/core";
4-
import { OctokitOptions } from "@octokit/core/dist-types/types.d";
5-
import { Groups, ThrottlingOptions } from "./types";
4+
import type { OctokitOptions } from "@octokit/core/dist-types/types.d";
5+
import type { Groups, ThrottlingOptions } from "./types";
66
import { VERSION } from "./version";
77

88
import { wrapRequest } from "./wrap-request";

‎test/deprecations.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { Octokit } from "@octokit/core";
2-
import { throttling } from "../src";
1+
// Linting in `npm run test` complains when this isn't used.
2+
// In the future when conducting deprecation testing, the below
3+
// lines may be uncommented.
34

4-
const TestOctokit = Octokit.plugin(throttling);
5+
// import { Octokit } from "@octokit/core";
6+
// import { throttling } from "../src";
7+
8+
// const TestOctokit = Octokit.plugin(throttling);
59

610
describe.skip("deprecations", () => {
711
it("No deprecations", () => {});

‎test/octokit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function testPlugin(octokit: Octokit) {
88
const __requestLog: string[] = [];
99
const __requestTimings: number[] = [];
1010

11-
octokit.hook.wrap("request", async (request, options) => {
11+
octokit.hook.wrap("request", async (_, options) => {
1212
__requestLog.push(`START ${options.method} ${options.url}`);
1313
__requestTimings.push(Date.now() - t0);
1414
await new Promise((resolve) => setTimeout(resolve, 0));

‎test/retry.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("Retry", function () {
123123
it("Should not leak retryCount between requests", async function () {
124124
let counter = 1;
125125

126-
const server = createServer((req, res) => {
126+
const server = createServer((_, res) => {
127127
if (counter++ % 3 === 0) {
128128
res
129129
.writeHead(200, { "Content-Type": "application/json" })
@@ -152,7 +152,7 @@ describe("Retry", function () {
152152
fallbackSecondaryRateRetryAfter: 0,
153153
retryAfterBaseValue: 50,
154154
onRateLimit: () => true,
155-
onSecondaryRateLimit: (retryAfter, options, octokit, retryCount) => {
155+
onSecondaryRateLimit: (retryCount) => {
156156
if (retryCount < 5) {
157157
return true;
158158
}

‎test/tsconfig.test.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"emitDeclarationOnly": false,
5+
"noEmit": true,
6+
"verbatimModuleSyntax": false
7+
},
8+
"include": ["src/**/*"]
9+
}

‎tsconfig.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
2+
"extends": "@octokit/tsconfig",
23
"compilerOptions": {
34
"esModuleInterop": true,
4-
"module": "esnext",
5-
"moduleResolution": "node",
6-
"strict": true,
7-
"target": "es2018"
5+
"declaration": true,
6+
"outDir": "pkg/dist-types",
7+
"emitDeclarationOnly": true,
8+
"sourceMap": true
89
},
9-
"include": ["src/**/*"]
10+
"include": [
11+
"src/**/*"
12+
]
1013
}

0 commit comments

Comments
 (0)
Please sign in to comment.