Skip to content

Commit eaf16ab

Browse files
fix(build): switch to esbuild and tsc from pika (#345)
* Switch to esbuild and tsc from pika * Fix linting errors * Explicitly mark imports * Undo overzealous type import * npm i --save-dev @octokit/types * Use node 14 to create lockfile * Redo appropriately zealous type import * Add --verbatimModuleSyntax build option * Move verbatimModuleSyntax to tsconfig * Create tsconfig.test.json * Bump version of octokit/tsconfig * Update tsconfig.test.json Co-authored-by: wolfy1339 <[email protected]> * Move tsconfig.test.json to test directory --------- Co-authored-by: wolfy1339 <[email protected]>
1 parent 7cb89fd commit eaf16ab

9 files changed

+1568
-18451
lines changed

package-lock.json

+1,448-18,417
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+13-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"version": "0.0.0-development",
77
"description": "GitHub API token authentication for browsers and Node.js",
88
"scripts": {
9-
"build": "pika-pack build",
9+
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
1010
"test": "jest --coverage",
1111
"pretest": "npm run -s lint",
1212
"lint": "prettier --check '{src,test}/**/*.{ts,md}' '*.md' package.json",
@@ -21,19 +21,15 @@
2121
],
2222
"author": "Gregor Martynus (https://github.com/gr2m)",
2323
"license": "MIT",
24-
"dependencies": {
25-
"@octokit/types": "^9.0.0"
26-
},
2724
"devDependencies": {
28-
"@octokit/core": "^4.0.0",
2925
"@octokit/request": "^6.0.0",
30-
"@pika/pack": "^0.3.7",
31-
"@pika/plugin-build-node": "^0.9.0",
32-
"@pika/plugin-build-web": "^0.9.0",
33-
"@pika/plugin-ts-standard-pkg": "^0.9.0",
26+
"@octokit/tsconfig": "^2.0.0",
27+
"@octokit/types": "^9.2.3",
3428
"@types/fetch-mock": "^7.3.1",
3529
"@types/jest": "^29.0.0",
30+
"esbuild": "^0.17.19",
3631
"fetch-mock": "^9.0.0",
32+
"glob": "^10.2.6",
3733
"jest": "^29.0.0",
3834
"prettier": "2.8.8",
3935
"semantic-release": "^21.0.0",
@@ -42,6 +38,14 @@
4238
},
4339
"jest": {
4440
"preset": "ts-jest",
41+
"transform": {
42+
"^.+\\.(ts|tsx)$": [
43+
"ts-jest",
44+
{
45+
"tsconfig": "test/tsconfig.test.json"
46+
}
47+
]
48+
},
4549
"coverageThreshold": {
4650
"global": {
4751
"statements": 100,
@@ -73,22 +77,6 @@
7377
]
7478
]
7579
},
76-
"@pika/pack": {
77-
"pipeline": [
78-
[
79-
"@pika/plugin-ts-standard-pkg"
80-
],
81-
[
82-
"@pika/plugin-build-node",
83-
{
84-
"minNodeVersion": "14"
85-
}
86-
],
87-
[
88-
"@pika/plugin-build-web"
89-
]
90-
]
91-
},
9280
"engines": {
9381
"node": ">= 14"
9482
}

scripts/build.mjs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import esbuild from "esbuild";
2+
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
3+
import { glob } from "glob";
4+
5+
const sharedOptions = {
6+
sourcemap: "external",
7+
sourcesContent: true,
8+
minify: false,
9+
allowOverwrite: true,
10+
packages: "external",
11+
};
12+
13+
async function main() {
14+
// Start with a clean slate
15+
await rm("pkg", { recursive: true, force: true });
16+
// Build the source code for a neutral platform as ESM
17+
await esbuild.build({
18+
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
19+
outdir: "pkg/dist-src",
20+
bundle: false,
21+
platform: "neutral",
22+
format: "esm",
23+
...sharedOptions,
24+
sourcemap: false,
25+
});
26+
27+
// Remove the types file from the dist-src folder
28+
const typeFiles = await glob([
29+
"./pkg/dist-src/**/types.js.map",
30+
"./pkg/dist-src/**/types.js",
31+
]);
32+
for (const typeFile of typeFiles) {
33+
await rm(typeFile);
34+
}
35+
36+
const entryPoints = ["./pkg/dist-src/index.js"];
37+
38+
await Promise.all([
39+
// Build the a CJS Node.js bundle
40+
esbuild.build({
41+
entryPoints,
42+
outdir: "pkg/dist-node",
43+
bundle: true,
44+
platform: "node",
45+
target: "node14",
46+
format: "cjs",
47+
...sharedOptions,
48+
}),
49+
// Build an ESM browser bundle
50+
esbuild.build({
51+
entryPoints,
52+
outdir: "pkg/dist-web",
53+
bundle: true,
54+
platform: "browser",
55+
format: "esm",
56+
...sharedOptions,
57+
}),
58+
]);
59+
60+
// Copy the README, LICENSE to the pkg folder
61+
await copyFile("LICENSE", "pkg/LICENSE");
62+
await copyFile("README.md", "pkg/README.md");
63+
64+
// Handle the package.json
65+
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
66+
// Remove unnecessary fields from the package.json
67+
delete pkg.scripts;
68+
delete pkg.prettier;
69+
delete pkg.release;
70+
delete pkg.jest;
71+
await writeFile(
72+
"pkg/package.json",
73+
JSON.stringify(
74+
{
75+
...pkg,
76+
files: ["dist-*/**", "bin/**"],
77+
main: "dist-node/index.js",
78+
browser: "dist-web/index.js",
79+
types: "dist-types/index.d.ts",
80+
module: "dist-src/index.js",
81+
sideEffects: false,
82+
},
83+
null,
84+
2
85+
)
86+
);
87+
}
88+
main();

src/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Token, Authentication } from "./types";
1+
import type { Token, Authentication } from "./types";
22

33
const REGEX_IS_INSTALLATION_LEGACY = /^v1\./;
44
const REGEX_IS_INSTALLATION = /^ghs_/;

src/hook.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
AnyResponse,
33
EndpointDefaults,
44
EndpointOptions,

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { auth } from "./auth";
22
import { hook } from "./hook";
3-
import { StrategyInterface, Token, Authentication } from "./types";
3+
import type { StrategyInterface, Token, Authentication } from "./types";
44

55
export type Types = {
66
StrategyOptions: Token;

test/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ test('auth.hook(request, "GET /user")', async () => {
155155
"user-agent": "test",
156156
};
157157

158-
const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
158+
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
159159
expect(url).toEqual("https://api.github.com/user");
160160
expect(headers).toStrictEqual(expectedRequestHeaders);
161161
return true;
@@ -184,7 +184,7 @@ test("auth.hook() with JWT", async () => {
184184
"user-agent": "test",
185185
};
186186

187-
const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
187+
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
188188
expect(url).toEqual("https://api.github.com/user");
189189
expect(headers).toStrictEqual(expectedRequestHeaders);
190190
return true;

test/tsconfig.test.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"emitDeclarationOnly": false,
5+
"noEmit": true,
6+
"verbatimModuleSyntax": false
7+
},
8+
"include": ["src/**/*"]
9+
}

tsconfig.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2+
"extends": "@octokit/tsconfig",
23
"compilerOptions": {
34
"esModuleInterop": true,
4-
"module": "esnext",
5-
"moduleResolution": "node",
6-
"strict": true,
7-
"target": "es2018"
5+
"declaration": true,
6+
"outDir": "pkg/dist-types",
7+
"emitDeclarationOnly": true,
8+
"sourceMap": true,
89
},
910
"include": ["src/**/*"]
1011
}

0 commit comments

Comments
 (0)