From edeac855fd1ac1ef87006f62af2ac88eff020ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Thu, 27 Mar 2025 15:46:08 +0800 Subject: [PATCH 1/3] chore: use self eslint --- .fatherrc.ts | 5 ++- package.json | 8 +++-- src/index.ts | 91 +++++++++++++++++++++++++++++++++------------------- 3 files changed, 66 insertions(+), 38 deletions(-) diff --git a/.fatherrc.ts b/.fatherrc.ts index f0c53dc..eb6439b 100644 --- a/.fatherrc.ts +++ b/.fatherrc.ts @@ -1,8 +1,7 @@ import { defineConfig } from 'father'; +import path from 'path'; export default defineConfig({ cjs: { output: 'dist' }, - targets: { - chrome: 85, - }, + plugins: [path.resolve(__dirname, 'src')], }); diff --git a/package.json b/package.json index 6805962..a60ee63 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "dist" ], "scripts": { - "build": "father build", + "build": "cross-env CHECK_TS_ONLY=1 father build", "dev": "father dev", "lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"", "prepare": "husky install", @@ -35,13 +35,17 @@ ] }, "dependencies": { + "@typescript-eslint/parser": "^8.28.0", + "eslint": "^8.23.0", "fs-extra": "^11.3.0" }, "devDependencies": { "@commitlint/cli": "^17.1.2", "@commitlint/config-conventional": "^17.1.0", + "@types/eslint": "^8.56.12", + "@types/fs-extra": "^11.0.4", "@umijs/lint": "^4", - "eslint": "^8.23.0", + "cross-env": "^7.0.3", "father": "^4.1.0", "husky": "^8.0.1", "lint-staged": "^13.0.3", diff --git a/src/index.ts b/src/index.ts index cf79c02..50cacff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { execSync } from 'child_process'; +import { ESLint } from 'eslint'; import type { IApi } from 'father'; import fs from 'fs-extra'; import path from 'path'; @@ -20,6 +20,8 @@ export default (api: IApi) => { return; } + console.log('Use RC Father Plugin...'); + // Break if current project not install `@rc-component/np` const packageJson = await fs.readJson(path.join(cwd, 'package.json')); @@ -34,41 +36,64 @@ export default (api: IApi) => { const inputFolder = api?.config?.esm?.input || api?.config?.esm?.input || 'src/'; - const isEslintInstalled = checkNpmPackageDependency(packageJson, 'eslint'); - if (isEslintInstalled) { - execSync( - // Requires compatibility with Windows environment - `npx eslint ${inputFolder} --ext .tsx,.ts --rule "@typescript-eslint/consistent-type-exports: error"`, - { - cwd, - env: process.env, - stdio: [process.stdin, process.stdout, process.stderr], - encoding: 'utf-8', + const eslint = new ESLint({ + useEslintrc: false, + overrideConfig: { + rules: { + '@typescript-eslint/consistent-type-exports': ['error'], }, - ); - } else { - console.log('ESLint is not installed, skip.'); - } + parser: '@typescript-eslint/parser', + parserOptions: { + ecmaVersion: 2021, + sourceType: 'module', + }, + }, + extensions: ['tsx', 'ts'], + }); + + const results = await eslint.lintFiles([inputFolder]); + const totalErrors = results.reduce((sum, item) => sum + item.errorCount, 0); + console.log('Total error count:', totalErrors); + console.log('results', results); + console.log(results[0].messages); + + // const isEslintInstalled = checkNpmPackageDependency(packageJson, 'eslint'); + // if (isEslintInstalled) { + // execSync( + // // Requires compatibility with Windows environment + // `npx eslint ${inputFolder} --ext .tsx,.ts --rule "@typescript-eslint/consistent-type-exports: error"`, + // { + // cwd, + // env: process.env, + // stdio: [process.stdin, process.stdout, process.stderr], + // encoding: 'utf-8', + // }, + // ); + // } else { + // console.log('ESLint is not installed, skip.'); + // } }); // modify default build config for all rc projects - api.modifyDefaultConfig((memo) => { - Object.assign(memo, { - esm: { - output: 'es', - // transform all rc-xx/lib to rc-xx/es for esm build - extraBabelPlugins: [require.resolve('./babelPluginImportLib2Es')], - }, - cjs: { - // specific platform to browser, father 4 build cjs for node by default - platform: 'browser', - output: 'lib', - }, - targets: { - chrome: 85, - }, - } as typeof memo); + if (!process.env.CHECK_TS_ONLY) { + api.modifyDefaultConfig((memo) => { + Object.assign(memo, { + esm: { + output: 'es', + // transform all rc-xx/lib to rc-xx/es for esm build + extraBabelPlugins: [require.resolve('./babelPluginImportLib2Es')], + }, + cjs: { + // specific platform to browser, father 4 build cjs for node by default + platform: 'browser', + output: 'lib', + }, + targets: { + chrome: 85, + }, + } as typeof memo); - return memo; - }); + return memo; + }); + } }; From 00b030eaa82d890c45acd0ae2aec589a12136faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Thu, 27 Mar 2025 15:52:02 +0800 Subject: [PATCH 2/3] chore: finish config --- package.json | 1 + src/index.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/package.json b/package.json index a60ee63..6ece4d9 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ ] }, "dependencies": { + "@typescript-eslint/eslint-plugin": "^8.28.0", "@typescript-eslint/parser": "^8.28.0", "eslint": "^8.23.0", "fs-extra": "^11.3.0" diff --git a/src/index.ts b/src/index.ts index 50cacff..ea11da8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,7 +46,9 @@ export default (api: IApi) => { parserOptions: { ecmaVersion: 2021, sourceType: 'module', + project: './tsconfig.json', }, + plugins: ['@typescript-eslint'], }, extensions: ['tsx', 'ts'], }); From 1ebae060b9488e3c7bdd2d503978a5b4d0293c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Thu, 27 Mar 2025 16:08:23 +0800 Subject: [PATCH 3/3] chore: eslint called --- package.json | 1 + src/index.ts | 72 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 6ece4d9..a40dafb 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "dependencies": { "@typescript-eslint/eslint-plugin": "^8.28.0", "@typescript-eslint/parser": "^8.28.0", + "chalk": "^4.1.2", "eslint": "^8.23.0", "fs-extra": "^11.3.0" }, diff --git a/src/index.ts b/src/index.ts index ea11da8..1f0efc5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import chalk from 'chalk'; import { ESLint } from 'eslint'; import type { IApi } from 'father'; import fs from 'fs-extra'; @@ -20,7 +21,7 @@ export default (api: IApi) => { return; } - console.log('Use RC Father Plugin...'); + console.log('Check Typescript exports...'); // Break if current project not install `@rc-component/np` const packageJson = await fs.readJson(path.join(cwd, 'package.json')); @@ -54,26 +55,55 @@ export default (api: IApi) => { }); const results = await eslint.lintFiles([inputFolder]); - const totalErrors = results.reduce((sum, item) => sum + item.errorCount, 0); - console.log('Total error count:', totalErrors); - console.log('results', results); - console.log(results[0].messages); - - // const isEslintInstalled = checkNpmPackageDependency(packageJson, 'eslint'); - // if (isEslintInstalled) { - // execSync( - // // Requires compatibility with Windows environment - // `npx eslint ${inputFolder} --ext .tsx,.ts --rule "@typescript-eslint/consistent-type-exports: error"`, - // { - // cwd, - // env: process.env, - // stdio: [process.stdin, process.stdout, process.stderr], - // encoding: 'utf-8', - // }, - // ); - // } else { - // console.log('ESLint is not installed, skip.'); - // } + + // Collect eslint errors + interface ErrorInfo { + line: number; + text: string; + error: string; + } + const errorMessages: { + filePath: string; + errors: ErrorInfo[]; + }[] = []; + + results.forEach((result) => { + const fullText = result.source || ''; + const textLines = fullText.split('\n'); + + const errorInfos: ErrorInfo[] = []; + + result.messages.forEach((message) => { + errorInfos.push({ + line: message.line, + text: textLines[message.line - 1], + error: message.message, + }); + }); + + if (errorInfos.length) { + errorMessages.push({ + filePath: result.filePath, + errors: errorInfos, + }); + } + }); + + if (errorMessages.length) { + console.log(''); + console.log(chalk.red('Eslint errors:')); + + errorMessages.forEach((error) => { + console.log(chalk.yellow(`${error.filePath}`)); + error.errors.forEach((item) => { + console.log(`${item.line}: ${item.text.trim()}`); + console.log(chalk.gray(`${item.error}`)); + }); + console.log(''); + }); + + process.exit(1); + } }); // modify default build config for all rc projects