此版本的 eslint-webpack-plugin 僅適用於 webpack 5。對於 webpack 4,請參閱 2.x 分支。
此外掛程式使用 eslint
在您的 JavaScript 程式碼中尋找並修正問題
首先,您需要安裝 eslint-webpack-plugin
npm install eslint-webpack-plugin --save-dev
或
yarn add -D eslint-webpack-plugin
或
pnpm add -D eslint-webpack-plugin
注意:
如果您尚未安裝,您還需要從 npm 安裝
eslint >= 8
npm install eslint --save-dev
或
yarn add -D eslint
或
pnpm add -D eslint
然後將外掛新增至 webpack 設定。例如
const ESLintPlugin = require('eslint-webpack-plugin');
module.exports = {
// ...
plugins: [new ESLintPlugin(options)],
// ...
};
您可以傳遞 eslint 選項。
注意
您提供的設定選項將傳遞給
ESLint
類別。這是一組與您在package.json
或.eslintrc
中指定的選項不同的選項。請參閱 eslint 文件 以取得更多詳細資訊。
警告:
在 eslint-webpack-plugin 版本 1 中,選項會傳遞給現在已棄用的 CLIEngine。
快取
type cache = boolean;
true
預設啟用快取以減少執行時間。
快取位置
type cacheLocation = string;
node_modules/.cache/eslint-webpack-plugin/.eslintcache
指定快取位置的路徑。可以是檔案或目錄。
設定類型
type configType = "flat" | "eslintrc";
eslintrc
指定要與 ESLint 搭配使用的設定類型。
eslintrc
是大多數 ESLint 版本中可用的經典設定格式。flat
是 ESLint 8.21.0 中引入的新格式。新的設定格式在其 專屬文件 中說明。
由於此設定格式被視為實驗性質,因此在 ESLint 8 中未在主 ESLint 模組中匯出。您需要將
eslintPath
設定為eslint/use-at-your-own-risk
,才能讓此設定格式運作。
內容
type context = string;
compiler.context
指示檔案根目錄的字串。
eslintPath
type eslintPath = string;
eslint
將用於 linting 的 eslint
實例路徑。如果 eslintPath
是資料夾,例如官方 eslint,或指定 formatter
選項。現在您不必安裝 eslint
。
extensions
type extensions = string | Array<string>;
'js'
指定應檢查的副檔名。
exclude
type exclude = string | Array<string>;
'node_modules'
指定要排除的檔案和/或目錄。必須與 options.context
相對。
resourceQueryExclude
type resourceQueryExclude = RegExp | Array<RegExp>;
[]
指定要排除的資源查詢。
files
type files = string | Array<string>;
null
指定目錄、檔案或 glob。必須與 options.context
相對。目錄會遞迴搜尋符合 options.extensions
的檔案。檔案和 glob 模式會忽略 options.extensions
。
fix
type fix = boolean;
false
將啟用 ESLint 自動修正功能。
請小心:此選項將變更原始檔。
formatter
type formatter = string| (
results: Array<import('eslint').ESLint.LintResult>,
data?: import('eslint').ESLint.LintResultData | undefined
) => string
'stylish'
接受具有一個參數(eslint 訊息陣列(物件))的函式。函式必須傳回輸出字串。您可以使用官方 eslint 格式化程式。
lintDirtyModulesOnly
type lintDirtyModulesOnly = boolean;
false
僅檢查已變更的檔案,在開始時略過檢查。
threads
type threads = boolean | number;
false
將在執行緒池中執行檢查工作。除非您指定數字,否則池大小會自動調整。
預設情況下,外掛程式會根據 eslint 錯誤/警告計數自動調整錯誤回報。您仍可以使用 emitError
或 emitWarning
選項強制執行此行為。
emitError
type emitError = boolean;
true
找到的錯誤將永遠發出,若要停用,請設定為 false
。
emitWarning
type emitWarning = boolean;
true
找到的警告將永遠發出,若要停用,請設定為 false
。
failOnError
type failOnError = boolean;
true
如果出現任何錯誤,將導致模組建置失敗,若要停用,請設定為 false
。
failOnWarning
type failOnWarning = boolean;
false
如果出現任何警告,將導致模組建置失敗,如果設定為 true
的話。
quiet
type quiet = boolean;
false
如果設定為 true
,將僅處理和回報錯誤,並忽略警告。
outputReport
type outputReport =
| boolean
| {
filePath?: string | undefined;
formatter?:
| (
| string
| ((
results: Array<import('eslint').ESLint.LintResult>,
data?: import('eslint').ESLint.LintResultData | undefined,
) => string)
)
| undefined;
};
false
將錯誤輸出寫入檔案,例如 checkstyle xml 檔案,以供 Jenkins CI 回報使用。
filePath
是絕對路徑或相對於 webpack 設定:output.path
。你可以傳入不同的 formatter
給輸出檔案,如果沒有傳入,將會使用預設/設定的 formatter。