
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
vite-plugin-htmlx
Advanced tools
English | 中文
It seems that vite-plugin-html is no longer being updated. I encountered some problems while using it, but the issues and pull requests haven't been addressed for a long time. Therefore, based on the source code and its functionality, I rewrote this plugin.
entry
template
node version: >=14.18.0
vite version: >=4.0.0
npm i vite-plugin-htmlx -D
index.html
, for example:<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%- title %></title>
<%- injectScript %>
</head>
vite.config.ts
. This way, you can import the required functionality on demand.import { defineConfig } from 'vite'
import html from 'vite-plugin-htmlx'
export default defineConfig({
plugins: [
html({
minify: true,
page: {
/**
* Once you write the entry here, you will not need to add a script tag inside `index.html`,
* and the original tag needs to be deleted.
*/
entry: 'src/main.ts',
/**
* If you want to store the `index.html` file in a specified folder,
* you can modify it, otherwise no configuration is needed
* @default index.html
*/
template: 'public/index.html',
/**
* Data to inject into the EJS template
*/
inject: {
data: {
title: 'index',
injectScript: `<script src="./inject.js"></script>`,
},
tags: [
{
injectTo: 'body-prepend',
tag: 'div',
attrs: {
id: 'tag',
},
},
],
},
},
}),
],
})
import { defineConfig } from 'vite'
import html from 'vite-plugin-htmlx'
export default defineConfig({
plugins: [
html({
minify: true,
page: [
{
entry: 'src/main.ts',
filename: 'index.html',
template: 'public/index.html',
inject: {
data: {
title: 'index',
injectScript: `<script src="./inject.js"></script>`,
},
tags: [
{
injectTo: 'body-prepend',
tag: 'div',
attrs: {
id: 'tag1',
},
},
],
},
},
{
entry: 'src/other-main.ts',
filename: 'other.html',
template: 'public/other.html',
inject: {
data: {
title: 'other page',
injectScript: `<script src="./inject.js"></script>`,
},
tags: [
{
injectTo: 'body-prepend',
tag: 'div',
attrs: {
id: 'tag2',
},
},
],
},
},
],
}),
],
})
import { defineConfig } from 'vite'
import html from 'vite-plugin-htmlx'
export default defineConfig({
plugins: [
createHtmlPlugin({
minify: true,
page: [
{
entry: 'src/main.ts',
filename: 'index.html',
template: 'public/index.html',
},
{
entry: 'src/other.ts', // Different entry file
filename: 'other.html', // Different filename
template: 'public/index.html', // Same template
},
],
}),
],
})
In development mode, you can access MPA pages through ${origin}/filename
, such as:
http://127.0.0.1:5173/index.html
http://127.0.0.1:5173/other.html
Pages with a filename
of index
or index.html
are the default page, and navigating to a URL that does not match any page will navigate to this page.
html(options: UserOptions)
Parameter | Type | Default Value | Description |
---|---|---|---|
minify | boolean | MinifyOptions | true | Whether to compress html |
page | SpaPage | MpaPage[] | - | Page configuration |
Parameter | Type | Default Value | Description |
---|---|---|---|
filename | string | - | HTML file name |
template | string | - | Template path |
entry | string | - | Entry file |
inject | InjectOptions | - | Data injected into HTML |
Parameter | Type | Default Value | Description |
---|---|---|---|
template | string | index.html | Template path |
entry | string | - | Entry file |
inject | InjectOptions | - | Data injected into HTML |
Parameter | Type | Default Value | Description |
---|---|---|---|
data | Record<string, any> | - | Data injected into HTML |
ejsOptions | EJSOptions | - | Ejs configuration options EJSOptions |
tags | HtmlTagDescriptor | - | List of tags to be injected |
data
can be accessed in the html with ejs template syntax.
By default, the content of the .env
file is injected into index.html.
Default minification configuration.
{
collapseWhitespace: true,
keepClosingSlash: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
}
MIT
FAQs
A vite plugin for processing html.
The npm package vite-plugin-htmlx receives a total of 503 weekly downloads. As such, vite-plugin-htmlx popularity was classified as not popular.
We found that vite-plugin-htmlx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.