Skip to content

Commit

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

* Regenerate package-lock.json with Node v14
  • Loading branch information
kfcampbell authored Jun 9, 2023
1 parent 604fc9a commit 7fd20a0
Show file tree
Hide file tree
Showing 11 changed files with 1,596 additions and 18,447 deletions.
19,887 changes: 1,472 additions & 18,415 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 12 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Turns REST API endpoints into generic request options",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{scripts,src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{scripts,src,test}/**/*' README.md package.json",
"pretest": "npm run -s lint",
Expand All @@ -22,11 +22,10 @@
"author": "Gregor Martynus (https://github.com/gr2m)",
"license": "MIT",
"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": "^2.0.0",
"@types/jest": "^29.0.0",
"esbuild": "^0.17.19",
"glob": "^10.2.7",
"jest": "^29.0.0",
"prettier": "2.8.8",
"semantic-release": "^21.0.0",
Expand Down Expand Up @@ -72,7 +71,14 @@
]
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand All @@ -82,22 +88,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/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getUserAgent } from "universal-user-agent";
import { EndpointDefaults } from "@octokit/types";
import type { EndpointDefaults } from "@octokit/types";

import { VERSION } from "./version";

Expand Down
2 changes: 1 addition & 1 deletion src/endpoint-with-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EndpointOptions, RequestParameters, Route } from "@octokit/types";
import type { EndpointOptions, RequestParameters, Route } from "@octokit/types";

import { DEFAULTS } from "./defaults";
import { merge } from "./merge";
Expand Down
6 changes: 5 additions & 1 deletion src/merge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { EndpointDefaults, RequestParameters, Route } from "@octokit/types";
import type {
EndpointDefaults,
RequestParameters,
Route,
} from "@octokit/types";

import { lowercaseKeys } from "./util/lowercase-keys";
import { mergeDeep } from "./util/merge-deep";
Expand Down
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
EndpointDefaults,
RequestMethod,
RequestOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/with-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
EndpointInterface,
RequestParameters,
EndpointDefaults,
Expand Down
2 changes: 1 addition & 1 deletion test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EndpointOptions, EndpointDefaults } from "@octokit/types";
import type { EndpointOptions, EndpointDefaults } from "@octokit/types";

import { endpoint } from "../src";

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 7fd20a0

Please sign in to comment.