Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CJS and ESM support #2161

Merged
merged 7 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions .eslintrc

This file was deleted.

4 changes: 4 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://json.schemastore.org/mocharc.json",
"require": "tsx"
}
78 changes: 78 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import globals from "globals";
import tsParser from "@typescript-eslint/parser";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default [...compat.extends(
"eslint:recommended",
"prettier",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
), {
plugins: {
"@typescript-eslint": typescriptEslint,
},

languageOptions: {
globals: {
...globals.browser,
...globals.amd,
...globals.node,
},

parser: tsParser,
},

rules: {
"prettier/prettier": ["error", {
singleQuote: true,
trailingComma: "all",
endOfLine: "auto",
arrowParens: "avoid",
tabWidth: 2,
}],

"arrow-body-style": "off",
"prefer-arrow-callback": "off",
"@typescript-eslint/array-type": 0,
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-unused-vars": "off",
"arrow-parens": "off",
curly: "off",
"no-case-declarations": "off",
quotes: "off",

"@/quotes": ["error", "single", {
avoidEscape: true,
allowTemplateLiterals: true,
}],

"prefer-rest-params": "off",
"no-bitwise": "off",
"no-console": "off",

"no-empty": ["error", {
allowEmptyCatch: true,
}],

"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/no-empty-object-type": "off",
"space-before-function-paren": "off",
},
}];
25 changes: 25 additions & 0 deletions fixup.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require('fs');
const path = require('path');

const updateRequires = (filePath) => {
let content = fs.readFileSync(filePath, 'utf8');
//replace local imports eg. require("./ecpair.js") to require("ecpair.cjs")
content = content.replace(/require\('\.\/([^']*)\.js'\)/g, "require('./$1.cjs')");
content = content.replace(/require\('\.\.\/([^']*)\.js'\)/g, "require('../$1.cjs')");

fs.writeFileSync(filePath, content, 'utf8');
};

const processFiles = (dir) => {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
processFiles(filePath);
} else if (filePath.endsWith('.cjs')) {
updateRequires(filePath);
}
});
};

const dir = path.join(__dirname, 'src', 'cjs');
processFiles(dir);
Loading
Loading