Skip to content

fix(build): switch to esbuild and tsc instead of pika #644

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

Merged
merged 13 commits into from
Jun 7, 2023
13,693 changes: 1,010 additions & 12,683 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 13 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0-development",
"description": "Octokit plugin adding one method for all of api.github.com REST API endpoints",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*' '!src/generated/**' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' '!src/generated/**' README.md package.json",
"pretest": "npm run -s lint",
Expand All @@ -24,22 +24,20 @@
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/types": "^9.2.3",
"deprecation": "^2.3.1"
"@octokit/types": "^9.2.3"
},
"devDependencies": {
"@gimenete/type-writer": "^0.1.5",
"@octokit/core": "^4.0.0",
"@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": "^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",
"fs-extra": "^11.0.0",
"github-openapi-graphql-query": "^4.0.0",
"glob": "^10.2.6",
"jest": "^29.0.0",
"lodash.camelcase": "^4.3.0",
"lodash.set": "^4.3.2",
Expand All @@ -58,6 +56,14 @@
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand All @@ -67,22 +73,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
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: "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",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
module: "dist-src/index.js",
sideEffects: false,
},
null,
2
)
);
}
main();
11 changes: 7 additions & 4 deletions src/endpoints-to-methods.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Octokit } from "@octokit/core";
import {
import type { Octokit } from "@octokit/core";
import type {
EndpointOptions,
RequestParameters,
RequestMethod,
Route,
Url,
} from "@octokit/types";
import { EndpointsDefaultsAndDecorations, EndpointDecorations } from "./types";
import { RestEndpointMethods } from "./generated/method-types";
import type {
EndpointsDefaultsAndDecorations,
EndpointDecorations,
} from "./types";
import type { RestEndpointMethods } from "./generated/method-types";

type EndpointMethods = {
[methodName: string]: typeof Octokit.prototype.request;
Expand Down
2 changes: 1 addition & 1 deletion src/generated/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EndpointsDefaultsAndDecorations } from "../types";
import type { EndpointsDefaultsAndDecorations } from "../types";
const Endpoints: EndpointsDefaultsAndDecorations = {
actions: {
addCustomLabelsToSelfHostedRunnerForOrg: [
Expand Down
4 changes: 2 additions & 2 deletions src/generated/method-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EndpointInterface, RequestInterface } from "@octokit/types";
import { RestEndpointMethodTypes } from "./parameters-and-response-types";
import type { EndpointInterface, RequestInterface } from "@octokit/types";
import type { RestEndpointMethodTypes } from "./parameters-and-response-types";

export type RestEndpointMethods = {
actions: {
Expand Down
2 changes: 1 addition & 1 deletion src/generated/parameters-and-response-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Endpoints, RequestParameters } from "@octokit/types";
import type { Endpoints, RequestParameters } from "@octokit/types";

export type RestEndpointMethodTypes = {
actions: {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Octokit } from "@octokit/core";
import type { Octokit } from "@octokit/core";

import ENDPOINTS from "./generated/endpoints";
export { RestEndpointMethodTypes } from "./generated/parameters-and-response-types";
export type { RestEndpointMethodTypes } from "./generated/parameters-and-response-types";
import { VERSION } from "./version";
import { Api } from "./types";
import type { Api } from "./types";
import { endpointsToMethods } from "./endpoints-to-methods";

export function restEndpointMethods(octokit: Octokit): Api {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route, RequestParameters } from "@octokit/types";
import type { Route, RequestParameters } from "@octokit/types";

import { RestEndpointMethods } from "./generated/method-types";
import type { RestEndpointMethods } from "./generated/method-types";

export type Api = { rest: RestEndpointMethods };

Expand Down
4 changes: 2 additions & 2 deletions test/rest-endpoint-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("REST API endpoint methods", () => {
headers: {
"content-type": "text/plain; charset=utf-8",
},
matcher: (url, { body, headers }) => {
matcher: (_, { body }) => {
expect(body).toEqual("# Hello, world!");
return true;
},
Expand Down Expand Up @@ -94,7 +94,7 @@ describe("REST API endpoint methods", () => {
name: "test.txt",
label: "test",
},
matcher: (url, { body }) => {
matcher: (_, { body }) => {
expect(body).toEqual("test 1, 2");
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/**/*"]
}
2 changes: 1 addition & 1 deletion test/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Smoke test", () => {
return updateLabel(options);

async function updateLabel(
options: RestEndpointMethodTypes["issues"]["updateLabel"]["parameters"]
_: RestEndpointMethodTypes["issues"]["updateLabel"]["parameters"]
): Promise<RestEndpointMethodTypes["issues"]["updateLabel"]["response"]> {
return {
headers: {},
Expand Down
13 changes: 6 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +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/**/*"
]
"include": ["src/**/*"]
}