Skip to content
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
2 changes: 0 additions & 2 deletions .fatherrc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { defineConfig } from 'father';
import path from 'path';

export default defineConfig({
cjs: { output: 'dist' },
plugins: [path.resolve(__dirname, 'src')],
});
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rc-component/father-plugin",
"version": "2.1.2",
"version": "2.1.3",
"description": "The father plugin for all react-component projects",
"keywords": [],
"repository": "https://github.com/react-component/father-plugin.git",
Expand All @@ -10,11 +10,11 @@
"dist"
],
"scripts": {
"build": "cross-env CHECK_TS_ONLY=1 father build",
"build": "father build",
"dev": "father dev",
"lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
"prepare": "husky install",
"prepublishOnly": "cross-env CHECK_TS_ONLY=1 father doctor && npm run build"
"prepublishOnly": "father doctor && npm run build"
},
"commitlint": {
"extends": [
Expand All @@ -35,10 +35,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"
},
"devDependencies": {
Expand All @@ -47,7 +44,7 @@
"@types/eslint": "^8.56.12",
"@types/fs-extra": "^11.0.4",
"@umijs/lint": "^4",
"cross-env": "^7.0.3",
"eslint": "^8.23.0",
"father": "^4.1.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
Expand Down
125 changes: 32 additions & 93 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from 'chalk';
import { ESLint } from 'eslint';
import { execSync } from 'child_process';
import type { IApi } from 'father';
import fs from 'fs-extra';
import path from 'path';
Expand Down Expand Up @@ -37,101 +36,41 @@ export default (api: IApi) => {
const inputFolder =
api?.config?.esm?.input || api?.config?.esm?.input || 'src/';

const eslint = new ESLint({
useEslintrc: false,
errorOnUnmatchedPattern: false,
overrideConfig: {
ignorePatterns: ['__tests__', 'demo', 'locale'],
rules: {
'@typescript-eslint/consistent-type-exports': ['error'],
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',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module',
project: './tsconfig.json',
},
plugins: ['@typescript-eslint'],
},
extensions: ['tsx', 'ts'],
});

const results = await eslint.lintFiles([inputFolder]);

// 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) => {
if (/Definition for rule .* was not found./.test(message.message)) {
return;
}

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);
);
} else {
console.log('ESLint is not installed, skip.');
}
});

// modify default build config for all rc projects
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);
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;
});
};