在vite中,有没有办法从index.html更新根html名称
我正在尝试将现有项目更新为 vite,但我在文档中读到 Vite 需要一个 index.html 文件来工作。 是否有指定 vite 应该构建的另一个文件名? 就我而言 main.html
I'm trying to update an existing project to vite but i read in the docs Vite expects an index.html file to work from.
Is there anyway to specify another file name from which vite should build?
in my case main.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
入口点在
build.rollupOptions.input
中配置:您可以将其更改为
main.html
,如下所示。提供应用程序时,您必须手动导航到/main.html
,但您可以配置server.open
自动打开该文件:演示
The entry point is configured in
build.rollupOptions.input
:You can change that to
main.html
, as shown below. When serving the app, you'll have to manually navigate to/main.html
, but you could configureserver.open
to open that file automatically:demo
如果您不仅尝试更改根 HTML 页面的名称,还尝试更改其路径,则更改
build
或server
选项会成功没有帮助。例如,如果您想加载/src/main.html
而不是/index.html
,您可以通过以下地址访问它:http://localhost:3000/src/main.html
,但不只是localhost:3000
。要从不同的路径提供文件,您需要设置
root
在配置文件中。请注意,您还需要定义与此新根相关的其他路径,例如dist
。否则,打包后的文件将输出到/src/dist
。从
/src
加载 HTML 文件的更完整的配置文件如下所示:If you're trying to change not just the name of the root HTML page but also the path to it, changing
build
orserver
options won't help. For example, if you want to load<project root>/src/main.html
instead of<project root>/index.html
, you can access it athttp://localhost:3000/src/main.html
, but not at simplylocalhost:3000
.To serve files from a different path, you'll need to set
root
in the config file. Note that you'll also need to define other paths relative to this new root, likedist
. Otherwise, the packaged files will be output to/src/dist
.A more complete config file that loads the HTML file from
/src
looks like this:使用Vite的transformIndexHtml()来交换HTML内容
可能有更有效的方法,但这种方法可能在特殊用例中有一席之地。
TLDR: 在构建时,我们将index.html 的内容与另一个html 文件的内容交换。
对于我的用例,项目暂时需要静态构建输出的两种变体:1. Vue 应用程序,2. 纯 JS 应用程序。交换
index.html
内容是实现这一目标的最直接的方法,而且稍微复杂一点。为此,会发生文件转换(使用
fs
),并且在项目的一次性构建步骤期间没有问题。如果在另一种情况下,存在常规文件操作(不是一次性),则性能可能会成为问题。Vite 文档:transformIndexHTML()
1.拥有多个html源文件
至少需要一个
index.html
;制作你自己的变体。2.在 vite.config 中创建一个 HTML 替换插件
这里我们直接在 vite 配置中创建一个内联插件。这将使用您选择的任何源文件覆盖index.html。它发生在其他构建步骤之前,基本上相当于您在运行
build
之前手动重命名文件。vite.config.js
Use Vite's transformIndexHtml() to swap HTML content
There may be more efficient ways, but this method may have a place in special use cases.
TLDR: At build-time, we swap the content of index.html with that of another html file.
For my use-case, a project temporarily needed two variants of static build outputs: 1. a Vue app, 2. a plain JS app. Swapping
index.html
content was the most straightforward way to achieve that with a little less complication.For this a file transformation occurs (using
fs
), and is fine during the one-time build step of a project. If, in another context, there would be regular file operations (not one-time), performance might be an issue.Vite docs: transformIndexHTML()
1. Have multiple html source files
At least one
index.html
is required; make your own variations of it.2. Make an HTML-replacement plugin in your vite.config
Here we make a plugin inline directly in the vite config. This will overwrite index.html with whatever source file you choose. It happens before other build steps, and is basically the equivalent of you manually renaming files before running
build
.vite.config.js
对于任何试图提供位于源文件深处的
index.html
文件,但希望开发服务器实际在/
上提供该文件的人,您可以使用像这样的 vite 插件,在你的vite.config
中:For anyone trying to serve an
index.html
file that's somewhere deep in your source files, but wants the dev server to actually serve this on/
, you can do it with a vite plugin like this, in yourvite.config
:使用Vite开发应用时,
index.html
是入口点。您可以通过将build.rollupOptions.input
设置为./main.html
来调整它。在库模式中,入口点是由
build.lib.entry
而不是index.html
指定。在这种情况下,index.html
演示页面仅与开发服务器相关,它将自动转换任何以.html
结尾的文件,因此您只需打开http ://localhost:3000/main.html,无需调整配置。在库模式下设置build.rollupOptions.input
将覆盖build.lib.entry
指定的入口点,并将演示页面代码捆绑为库的一部分,同时破坏全球 UMD 出口。When developing an app with Vite,
index.html
is the entry point. You can adjust it by settingbuild.rollupOptions.input
to./main.html
.In Library Mode, the entry point is the one specified by
build.lib.entry
rather thanindex.html
. In this case, theindex.html
demo page is only relevant for the dev server, which will automatically transform any files ending in.html
, so you simply need to open http://localhost:3000/main.html, without adjusting the config. Settingbuild.rollupOptions.input
in library mode will override the entry point specified bybuild.lib.entry
and bundle the demo page code as part of the library, along with breaking the global UMD export.刚刚在 Vite 上遇到了同样的问题并做出了反应。想要提供不同的 html 文件。构建没有问题。只服务。
唯一有效的解决方案是添加插件:
您可以通过 Vite env 配置设置模式。
https://vitejs.dev/guide/env-and-mode
Just had the same problem with Vite and react. Wanted to serve different html files. No problem with the build. Only serve.
The only solution that worked was to add the plugin:
You can set the mode via the Vite env configuration.
https://vitejs.dev/guide/env-and-mode
这是 server.open 属性,这是来自他们的网站,ChatGPT 再次失败:)
It's the server.open property, this is from their site, ChatGPT has failed once again:)