Skip to content

Commit

Permalink
fix(build): replace pika with esbuild and tsc (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 authored May 21, 2023
1 parent 9942496 commit bbf539f
Show file tree
Hide file tree
Showing 11 changed files with 1,687 additions and 4,611 deletions.
6,129 changes: 1,565 additions & 4,564 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 4 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "GitHub GraphQL API client for browsers and Node",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check {src,test}/* README.md package.json",
"lint:fix": "prettier --write {src,test}/* README.md package.json",
"pretest": "npm run lint -s",
Expand All @@ -27,14 +27,13 @@
"universal-user-agent": "^6.0.0"
},
"devDependencies": {
"@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.2.5",
"@types/jest": "^29.0.0",
"@types/node": "^18.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.0.0",
"glob": "^10.2.6",
"jest": "^29.0.0",
"prettier": "2.8.8",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
Expand All @@ -52,22 +51,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
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 2 additions & 7 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { ResponseHeaders } from "@octokit/types";
import {
GraphQlEndpointOptions,
GraphQlQueryResponse,
GraphQlQueryResponseData,
GraphQlResponse,
} from "./types";
import type { ResponseHeaders } from "@octokit/types";
import type { GraphQlEndpointOptions, GraphQlQueryResponse } from "./types";

type ServerResponseData<T> = Required<GraphQlQueryResponse<T>>;

Expand Down
4 changes: 2 additions & 2 deletions src/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { request as Request } from "@octokit/request";
import { ResponseHeaders } from "@octokit/types";
import type { ResponseHeaders } from "@octokit/types";
import { GraphqlResponseError } from "./error";
import {
import type {
GraphQlEndpointOptions,
RequestParameters,
GraphQlQueryResponse,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const graphql = withDefaults(request, {
url: "/graphql",
});

export { GraphQlQueryResponseData } from "./types";
export type { GraphQlQueryResponseData } from "./types";
export { GraphqlResponseError } from "./error";

export function withCustomRequest(customRequest: typeof request) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
EndpointOptions,
RequestParameters as RequestParametersType,
EndpointInterface,
Expand Down
6 changes: 5 additions & 1 deletion src/with-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { request as Request } from "@octokit/request";
import { graphql as ApiInterface, Query, RequestParameters } from "./types";
import type {
graphql as ApiInterface,
Query,
RequestParameters,
} from "./types";
import { graphql } from "./graphql";

export function withDefaults(
Expand Down
8 changes: 4 additions & 4 deletions test/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("errors", () => {
.post("https://api.github.com/graphql", mockResponse),
},
})
.then((result) => {
.then(() => {
throw new Error("Should not resolve");
})

Expand Down Expand Up @@ -80,7 +80,7 @@ describe("errors", () => {
.post("https://api.github.com/graphql", mockResponse),
},
})
.then((result) => {
.then(() => {
throw new Error("Should not resolve");
})

Expand Down Expand Up @@ -142,7 +142,7 @@ describe("errors", () => {
}),
},
})
.then((result) => {
.then(() => {
throw new Error("Should not resolve");
})
.catch((error) => {
Expand Down Expand Up @@ -175,7 +175,7 @@ describe("errors", () => {
fetch: fetchMock.sandbox().post("https://api.github.com/graphql", 500),
},
})
.then((result) => {
.then(() => {
throw new Error("Should not resolve");
})
.catch((error) => {
Expand Down
10 changes: 5 additions & 5 deletions test/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("graphql()", () => {
.sandbox()
.post(
"https://api.github.com/graphql",
(url, options: OctokitTypes.RequestOptions) => {
(_url, options: OctokitTypes.RequestOptions) => {
const body = JSON.parse(options.body);
expect(body.query).toEqual(query);
expect(body.variables).toStrictEqual({
Expand Down Expand Up @@ -139,7 +139,7 @@ describe("graphql()", () => {
.sandbox()
.post(
"https://api.github.com/graphql",
(url, options: OctokitTypes.RequestOptions) => {
(_url, options: OctokitTypes.RequestOptions) => {
const body = JSON.parse(options.body);
expect(body.query).toEqual(query);
expect(body.variables).toStrictEqual({
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("graphql()", () => {
.sandbox()
.post(
"https://api.github.com/graphql",
(url, options: OctokitTypes.RequestOptions) => {
(_url, options: OctokitTypes.RequestOptions) => {
const body = JSON.parse(options.body);
expect(body.query).toEqual(query);
expect(body.variables).toStrictEqual({
Expand Down Expand Up @@ -214,7 +214,7 @@ describe("graphql()", () => {
.sandbox()
.post(
"https://api.github.com/graphql",
(url, options: OctokitTypes.RequestOptions) => {
(_url, options: OctokitTypes.RequestOptions) => {
const body = JSON.parse(options.body);
expect(body.query).toEqual(query);
expect(body.variables).toEqual(undefined);
Expand All @@ -241,7 +241,7 @@ describe("graphql()", () => {
.sandbox()
.post(
"https://api.github.com/graphql",
(url, options: OctokitTypes.RequestOptions) => {
(_url, options: OctokitTypes.RequestOptions) => {
expect(options.headers.accept).toContain("antiope-preview");
expect(options.headers.accept).toContain("testpkg-preview");
return { data: {} };
Expand Down
11 changes: 6 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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 bbf539f

Please sign in to comment.