vue自定义入口html文件
# vue2
在vue.config.js中添加如下配置
module.exports = {
pages: {
index: {
// 修改入口文件
entry: 'src/main.js', // 入口文件路径
template: 'public/custom.html', // 模板文件路径
filename: 'index.html', // 输出文件名
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# vue3
在vite.config.js中添加rollupOptions配置
// 自定义插件
const renameHtmlPlugin = () => {
return {
name: 'index.html',
enforce: 'post',
generateBundle(_, bundle) {
if (bundle['custom.html']) {
bundle['custom.html'].fileName = 'index.html';
}
},
};
};
export default defineConfig(({ mode }) => {
// 加载环境变量
const env = loadEnv(mode, process.cwd())
const isCustom = env.VITE_APP_CUSTOM
return {
build: {
outDir: 'dist', // 打包输出目录
// 配置入口html
rollupOptions: {
input: isCustom ? '/custom.html' : '/index.html',
},
},
plugins: [
renameHtmlPlugin()
]
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
修改入口html后打包产物也是按custom命名的,这里我们需要把入口再重命名为index.html,如上,我们在plugins中添加自定义插件即可。
上次更新: 2025/09/05, 8:09:00