Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build): replace pika with esbuild and tsc #297

Merged
merged 8 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
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
3,125 changes: 510 additions & 2,615 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 4 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@
"@octokit/auth": "^3.0.3",
"@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 @@ -56,7 +55,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 @@ -66,22 +65,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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/**/*"]
}