Skip to content

Commit

Permalink
fix(build): replace pika with esbuild and tsc (#297)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wolfy1339 committed May 20, 2023
1 parent ca5b0d4 commit 5ca150f
Show file tree
Hide file tree
Showing 8 changed files with 1,410 additions and 3,455 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Expand Up @@ -18,8 +18,6 @@ jobs:
cache: npm
- run: npm ci
- run: npm run build
- name: "Fix pkg.files file pattern"
run: node scripts/fix-package-json.js
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4,711 changes: 1,303 additions & 3,408 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 4 additions & 21 deletions package.json
Expand Up @@ -42,13 +42,12 @@
"@octokit/auth-app": "^4.0.9",
"@octokit/fixtures-server": "^7.0.0",
"@octokit/request": "^6.0.0",
"@pika/pack": "^0.3.7",
"@pika/plugin-build-node": "^0.9.2",
"@pika/plugin-build-web": "^0.9.2",
"@pika/plugin-ts-standard-pkg": "^0.9.2",
"@octokit/tsconfig": "^1.0.2",
"@types/jest": "^29.0.0",
"@types/node": "^18.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.0.0",
"glob": "^10.2.5",
"jest": "^29.0.0",
"prettier": "2.8.8",
"semantic-release": "^21.0.0",
Expand All @@ -57,7 +56,7 @@
"typescript": "^5.0.0"
},
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"coverage": "nyc report --reporter=html && open coverage/index.html",
"lint": "prettier --check \"{src,test}/**/*.{js,json,ts}\" \"docs/*.js\" docs/package.json \"docs/src/**/*\" *.md package.json",
"lint:fix": "prettier --write \"{src,test}/**/*.{js,json,ts}\" \"docs/*.js\" docs/package.json \"docs/src/**/*\" *.md package.json",
Expand All @@ -67,22 +66,6 @@
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals test/typescript-validate.ts"
},
"license": "MIT",
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-ts-standard-pkg"
],
[
"@pika/plugin-build-node",
{
"minNodeVersion": "14"
}
],
[
"@pika/plugin-build-web"
]
]
},
"jest": {
"preset": "ts-jest",
"testPathIgnorePatterns": [
Expand Down
92 changes: 92 additions & 0 deletions scripts/build.mjs
@@ -0,0 +1,92 @@
// @ts-check
import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "fs/promises";
import { glob } from "glob";

/**
* @type {esbuild.BuildOptions}
*/
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",
module: "dist-web/index.js",
types: "dist-types/index.d.ts",
source: "dist-src/index.js",
sideEffects: false,
},
null,
2
)
);
}
main();
15 changes: 0 additions & 15 deletions scripts/fix-package-json.js

This file was deleted.

3 changes: 2 additions & 1 deletion test/integration/smoke.test.ts
Expand Up @@ -9,14 +9,15 @@ describe("Smoke tests", () => {
});

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

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

Expand Down
8 changes: 4 additions & 4 deletions test/scenarios/release-assets.test.ts
Expand Up @@ -37,7 +37,7 @@ describe("api.github.com", () => {
});
})

.then((result) => {
.then(() => {
assetId = releaseId;

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

.then((result) => {
.then(() => {
return octokit.rest.repos.getReleaseAsset({
owner: "octokit-fixture-org",
repo: "release-assets",
asset_id: assetId,
});
})

.then((result) => {
.then(() => {
return octokit.rest.repos.updateReleaseAsset({
owner: "octokit-fixture-org",
repo: "release-assets",
Expand All @@ -65,7 +65,7 @@ describe("api.github.com", () => {
});
})

.then((result) => {
.then(() => {
return octokit.rest.repos.deleteReleaseAsset({
owner: "octokit-fixture-org",
repo: "release-assets",
Expand Down
9 changes: 5 additions & 4 deletions tsconfig.json
@@ -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 5ca150f

Please sign in to comment.