用于单个可执行应用
🌐 Use with Single Executable Applications
当以 单一可执行应用 运行,并使用 "useVfs": true 在 SEA 配置下构建时,打包的资源会自动以只读虚拟文件系统的形式挂载,注入的主脚本会从挂载点的根目录执行。无需额外设置。由于挂载点是保留的且在运行时选择,打包的代码通过相对于 __dirname 的路径和相对的 require() 调用来访问资源,而不是通过固定路径:
🌐 When running as a Single Executable Application built with
"useVfs": true in the SEA configuration, the bundled assets are
automatically mounted as a read-only virtual file system and the injected
main script is executed from the root of the mount. No additional setup is
required. Since the mount point is reserved and chosen at runtime, bundled
code accesses the assets through __dirname-relative paths and relative
require() calls rather than through a fixed path:
// In the SEA main script, __dirname is the root of the mounted assets.
const fs = require('node:fs');
const path = require('node:path');
const config = JSON.parse(
fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
const template = fs.readFileSync(
path.join(__dirname, 'templates/index.html'), 'utf8'); 支持 ESM 入口点("mainFormat": "module"):主模块通过 ESM 加载器从挂载点内部加载,import.meta.dirname 指向挂载根目录。
🌐 ESM entry points ("mainFormat": "module") are supported: the main module
is loaded from inside the mount through the ESM loader, and
import.meta.dirname points at the mount root.
"useVfs" 不能和 "useSnapshot" 或 "useCodeCache" 一起使用。
如果检测到任何一种组合,SEA 配置解析器会报错。
与其列出单独的 "assets",SEA 配置可以将 "vfsArchive" 指向一个预先构建的 ZIP 压缩包;然后挂载由嵌入式压缩包上的 ZipProvider 支持,并且每个文件在被读取时会被解压。详情见 从 ZIP 压缩包提供资源。
🌐 Instead of listing individual "assets", the SEA configuration can point
"vfsArchive" at a prebuilt ZIP archive; the mount is then backed by a
ZipProvider over the embedded archive, and each file is inflated when
it is read. See Serving the assets from a ZIP archive for details.
查看 单一可执行应用 文档,了解更多关于使用资源创建 SEA 构建的信息。
🌐 See the Single Executable Application documentation for more information on creating SEA builds with assets.