Skip to content

Commit 5906367

Browse files
authored
chore: switch to tsx cli from esbuild-register and to mitata from benchmark.js (#107)
1 parent 2b6bcbd commit 5906367

File tree

7 files changed

+1747
-1100
lines changed

7 files changed

+1747
-1100
lines changed

.mocharc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"extension": ["js", "ts"],
33
"enable-source-maps": true,
44
"watchFiles": ["src/**/*.ts", "src/**/*.marko"],
5-
"require": ["esbuild-register", "mocha-snap"]
5+
"require": ["tsx", "mocha-snap"]
66
}

bench.mts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import "./build.mts";
2+
3+
import os from "os";
4+
import path from "path";
5+
import fs from "fs/promises";
6+
import cp from "child_process";
7+
import { promisify } from "util";
8+
9+
import degit from "degit";
10+
import { group, bench, run } from "mitata";
11+
12+
const exec = promisify(cp.exec);
13+
const api = (await import("./dist/index.mjs")) as unknown as typeof import("./src");
14+
const FIXTURES = path.resolve("src/__tests__/fixtures");
15+
const GREP = new RegExp(process.env.GREP || ".", "g");
16+
const COMPARE = process.env.COMPARE;
17+
let compareAPI: undefined | typeof api;
18+
19+
if (COMPARE) {
20+
const cwd = path.join(os.tmpdir(), `htmljs-bench-${COMPARE}`);
21+
const exists = await fs.mkdir(cwd).catch(() => true);
22+
if (!exists) {
23+
await degit(`marko-js/htmljs-parser#${COMPARE}`).clone(cwd);
24+
await exec("npm ci && npm run --if-present build", { cwd });
25+
}
26+
27+
const pkg = JSON.parse(
28+
await fs.readFile(path.join(cwd, "package.json"), "utf-8")
29+
);
30+
compareAPI = await import(
31+
path.join(cwd, pkg.exports?.["."].import ?? pkg.main)
32+
);
33+
}
34+
35+
for (const entry of await fs.readdir(FIXTURES)) {
36+
if (!GREP.test(entry)) continue;
37+
const filename = path.join(FIXTURES, entry, "input.marko");
38+
const src = await fs.readFile(filename, "utf-8");
39+
const check = (mod: typeof api) => () => mod.createParser({}).parse(src);
40+
if (compareAPI) {
41+
group(() => {
42+
bench(entry, check(api));
43+
bench(`${entry}#${COMPARE}`, check(compareAPI));
44+
});
45+
} else {
46+
bench(entry, check(api));
47+
}
48+
}
49+
50+
await run();

build.mts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { build, BuildOptions } from "esbuild";
2+
3+
const opts: BuildOptions = {
4+
bundle: true,
5+
outdir: "dist",
6+
platform: "node",
7+
target: ["node14"],
8+
entryPoints: ["src/index.ts"],
9+
};
10+
11+
await Promise.all([
12+
build({
13+
...opts,
14+
format: "cjs",
15+
}),
16+
build({
17+
...opts,
18+
format: "esm",
19+
outExtension: { ".js": ".mjs" },
20+
}),
21+
]);

build.ts

-23
This file was deleted.

0 commit comments

Comments
 (0)