Skip to content

fix(build): replace Pika with esbuild and tsc #221

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 6 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,055 changes: 1,516 additions & 4,539 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 API token authentication for browsers and Node.js",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check 'src/**/*.ts' 'test/**/*.ts' '*.md' package.json",
"lint:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts' '*.md' package.json",
"pretest": "npm run lint -s",
Expand All @@ -28,13 +28,12 @@
"devDependencies": {
"@octokit/core": "^4.0.0",
"@octokit/request": "^6.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": "^1.0.2",
"@tsconfig/node10": "^1.0.3",
"@types/jest": "^29.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.10.7",
"glob": "^10.2.5",
"jest": "^29.0.0",
"prettier": "2.8.8",
"ts-jest": "^29.0.0",
Expand Down Expand Up @@ -78,22 +77,6 @@
}
]
},
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-ts-standard-pkg"
],
[
"@pika/plugin-build-node",
{
"minNodeVersion": "14"
}
],
[
"@pika/plugin-build-web"
]
]
},
"engines": {
"node": ">= 14"
}
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();
2 changes: 0 additions & 2 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RequestError } from "@octokit/request-error";

import {
AnyResponse,
EndpointDefaults,
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test('auth.hook(request, "GET /repos/octocat/hello-world")', async () => {
"user-agent": "test",
};

const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
expect(headers).toStrictEqual(expectedRequestHeaders);
return true;
Expand All @@ -62,7 +62,7 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns 404', async (
"user-agent": "test",
};

const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
expect(headers).toStrictEqual(expectedRequestHeaders);
return true;
Expand Down Expand Up @@ -100,7 +100,7 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
"user-agent": "test",
};

const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
expect(headers).toStrictEqual(expectedRequestHeaders);
return true;
Expand Down Expand Up @@ -142,7 +142,7 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
"user-agent": "test",
};

const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
expect(headers).toStrictEqual(expectedRequestHeaders);
return true;
Expand Down
2 changes: 1 addition & 1 deletion test/issues.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { request } from "@octokit/request";
import fetchMock, { MockMatcherFunction } from "fetch-mock";
import fetchMock from "fetch-mock";

import { createUnauthenticatedAuth } from "../src/index";

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/**/*"]
}