Skip to content

Commit 5ff96da

Browse files
authoredJun 2, 2023
fix(build): switch to esbuild (#338)
1 parent dc68be2 commit 5ff96da

File tree

6 files changed

+4252
-7843
lines changed

6 files changed

+4252
-7843
lines changed
 

‎package-lock.json

+4,151-7,813
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+4-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"description": "Error class for Octokit request errors",
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",
@@ -27,16 +27,13 @@
2727
"once": "^1.4.0"
2828
},
2929
"devDependencies": {
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-bundle-web": "^0.9.0",
34-
"@pika/plugin-ts-standard-pkg": "^0.9.0",
30+
"@octokit/tsconfig": "^1.0.2",
3531
"@types/jest": "^29.0.0",
3632
"@types/node": "^18.0.0",
3733
"@types/once": "^1.4.0",
34+
"esbuild": "^0.17.19",
35+
"glob": "^10.2.6",
3836
"jest": "^29.0.0",
39-
"pika-plugin-unpkg-field": "^1.1.0",
4037
"prettier": "2.8.8",
4138
"ts-jest": "^29.0.0",
4239
"typescript": "^5.0.0"
@@ -52,22 +49,6 @@
5249
}
5350
}
5451
},
55-
"@pika/pack": {
56-
"pipeline": [
57-
[
58-
"@pika/plugin-ts-standard-pkg"
59-
],
60-
[
61-
"@pika/plugin-build-node",
62-
{
63-
"minNodeVersion": 18
64-
}
65-
],
66-
[
67-
"@pika/plugin-build-web"
68-
]
69-
]
70-
},
7152
"release": {
7253
"branches": [
7354
"+([0-9]).x",

‎scripts/build.mjs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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: "node18",
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+
unpkg: "dist-web/index.js",
83+
},
84+
null,
85+
2
86+
)
87+
);
88+
}
89+
main();

‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import once from "once";
33
const logOnceCode = once((deprecation: any) => console.warn(deprecation));
44
const logOnceHeaders = once((deprecation: any) => console.warn(deprecation));
55

6-
import {
6+
import type {
77
RequestOptions,
88
ResponseHeaders,
99
OctokitResponse,
1010
} from "@octokit/types";
11-
import { RequestErrorOptions } from "./types";
11+
import type { RequestErrorOptions } from "./types";
1212

1313
/**
1414
* Error with extra properties to help with debugging

‎src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
RequestOptions,
33
ResponseHeaders,
44
OctokitResponse,

‎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)
Please sign in to comment.