Skip to content

Commit

Permalink
fix(build): switch to esbuild and tsc instead of pika (#603)
Browse files Browse the repository at this point in the history
Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>
  • Loading branch information
kfcampbell and wolfy1339 authored Jun 7, 2023
1 parent 43b12d3 commit 52f74d8
Show file tree
Hide file tree
Showing 9 changed files with 861 additions and 2,610 deletions.
3,307 changes: 732 additions & 2,575 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 12 additions & 22 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 @@ -46,7 +45,14 @@
"typescript": "^5.0.0"
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand All @@ -56,22 +62,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/**/*"
]
}

0 comments on commit 52f74d8

Please sign in to comment.