Skip to content

Commit 1b41816

Browse files
committed
Add CommonJS build for compatibility with ESLint < 9
1 parent 8751b49 commit 1b41816

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
"exports": {
1616
".": {
1717
"types": "./dist/index.d.ts",
18-
"import": "./dist/index.js"
18+
"import": "./dist/index.js",
19+
"require": "./dist/index.cjs"
1920
}
2021
},
2122
"files": [
2223
"dist"
2324
],
2425
"scripts": {
25-
"build": "tsc",
26+
"build": "node scripts/build.js",
2627
"format": "eslint . --fix && prettier . --log-level warn --write",
2728
"test": "vitest",
2829
"release": "np"
@@ -48,6 +49,6 @@
4849
"vitest": "^1.3.1"
4950
},
5051
"peerDependencies": {
51-
"eslint": "^8.0.0"
52+
"eslint": ">=8.0.0"
5253
}
5354
}

scripts/build.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { readdir, rename, rm } from 'node:fs/promises';
2+
import { join, dirname } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
const __dirname = dirname(fileURLToPath(import.meta.url));
6+
const cjsDir = join(__dirname, '../dist/cjs-temp');
7+
const distDir = join(__dirname, '../dist');
8+
9+
async function renameCjsFiles() {
10+
// Recursively get all .js files
11+
async function getJsFiles(dir) {
12+
const entries = await readdir(dir, { withFileTypes: true });
13+
const files = await Promise.all(entries.map((entry) => {
14+
const res = join(dir, entry.name);
15+
return entry.isDirectory() ? getJsFiles(res) : res;
16+
}));
17+
return files.flat().filter(file => file.endsWith('.js'));
18+
}
19+
20+
// Rename all .js files to .cjs and move them to dist
21+
const jsFiles = await getJsFiles(cjsDir);
22+
await Promise.all(jsFiles.map(async (file) => {
23+
const relativePath = file.split('cjs-temp/')[1];
24+
const destPath = join(distDir, relativePath.replace('.js', '.cjs'));
25+
await rename(file, destPath);
26+
}));
27+
28+
// Clean up temporary directory
29+
await rm(cjsDir, { recursive: true });
30+
}
31+
32+
// Run TypeScript builds
33+
const { execSync } = await import('node:child_process');
34+
execSync('tsc -p tsconfig.json', { stdio: 'inherit' });
35+
execSync('tsc -p tsconfig.cjs.json', { stdio: 'inherit' });
36+
37+
// Rename CJS files
38+
await renameCjsFiles();

tsconfig.cjs.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"moduleResolution": "Node",
6+
"outDir": "./dist/cjs-temp"
7+
}
8+
}

0 commit comments

Comments
 (0)