如何在没有 Surge 或 Vercel 的情况下从 Svelte 导出静态 HTML?

How to export static HTML from Svelte without Surge or Vercel?

我想将我的 Svelte 网络应用程序发布到 GitHub 个页面,并将我的应用程序基于模板 https://github.com/sveltejs/template。当我 运行 npm run build 时, public/build/bundle.js 被创建但没有 index.html。我找到的所有教程都在谈论如何部署 Sapper 项目,或者使用 Vercel 和 Surge 等外部工具,但是是否可以在没有任何外部工具的情况下构建 Svelte?我想要的只是一个静态 HTML 页面,我可以将其复制到 GitHub 页。

编辑:请参阅一般方法的已接受答案,但是对于非根目录部署,您仍然需要使路径相对。我在 https://github.com/sveltejs/template/pull/239.

创建了一个拉取请求

在 svelte 中,index.html 是一个静态文件,它将导入您的 bundle.js 和 运行。

index.html 位于 /public/index.html 而您的 bundle.js 位于 /public/build/bundle.js

在 svelte 模板中,index.html 使用脚本标记导入 /build/bundle.js 以初始化应用程序。

部署时,您只需上传整个 /public 文件夹,一切都应该可以运行。