I want to add linting by ESlint, Stylelint, HTMLHint and code formatting by Prettier.
Now I’m trying to make my own detailed guide to describe step-by-step process to configure linting for a Sage 11 theme. Let me show what I have done.
Step 1: Install the Necessary Packages
First, navigate to your Sage 11 theme directory in the terminal and install the required packages using npm.
#Install Prettier and its plugins
npm install -D prettier prettier-plugin-tailwindcss
#Install ESLint and its plugins
npm install -D eslint eslint-plugin-import eslint-config-prettier
#Install Stylelint and its plugins
npm install -D stylelint stylelint-config-standard stylelint-prettier
#Install HTMLHint
npm install -D htmlhint
prettier-plugin-tailwindcss
: This is a crucial plugin that automatically sorts your Tailwind CSS classes in a consistent, readable order.
eslint-config-prettier
: This package ensures that ESLint rules don’t conflict with Prettier’s formatting, preventing a mess of errors.
stylelint-prettier
: The equivalent plugin for Stylelint, disabling any conflicting rules with Prettier.
Step 2: Configure Prettier
Prettier is an opinionated code formatter that ensures a consistent style across all your files.
Create a .prettierrc.json
file in your theme’s root directory with the following configuration.
{
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindFunctions": ["tw"]
}
-
"singleQuote"
and "semi"
are common formatting preferences. You can adjust these to your team’s style.
-
"plugins": ["prettier-plugin-tailwindcss"]
: This loads the Tailwind plugin, which is essential for sorting classes correctly.
-
"tailwindFunctions": ["tw"]
: For Tailwind 4.0’s new tw()
function, this setting ensures Prettier recognizes and formats its contents.
Next, create a .prettierignore
file to specify files and directories that Prettier should not format.
build
dist
vendor
node_modules
Step 3: Configure ESLint
ESLint is a powerful linter for JavaScript that helps catch syntax errors and enforces best practices.
Create an .eslintrc.cjs
file in your theme’s root. This uses the CommonJS format, which is standard for most Node.js build tools.
JavaScript
module.exports = {
root: true,
env: {
browser: true,
node: true,
es2021: true
},
extends: [
'eslint:recommended',
'eslint-config-prettier' // This must be the last extension to disable conflicting rules
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
// You can add custom ESLint rules here
'prettier/prettier': 'error'
}
};
-
extends
: This pulls in the standard ESLint recommended rules and the eslint-config-prettier
to prevent formatting conflicts.
-
parserOptions
: These options are configured for modern JavaScript, which is what Vite.js uses.
-
'prettier/prettier': 'error'
: This rule ensures that any code that doesn’t conform to your Prettier configuration will be flagged as an ESLint error.
Now, create an .eslintignore
file.
build
dist
vendor
node_modules
Step 4: Configure Stylelint
Stylelint is a linter for CSS, ensuring your stylesheets are consistent and error-free.
Create a .stylelintrc.json
file. Since you are primarily using Tailwind utility classes, Stylelint will focus on any custom CSS you write.
JSON
{
"extends": [
"stylelint-config-standard",
"stylelint-prettier/recommended"
],
"rules": {
"prettier/prettier": true
}
}
Create a .stylelintignore
file.
build
dist
vendor
node_modules
Step 5: Configure HTMLHint
HTMLHint is a static analysis tool that checks your HTML for syntax errors, accessibility issues, and best practices. It’s perfect for linting your Blade files.
Create an .htmlhintrc
file in your theme’s root.
JSON
{
"tag-pair": true,
"tagname-lowercase": true,
"attr-lowercase": true,
"attr-sorted": true,
"doctype-first": true,
"id-unique": true,
"spec-char-escape": true,
"img-alt-require": true,
"inline-style-disabled": true
}
This configuration is a good starting point for a Blade-based project. It ensures proper tag structure, attributes, and basic accessibility checks.
Step 6: Add Scripts to package.json
Add a few scripts to your package.json
file to make running these tools easy. This will allow you to format and lint your entire project with simple commands.
Open package.json
and add the following to the "scripts"
section:
"scripts": {
"dev": "vite",
"build": "vite build",
"lint:js": "eslint 'resources/scripts/**/*.js'",
"lint:css": "stylelint 'resources/styles/**/*.css'",
"lint:html": "htmlhint 'resources/views/**/*.blade.php'",
"format": "prettier --write 'resources/**/*.{js,css,json,blade.php}'",
"lint": "npm run lint:js && npm run lint:css && npm run lint:html"
}
Step 6: Add specific extensions for your IDE.
VS Code, with the right extensions, is an excellent environment for a seamless linting and formatting workflow. Instead of relying on Vite plugins for real-time feedback, your IDE will provide it directly in the editor as you type.
Here are the essential VS Code extensions you need to install and configure for your Sage 11 setup:
1. Prettier - Code formatter
This is the most critical extension. It integrates Prettier directly into your editor, allowing you to format your code on save or on command.
Why you need it: This extension reads your .prettierrc.json
file and automatically applies the defined formatting rules to your code. It’s the engine that makes all the formatting happen.
Recommended VS Code Settings: To make Prettier work automatically when you save a file, you need to configure a few settings in VS Code.
-
Open the Command Palette (Ctrl+Shift+P
on Windows/Linux or Cmd+Shift+P
on macOS).
-
Search for and select Preferences: Open User Settings (JSON)
.
-
Add the following lines:
JSON
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
-
"editor.defaultFormatter"
: This tells VS Code to use the Prettier extension as the default formatter for all supported file types.
-
"editor.formatOnSave"
: This is a powerful setting that automatically formats the file every time you save it, making code formatting effortless.
2. ESLint
This extension provides real-time linting for your JavaScript files. It highlights issues directly in the editor and can even offer auto-fixes.
Why you need it: This extension reads your .eslintrc.cjs
file and displays any JavaScript errors or warnings as squiggly underlines in your code. It works in conjunction with the Prettier extension to ensure that formatting issues are also flagged as ESLint errors, as per the eslint-plugin-prettier
setup.
Key features:
-
Shows linting errors and warnings.
-
Provides code actions to fix problems (source.fixAll.eslint
).
-
Integrates with the Problems panel in VS Code.
3. Stylelint
For your CSS files, this extension is a must-have. It highlights Stylelint errors and warnings.
Why you need it: Just like the ESLint extension, this one reads your .stylelintrc.json
file and provides live feedback on your CSS code. This is particularly useful for any custom CSS you write outside of Tailwind’s utility classes.
Recommended VS Code Settings: To ensure the Stylelint extension works on all your CSS-like files, you might need to add a small configuration, especially if you’re using CSS-in-JS or other specific file types. For a standard Sage 11 setup, the default configuration should be sufficient.
4. HTMLHint
While this one is a bit more niche, it’s the perfect extension for linting your Laravel Blade templates.
-
Extension Name: HTMLHint
-
Publisher: ctf0
-
ID: ctf0.htmlhint
Why you need it: It reads your .htmlhintrc
file and checks your Blade files for HTML syntax errors and best practices. While Blade itself is a PHP templating engine, the output is pure HTML, and this extension helps you keep that HTML clean and valid.
Summary of What to Install
-
esbenp.prettier-vscode
-
dbaeumer.vscode-eslint
-
stylelint.vscode-stylelint
-
ctf0.htmlhint
By installing these four extensions and configuring editor.formatOnSave
for Prettier, you will have a comprehensive and powerful linting setup that provides instant feedback right within your editor, making for a smooth and efficient development experience.