HTML-Validate Vue.js Plugin
html-validate-vue is an official plugin for HTML-Validate, designed to extend its robust, offline HTML5 validation capabilities to Vue.js projects. Currently at stable version 8.2.0, the package generally aligns its major releases with html-validate, which typically maintains major versions for about two years, encouraging users to stay updated for critical bug fixes and new features. The plugin seamlessly integrates by transforming Vue Single File Components (SFCs) and raw template strings, ensuring that Vue-specific syntax is correctly parsed and validated. A key differentiator is its ability to augment HTML-Validate's element metadata, enabling component-aware linting for standard Vue components like `<component>`, `<slot>`, `<transition>`, and `<router-link>`, as well as custom user-defined components. It also introduces specialized Vue-centric rules for validating slot usage (e.g., `vue/available-slots`, `vue/required-slots`), promoting best practices and identifying deprecated patterns within Vue templates. This allows for strict and non-forgiving local validation, providing consistent feedback without relying on online services or runtime checks, which is particularly beneficial for maintaining high code quality in component-driven architectures.
Common errors
-
Error: Cannot find module 'html-validate' from '.../node_modules/html-validate-vue'
cause The peer dependency `html-validate` is not installed or cannot be resolved by the system.fixRun `npm install html-validate` or `yarn add html-validate` in your project. -
Error: No transform found for "src/MyComponent.vue" (tried: ...)
cause The `transform` configuration in `.htmlvalidate.json` does not match the file path, preventing the Vue transformer from being applied.fixVerify the regular expression in your `transform` configuration (e.g., `"^.*\\.vue$": "html-validate-vue"`) or explicitly use a named transform like `"html-validate-vue:sfc"`. -
Element <my-component> is missing required slot 'body'
cause The `vue/required-slots` rule detected that a required slot (`body` in this case) was not provided when using a component.fixAdd the missing required slot to your component usage, e.g., `<my-component><template #body>...</template></my-component>`. -
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '.../my-local-plugin' is not supported resolving ES modules
cause Occurs when `html-validate` v9+ is used in a CommonJS project, attempting to import a directory as a plugin instead of an explicit file.fixExplicitly reference the entry file for local plugins, e.g., change `"plugins": ["./my-local-plugin"]` to `"plugins": ["./my-local-plugin/index.js"]` in your `.htmlvalidate.json`.
Warnings
- breaking html-validate-vue v8.x, aligning with its peer dependency html-validate v8.x, requires Node.js v20 or later. Running on older Node.js versions will result in installation or runtime failures.
- breaking When upgrading the peer dependency `html-validate` to v9 or later, projects using CommonJS (CJS) might encounter `ERR_UNSUPPORTED_DIR_IMPORT` errors. `html-validate` v9 defaults to ESM, requiring explicit file extensions for local plugin paths in configuration files.
- gotcha Incorrect configuration of the `transform` property in `.htmlvalidate.json` can lead to Vue templates not being validated or being parsed incorrectly. The plugin offers specific named transformers (`html-validate-vue:auto`, `:js`, `:html`, `:sfc`).
- gotcha html-validate-vue has a peer dependency on `html-validate`. If `html-validate` is not installed or its version is outside the supported range (`^8.21.0 || ^9.0.0 || ^10.0.0`), the plugin will fail to load or operate correctly.
- gotcha Defining custom element metadata for specific component slots requires a precise `component#slot` syntax (e.g., `my-component#heading`). Deviating from this delimiter will prevent slot-specific rules from being applied.
Install
-
npm install html-validate-vue -
yarn add html-validate-vue -
pnpm add html-validate-vue
Imports
- html-validate-vue
{ "plugins": ["html-validate-vue"], "transform": { "^.*\\.vue$": "html-validate-vue" } } - html-validate-vue:sfc
{ "transform": { "^.*\\.vue$": "html-validate-vue:sfc" } } - html-validate-vue:recommended
{ "extends": ["html-validate:recommended", "html-validate-vue:recommended"] }
Quickstart
npm install --save-dev html-validate html-validate-vue
// .htmlvalidate.json
// Place this file in your project root.
{
"plugins": ["html-validate-vue"],
"extends": [
"html-validate:recommended",
"html-validate-vue:recommended"
],
"elements": ["html5"],
"transform": {
"^.*\\.vue$": "html-validate-vue"
}
}
// Example Vue Single File Component (src/MyComponent.vue)
// This component would be validated by html-validate-vue
// (No actual JavaScript execution here, just demonstrating setup)
/*
<template>
<section>
<h1>Welcome</h1>
<my-button>
<template #default>
Click me
</template>
</my-button>
</section>
</template>
<script>
import MyButton from './MyButton.vue';
export default {
components: { MyButton },
};
</script>
<style scoped>
/* Your styles here */
</style>
*/