Vue HTML Loader
The `vue-html-loader` (version 1.2.4) is a Webpack loader specifically designed to process HTML templates for Vue.js applications. It functions as a fork of the generic `html-loader`, adapted to recognize and handle specific Vue template requirements. Its core functionality involves exporting HTML as a string, enabling asset path resolution for elements like `<img>` tags, and supporting optional HTML minimization. The loader also provides configuration options for custom tag-attribute processing and handling 'root-relative' URLs within HTML. However, its last update was in early 2016, indicating it is no longer actively maintained. Due to the significant evolution of both Webpack (v4/v5) and Vue.js (v2/v3, and modern build tools like Vue CLI and Vite), this loader is largely obsolete and incompatible with contemporary Vue development workflows.
Common errors
-
Module build failed: Error: Cannot find module 'vue-html-loader'
cause The `vue-html-loader` package has not been installed or is not resolvable in your project.fixRun `npm install --save-dev vue-html-loader` or `yarn add -D vue-html-loader` to install the package. -
Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
cause Webpack is encountering an HTML file but does not have `vue-html-loader` or `html-loader` configured in its rules to process it.fixAdd a rule to your `webpack.config.js` to handle HTML files using `vue-html-loader`: `{ test: /\.html$/, use: 'vue-html-loader' }`. -
Assets referenced in HTML (e.g., <img> src) are not being resolved or result in broken paths.
cause `vue-html-loader` processes asset paths, but the necessary subsequent loaders (like `file-loader` or `url-loader`) or Webpack 5 Asset Modules for handling those assets are missing or misconfigured.fixEnsure your `webpack.config.js` includes rules for image files (e.g., `test: /\.(png|jpe?g|gif|svg)$/i`) with `file-loader`, `url-loader`, or Webpack 5's built-in Asset Modules (`type: 'asset/resource'`). Also, check the `attrs` option for `vue-html-loader` to ensure it's configured to process the relevant tag-attribute combinations.
Warnings
- breaking This package is explicitly abandoned and has not been updated since 2016. It is severely outdated and will not be compatible with modern Webpack versions (v4, v5, or v6) or Vue.js versions (Vue 2 with `vue-loader` v15+, Vue 3, Vue CLI, or Vite). Using it in new projects or upgrading existing ones is highly discouraged.
- gotcha Due to its age, `vue-html-loader` might lack features or have incompatible syntax compared to current Webpack loader options. Configuration methods (like query strings vs. `options` object) have evolved significantly in Webpack.
- breaking Webpack 5 introduced Asset Modules, which supersede `file-loader`, `url-loader`, and `raw-loader`. `vue-html-loader` was designed to work with these older loaders for asset handling, meaning direct migration to Webpack 5's asset modules may break its functionality.
- gotcha The loader relies on correct setup of other loaders (e.g., `file-loader` or `url-loader`) for asset processing (e.g., `<img src="image.png">`). If these are not configured or are incompatible, image paths will not be resolved correctly.
Install
-
npm install vue-html-loader -
yarn add vue-html-loader -
pnpm add vue-html-loader
Imports
- module.rules.use
import VueHtmlLoader from 'vue-html-loader';
module.exports = { module: { rules: [ { test: /\.html$/, use: 'vue-html-loader' } ] } } - vue.html (options)
module.exports = { htmlLoader: { /* options */ } }module.exports = { vue: { html: { /* loader options */ } } } - attrs (query parameter)
{ test: /\.html$/, use: 'vue-html-loader?attrs=img.src' }{ test: /\.html$/, use: 'vue-html-loader?attrs[]=img:src&attrs[]=img:data-src' }
Quickstart
const path = require('path');
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.html$/,
use: ['vue-html-loader?root=.', 'html-loader'], // Chaining with html-loader might be necessary for some features
exclude: /index.html/ // Typically exclude main index.html if using HtmlWebpackPlugin
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash].[ext]',
outputPath: 'assets/images',
},
},
],
},
// Note: For actual Vue SFCs, you would use vue-loader and vue-template-compiler
// This example focuses on standalone HTML templates processed by vue-html-loader
]
},
// Example of vue-html-loader specific options (older Webpack config style)
vue: {
html: {
minimize: true, // Example: Enable HTML minimization
// Other options like 'attrs' can be specified here as well for older configs
// e.g., attrs: ['img:src', 'img:data-src']
}
},
// Ensure Webpack 5 Asset Modules are handled correctly alongside older loaders
// For Webpack 5, it's generally recommended to use built-in Asset Modules instead of file-loader/url-loader
// However, for compatibility with vue-html-loader, older loaders might be required.
};