Skip to content

Commit

Permalink
fix(build): replace Pika with esbuild and tsc (#562)
Browse files Browse the repository at this point in the history
* build: remove pika

* build: use `@octokit/tsconfig`

* test: remove unused variables and parameters

* build: add esbuild

* build: update `build` script

* buid(tsconfig): add options for `.d.ts` generation
  • Loading branch information
wolfy1339 committed May 18, 2023
1 parent e1fd75e commit 5d1c767
Show file tree
Hide file tree
Showing 9 changed files with 1,992 additions and 4,674 deletions.
6,515 changes: 1,880 additions & 4,635 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 4 additions & 21 deletions package.json
Expand Up @@ -6,7 +6,7 @@
},
"description": "Extendable client for GitHub's REST & GraphQL APIs",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*.{ts,md}' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*.{ts,md}' README.md package.json",
"pretest": "npm run -s lint",
Expand Down Expand Up @@ -34,15 +34,14 @@
},
"devDependencies": {
"@octokit/auth": "^3.0.1",
"@pika/pack": "^0.3.7",
"@pika/plugin-build-node": "^0.9.0",
"@pika/plugin-build-web": "^0.9.0",
"@pika/plugin-ts-standard-pkg": "^0.9.0",
"@octokit/tsconfig": "^1.0.2",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/lolex": "^5.1.0",
"@types/node": "^18.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.0.0",
"glob": "^10.2.5",
"http-proxy-agent": "^6.0.0",
"jest": "^29.0.0",
"lolex": "^6.0.0",
Expand All @@ -64,22 +63,6 @@
}
}
},
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-ts-standard-pkg"
],
[
"@pika/plugin-build-node",
{
"minNodeVersion": "14"
}
],
[
"@pika/plugin-build-web"
]
]
},
"release": {
"branches": [
"+([0-9]).x",
Expand Down
92 changes: 92 additions & 0 deletions scripts/build.mjs
@@ -0,0 +1,92 @@
// @ts-check
import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "fs/promises";
import { glob } from "glob";

/**
* @type {esbuild.BuildOptions}
*/
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: "node14",
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",
module: "dist-web/index.js",
types: "dist-types/index.d.ts",
source: "dist-src/index.js",
sideEffects: false,
},
null,
2
)
);
}
main();
2 changes: 1 addition & 1 deletion test/constructor.test.ts
Expand Up @@ -57,7 +57,7 @@ describe("Smoke test", () => {
},
});

octokit.hook.wrap("request", (request, options) => {
octokit.hook.wrap("request", (_request, options) => {
// @ts-ignore
expect(options.request.foo).toEqual("bar");
return {
Expand Down
6 changes: 3 additions & 3 deletions test/defaults.test.ts
Expand Up @@ -198,7 +198,7 @@ describe("Octokit.defaults", () => {
});

it("Octokit.defaults(function)", () => {
const plugin = (octokit: Octokit, options: any) => {
const plugin = (_octokit: Octokit, options: any) => {
expect(options).toStrictEqual({
foo: {
bar: 1,
Expand All @@ -225,7 +225,7 @@ describe("Octokit.defaults", () => {
});

it("Octokit.defaults(opts).defaults(function)", () => {
const plugin = (octokit: Octokit, options: any) => {
const plugin = (_octokit: Octokit, options: any) => {
expect(options).toStrictEqual({
other: "foo",
foo: {
Expand Down Expand Up @@ -257,7 +257,7 @@ describe("Octokit.defaults", () => {
});

it("Octokit.defaults(function).defaults(opts)", () => {
const plugin = (octokit: Octokit, options: any) => {
const plugin = (_octokit: Octokit, options: any) => {
expect(options).toStrictEqual({
other: "foo",
foo: {
Expand Down
11 changes: 4 additions & 7 deletions test/graphql.test.ts
@@ -1,10 +1,7 @@
import { getUserAgent } from "universal-user-agent";
import fetchMock from "fetch-mock";

import { Octokit } from "../src";

const userAgent = `octokit-core.js/0.0.0-development ${getUserAgent()}`;

describe("octokit.graphql()", () => {
it("is a function", () => {
const octokit = new Octokit();
Expand All @@ -21,7 +18,7 @@ describe("octokit.graphql()", () => {
};
const mock = fetchMock
.sandbox()
.postOnce("https://api.github.com/graphql", (url, request) => {
.postOnce("https://api.github.com/graphql", (_url, request) => {
const body = JSON.parse(request.body!.toString());
expect(body.query).toEqual(query);

Expand Down Expand Up @@ -60,7 +57,7 @@ describe("octokit.graphql()", () => {
};
const mock = fetchMock
.sandbox()
.postOnce("https://github.acme-inc.com/api/graphql", (url, request) => {
.postOnce("https://github.acme-inc.com/api/graphql", (_url, request) => {
const body = JSON.parse(request.body!.toString());
expect(body.query).toEqual(query);

Expand Down Expand Up @@ -93,7 +90,7 @@ describe("octokit.graphql()", () => {
it("custom headers: octokit.graphql({ query, headers })", async () => {
const mock = fetchMock
.sandbox()
.postOnce("https://api.github.com/graphql", (url, request) => {
.postOnce("https://api.github.com/graphql", (_url, request) => {
// @ts-ignore `request.headers` are typed incorrectly by fetch-mock
expect(request.headers["x-custom"]).toEqual("value");

Expand Down Expand Up @@ -122,7 +119,7 @@ describe("octokit.graphql()", () => {
it("custom headers: octokit.graphql(query, { headers })", async () => {
const mock = fetchMock
.sandbox()
.postOnce("https://api.github.com/graphql", (url, request) => {
.postOnce("https://api.github.com/graphql", (_url, request) => {
// @ts-ignore `request.headers` are typed incorrectly by fetch-mock
expect(request.headers["x-custom"]).toEqual("value");

Expand Down
4 changes: 2 additions & 2 deletions test/hook.test.ts
Expand Up @@ -201,11 +201,11 @@ describe("octokit.hook", () => {
const octokit = new Octokit();

let beforeMagicCalled = false;
octokit.hook.before("magic", (options: any) => {
octokit.hook.before("magic", (_options: any) => {
beforeMagicCalled = true;
});

await octokit.hook("magic", (options: any) => {
await octokit.hook("magic", (_options: any) => {
return {
magic: true,
};
Expand Down
2 changes: 1 addition & 1 deletion test/plugin.test.ts
Expand Up @@ -33,7 +33,7 @@ describe("Octokit.plugin()", () => {
});

it("receives client options", () => {
const MyOctokit = Octokit.plugin((octokit, options) => {
const MyOctokit = Octokit.plugin((_octokit, options) => {
expect(options).toStrictEqual({
foo: "bar",
});
Expand Down
9 changes: 5 additions & 4 deletions tsconfig.json
@@ -1,10 +1,11 @@
{
"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/**/*"]
}

0 comments on commit 5d1c767

Please sign in to comment.