Skip to content

Commit ad511f8

Browse files
authoredNov 1, 2023
docs: Add CommonJS example to README (#134)
Fixes #133
1 parent 3b386f7 commit ad511f8

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed
 

‎README.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm install @eslint/eslintrc --save-dev
1616
yarn add @eslint/eslintrc -D
1717
```
1818

19-
## Usage
19+
## Usage (ESM)
2020

2121
The primary class in this package is `FlatCompat`, which is a utility to translate ESLintRC-style configs into flat configs. Here's how you use it inside of your `eslint.config.js` file:
2222

@@ -66,6 +66,50 @@ export default [
6666
];
6767
```
6868
69+
## Usage (CommonJS)
70+
71+
Using `FlatCompat` in CommonJS files is similar to ESM, but you'll use `require()` and `module.exports` instead of `import` and `export`. Here's how you use it inside of your `eslint.config.js` CommonJS file:
72+
73+
```js
74+
const { FlatCompat } = require("@eslint/eslintrc");
75+
const js = require("@eslint/js");
76+
77+
const compat = new FlatCompat({
78+
baseDirectory: __dirname, // optional; default: process.cwd()
79+
resolvePluginsRelativeTo: __dirname, // optional
80+
recommendedConfig: js.configs.recommended, // optional
81+
allConfig: js.configs.all, // optional
82+
});
83+
84+
module.exports = [
85+
86+
// mimic ESLintRC-style extends
87+
...compat.extends("standard", "example"),
88+
89+
// mimic environments
90+
...compat.env({
91+
es2020: true,
92+
node: true
93+
}),
94+
95+
// mimic plugins
96+
...compat.plugins("airbnb", "react"),
97+
98+
// translate an entire config
99+
...compat.config({
100+
plugins: ["airbnb", "react"],
101+
extends: "standard",
102+
env: {
103+
es2020: true,
104+
node: true
105+
},
106+
rules: {
107+
semi: "error"
108+
}
109+
})
110+
];
111+
```
112+
69113
## License
70114
71115
MIT License

0 commit comments

Comments
 (0)
Please sign in to comment.