Compilation Diagnostic Rules
Building diagnostic rules is similar to lint tools like ESLint
, but with a difference. ESLint
performs static code scanning and is independent of the build process. On the other hand, the code diagnostics here are closely related to the build process of Rspack
or Webpack
. It incorporates various internal parameters generated during the build process to assist in the analysis, such as ModuleGraph, internal markers for each module by Webpack, and runtime added after code transformation, and so on...
If any issues are detected during the build process, they will be displayed in the CLI and the diagnostic summary webpage, as shown in the following image:
How to Use
Using diagnostic rules is simple. They are configured under the linter
option of the entire plugin, as shown in the following example:
import { RsdoctorWebpackPlugin } from '@rsdoctor/webpack-plugin';
export default {
plugin: [
new RsdoctorWebpackPlugin({
linter: {
level: 'Error',
extends: [],
rules: {
'default-import-check': 'off',
'duplicate-package': [
'Error',
{
checkVersion: 'minor',
ignore: ['chalk', '@babel/runtime'],
},
],
},
},
}),
],
};
The type of linter
:
interface Options {
rules?: RulesMap;
level?: SeverityString;
extends?: ExtendRuleData[];
}
/**
* Validation Level
* - When set to `'Warn'`, only rules with the category `'Warn'` will be executed
* - When set to `'Error'`, all rules will be executed
*/
type SeverityString = 'Warn' | 'Error';
/** Rule Level */
type SeverityInput = SeverityString | 'off' | 'on';
/** Rule Configuration */
type RulesMap = Record<string, RuleConfigItem>;
/** Individual Rule Configuration */
type RuleConfigItem =
// Only severity level, which takes precedence over the rule's own configuration
| SeverityInput
// In the case of an array, the first item is the severity level, and the second item is the rule configuration
| [SeverityInput, unknown];
Existing Internal Rules
There are already three rules included in the existing diagnostic tool, which are:
- [E1001] Duplicate Packages
- [E1002] Default Import Check
- [E1003] Loader Performance Optimization
- [E1004] ECMA Version Check