Skip to content

Commit 303b926

Browse files
committed
feat: migrate to ESM
1 parent d91ce6f commit 303b926

16 files changed

+5513
-8646
lines changed

.eslintrc

+8-9
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
"jest": true
88
},
99
"parser": "@typescript-eslint/parser",
10-
"extends": [
11-
"eslint:recommended",
12-
"plugin:@typescript-eslint/recommended",
13-
"plugin:prettier/recommended",
14-
"prettier"
15-
],
16-
"plugins": [
17-
"import"
18-
],
1910
"parserOptions": {
2011
"project": "tsconfig.json",
2112
"sourceType": "module"
2213
},
14+
"plugins": [
15+
"import"
16+
],
17+
"extends": [
18+
"eslint:recommended",
19+
"plugin:@typescript-eslint/recommended",
20+
"plugin:prettier/recommended"
21+
],
2322
"rules": {
2423
"linebreak-style": ["error", "unix"],
2524
"no-empty": 1,

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ npm install
5858
# build the dist
5959
npm run build
6060
# run the repl (this allows you to import from ./src)
61-
npm run ts-node
61+
npm run tsx
6262
# run the tests
6363
npm run test
6464
# lint the source code

benches/index.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
#!/usr/bin/env ts-node
22

3-
import fs from 'fs';
4-
import path from 'path';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import url from 'node:url';
56
import si from 'systeminformation';
6-
import workerManager from './worker_manager';
7+
import { benchesPath } from './utils/utils.js';
8+
import workerManager from './worker_manager.js';
79

810
async function main(): Promise<void> {
9-
await fs.promises.mkdir(path.join(__dirname, 'results'), { recursive: true });
11+
await fs.promises.mkdir(path.join(benchesPath, 'results'), {
12+
recursive: true,
13+
});
1014
await workerManager();
1115
const resultFilenames = await fs.promises.readdir(
12-
path.join(__dirname, 'results'),
16+
path.join(benchesPath, 'results'),
1317
);
1418
const metricsFile = await fs.promises.open(
15-
path.join(__dirname, 'results', 'metrics.txt'),
19+
path.join(benchesPath, 'results', 'metrics.txt'),
1620
'w',
1721
);
1822
let concatenating = false;
1923
for (const resultFilename of resultFilenames) {
2024
if (/.+_metrics\.txt$/.test(resultFilename)) {
2125
const metricsData = await fs.promises.readFile(
22-
path.join(__dirname, 'results', resultFilename),
26+
path.join(benchesPath, 'results', resultFilename),
2327
);
2428
if (concatenating) {
2529
await metricsFile.write('\n');
@@ -35,9 +39,16 @@ async function main(): Promise<void> {
3539
system: 'model, manufacturer',
3640
});
3741
await fs.promises.writeFile(
38-
path.join(__dirname, 'results', 'system.json'),
42+
path.join(benchesPath, 'results', 'system.json'),
3943
JSON.stringify(systemData, null, 2),
4044
);
4145
}
4246

43-
void main();
47+
if (import.meta.url.startsWith('file:')) {
48+
const modulePath = url.fileURLToPath(import.meta.url);
49+
if (process.argv[1] === modulePath) {
50+
void main();
51+
}
52+
}
53+
54+
export default main;

benches/utils/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './utils';
1+
export * from './utils.js';

benches/utils/utils.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import url from 'node:url';
34
import b from 'benny';
45
import { codeBlock } from 'common-tags';
5-
import packageJson from '../../package.json';
6+
import packageJson from '../../package.json' assert { type: 'json' };
7+
8+
const benchesPath = path.dirname(
9+
path.dirname(url.fileURLToPath(import.meta.url)),
10+
);
611

712
const suiteCommon = [
813
b.cycle(),
914
b.complete(),
1015
b.save({
1116
file: (summary) => summary.name,
12-
folder: path.join(__dirname, '../results'),
17+
folder: path.join(benchesPath, 'results'),
1318
version: packageJson.version,
1419
details: true,
1520
}),
1621
b.save({
1722
file: (summary) => summary.name,
18-
folder: path.join(__dirname, '../results'),
23+
folder: path.join(benchesPath, 'results'),
1924
version: packageJson.version,
2025
format: 'chart.html',
2126
}),
2227
b.complete((summary) => {
2328
const filePath = path.join(
24-
__dirname,
25-
'../results',
29+
benchesPath,
30+
'results',
2631
summary.name + '_metrics.txt',
2732
);
2833
fs.writeFileSync(
@@ -58,4 +63,4 @@ const suiteCommon = [
5863
}),
5964
];
6065

61-
export { suiteCommon };
66+
export { benchesPath, suiteCommon };

benches/worker_manager.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import type { WorkerModule } from '@/worker';
2-
import path from 'path';
3-
import crypto from 'crypto';
1+
import type { WorkerModule } from '#worker.js';
2+
import path from 'node:path';
3+
import url from 'node:url';
4+
import crypto from 'node:crypto';
45
import b from 'benny';
56
import { spawn, Worker, Transfer } from 'threads';
67
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
7-
import WorkerManager from '@/WorkerManager';
8-
import { suiteCommon } from './utils';
8+
import { suiteCommon } from './utils/index.js';
9+
import WorkerManager from '#WorkerManager.js';
10+
11+
const filePath = url.fileURLToPath(import.meta.url);
912

1013
const logger = new Logger('WorkerManager Bench', LogLevel.WARN, [
1114
new StreamHandler(),
@@ -22,7 +25,7 @@ async function main() {
2225
// 1 KiB of data is still too small
2326
const bytes = crypto.randomBytes(1024 * 1024);
2427
const summary = await b.suite(
25-
path.basename(__filename, path.extname(__filename)),
28+
path.basename(filePath, path.extname(filePath)),
2629
b.add('call overhead', async () => {
2730
// This calls a noop, this will show the overhead costs
2831
// All parallelised operation can never be faster than this
@@ -104,8 +107,11 @@ async function main() {
104107
return summary;
105108
}
106109

107-
if (require.main === module) {
108-
void main();
110+
if (import.meta.url.startsWith('file:')) {
111+
const modulePath = url.fileURLToPath(import.meta.url);
112+
if (process.argv[1] === modulePath) {
113+
void main();
114+
}
109115
}
110116

111117
export default main;

jest.config.js jest.config.mjs

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
const path = require('path');
2-
const { pathsToModuleNameMapper } = require('ts-jest');
3-
const { compilerOptions } = require('./tsconfig');
1+
import path from 'node:path';
2+
import url from 'node:url';
3+
import tsconfigJSON from './tsconfig.json' assert { type: "json" };
44

5-
const moduleNameMapper = pathsToModuleNameMapper(compilerOptions.paths, {
6-
prefix: '<rootDir>/src/',
7-
});
5+
const projectPath = path.dirname(url.fileURLToPath(import.meta.url));
86

97
// Global variables that are shared across the jest worker pool
108
// These variables must be static and serializable
119
const globals = {
1210
// Absolute directory to the project root
13-
projectDir: __dirname,
11+
projectDir: projectPath,
1412
// Absolute directory to the test root
15-
testDir: path.join(__dirname, 'tests'),
13+
testDir: path.join(projectPath, 'tests'),
1614
// Default asynchronous test timeout
1715
defaultTimeout: 20000,
1816
// Timeouts rely on setTimeout which takes 32 bit numbers
@@ -24,7 +22,7 @@ const globals = {
2422
// They can however receive the process environment
2523
// Use `process.env` to set variables
2624

27-
module.exports = {
25+
const config = {
2826
testEnvironment: 'node',
2927
verbose: true,
3028
collectCoverage: false,
@@ -40,10 +38,10 @@ module.exports = {
4038
parser: {
4139
syntax: "typescript",
4240
tsx: true,
43-
decorators: compilerOptions.experimentalDecorators,
41+
decorators: tsconfigJSON.compilerOptions.experimentalDecorators,
4442
dynamicImport: true,
4543
},
46-
target: compilerOptions.target.toLowerCase(),
44+
target: tsconfigJSON.compilerOptions.target.toLowerCase(),
4745
keepClassNames: true,
4846
},
4947
}
@@ -77,5 +75,10 @@ module.exports = {
7775
'jest-extended/all',
7876
'<rootDir>/tests/setupAfterEnv.ts'
7977
],
80-
moduleNameMapper: moduleNameMapper,
78+
moduleNameMapper: {
79+
"^(\\.{1,2}/.*)\\.js$": "$1",
80+
},
81+
extensionsToTreatAsEsm: ['.ts', '.tsx', '.mts'],
8182
};
83+
84+
export default config;

0 commit comments

Comments
 (0)