静的サイトのための設定方法


単純な静的サイトを構築するときはいつも、私は常にeslint、prettier、editorconfigを設定する必要があります.これらのツールは、より良いコード標準とベストプラクティスに従うように書くコードをリントし、フォーマットするのに役立ちます.各ツールは、その利点を持って、彼らはすべてのコードを書くときに大きな流れを提供するために一緒に動作することができます.ESLint - 私のコードを分析し、より良いコーディングパターンを奨励するために従う潜在的な問題とベストプラクティスを指摘するのに役立ちますPrettier - 私がコードベースのために設定したカスタム構成に基づく私のコードを自動フォーマットするのを手伝うEditorConfig - CodeBaseの一貫したスタイリングを実施するために、私のエディタをカスタマイズすることに役立ちます
これらのツールのすべての3つはいくぶん似ており、彼らは時々重複しています.これは、開発者が適切にそれらを設定する方法を知って非常に混乱させる.正直なところ、私はまだ何の設定は、特定の問題を処理するために変更するには混乱しているときは、これは私自身と他の開発者のためのリファレンスとして、このガイドを作成したときに標準的なセットアップガイドに従って静的なウェブサイトを構築する.

目次
  • Prerequisites
  • Setup Project & Initialize NPM
  • How To Set Up ESLint
  • How To Setup Prettier
  • EditorConfig
  • Auto Lint and Format On Commit with Husky

  • 必要条件

    Before we get started, make sure you have latest version of Node.js installed along with VSCode and Terminal:



    セットアッププロジェクトとNPMの初期化
    プロジェクト用の新しいディレクトリを作成します
    mkdir <PROJECT_NAME>
    cd <PROJECT_NAME>
    
    npmを初期化します.
    npm init
    


    ハウツーとスタイル
    依存関係のパッケージとしてインストールする
    npm install eslint --save-dev
    
    Install ESLint
    eslint設定ファイルの設定
    npm init @eslint/config
    
    上記のコマンドを実行すると、eslint設定ファイルを設定するための質問の一覧が表示されます.我々は主にHTML、CSS、およびJavaScriptファイルを使用して単純な静的なウェブサイトを処理することに懸念しているので、我々はそれを簡単に保つ.

    ESLint should check syntax, report problems, and enforce a popular coding style



    Let's go with JavaScript ES Modules



    We won't be using any framework as we are building a simple static site



    No TypeScript either, but if you want to use it, select "Yes"



    Our code will only be executed on the browser



    We will use a popular style guide to enforce a set of coding standards



    We will go with Airbnb's style guide as it is a common choice for most open source projects



    Save ESLint configuration file as a JavaScript file


    ESLint Format JavaScript

    Since we are using Airbnb's style guide, we'll need to install a few more dependencies



    それだ!今、私たちは.eslintrc.js プロジェクトのルートに作成されたファイル.
    // .eslintrc.js
    
    module.exports = {
      root: true,
      env: {
        browser: true,
        es2021: true,
      },
      extends: [
        'airbnb-base',
      ],
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module',
      },
      rules: {
      },
    };
    
    さあ、インストールしましょうESLint vscodeの拡張.この拡張は、コードを書くときに、VSCodeエディタの潜在的な構文上の問題を強調しているため、重要です.
    クリエイトア.eslintignore 特定のファイルまたはディレクトリのリンギングを無視するファイル
    // .eslintignore
    
    node_modules
    
    以下を含みますnode_modules ここでは特に、それが我々のコードの一部でないので、それにどんなリンティングも走らせないようにEslintに伝えます.
    最後にセットアップをしましょうlint パッケージ内のスクリプト.コマンドラインからエラーをチェックするJSON
    "scripts": {
      "lint": "npx eslint ./"
    },
    
    このセットアップで、現在我々は走ることができますnpm run lint eslintにどんなエラーでも吐き出すよう依頼すること
    よし、それは我々がeslintのために必要であるすべてです.さっさと行きましょう!

    ハウツーとスタイル
    最初に、NPMからprettierパッケージをdev依存性としてインストールします.
    npm install --save-dev --save-exact prettier
    
    設定ファイルを作成する.prettierrc.json ルートレベルのファイル
    {
      "trailingComma": "es5",
      "tabWidth": 2,
      "semi": true,
      "singleQuote": true
    }
    
    これらは私が使用しているいくつかの正気のデフォルトです、この徹底的なlist .
    さて、インストールPrettier - Code formatter vscodeの拡張.eslint拡張子と同様に、この1つはまた、非常に便利です.なぜ?それは自動的にそれを変更するようにコードをフォーマットするので!
    保存時にコードをフォーマットするためにvscodeを設定しましょう.

    On VSCode, go over to Settings from the bottom left corner



    Then, type in format on the search setting input (up top)


  • 保証Editor: Default Formatter => Prettier - Code formatter
  • 保証Editor: Format On Paste チェックボックスをチェック
  • 保証Editor: Format On Save チェックボックスをチェック
  • また、セットアップすることができます.prettierignore 特定のファイルまたはディレクトリの自動フォーマットを無視するファイル
    // .prettierignore
    
    node_modules
    
    最後に、Aをセットアップしましょうformat スクリプトpackage.json コマンドラインからすべてのプロジェクトファイルを自動フォーマットするには、次の手順に従います.
    "scripts": {
      "format": "npx prettier --write ."
    },
    
    このセットアップで、現在我々は走ることができますnpm run format プロジェクト全体ディレクトリ内のすべてのファイルを自動でフォーマットするように指示します.

    Important!
    Let's pause for a bit... our Prettier configuration is now all set and should be working, but there is a hidden problem. ESLint comes with a few default formatting rules out of the box that may collide with Prettier formatting rules. So, to get around it, we need to override those specific ESLint rules in favor of Prettier because Prettier deals with all formatting rules.


    前に行って、きれいなオーバーライドを実行しましょう.
    インストールeslint-config-prettier NPMからのパッケージ
    npm install eslint-config-prettier --save-dev
    
    このパッケージは、きれいにオーバーラップするすべてのeslintルールを無効にしたので、きれいなルールを有効にするには、“プリティ”をextends アレイ内部.eslintrc.js ファイル.
    extends: ['airbnb-base', 'prettier'],
    
    今、我々はすべての良いはずです!コーディングの標準と書式規則を同時に実行するには、ESLILTと一緒に動作する必要があります.

    編集
    ほとんどのコード整形の面倒を見ている間、EditorConfigは開発者のエディタに追加の制限を適用できます.場合によっては、pretconfig設定を使用して、コードを自動でフォーマットすることもできます..prettierrc ファイルが提供されず、( 2 )きれいなvscode拡張モジュールが既にインストールされています.
    を作成しましょう.editorconfig プロジェクトのルートレベルのファイル:
    root = true
    
    [*]
    charset = utf-8
    end_of_line = lf
    insert_final_newline = true
    indent_style = tab
    indent_size = 2
    trim_trailing_whitespace = true
    
    [*.md]
    trim_trailing_whitespace = false
    
    追加の設定については、徹底的にlist

    ハスキーとコミットのオートリントとフォーマット
    ハスキーは、我々がgitフックの上で特定の行動を統合するのを可能にするすばらしいツールです.これは、私たちのために私たちのコードをリンギングし、書式設定に来ることができます.開発者として、我々はできるだけ多くの我々の繰り返された行動を自動化しようとします.ここで、オートリントを試み、コードをコミットします.
    まず、依存関係としてNPMからHuskyをインストールします.
    npm install husky --save-dev
    
    また、ouプロジェクト内のgitを初期化しましょう.
    git init
    
    次に実行します.
    npx husky install
    
    その後、コミットフックを追加します.
    npx husky add .husky/pre-commit "npm run lint && npm run format && git add ."
    git add .husky/pre-commit
    
    さて、変更をコミットするたびに最初に実行されますnpm run lint and npm run format 前コミットアクションとして.これは、すべてのコードがリンクされ、ソースコントロールにコミットされる前に書式設定されます.
    私が去る前に、若干のもの...
  • 📑 このページをブックマークします
  • 👍 あなたの同僚や誰かと共有し、この記事を共有するeslint、きれいな、そして
  • 💌 私がこの記事で何か間違っているならば、親切なフィードバックを残してください
  • リソース
  • https://blog.theodo.com/2019/08/empower-your-dev-environment-with-eslint-prettier-and-editorconfig-with-no-conflicts/
  • https://betterprogramming.pub/comparing-the-top-three-style-guides-and-setting-them-up-with-eslint-98ea0d2fc5b7
  • https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties
  • https://www.npmjs.com/package/husky
  • https://eslint.org/docs/user-guide/getting-started
  • https://codeburst.io/eslint-everything-you-need-to-know-about-enforcing-a-style-guide-with-eslint-d4520c732dcb
  • https://seeklogo.com/vector-logo/397338/editorconfig
  • https://github.com/spencerdcarlson/husky-elixir
  • https://freebiesupply.com/logos/eslint-logo/
  • https://prettier.io/