Skip to content

Commit 5ca150f

Browse files
authoredMay 20, 2023
fix(build): replace pika with esbuild and tsc (#297)
Pika has been deprecated for a while now, and the project has now been archived, and because of that any released npm package is missing most of the files generated by the build step. Uses esbuild to transpile the TS source code into an ESM source, NodeJs bundle, and a browser bundle Uses the TypeScript compiler to generate the types
1 parent ca5b0d4 commit 5ca150f

File tree

8 files changed

+1410
-3455
lines changed

8 files changed

+1410
-3455
lines changed
 

‎.github/workflows/release.yml

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jobs:
1818
cache: npm
1919
- run: npm ci
2020
- run: npm run build
21-
- name: "Fix pkg.files file pattern"
22-
run: node scripts/fix-package-json.js
2321
- run: npx semantic-release
2422
env:
2523
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎package-lock.json

+1,303-3,408
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+4-21
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@
4242
"@octokit/auth-app": "^4.0.9",
4343
"@octokit/fixtures-server": "^7.0.0",
4444
"@octokit/request": "^6.0.0",
45-
"@pika/pack": "^0.3.7",
46-
"@pika/plugin-build-node": "^0.9.2",
47-
"@pika/plugin-build-web": "^0.9.2",
48-
"@pika/plugin-ts-standard-pkg": "^0.9.2",
45+
"@octokit/tsconfig": "^1.0.2",
4946
"@types/jest": "^29.0.0",
5047
"@types/node": "^18.0.0",
48+
"esbuild": "^0.17.19",
5149
"fetch-mock": "^9.0.0",
50+
"glob": "^10.2.5",
5251
"jest": "^29.0.0",
5352
"prettier": "2.8.8",
5453
"semantic-release": "^21.0.0",
@@ -57,7 +56,7 @@
5756
"typescript": "^5.0.0"
5857
},
5958
"scripts": {
60-
"build": "pika-pack build",
59+
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
6160
"coverage": "nyc report --reporter=html && open coverage/index.html",
6261
"lint": "prettier --check \"{src,test}/**/*.{js,json,ts}\" \"docs/*.js\" docs/package.json \"docs/src/**/*\" *.md package.json",
6362
"lint:fix": "prettier --write \"{src,test}/**/*.{js,json,ts}\" \"docs/*.js\" docs/package.json \"docs/src/**/*\" *.md package.json",
@@ -67,22 +66,6 @@
6766
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals test/typescript-validate.ts"
6867
},
6968
"license": "MIT",
70-
"@pika/pack": {
71-
"pipeline": [
72-
[
73-
"@pika/plugin-ts-standard-pkg"
74-
],
75-
[
76-
"@pika/plugin-build-node",
77-
{
78-
"minNodeVersion": "14"
79-
}
80-
],
81-
[
82-
"@pika/plugin-build-web"
83-
]
84-
]
85-
},
8669
"jest": {
8770
"preset": "ts-jest",
8871
"testPathIgnorePatterns": [

‎scripts/build.mjs

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

‎scripts/fix-package-json.js

-15
This file was deleted.

‎test/integration/smoke.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ describe("Smoke tests", () => {
99
});
1010

1111
it("can be used as a type", () => {
12+
// @ts-expect-error TS6133 Unused variable
1213
let octokit: Octokit;
1314
octokit = new Octokit();
1415
});
1516

1617
it("userAgent option", () => {
1718
const mock = fetchMock
1819
.sandbox()
19-
.getOnce("https://api.github.com/", (url, { headers }) => {
20+
.getOnce("https://api.github.com/", (_url, { headers }) => {
2021
// @ts-ignore headers has wrong typing in fetch-mock 8.3.2
2122
expect(headers["user-agent"]).toMatch(/^my-app\/1.2.3 /);
2223

‎test/scenarios/release-assets.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("api.github.com", () => {
3737
});
3838
})
3939

40-
.then((result) => {
40+
.then(() => {
4141
assetId = releaseId;
4242

4343
return octokit.rest.repos.listReleaseAssets({
@@ -47,15 +47,15 @@ describe("api.github.com", () => {
4747
});
4848
})
4949

50-
.then((result) => {
50+
.then(() => {
5151
return octokit.rest.repos.getReleaseAsset({
5252
owner: "octokit-fixture-org",
5353
repo: "release-assets",
5454
asset_id: assetId,
5555
});
5656
})
5757

58-
.then((result) => {
58+
.then(() => {
5959
return octokit.rest.repos.updateReleaseAsset({
6060
owner: "octokit-fixture-org",
6161
repo: "release-assets",
@@ -65,7 +65,7 @@ describe("api.github.com", () => {
6565
});
6666
})
6767

68-
.then((result) => {
68+
.then(() => {
6969
return octokit.rest.repos.deleteReleaseAsset({
7070
owner: "octokit-fixture-org",
7171
repo: "release-assets",

‎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.