Skip to content

Commit 01c0f8d

Browse files
fix(build): switch to esbuild in place of pika (#161)
* Initial pass at switching to esbuild * Clean up build by importing/exporting types and tweaking config * Bump jest and jest types versions * Correct fetch-mock import statements * Remove pika key from package.json * Apply suggestions from code review --------- Co-authored-by: wolfy1339 <[email protected]>
1 parent e6b2249 commit 01c0f8d

10 files changed

+788
-3548
lines changed

package-lock.json

Lines changed: 660 additions & 3516 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"version": "0.0.0-development",
77
"description": "Convenience method to create/edit/delete a text file based on its current content",
88
"scripts": {
9-
"build": "pika-pack build",
9+
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
1010
"lint": "prettier --check '{src,test}/**/*' README.md package.json",
1111
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
1212
"pretest": "npm run -s lint",
@@ -24,15 +24,13 @@
2424
},
2525
"devDependencies": {
2626
"@octokit/core": "^4.0.0",
27-
"@octokit/tsconfig": "^1.0.2",
28-
"@pika/pack": "^0.5.0",
29-
"@pika/plugin-build-node": "^0.9.2",
30-
"@pika/plugin-build-web": "^0.9.2",
31-
"@pika/plugin-ts-standard-pkg": "^0.9.2",
32-
"@types/jest": "^29.0.0",
27+
"@octokit/tsconfig": "^2.0.0",
28+
"@types/jest": "^29.5.2",
3329
"@types/node": "^18.0.0",
30+
"esbuild": "^0.17.19",
3431
"fetch-mock": "^9.11.0",
35-
"jest": "^29.0.0",
32+
"glob": "^10.2.7",
33+
"jest": "^29.5.0",
3634
"prettier": "2.8.8",
3735
"semantic-release": "^21.0.0",
3836
"semantic-release-plugin-update-version-in-files": "^1.1.0",
@@ -43,7 +41,14 @@
4341
"@octokit/core": ">= 3"
4442
},
4543
"jest": {
46-
"preset": "ts-jest",
44+
"transform": {
45+
"^.+\\.(ts|tsx)$": [
46+
"ts-jest",
47+
{
48+
"tsconfig": "test/tsconfig.test.json"
49+
}
50+
]
51+
},
4752
"coverageThreshold": {
4853
"global": {
4954
"statements": 100,
@@ -53,22 +58,6 @@
5358
}
5459
}
5560
},
56-
"@pika/pack": {
57-
"pipeline": [
58-
[
59-
"@pika/plugin-ts-standard-pkg"
60-
],
61-
[
62-
"@pika/plugin-build-node",
63-
{
64-
"minNodeVersion": "14"
65-
}
66-
],
67-
[
68-
"@pika/plugin-build-web"
69-
]
70-
]
71-
},
7261
"release": {
7362
"branches": [
7463
"+([0-9]).x",

scripts/build.mjs

Lines changed: 88 additions & 0 deletions
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/compose-create-or-update-text-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Octokit } from "@octokit/core";
22

33
import { getFileContents } from "./get-file-content";
4-
import { Options, Response, ContentUpdateFunctionOptions } from "./types";
4+
import type { Options, Response, ContentUpdateFunctionOptions } from "./types";
55
import { utf8ToBase64 } from "./utils";
66

77
/**

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { Octokit } from "@octokit/core";
22

33
import { composeCreateOrUpdateTextFile } from "./compose-create-or-update-text-file";
44
import { VERSION } from "./version";
5-
import { Options } from "./types";
5+
import type { Options } from "./types";
66

77
export { composeCreateOrUpdateTextFile } from "./compose-create-or-update-text-file";
8-
export { Options, Response, ContentUpdateFunction } from "./types";
8+
export type { Options, Response, ContentUpdateFunction } from "./types";
99

1010
/**
1111
* @param octokit Octokit instance

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Endpoints } from "@octokit/types";
1+
import type { Endpoints } from "@octokit/types";
22

33
type User = {
44
name: string;

test/create-or-update-text-file.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fetchMock from "fetch-mock";
1+
import fetchMock from "fetch-mock";
22
import { Octokit } from "@octokit/core";
33

44
import { createOrUpdateTextFile } from "../src";

test/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fetchMock from "fetch-mock";
1+
import fetchMock from "fetch-mock";
22
import { Octokit } from "@octokit/core";
33

44
import { createOrUpdateTextFile } from "../src";

test/tsconfig.test.json

Lines changed: 9 additions & 0 deletions
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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
{ "extends": "@octokit/tsconfig", "include": ["src/**/*"] }
1+
{
2+
"extends": "@octokit/tsconfig",
3+
"compilerOptions": {
4+
"esModuleInterop": true,
5+
"declaration": true,
6+
"outDir": "pkg/dist-types",
7+
"emitDeclarationOnly": true,
8+
"sourceMap": true
9+
},
10+
"include": ["src/**/*"]
11+
}

0 commit comments

Comments
 (0)