Skip to content

Commit d6fe656

Browse files
authored
Merge pull request #2161 from Nesopie/feat/hybrid
Add CJS and ESM support
2 parents f06bbce + 240be2f commit d6fe656

File tree

165 files changed

+12546
-8951
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+12546
-8951
lines changed

.eslintrc

Lines changed: 0 additions & 57 deletions
This file was deleted.

.mocharc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://json.schemastore.org/mocharc.json",
3+
"require": "tsx"
4+
}

eslint.config.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import typescriptEslint from "@typescript-eslint/eslint-plugin";
2+
import globals from "globals";
3+
import tsParser from "@typescript-eslint/parser";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
import js from "@eslint/js";
7+
import { FlatCompat } from "@eslint/eslintrc";
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
11+
const compat = new FlatCompat({
12+
baseDirectory: __dirname,
13+
recommendedConfig: js.configs.recommended,
14+
allConfig: js.configs.all
15+
});
16+
17+
export default [...compat.extends(
18+
"eslint:recommended",
19+
"prettier",
20+
"plugin:@typescript-eslint/recommended",
21+
"plugin:prettier/recommended",
22+
), {
23+
plugins: {
24+
"@typescript-eslint": typescriptEslint,
25+
},
26+
27+
languageOptions: {
28+
globals: {
29+
...globals.browser,
30+
...globals.amd,
31+
...globals.node,
32+
},
33+
34+
parser: tsParser,
35+
},
36+
37+
rules: {
38+
"prettier/prettier": ["error", {
39+
singleQuote: true,
40+
trailingComma: "all",
41+
endOfLine: "auto",
42+
arrowParens: "avoid",
43+
tabWidth: 2,
44+
}],
45+
46+
"arrow-body-style": "off",
47+
"prefer-arrow-callback": "off",
48+
"@typescript-eslint/array-type": 0,
49+
"@typescript-eslint/no-inferrable-types": "off",
50+
"@typescript-eslint/ban-types": "off",
51+
"@typescript-eslint/no-unused-vars": "off",
52+
"arrow-parens": "off",
53+
curly: "off",
54+
"no-case-declarations": "off",
55+
quotes: "off",
56+
57+
"@/quotes": ["error", "single", {
58+
avoidEscape: true,
59+
allowTemplateLiterals: true,
60+
}],
61+
62+
"prefer-rest-params": "off",
63+
"no-bitwise": "off",
64+
"no-console": "off",
65+
66+
"no-empty": ["error", {
67+
allowEmptyCatch: true,
68+
}],
69+
70+
"@typescript-eslint/no-var-requires": "off",
71+
"@typescript-eslint/no-non-null-assertion": "off",
72+
"@typescript-eslint/no-explicit-any": "off",
73+
"no-unused-expressions": "off",
74+
"@typescript-eslint/no-unused-expressions": "off",
75+
"@typescript-eslint/no-empty-object-type": "off",
76+
"space-before-function-paren": "off",
77+
},
78+
}];

fixup.cjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const updateRequires = (filePath) => {
5+
let content = fs.readFileSync(filePath, 'utf8');
6+
//replace local imports eg. require("./ecpair.js") to require("ecpair.cjs")
7+
content = content.replace(/require\('\.\/([^']*)\.js'\)/g, "require('./$1.cjs')");
8+
content = content.replace(/require\('\.\.\/([^']*)\.js'\)/g, "require('../$1.cjs')");
9+
10+
fs.writeFileSync(filePath, content, 'utf8');
11+
};
12+
13+
const processFiles = (dir) => {
14+
fs.readdirSync(dir).forEach((file) => {
15+
const filePath = path.join(dir, file);
16+
if (fs.lstatSync(filePath).isDirectory()) {
17+
processFiles(filePath);
18+
} else if (filePath.endsWith('.cjs')) {
19+
updateRequires(filePath);
20+
}
21+
});
22+
};
23+
24+
const dir = path.join(__dirname, 'src', 'cjs');
25+
processFiles(dir);

0 commit comments

Comments
 (0)