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): switch to esbuild in place of pika #161

Merged
merged 6 commits into from
Jun 7, 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
4,176 changes: 660 additions & 3,516 deletions package-lock.json

Large diffs are not rendered by default.

39 changes: 14 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"version": "0.0.0-development",
"description": "Convenience method to create/edit/delete a text file based on its current content",
"scripts": {
"build": "pika-pack build",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
"pretest": "npm run -s lint",
Expand All @@ -24,15 +24,13 @@
},
"devDependencies": {
"@octokit/core": "^4.0.0",
"@octokit/tsconfig": "^1.0.2",
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.9.2",
"@pika/plugin-build-web": "^0.9.2",
"@pika/plugin-ts-standard-pkg": "^0.9.2",
"@types/jest": "^29.0.0",
"@octokit/tsconfig": "^2.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^18.0.0",
"esbuild": "^0.17.19",
"fetch-mock": "^9.11.0",
"jest": "^29.0.0",
"glob": "^10.2.7",
"jest": "^29.5.0",
"prettier": "2.8.8",
"semantic-release": "^21.0.0",
"semantic-release-plugin-update-version-in-files": "^1.1.0",
Expand All @@ -43,7 +41,14 @@
"@octokit/core": ">= 3"
},
"jest": {
"preset": "ts-jest",
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
}
]
},
"coverageThreshold": {
"global": {
"statements": 100,
Expand All @@ -53,22 +58,6 @@
}
}
},
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-ts-standard-pkg"
],
[
"@pika/plugin-build-node",
{
"minNodeVersion": "14"
}
],
[
"@pika/plugin-build-web"
]
]
},
"release": {
"branches": [
"+([0-9]).x",
Expand Down
88 changes: 88 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import esbuild from "esbuild";
import { copyFile, readFile, writeFile, rm } from "node:fs/promises";
import { glob } from "glob";

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",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
module: "dist-src/index.js",
sideEffects: false,
},
null,
2
)
);
}
main();
2 changes: 1 addition & 1 deletion src/compose-create-or-update-text-file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Octokit } from "@octokit/core";

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

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Octokit } from "@octokit/core";

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

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

/**
* @param octokit Octokit instance
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Endpoints } from "@octokit/types";
import type { Endpoints } from "@octokit/types";

type User = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion test/create-or-update-text-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fetchMock from "fetch-mock";
import fetchMock from "fetch-mock";
import { Octokit } from "@octokit/core";

import { createOrUpdateTextFile } from "../src";
Expand Down
2 changes: 1 addition & 1 deletion test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fetchMock from "fetch-mock";
import fetchMock from "fetch-mock";
import { Octokit } from "@octokit/core";

import { createOrUpdateTextFile } from "../src";
Expand Down
9 changes: 9 additions & 0 deletions test/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"verbatimModuleSyntax": false
},
"include": ["src/**/*"]
}
12 changes: 11 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{ "extends": "@octokit/tsconfig", "include": ["src/**/*"] }
{
"extends": "@octokit/tsconfig",
"compilerOptions": {
"esModuleInterop": true,
"declaration": true,
"outDir": "pkg/dist-types",
"emitDeclarationOnly": true,
"sourceMap": true
},
"include": ["src/**/*"]
}