Skip to content

Commit 670688c

Browse files
yi-boideAndyantfu
authored
feat(eslint): support generate mjs/cjs eslint config #488 (#494)
Co-authored-by: Andy <andy.gui@gempoll.com> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 87baffc commit 670688c

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ AutoImport({
312312
// eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
313313
eslintrc: {
314314
enabled: false, // Default `false`
315+
// provide path ending with `.mjs` or `.cjs` to generate the file with the respective format
315316
filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
316317
globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
317318
},

src/core/ctx.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,15 @@ ${dts}`.trim()}\n`
193193
)
194194
}
195195
if (eslintrc.enabled && eslintrc.filepath) {
196+
const filepath = eslintrc.filepath
196197
promises.push(
197-
generateESLint().then((content) => {
198+
generateESLint().then(async (content) => {
199+
if (filepath.endsWith('.cjs')) {
200+
content = `module.exports = ${content}`
201+
}
202+
else if (filepath.endsWith('.mjs') || filepath.endsWith('.js')) {
203+
content = `export default ${content}`
204+
}
198205
content = `${content}\n`
199206
if (content.trim() !== lastESLint?.trim()) {
200207
lastESLint = content

src/core/eslintrc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function generateESLintConfigs(
66
eslintrc: ESLintrc,
77
globals: Record<string, ESLintGlobalsPropValue> = {},
88
) {
9-
const eslintConfigs = { globals }
9+
const eslintConfigs: any = { globals }
1010

1111
imports
1212
.map(i => i.as ?? i.name)

0 commit comments

Comments
 (0)