Skip to content

Commit

Permalink
feat: support esm and cjs build (#28)
Browse files Browse the repository at this point in the history
* Support esm and cjs build

* Fix lint errors for ts module resolution

* Fix benchmark loader for esm

* Update doc script
  • Loading branch information
nazarhussain authored Nov 28, 2024
1 parent 642b31e commit 7c06ba6
Show file tree
Hide file tree
Showing 46 changed files with 230 additions and 131 deletions.
4 changes: 2 additions & 2 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require:
- ts-node/register
loader:
- ts-node/esm
colors: true
timeout: 5000
exit: true
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
printWidth: 120,
tabWidth: 2,
useTabs: false,
Expand Down
3 changes: 3 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

await import("../lib/esm/cli.js");
7 changes: 7 additions & 0 deletions eslint.config.mjs → eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default ts.config(
],
"func-call-spacing": "error",
"import/no-duplicates": "off",
"import/no-unresolved": ["error", {commonjs: false}],
"new-parens": "error",
"no-caller": "error",
"no-bitwise": "off",
Expand All @@ -58,6 +59,12 @@ export default ts.config(
project: "./tsconfig.json",
},
},
settings: {
"import/resolver": {
"typescript": {
}
}
},
},
{
files: ["**/test/**/*.ts"],
Expand Down
23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "@dapplion/benchmark",
"version": "0.2.5",
"main": "lib/index.js",
"repository": "[email protected]:dapplion/benchmark.git",
"author": "dapplion <[email protected]>",
"license": "MIT",
"bin": {
"benchmark": "lib/cli.js"
"benchmark": "./bin/index.js"
},
"files": [
"lib/**/*.d.ts",
Expand All @@ -15,25 +14,37 @@
"*.d.ts",
"*.js"
],
"type": "module",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"exports": {
".": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js"
}
},
"scripts": {
"build": "tsc -p tsconfig.build.json && chmod +x lib/cli.js",
"build": "yarn build:esm && yarn build:cjs",
"build:esm": "tsc -p tsconfig.build.esm.json && echo '{\"type\": \"module\"}' > ./lib/esm/package.json",
"build:cjs": "tsc -p tsconfig.build.cjs.json && echo '{\"type\": \"commonjs\"}' > ./lib/cjs/package.json",
"test:unit": "mocha test/unit/**/*.test.ts",
"lint": "eslint --color src/ test/",
"prepublishOnly": "yarn build",
"benchmark": "node -r ts-node/register src/cli.ts 'test/perf/**/*.test.ts'",
"writeDocs": "ts-node scripts/writeOptionsMd.ts"
"benchmark": "node --loader ts-node/esm ./src/cli.ts 'test/perf/**/*.test.ts'",
"writeDocs": "node --loader ts-node/esm scripts/writeOptionsMd.ts"
},
"devDependencies": {
"@types/chai": "^4.2.19",
"@types/mocha": "^10.0.9",
"@types/node": "^15.12.4",
"@types/node": "^18.15.3",
"@types/rimraf": "^3.0.0",
"@types/yargs": "^17.0.33",
"chai": "^4.5.0",
"dotenv": "^10.0.0",
"eslint": "^9.15.0",
"@eslint/js": "^9.15.0",
"eslint-plugin-import": "^2.31.0",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-prettier": "^5.2.1",
"eslint-config-prettier": "^9.1.0",
"mocha": "^10.8.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/writeOptionsMd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {options} from "../src/options";
import {options} from "../src/options.js";

const sections: string[] = [];

Expand Down
12 changes: 5 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env node

// Must not use `* as yargs`, see https://github.com/yargs/yargs/issues/1131
import yargs from "yargs";
import {loadOptions, handleRequires} from "./utils/mochaCliExports";
import {options, optionsDefault} from "./options";
import {run} from "./run";
import {Opts} from "./types";
import {loadOptions, handleRequires} from "./utils/mochaCliExports.js";
import {options, optionsDefault} from "./options.js";
import {run} from "./run.js";
import {Opts} from "./types.js";

/**
* Common factory for running the CLI and running integration tests
Expand All @@ -15,7 +13,7 @@ const argv = process.argv.slice(2);

const args = loadOptions(argv);

void yargs
void yargs()
.env("BENCHMARK")
.scriptName("benchmark")
.command({
Expand Down
2 changes: 1 addition & 1 deletion src/compare/compute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ResultComparision, BenchmarkComparision, Benchmark, BenchmarkResult} from "../types";
import {ResultComparision, BenchmarkComparision, Benchmark, BenchmarkResult} from "../types.js";

export function computeBenchComparision(
currBench: Benchmark,
Expand Down
10 changes: 5 additions & 5 deletions src/compare/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as github from "@actions/github";
import {Benchmark, Opts} from "../types";
import {getGithubEventData, GithubActionsEventData, parseBranchFromRef, getDefaultBranch} from "../utils";
import {isGaRun} from "../github/context";
import {IHistoryProvider} from "../history/provider";
import {validateBenchmark} from "../history/schema";
import {Benchmark, Opts} from "../types.js";
import {getGithubEventData, GithubActionsEventData, parseBranchFromRef, getDefaultBranch} from "../utils/index.js";
import {isGaRun} from "../github/context.js";
import {IHistoryProvider} from "../history/provider.js";
import {validateBenchmark} from "../history/schema.js";

enum CompareWithType {
latestCommitInBranch = "latestCommitInBranch",
Expand Down
6 changes: 3 additions & 3 deletions src/github/comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as github from "@actions/github";
import {BenchmarkComparision} from "../types";
import {commetToPrUpdatable, commentToCommit} from "./octokit";
import {getGithubEventData, GithubActionsEventData, renderComment} from "../utils";
import {BenchmarkComparision} from "../types.js";
import {commetToPrUpdatable, commentToCommit} from "./octokit.js";
import {getGithubEventData, GithubActionsEventData, renderComment} from "../utils/index.js";

export async function postGaComment(resultsComp: BenchmarkComparision): Promise<void> {
switch (github.context.eventName) {
Expand Down
2 changes: 1 addition & 1 deletion src/github/octokit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getContext} from "./context";
import {getContext} from "./context.js";

const commentTag = "benchmarkbot/action";

Expand Down
2 changes: 1 addition & 1 deletion src/history/append.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Benchmark, BenchmarkHistory, Opts} from "../types";
import {Benchmark, BenchmarkHistory, Opts} from "../types.js";

export function appendBenchmarkToHistoryAndPrune(
history: BenchmarkHistory,
Expand Down
8 changes: 4 additions & 4 deletions src/history/gaCache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from "fs";
import fs from "node:fs";
import * as cache from "@actions/cache";
import {Benchmark} from "../types";
import {LocalHistoryProvider} from "./local";
import {HistoryProviderType, IHistoryProvider} from "./provider";
import {Benchmark} from "../types.js";
import {LocalHistoryProvider} from "./local.js";
import {HistoryProviderType, IHistoryProvider} from "./provider.js";

/**
* Persist results in CSV, one benchmark result per file
Expand Down
14 changes: 7 additions & 7 deletions src/history/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Opts} from "../types";
import {resolveHistoryLocation} from "./location";
import {LocalHistoryProvider} from "./local";
import {getGaCacheHistoryProvider} from "./gaCache";
import {IHistoryProvider} from "./provider";
import {optionsDefault} from "../options";
import {S3HistoryProvider} from "./s3";
import {Opts} from "../types.js";
import {resolveHistoryLocation} from "./location.js";
import {LocalHistoryProvider} from "./local.js";
import {getGaCacheHistoryProvider} from "./gaCache.js";
import {IHistoryProvider} from "./provider.js";
import {optionsDefault} from "../options.js";
import {S3HistoryProvider} from "./s3.js";
export {resolveHistoryLocation};

export function getHistoryProvider(opts: Opts): IHistoryProvider {
Expand Down
12 changes: 6 additions & 6 deletions src/history/local.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "fs";
import path from "path";
import {HistoryProviderType, IHistoryProvider} from "./provider";
import {Benchmark, BenchmarkResults} from "../types";
import {fromCsv, toCsv} from "../utils/file";
import {FsError} from "../utils";
import fs from "node:fs";
import path from "node:path";
import {HistoryProviderType, IHistoryProvider} from "./provider.js";
import {Benchmark, BenchmarkResults} from "../types.js";
import {fromCsv, toCsv} from "../utils/file.js";
import {FsError} from "../utils/index.js";

const extension = ".csv";
const historyDir = "history";
Expand Down
6 changes: 3 additions & 3 deletions src/history/location.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isGaRun} from "../github/context";
import {Opts} from "../types";
import {optionsDefault} from "../options";
import {isGaRun} from "../github/context.js";
import {Opts} from "../types.js";
import {optionsDefault} from "../options.js";

export type HistoryLocation = {type: "local"; path: string} | {type: "ga-cache"; key: string};

Expand Down
2 changes: 1 addition & 1 deletion src/history/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Benchmark} from "../types";
import {Benchmark} from "../types.js";

export enum HistoryProviderType {
Local = "Local",
Expand Down
10 changes: 5 additions & 5 deletions src/history/s3.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from "path";
import S3 from "aws-sdk/clients/s3";
import {Benchmark, BenchmarkResults} from "../types";
import {fromCsv, toCsv, extendError, AwsError} from "../utils";
import {HistoryProviderType, IHistoryProvider} from "./provider";
import path from "node:path";
import S3 from "aws-sdk/clients/s3.js";
import {Benchmark, BenchmarkResults} from "../types.js";
import {fromCsv, toCsv, extendError, AwsError} from "../utils/index.js";
import {HistoryProviderType, IHistoryProvider} from "./provider.js";

export type S3Config = Pick<S3.Types.ClientConfiguration, "accessKeyId" | "secretAccessKey" | "region" | "endpoint"> & {
Bucket: string;
Expand Down
2 changes: 1 addition & 1 deletion src/history/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Ajv from "ajv";
import {BenchmarkHistory, Benchmark} from "../types";
import {BenchmarkHistory, Benchmark} from "../types.js";

export const emptyBenchmarkHistory: BenchmarkHistory = {benchmarks: {}};

Expand Down
4 changes: 2 additions & 2 deletions src/history/shouldPersist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Opts} from "../types";
import {getDefaultBranch} from "../utils/defaultBranch";
import {Opts} from "../types.js";
import {getDefaultBranch} from "../utils/defaultBranch.js";

export async function resolveShouldPersist(opts: Opts, branch: string): Promise<boolean> {
// Force persist
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./mochaPlugin";
export * from "./mochaPlugin/index.js";
12 changes: 6 additions & 6 deletions src/mochaPlugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "fs";
import path from "path";
import {BenchmarkOpts} from "../types";
import {optsByRootSuite, optsMap, resultsByRootSuite} from "./globalState";
import {BenchmarkRunOptsWithFn, runBenchFn} from "./runBenchFn";
import {getRootSuite, getParentSuite} from "./utils";
import fs from "node:fs";
import path from "node:path";
import {BenchmarkOpts} from "../types.js";
import {optsByRootSuite, optsMap, resultsByRootSuite} from "./globalState.js";
import {BenchmarkRunOptsWithFn, runBenchFn} from "./runBenchFn.js";
import {getRootSuite, getParentSuite} from "./utils.js";

type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

Expand Down
8 changes: 4 additions & 4 deletions src/mochaPlugin/mochaRunner.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import Mocha from "mocha";
import {optsByRootSuite, resultsByRootSuite} from "./globalState";
import {collectFiles, FileCollectionOptions} from "../utils/mochaCliExports";
import {benchmarkReporterWithPrev} from "./reporter";
import {Benchmark, BenchmarkOpts, BenchmarkResult, onlyBenchmarkOpts, Opts} from "../types";
import {optsByRootSuite, resultsByRootSuite} from "./globalState.js";
import {collectFiles, FileCollectionOptions} from "../utils/mochaCliExports.js";
import {benchmarkReporterWithPrev} from "./reporter.js";
import {Benchmark, BenchmarkOpts, BenchmarkResult, onlyBenchmarkOpts, Opts} from "../types.js";

export async function runMochaBenchmark(
opts: Opts & BenchmarkOpts,
Expand Down
8 changes: 4 additions & 4 deletions src/mochaPlugin/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import Mocha from "mocha";
import {Benchmark, BenchmarkResult} from "../types";
import {resultsByRootSuite} from "./globalState";
import {formatResultRow} from "./format";
import {getRootSuite} from "./utils";
import {Benchmark, BenchmarkResult} from "../types.js";
import {resultsByRootSuite} from "./globalState.js";
import {formatResultRow} from "./format.js";
import {getRootSuite} from "./utils.js";

const {
EVENT_RUN_BEGIN,
Expand Down
2 changes: 1 addition & 1 deletion src/mochaPlugin/runBenchFn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BenchmarkResult, BenchmarkOpts} from "../types";
import {BenchmarkResult, BenchmarkOpts} from "../types.js";

export type BenchmarkRunOpts = BenchmarkOpts & {
id: string;
Expand Down
4 changes: 2 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Options} from "yargs";
import {Opts, BenchmarkOpts} from "./types";
import {FileCollectionOptions} from "./utils/mochaCliExports";
import {Opts, BenchmarkOpts} from "./types.js";
import {FileCollectionOptions} from "./utils/mochaCliExports.js";

export const optionsDefault = {
threshold: 2,
Expand Down
20 changes: 10 additions & 10 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as github from "@actions/github";
import {getHistoryProvider} from "./history";
import {resolveShouldPersist} from "./history/shouldPersist";
import {validateBenchmark} from "./history/schema";
import {Benchmark, BenchmarkOpts, Opts} from "./types";
import {renderCompareWith, resolveCompareWith, resolvePrevBenchmark} from "./compare";
import {parseBranchFromRef, getCurrentCommitInfo, shell, getCurrentBranch} from "./utils";
import {runMochaBenchmark} from "./mochaPlugin/mochaRunner";
import {computeBenchComparision} from "./compare/compute";
import {postGaComment} from "./github/comment";
import {isGaRun} from "./github/context";
import {getHistoryProvider} from "./history/index.js";
import {resolveShouldPersist} from "./history/shouldPersist.js";
import {validateBenchmark} from "./history/schema.js";
import {Benchmark, BenchmarkOpts, Opts} from "./types.js";
import {renderCompareWith, resolveCompareWith, resolvePrevBenchmark} from "./compare/index.js";
import {parseBranchFromRef, getCurrentCommitInfo, shell, getCurrentBranch} from "./utils/index.js";
import {runMochaBenchmark} from "./mochaPlugin/mochaRunner.js";
import {computeBenchComparision} from "./compare/compute.js";
import {postGaComment} from "./github/comment.js";
import {isGaRun} from "./github/context.js";

/* eslint-disable no-console */

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 {FileCollectionOptions} from "./utils/mochaCliExports";
import {FileCollectionOptions} from "./utils/mochaCliExports.js";

export type Opts = Partial<FileCollectionOptions> & {
defaultBranch?: string;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/currentBranch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as github from "@actions/github";
import {getGithubEventData, GithubActionsEventData, parseBranchFromRef} from "../utils";
import {isGaRun} from "../github/context";
import {shell} from "./shell";
import {getGithubEventData, GithubActionsEventData, parseBranchFromRef} from "../utils/index.js";
import {isGaRun} from "../github/context.js";
import {shell} from "./shell.js";

export async function getCurrentBranch(): Promise<string> {
if (isGaRun()) {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/defaultBranch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isGaRun} from "../github/context";
import {getGithubDefaultBranch} from "../github/octokit";
import {Opts} from "../types";
import {shell} from "./shell";
import {isGaRun} from "../github/context.js";
import {getGithubDefaultBranch} from "../github/octokit.js";
import {Opts} from "../types.js";
import {shell} from "./shell.js";

let defaultBranch: string | null = null;

Expand Down
6 changes: 3 additions & 3 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import csvParse from "csv-parse/lib/sync";
import csvStringify from "csv-stringify/lib/sync";
import fs from "node:fs";
import csvParse from "csv-parse/lib/sync.js";
import csvStringify from "csv-stringify/lib/sync.js";

type CsvMetadata = Record<string, string>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
2 changes: 1 addition & 1 deletion src/utils/gaContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from "fs";
import fs from "node:fs";

/**
* Github actions store the event data payload at a JSON file with path
Expand Down
2 changes: 1 addition & 1 deletion src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {shell} from "./shell";
import {shell} from "./shell.js";

export async function getCurrentCommitInfo(): Promise<{
/** commit hash `71f08b12d45d44255c31f7b7d135bd15a93fdaac` */
Expand Down
Loading

0 comments on commit 7c06ba6

Please sign in to comment.