Skip to content

Commit

Permalink
fix(build): switch to esbuild and tsc instead of pika (#644)
Browse files Browse the repository at this point in the history
* Switch to esbuild and tsc instead of pika

* Fix linting errors

* import/export types explicitly

* Move types to a dev dependency

* Remove unused deprecation dependency

* Move types back to a regular dependency

* Update src/endpoints-to-methods.ts

Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>

* Update src/index.ts

Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>

* Regenerate package-lock.json with npm 14

* npm run lint:fix

* Bump major version of tsconfig

* Update package.json

Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>

---------

Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>
  • Loading branch information
kfcampbell and wolfy1339 committed Jun 7, 2023
1 parent ca3eae8 commit 917af29
Show file tree
Hide file tree
Showing 13 changed files with 1,145 additions and 12,730 deletions.
13,693 changes: 1,010 additions & 12,683 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 13 additions & 24 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 @@ -57,7 +55,14 @@
"@octokit/core": ">=3"
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand All @@ -67,22 +72,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/**/*"]
}

0 comments on commit 917af29

Please sign in to comment.