Skip to content

Commit c91866c

Browse files
nzakasfasttime
andauthored
fix: CommonJS types in all packages (#148)
Co-authored-by: Francesco Trotta <[email protected]>
1 parent 7dfc0d9 commit c91866c

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default [
3131
{
3232
files: [
3333
"scripts/**",
34+
"tools/**",
3435
"packages/migrate-config/src/migrate-config-cli.js",
3536
],
3637
rules: {

packages/compat/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"test": "tests"
2626
},
2727
"scripts": {
28-
"build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"",
28+
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
2929
"build": "rollup -c && tsc -p tsconfig.esm.json && npm run build:cts",
3030
"test:jsr": "npx jsr@latest publish --dry-run",
3131
"test": "mocha tests/*.js",

packages/config-array/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"homepage": "https://github.com/eslint/rewrite#readme",
3333
"scripts": {
3434
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
35-
"build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"",
35+
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
3636
"build:std__path": "rollup -c rollup.std__path-config.js && node fix-std__path-imports",
3737
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts && npm run build:std__path",
3838
"test:jsr": "npx jsr@latest publish --dry-run",

packages/object-schema/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"test": "tests"
2626
},
2727
"scripts": {
28-
"build:cts": "node -e \"fs.copyFileSync('dist/esm/index.d.ts', 'dist/cjs/index.d.cts')\"",
28+
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
2929
"build": "rollup -c && tsc -p tsconfig.esm.json && npm run build:cts",
3030
"test:jsr": "npx jsr@latest publish --dry-run",
3131
"test": "mocha tests/",

packages/plugin-kit/build-cts.js

-16
This file was deleted.

packages/plugin-kit/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"homepage": "https://github.com/eslint/rewrite#readme",
3333
"scripts": {
3434
"build:dedupe-types": "node ../../tools/dedupe-types.js dist/cjs/index.cjs dist/esm/index.js",
35-
"build:cts": "node ./build-cts.js",
35+
"build:cts": "node ../../tools/build-cts.js dist/esm/index.d.ts dist/cjs/index.d.cts",
3636
"build": "rollup -c && npm run build:dedupe-types && tsc -p tsconfig.esm.json && npm run build:cts",
3737
"pretest": "npm run build",
3838
"test": "mocha tests/",

tools/build-cts.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @fileoverview Rewrites import expressions for CommonJS compatibility.
3+
* This script creates "dist/cjs/index.d.cts" from "dist/esm/index.d.ts" by modifying imports
4+
* from `"./types.ts"` to `"./types.cts"`.
5+
*
6+
* node tools/build-cts.js /path/to/esm/index.d.ts path/to/cjs/index.d.cts
7+
*
8+
* @author Francesco Trotta
9+
*/
10+
11+
import { readFile, writeFile } from "node:fs/promises";
12+
13+
const filename = process.argv[2];
14+
const newFilename = process.argv[3];
15+
16+
if (!filename) {
17+
console.error("No filename provided.");
18+
process.exit(1);
19+
}
20+
21+
if (!newFilename) {
22+
console.error("No new filename provided.");
23+
process.exit(1);
24+
}
25+
26+
const oldSourceText = await readFile(filename, "utf-8");
27+
const newSourceText = oldSourceText.replaceAll(
28+
'import("./types.ts")',
29+
'import("./types.cts")',
30+
);
31+
32+
await writeFile(newFilename, newSourceText);

0 commit comments

Comments
 (0)