このバージョンのstylelint-webpack-pluginは、webpack 5でのみ動作します。webpack 4については、2.x ブランチを参照してください。
このプラグインは、スタイルシートのエラーを回避し、規約を強制するのに役立つstylelint
を使用します。
まず、stylelint-webpack-plugin
をインストールする必要があります。
npm install stylelint-webpack-plugin --save-dev
または
yarn add -D stylelint-webpack-plugin
または
pnpm add -D stylelint-webpack-plugin
注意:
まだインストールしていない場合は、npmから
stylelint >= 13
もインストールする必要があります。
npm install stylelint --save-dev
または
yarn add -D stylelint
または
pnpm add -D stylelint
注意:
Stylelint 14+ではなく13を使用している場合、stylelint関連の型エラーが発生する場合は、開発依存関係として
@types/stylelint
もインストールする必要があるかもしれません。
次に、プラグインをwebpack設定に追加します。例:
const StylelintPlugin = require('stylelint-webpack-plugin');
module.exports = {
// ...
plugins: [new StylelintPlugin(options)],
// ...
};
利用可能なオプションの完全なリストについては、stylelintのオプションを参照してください。これらのオプションは、stylelint
に直接渡されます。
cache
type cache = boolean;
true
キャッシュは実行時間を短縮するためにデフォルトで有効になっています。
cacheLocation
type cacheLocation = string;
node_modules/.cache/stylelint-webpack-plugin/.stylelintcache
キャッシュの場所へのパスを指定します。ファイルまたはディレクトリを指定できます。
configFile
type context = string;
undefined
stylelint
で使用する設定ファイルの場所を指定します。
注意
デフォルトでは、これは
stylelint
によって処理されます。
context
type context = string;
compiler.context
ファイルのルートを示す文字列。
exclude
type exclude = string | Array<string>;
['node_modules', compiler.options.output.path]
除外するファイルやディレクトリを指定します。options.context
からの相対パスでなければなりません。
extensions
type extensions = string | Array<string>;
['css', 'scss', 'sass']
チェックする拡張子を指定します。
files
type files = string | Array<string>;
null
ディレクトリ、ファイル、またはglobを指定します。options.context
からの相対パスでなければなりません。ディレクトリは、options.extensions
に一致するファイルを再帰的に検索します。ファイルとglobパターンは、options.extensions
を無視します。
fix
type fix = boolean;
false
true
の場合、stylelint
は可能な限り多くのエラーを修正します。修正は実際のソースファイルに対して行われます。修正されていないエラーはすべて報告されます。エラーの自動修正ドキュメントを参照してください。
formatter
type formatter = string | (
results: Array<import('stylelint').LintResult>
) => string
'string'
結果をフォーマットするために使用するフォーマッタを指定します。formatterオプションを参照してください。
lintDirtyModulesOnly
type lintDirtyModulesOnly = boolean;
false
変更されたファイルのみをlintし、開始時のlintをスキップします。
stylelintPath
type stylelintPath = string;
stylelint
lintに使用されるstylelint
インスタンスへのパス。
threads
type threads = boolean | number;
false
cpuの数に基づいて自動選択されたプールサイズの場合はtrueに設定します。明示的なプールサイズを設定するには、1より大きい数に設定します。無効にしてメインプロセスでのみ実行する場合は、false、1、またはそれ以下に設定します。
デフォルトでは、プラグインはstylelintのエラー/警告の数に応じてエラーレポートを自動調整します。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('stylelint').LintResult>) => string)
)
| undefined;
};
false
エラーの出力をファイル(たとえば、レポートに使用するjson
ファイル)に書き込みます。filePath
はwebpack設定のoutput.path
からの相対パスです。出力ファイルに別のフォーマッタを渡すことができます。渡されない場合は、デフォルト/構成済みのフォーマッタが使用されます。
{
filePath: 'path/to/file';
formatter: 'json';
}