Skip to content

Commit

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

* build: use `@octokit/tsconfig`

* build: add esbuild

* build: remove defunct script

* build(tsconfig): add compiler options needed to generate the `.d.ts`

* test: remove unused variables

* build: update `build` script
  • Loading branch information
wolfy1339 authored May 18, 2023
1 parent eea39fd commit f588939
Show file tree
Hide file tree
Showing 7 changed files with 1,637 additions and 4,586 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ jobs:
cache: npm
- run: npm ci
- run: npm run build
# https://github.com/octokit/auth-app.js/pull/475
- name: "Fix pkg.files file pattern"
run: node scripts/fix-package-json.js
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
6,060 changes: 1,528 additions & 4,532 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 @@
"version": "0.0.0-development",
"description": "GitHub App authentication for JavaScript",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*.{ts,md}' README.md *.json",
"lint:fix": "prettier --write '{src,test}/**/*.{ts,md}' README.md *.json",
"pretest": "npm run -s lint",
Expand Down Expand Up @@ -34,16 +34,15 @@
"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",
"@sinonjs/fake-timers": "^8.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^18.11.18",
"@types/sinonjs__fake-timers": "^8.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.0.0",
"glob": "^10.2.5",
"jest": "^29.0.0",
"prettier": "2.8.8",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
Expand All @@ -61,22 +60,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();
18 changes: 0 additions & 18 deletions scripts/fix-package-json.js

This file was deleted.

16 changes: 8 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ test("installationId strategy option fails with no installationId", async () =>

test("repositoryIds auth option", async () => {
const matchCreateInstallationAccessToken: MockMatcherFunction = (
url,
_url,
{ body }
) => {
expect(JSON.parse(String(body))).toStrictEqual({
Expand Down Expand Up @@ -399,7 +399,7 @@ test("repositoryIds auth option", async () => {

test("repositoryNames auth option", async () => {
const matchCreateInstallationAccessToken: MockMatcherFunction = (
url,
_url,
{ body }
) => {
expect(JSON.parse(String(body))).toStrictEqual({
Expand Down Expand Up @@ -464,7 +464,7 @@ test("repositoryNames auth option", async () => {

test("Consistenly handling reposID & reposName", async () => {
const matchCreateInstallationAccessToken: MockMatcherFunction = (
url,
_url,
{ body }
) => {
expect(JSON.parse(String(body))).toStrictEqual({
Expand Down Expand Up @@ -529,7 +529,7 @@ test("Consistenly handling reposID & reposName", async () => {

test("permissions auth option", async () => {
const matchCreateInstallationAccessToken: MockMatcherFunction = (
url,
_url,
{ body }
) => {
expect(JSON.parse(String(body))).toStrictEqual({
Expand Down Expand Up @@ -764,11 +764,11 @@ test("installation auth with selected permissions from cache", async () => {
});

test("installation cache with different options", async () => {
const matchCreateAccessToken1: MockMatcherFunction = (url, { body }) => {
const matchCreateAccessToken1: MockMatcherFunction = (_url, { body }) => {
expect(body).toBeUndefined();
return true;
};
const matchCreateAccessToken2: MockMatcherFunction = (url, { body }) => {
const matchCreateAccessToken2: MockMatcherFunction = (_url, { body }) => {
expect(JSON.parse(String(body))).toStrictEqual({
permissions: {
metadata: "read",
Expand Down Expand Up @@ -1786,7 +1786,7 @@ test("auth.hook(): handle 401 in first 5 seconds (#65)", async () => {
},
repository_selection: "all",
})
.get("https://api.github.com/repos/octocat/hello-world", (url) => {
.get("https://api.github.com/repos/octocat/hello-world", () => {
if (Date.now() < FIVE_SECONDS_IN_MS) {
return {
status: 401,
Expand Down Expand Up @@ -1877,7 +1877,7 @@ test("auth.hook(): throw error with custom message after unsuccessful retries (#
},
repository_selection: "all",
})
.get("https://api.github.com/repos/octocat/hello-world", (url) => {
.get("https://api.github.com/repos/octocat/hello-world", () => {
return {
status: 401,
body: {
Expand Down
9 changes: 5 additions & 4 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 f588939

Please sign in to comment.