From 670688ce8164e2b26735ecfbdc7e22fd6ad51461 Mon Sep 17 00:00:00 2001 From: Marvin <63292605+yi-boide@users.noreply.github.com> Date: Fri, 10 May 2024 19:59:27 +0800 Subject: [PATCH] feat(eslint): support generate mjs/cjs eslint config #488 (#494) Co-authored-by: Andy Co-authored-by: Anthony Fu --- README.md | 1 + src/core/ctx.ts | 9 ++++++++- src/core/eslintrc.ts | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5787856..f34293c 100644 --- a/README.md +++ b/README.md @@ -312,6 +312,7 @@ AutoImport({ // eslint globals Docs - https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals eslintrc: { enabled: false, // Default `false` + // provide path ending with `.mjs` or `.cjs` to generate the file with the respective format filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json` globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable') }, diff --git a/src/core/ctx.ts b/src/core/ctx.ts index ac71b7f..7480fee 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -193,8 +193,15 @@ ${dts}`.trim()}\n` ) } if (eslintrc.enabled && eslintrc.filepath) { + const filepath = eslintrc.filepath promises.push( - generateESLint().then((content) => { + generateESLint().then(async (content) => { + if (filepath.endsWith('.cjs')) { + content = `module.exports = ${content}` + } + else if (filepath.endsWith('.mjs') || filepath.endsWith('.js')) { + content = `export default ${content}` + } content = `${content}\n` if (content.trim() !== lastESLint?.trim()) { lastESLint = content diff --git a/src/core/eslintrc.ts b/src/core/eslintrc.ts index 696585d..77a4855 100644 --- a/src/core/eslintrc.ts +++ b/src/core/eslintrc.ts @@ -6,7 +6,7 @@ export function generateESLintConfigs( eslintrc: ESLintrc, globals: Record = {}, ) { - const eslintConfigs = { globals } + const eslintConfigs: any = { globals } imports .map(i => i.as ?? i.name)