Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to esbuild and tsc instead of pika #603

Merged
merged 6 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,307 changes: 732 additions & 2,575 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 12 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Octokit plugin for GitHub's recommended request throttling",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,scripts,test}/**/*' '!*/generated/**' README.md package.json",
"lint:fix": "prettier --write '{src,scripts,test}/**/*' '!*/generated/**' README.md package.json",
"pretest": "npm run -s lint",
Expand All @@ -29,15 +29,14 @@
"devDependencies": {
"@octokit/core": "^4.0.0",
"@octokit/request-error": "^3.0.0",
"@pika/pack": "^0.3.7",
"@pika/plugin-build-node": "^0.9.1",
"@pika/plugin-build-web": "^0.9.1",
"@pika/plugin-ts-standard-pkg": "^0.9.1",
"@octokit/tsconfig": "^2.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^18.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.0.0",
"github-openapi-graphql-query": "^4.0.0",
"glob": "^10.2.6",
"jest": "^29.0.0",
"npm-run-all": "^4.1.5",
"prettier": "2.8.8",
Expand All @@ -47,6 +46,14 @@
},
"jest": {
"preset": "ts-jest",
kfcampbell marked this conversation as resolved.
Show resolved Hide resolved
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand All @@ -56,22 +63,6 @@
}
}
},
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-ts-standard-pkg"
],
[
"@pika/plugin-build-node",
{
"minNodeVersion": 18
}
],
[
"@pika/plugin-build-web"
]
]
},
"release": {
"branches": [
"+([0-9]).x",
Expand Down
88 changes: 88 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
import { glob } from "glob";

const sharedOptions = {
sourcemap: "external",
sourcesContent: true,
minify: false,
allowOverwrite: true,
packages: "external",
};

async function main() {
// Start with a clean slate
await rm("pkg", { recursive: true, force: true });
// Build the source code for a neutral platform as ESM
await esbuild.build({
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
outdir: "pkg/dist-src",
bundle: false,
platform: "neutral",
format: "esm",
...sharedOptions,
sourcemap: false,
});

// Remove the types file from the dist-src folder
const typeFiles = await glob([
"./pkg/dist-src/**/types.js.map",
"./pkg/dist-src/**/types.js",
]);
for (const typeFile of typeFiles) {
await rm(typeFile);
}

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: "node18",
format: "cjs",
...sharedOptions,
}),
// Build an ESM browser bundle
esbuild.build({
entryPoints,
outdir: "pkg/dist-web",
bundle: true,
platform: "browser",
format: "esm",
...sharedOptions,
}),
]);

// Copy the README, LICENSE to the pkg folder
await copyFile("LICENSE", "pkg/LICENSE");
await copyFile("README.md", "pkg/README.md");

// Handle the package.json
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
// Remove unnecessary fields from the package.json
delete pkg.scripts;
delete pkg.prettier;
delete pkg.release;
delete pkg.jest;
await writeFile(
"pkg/package.json",
JSON.stringify(
{
...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",
sideEffects: false,
},
null,
2
)
);
}
main();
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-expect-error
import BottleneckLight from "bottleneck/light";
import { Octokit } from "@octokit/core";
import { OctokitOptions } from "@octokit/core/dist-types/types.d";
import { Groups, ThrottlingOptions } from "./types";
import type { OctokitOptions } from "@octokit/core/dist-types/types.d";
import type { Groups, ThrottlingOptions } from "./types";
import { VERSION } from "./version";

import { wrapRequest } from "./wrap-request";
Expand Down
10 changes: 7 additions & 3 deletions test/deprecations.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Octokit } from "@octokit/core";
import { throttling } from "../src";
// Linting in `npm run test` complains when this isn't used.
// In the future when conducting deprecation testing, the below
// lines may be uncommented.

const TestOctokit = Octokit.plugin(throttling);
// import { Octokit } from "@octokit/core";
// import { throttling } from "../src";

// const TestOctokit = Octokit.plugin(throttling);

describe.skip("deprecations", () => {
it("No deprecations", () => {});
Expand Down
2 changes: 1 addition & 1 deletion test/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function testPlugin(octokit: Octokit) {
const __requestLog: string[] = [];
const __requestTimings: number[] = [];

octokit.hook.wrap("request", async (request, options) => {
octokit.hook.wrap("request", async (_, options) => {
__requestLog.push(`START ${options.method} ${options.url}`);
__requestTimings.push(Date.now() - t0);
await new Promise((resolve) => setTimeout(resolve, 0));
Expand Down
4 changes: 2 additions & 2 deletions test/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe("Retry", function () {
it("Should not leak retryCount between requests", async function () {
let counter = 1;

const server = createServer((req, res) => {
const server = createServer((_, res) => {
if (counter++ % 3 === 0) {
res
.writeHead(200, { "Content-Type": "application/json" })
Expand Down Expand Up @@ -152,7 +152,7 @@ describe("Retry", function () {
fallbackSecondaryRateRetryAfter: 0,
retryAfterBaseValue: 50,
onRateLimit: () => true,
onSecondaryRateLimit: (retryAfter, options, octokit, retryCount) => {
onSecondaryRateLimit: (retryCount) => {
if (retryCount < 5) {
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions test/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false
},
"include": ["src/**/*"]
}
13 changes: 8 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"extends": "@octokit/tsconfig",
"compilerOptions": {
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"target": "es2018"
"declaration": true,
"outDir": "pkg/dist-types",
"emitDeclarationOnly": true,
"sourceMap": true
},
"include": ["src/**/*"]
"include": [
"src/**/*"
]
}