Skip to content

Commit

Permalink
fix(build): switch to esbuild and tsc from pika (#345)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
kfcampbell and wolfy1339 authored Jun 6, 2023
1 parent 7cb89fd commit eaf16ab
Show file tree
Hide file tree
Showing 9 changed files with 1,568 additions and 18,451 deletions.
19,865 changes: 1,448 additions & 18,417 deletions package-lock.json

Large diffs are not rendered by default.

38 changes: 13 additions & 25 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",
"test": "jest --coverage",
"pretest": "npm run -s lint",
"lint": "prettier --check '{src,test}/**/*.{ts,md}' '*.md' package.json",
Expand All @@ -21,19 +21,15 @@
],
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"dependencies": {
"@octokit/types": "^9.0.0"
},
"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": "^2.0.0",
"@octokit/types": "^9.2.3",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.0.0",
"glob": "^10.2.6",
"jest": "^29.0.0",
"prettier": "2.8.8",
"semantic-release": "^21.0.0",
Expand All @@ -42,6 +38,14 @@
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand Down Expand Up @@ -73,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
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();
2 changes: 1 addition & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Token, Authentication } from "./types";
import type { Token, Authentication } from "./types";

const REGEX_IS_INSTALLATION_LEGACY = /^v1\./;
const REGEX_IS_INSTALLATION = /^ghs_/;
Expand Down
2 changes: 1 addition & 1 deletion src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
AnyResponse,
EndpointDefaults,
EndpointOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { auth } from "./auth";
import { hook } from "./hook";
import { StrategyInterface, Token, Authentication } from "./types";
import type { StrategyInterface, Token, Authentication } from "./types";

export type Types = {
StrategyOptions: Token;
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ test('auth.hook(request, "GET /user")', async () => {
"user-agent": "test",
};

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

const matchGetUser: MockMatcherFunction = (url, { body, headers }) => {
const matchGetUser: MockMatcherFunction = (url, { headers }) => {
expect(url).toEqual("https://api.github.com/user");
expect(headers).toStrictEqual(expectedRequestHeaders);
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/**/*"]
}
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 eaf16ab

Please sign in to comment.