Skip to content

Commit 556dda7

Browse files
committed
chore(size-benchmark): refactor to use esm because p-map is only esm
1 parent ee71617 commit 556dda7

File tree

15 files changed

+221
-326
lines changed

15 files changed

+221
-326
lines changed

scripts/benchmark-size/runner/calculate-size/bundlers-size.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import commonjsPlugin from "@rollup/plugin-commonjs";
22
import jsonPlugin from "@rollup/plugin-json";
33
import resolvePlugin from "@rollup/plugin-node-resolve";
44
import { build as esbuild } from "esbuild";
5-
import { lstat } from "fs-extra";
5+
import fsExtra from "fs-extra";
66
import { join } from "path";
77
import { rollup } from "rollup";
88
import { terser as terserPlugin } from "rollup-plugin-terser";
@@ -11,6 +11,8 @@ import webpack from "webpack";
1111

1212
import type { SizeReportContext } from "../index";
1313

14+
const { lstat } = fsExtra;
15+
1416
export interface BundlerSizeReportContext extends SizeReportContext {
1517
projectDir: string;
1618
entryPoint: string;

scripts/benchmark-size/runner/calculate-size/generate-project.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import exec from "execa";
1+
import { execa as exec } from "execa";
22
import { promises as fsPromise } from "fs";
33
import { join } from "path";
44
import prettier from "prettier";

scripts/benchmark-size/runner/calculate-size/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { promises as fsPromise } from "fs";
2-
import { ListrContext, ListrTaskWrapper } from "listr2";
32
import { join } from "path";
43

54
import { SizeReportContext } from "../index";
65
import { PackageContext } from "../load-test-scope";
7-
import { getEsbuildSize, getRollupSize, getWebpackSize } from "./bundlers-size";
8-
import { generateProject } from "./generate-project";
9-
import { calculateNpmSize } from "./npm-size";
6+
import { getEsbuildSize, getRollupSize, getWebpackSize } from "./bundlers-size.js";
7+
import { generateProject } from "./generate-project.js";
8+
import { calculateNpmSize } from "./npm-size.js";
109

1110
export interface PackageSizeReportOptions extends SizeReportContext {
1211
packageName: string;

scripts/benchmark-size/runner/cli.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
1-
#!/usr/bin/env ts-node
21
import yargs from "yargs";
32

43
import {
54
DEFAULT_LIMIT_CONFIG_PATH,
65
DEFAULT_RAW_OUTPUT_PATH,
76
DEFAULT_REPORT_PATH,
87
DEFAULT_TEST_SCOPE,
9-
} from "./constants";
10-
import { sizeReport, SizeReportOptions } from "./index";
8+
} from "./constants.js";
9+
import { sizeReport, SizeReportOptions } from "./index.js";
1110

12-
(async () => {
13-
const args = yargs
14-
.option("since", {
15-
type: "string",
16-
choices: ["all", "last_release", "main"],
17-
default: "all",
18-
description: "Run the size benchmark on changed package since last release or main branch",
19-
})
20-
.option("scopeConfigPath", {
21-
type: "string",
22-
default: DEFAULT_TEST_SCOPE,
23-
})
24-
.option("limitConfigPath", {
25-
type: "string",
26-
default: DEFAULT_LIMIT_CONFIG_PATH,
27-
})
28-
.option("skipLocalPublish", {
29-
type: "boolean",
30-
default: false,
31-
describe:
32-
"Skip publishing the packages locally. You can skip it if you didn't change any packages since last execution",
33-
})
34-
.option("rawOutputPath", {
35-
type: "string",
36-
default: DEFAULT_RAW_OUTPUT_PATH,
37-
})
38-
.option("reportPath", {
39-
type: "string",
40-
default: DEFAULT_REPORT_PATH,
41-
})
42-
.option("skipRawOutput", {
43-
type: "boolean",
44-
default: false,
45-
describe: "Whether to generate the raw benchmark data to configured path",
46-
})
47-
.help().argv;
48-
try {
49-
await sizeReport(args as SizeReportOptions);
50-
} catch (e) {
51-
console.error(e);
52-
process.exit(1);
53-
}
54-
})();
11+
const args = yargs(process.argv.slice(2))
12+
.option("since", {
13+
type: "string",
14+
choices: ["all", "last_release", "main"],
15+
default: "all",
16+
description: "Run the size benchmark on changed package since last release or main branch",
17+
})
18+
.option("scopeConfigPath", {
19+
type: "string",
20+
default: DEFAULT_TEST_SCOPE,
21+
})
22+
.option("limitConfigPath", {
23+
type: "string",
24+
default: DEFAULT_LIMIT_CONFIG_PATH,
25+
})
26+
.option("skipLocalPublish", {
27+
type: "boolean",
28+
default: false,
29+
describe:
30+
"Skip publishing the packages locally. You can skip it if you didn't change any packages since last execution",
31+
})
32+
.option("rawOutputPath", {
33+
type: "string",
34+
default: DEFAULT_RAW_OUTPUT_PATH,
35+
})
36+
.option("reportPath", {
37+
type: "string",
38+
default: DEFAULT_REPORT_PATH,
39+
})
40+
.option("skipRawOutput", {
41+
type: "boolean",
42+
default: false,
43+
describe: "Whether to generate the raw benchmark data to configured path",
44+
})
45+
.help().argv;
46+
try {
47+
await sizeReport(args as SizeReportOptions);
48+
} catch (e) {
49+
console.error(e);
50+
process.exit(1);
51+
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { join } from "path";
1+
import { dirname, join } from "path";
2+
import { fileURLToPath } from "url";
23

3-
export const DEFAULT_TEST_SCOPE = join(__dirname, "..", "scope.json");
4-
export const DEFAULT_BUNDLERS_CONFIG = join(__dirname, "..", "bundlers.json");
5-
export const PROJECT_TEMPLATES_DIR = join(__dirname, "templates");
6-
export const PROJECT_ROOT = join(__dirname, "..", "..", "..");
4+
const dir = dirname(fileURLToPath(import.meta.url));
5+
export const DEFAULT_TEST_SCOPE = join(dir, "..", "scope.json");
6+
export const DEFAULT_BUNDLERS_CONFIG = join(dir, "..", "bundlers.json");
7+
export const PROJECT_TEMPLATES_DIR = join(dir, "templates");
8+
export const PROJECT_ROOT = join(dir, "..", "..", "..");
79
export const DEFAULT_RAW_OUTPUT_PATH = join(PROJECT_ROOT, "benchmark", "size", "raw");
810
export const DEFAULT_REPORT_PATH = join(PROJECT_ROOT, "benchmark", "size", "report.md");
9-
export const DEFAULT_LIMIT_CONFIG_PATH = join(__dirname, "..", "limit.json");
11+
export const DEFAULT_LIMIT_CONFIG_PATH = join(dir, "..", "limit.json");
1012
export const PORT = 4873;

scripts/benchmark-size/runner/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import { tmpdir } from "os";
55
import map from "p-map";
66
import { join } from "path";
77

8-
import { generatePackageSizeReport, PackageSizeReportOutput } from "./calculate-size";
8+
import { generatePackageSizeReport, PackageSizeReportOutput } from "./calculate-size/index.js";
99
import {
1010
DEFAULT_LIMIT_CONFIG_PATH,
1111
DEFAULT_RAW_OUTPUT_PATH,
1212
DEFAULT_REPORT_PATH,
1313
PORT,
1414
PROJECT_TEMPLATES_DIR,
15-
} from "./constants";
16-
import { loadPackageContext } from "./load-test-scope";
17-
import { localPublishChangedPackages, spawnLocalRegistry } from "./local-registry";
18-
import { updateReport } from "./reporter";
19-
import { sleep, validateRuntime } from "./utils";
20-
import { loadWorkspacePackages, SinceOption, validatePackagesAlreadyBuilt, WorkspacePackage } from "./workspace";
15+
} from "./constants.js";
16+
import { loadPackageContext } from "./load-test-scope.js";
17+
import { localPublishChangedPackages, spawnLocalRegistry } from "./local-registry.js";
18+
import { updateReport } from "./reporter/index.js";
19+
import { sleep, validateRuntime } from "./utils.js";
20+
import { loadWorkspacePackages, SinceOption, validatePackagesAlreadyBuilt, WorkspacePackage } from "./workspace.js";
2121

2222
export interface SizeReportContext {
2323
localRegistry: string;

scripts/benchmark-size/runner/load-test-scope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFileSync } from "fs";
22

3-
import { DEFAULT_TEST_SCOPE } from "./constants";
4-
import { loadWorkspacePackages } from "./workspace";
3+
import { DEFAULT_TEST_SCOPE } from "./constants.js";
4+
import { loadWorkspacePackages } from "./workspace.js";
55

66
export type PackageContext = {
77
package: string;

scripts/benchmark-size/runner/local-registry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import exec from "execa";
1+
import { execa as exec } from "execa";
22
import { readdirSync } from "fs";
33
import { join } from "path";
44

5-
import { PROJECT_ROOT } from "./constants";
5+
import { PROJECT_ROOT } from "./constants.js";
66

77
/**
88
* Publish the changed packages locally using Verdaccio. Return the path containing

scripts/benchmark-size/runner/package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "A runner generating the SDK size metrics with or without bundlers",
55
"main": "index.js",
66
"private": true,
7+
"type": "module",
78
"scripts": {
89
"test": "exit 0"
910
},
@@ -14,19 +15,15 @@
1415
"@types/execa": "^2.0.0",
1516
"@types/p-map": "^2.0.0",
1617
"esbuild": "latest",
17-
"execa": "^5.1.1",
18+
"execa": "^6.1.0",
1819
"fs-extra": "^10.1.0",
1920
"handlebars": "^4.7.7",
20-
"listr2": "^4.0.5",
2121
"p-map": "^5.5.0",
2222
"rollup": "latest",
2323
"rollup-plugin-terser": "^7.0.2",
2424
"semver": "^7.3.7",
2525
"webpack": "latest"
2626
},
2727
"author": "",
28-
"license": "UNLICENSED",
29-
"devDependencies": {
30-
"@types/listr": "^0.14.4"
31-
}
28+
"license": "UNLICENSED"
3229
}

scripts/benchmark-size/runner/reporter/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import esbuild from "esbuild";
12
import { existsSync, lstatSync, readFileSync, writeFileSync } from "fs";
23
import { ensureFileSync } from "fs-extra";
4+
import * as rollup from "rollup";
5+
import webpack from "webpack";
36

47
import { PackageSizeReportOutput } from "../calculate-size";
5-
import { prettifySize } from "../utils";
6-
import { LimitValidator } from "./limit";
8+
import { prettifySize } from "../utils.js";
9+
import { LimitValidator } from "./limit.js";
710

811
const HEADER = `| Package | Version | Publish Size | browser:Webpack | browser:Rollup | browser:EsBuild |
912
| :------ | :------ | :----------- | :------ | :----- | :------- |`;
@@ -101,9 +104,9 @@ export const updateReport = (
101104
console.info(`Updating ${output.length} rows in the report ${options.reportPath}`);
102105
const validator = new LimitValidator(options);
103106
const report = readReport(options.reportPath);
104-
const esbuildVersion = require("esbuild")?.version;
105-
const webpackVersion = require("webpack")?.version;
106-
const rollupVersion = require("rollup")?.VERSION;
107+
const { version: esbuildVersion } = esbuild;
108+
const { version: webpackVersion } = webpack;
109+
const { VERSION: rollupVersion } = rollup;
107110
const newLines = output.map((line) =>
108111
populateLineContent(line, {
109112
esbuildVersion,

scripts/benchmark-size/runner/reporter/limit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from "fs";
22

3-
import { readSize } from "../utils";
3+
import { readSize } from "../utils.js";
44

55
const validatingKeys = ["publishSize", "publishFiles"];
66
type ValidatingKeys = "publishSize" | "publishFiles";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext", // to run ts-node with ESM dependencies
4+
"target": "ESNext",
5+
"moduleResolution": "node",
6+
"allowSyntheticDefaultImports": true
7+
},
8+
"ts-node": {
9+
// Tell ts-node CLI to install the --loader automatically, explained below
10+
"esm": true
11+
}
12+
}

scripts/benchmark-size/runner/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import exec from "execa";
2-
import { promises as fsPromise, existsSync } from "fs";
1+
import { execa as exec } from "execa";
2+
import { existsSync, promises as fsPromise } from "fs";
33

4-
import { PROJECT_ROOT } from "./constants";
4+
import { PROJECT_ROOT } from "./constants.js";
55

66
export const sleep = (ms: number) =>
77
new Promise<void>((resolve) => {

scripts/benchmark-size/runner/workspace.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import exec from "execa";
1+
import { execa as exec } from "execa";
22
import { promises as fsPromise } from "fs";
33
import { join } from "path";
44

5-
import { PROJECT_ROOT } from "./constants";
6-
import { isFile } from "./utils";
5+
import { PROJECT_ROOT } from "./constants.js";
6+
import { isFile } from "./utils.js";
77

88
export interface WorkspacePackage {
99
name: string;

0 commit comments

Comments
 (0)