vite-plugin-auto-mpa-html
English | 中文文档
A file directory-based automated multi-page Vite plugin that supports HTML templates using Handlebars.
基于文件目录的Vite自动化多页面构建插件,支持使用 Handlebars 的 HTML 模板。
Quick Start
npm i vite-plugin-auto-mpa-html@latest -D
Then, add plugin to your vite.config.(js/ts)
, like this,
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import autoMpaHtmlPlugin from 'vite-plugin-auto-mpa-html'
export default defineConfig({
plugins: [react(), autoMpaHtmlPlugin({
entryName: "main.tsx",
sharedData: {},
enableDevDirectory: true,
renderEngineOption: {}
})],
})
Now, focus on your PAGES, for example, a project like this,
Using the official template of react-ts
, copied page's assets to 2 pages, the "index" and the "page2"
.
├── package.json
├── package-lock.json
├── public
│ └── vite.svg
├── src
│ ├── index
│ │ ├── App.css
│ │ ├── App.tsx
│ │ ├── assets
│ │ │ └── react.svg
│ │ ├── config.json
│ │ ├── index.css
│ │ ├── main.tsx
│ │ └── vite-env.d.ts
│ └── page2
│ ├── App.css
│ ├── App.tsx
│ ├── assets
│ │ └── react.svg
│ ├── config.json
│ ├── index.css
│ ├── main.tsx
│ └── vite-env.d.ts
├── templates
│ └── index.html
├── tsconfig.json
├── tsconfig.node.json
└── vite.config.ts
How to generate a beautiful tree like this? Run tree -I "node_modules|dist" > folders.txt
with tree package!
Manually create a config.json
in the subdirectory of the page, with these content,
{
"template": "../../templates/index.html",
"data": {
"title": "This is page 2",
"description": "page 22222222",
"keywords": "2"
}
}
We put all pages into different folders under src
, some additional config should be put into vite.config.js
export default defineConfig({
root: "src",
base: "",
build: {
outDir: "../dist",
emptyOutDir: true,
}
})
Finished, everything is ready, run npm run build
to see what is built with vite-plugin-auto-mpa-html
.
A temporary index.html
will be generated to every entry, just beside your entry file, such as main.jsx
or main.js
, PLEASE take care!
Plugin Options
{
enableDevDirectory?: boolean
sharedData?: object
renderEngineOption?: object
entryName: string
configName?: string
experimental?: {
customTemplateName?: string
rootEntryDistName?: string
}
}
Page Config Option
{
template: string
data?: object
}
If you need to dynamically control the page configuration used during compilation for each page based on some external variables, you can also modify the configName
to a ".js" file and export a default configuration in the file.
import { pageConfigGenerator } from 'vite-plugin-auto-mpa-html'
export default pageConfigGenerator({
"template": "../../template/index.html"
})
Conditional page configuration
We have an option called sharedData
cross pages, so you can inject the variables you need, then read them in page's config (or directly use in Handlebars templates), like this,
import { pageConfigGenerator } from 'vite-plugin-auto-mpa-html'
export default pageConfigGenerator((opt) => {
console.log(opt.sharedData);
return {
"template": "../template/index.html",
data: {
isProd: opt.sharedData.isProd
}
}
})
Limitation
-
Nested folder is SUPPORTED from v1, but will generate a temporary index.html
besides your entry file.
-
Env files is only supported in root folder (same level as vite.config.js
), env-per-entry is NOT SUPPORTED.
-
When experimental.customTemplateName
equals ".html" (which means you want to reduce folder levels), it's NOT allowed to put entry files directly under root folder.
-
Auto restart when page's config changed is not originally supported by this plugin, but you can use vite-plugin-restart which is maintained by @antfu, and add config like this,
ViteRestart({
restart: [
'config.[jt]s',
]
})
Vite native features
Build Setup
git clone https://github.com/iamspark1e/vite-plugin-auto-mpa-html.git
cd vite-plugin-auto-mpa-html
npm install
npm run build
Similar function plug-ins
I'm not familar with vite plugin development, so I've read some plugins' awesome code. Some ideas and my needs was added during development. There are some other plugins may solve your problems or meet your needs, I also suggest these,