Trunk 支持分层配置系统。最底层,配置文件可以封装项目特定的默认值、路径、端口和其他配置。环境变量可以用来覆盖配置文件中的值。最后,CLI 参数/选项具有最终的优先级。
Trunk.toml
Trunk 支持可选的 Trunk.toml
配置文件。示例配置文件包含在 Trunk 仓库 中,并显示了所有可用的配置选项及其默认值。默认情况下,Trunk 将在当前工作目录中查找 Trunk.toml
配置文件。Trunk 支持全局 --config
选项来指定文件的替代位置。
请注意,在 Trunk.toml
文件中声明的任何相对路径都将被视为相对于 Trunk.toml
文件本身。
Trunk 版本
从 0.19.0-alpha.2
开始,可以强制要求使用特定版本的 trunk 构建项目。
随着新功能被添加到 trunk 中,这可能有助于确保当前构建所使用的 trunk 版本实际上能够做到这一点。这可以使用 Trunk.toml
文件根级别的 trunk-version
(或使用别名 trunk_version
)来完成。
版本格式是“版本要求”,与您可能从 Cargo 的依赖项版本字段中了解到的格式相同,即 语义化版本约束。
这也支持预发布版本要求,从而可以提前采用即将推出的功能。
注意: 早于 0.19.0-alpha.2
的版本目前不支持此检查,因此它们现在将静默忽略此类错误。
trunk-version = "^0.20.1"
Build 节
Build 节包含构建过程的配置设置。这些设置控制传递给 Cargo 以构建应用程序的参数,以及资源文件的生成。
[build]
target = "index.html" # The index HTML file to drive the bundling process.
html_output = "index.html" # The name of the output HTML file.
release = false # Build in release mode.
dist = "dist" # The output dir for all final assets.
public_url = "/" # The public URL from which assets are to be served.
filehash = true # Whether to include hash values in the output file names.
inject_scripts = true # Whether to inject scripts (and module preloads) into the finalized output.
offline = false # Run without network access
frozen = false # Require Cargo.lock and cache are up to date
locked = false # Require Cargo.lock is up to date
minify = "never" # Control minification: can be one of: never, on_release, always
no_sri = false # Allow disabling sub-resource integrity (SRI)
Watch 节
Trunk 内置支持监视源文件更改,这会触发重新构建和浏览器刷新。在本节中,您可以覆盖要监视的路径并设置要忽略的文件。
[watch]
watch = [] # Paths to watch. The `build.target`'s parent folder is watched by default.
ignore = [] # Paths to ignore.
Server 节
Trunk 有一个内置服务器,用于在运行 trunk serve
时提供应用程序服务。本节允许您覆盖其工作方式。
[serve]
addresses = ["127.0.0.1"] # The address to serve on.
port = 8080 # The port to serve on.
aliases = ["https://127.0.0.1.mywebsite.com"] # The aliases to serve on.
open = false # Open a browser tab once the initial build is complete.
no_spa = false # Whether to disable fallback to index.html for missing files.
no_autoreload = false # Disable auto-reload of the web app.
no_error_reporting = false # Disable error reporting
ws_protocol = "ws" # Protocol used for autoreload WebSockets connection.
# Additional headers set for responses.
headers = { "test-header" = "header value", "test-header2" = "header value 2" }
# The certificate/private key pair to use for TLS, which is enabled if both are set.
tls_key_path = "self_signed_certs/key.pem"
tls_cert_path = "self_signed_certs/cert.pem"
Clean 节
Clean 节控制运行 trunk clean
时的行为,这将删除构建产物。
[clean]
dist = "dist" # The output dir for all final assets.
cargo = false # Optionally perform a cargo clean.
Proxy 节
Trunk.toml
配置文件接受多个 [[proxy]]
节,这允许配置多个代理。每个节至少需要 backend
字段,并可选地接受 rewrite
和 ws
字段,这两个字段都对应于下面讨论的 --proxy-*
CLI 标志。
与其他 Trunk 配置一样,通过 CLI 声明的代理将具有最终优先级,并将导致忽略任何配置文件代理,即使配置文件中声明了多个代理也是如此。
[[proxy]]
backend = "https://127.0.0.1:9000/api/v1" # Address to proxy requests to
ws = false # Use WebSocket for this proxy
insecure = false # Disable certificate validation
no_system_proxy = false # Disable system proxy
rewrite = "" # Strip the given prefix off paths
no_redirect = false # Disable following redirects of proxy responses
Hooks 节
Hooks 是在构建之前、期间或之后运行的任务。您可以在此处运行任意命令,并且可以指定要运行的多个 hooks。
[[hooks]]
stage = "post_build" # When to run hook, must be one of "pre_build", "build", "post_build"
command = "ls" # Command to run
command_arguments = [] # Arguments to pass to command
环境变量
Trunk 环境变量镜像了 Trunk.toml
配置模式。所有 Trunk 环境变量都具有以下 3 部分形式 TRUNK_<SECTION>_<ITEM>
,其中 TRUNK_
是必需的前缀,<SECTION>
是 Trunk.toml
节之一,<ITEM>
是来自相应节的特定配置项。例如,TRUNK_SERVE_PORT=80
将导致 trunk serve
监听端口 80
。等效的 CLI 调用将是 trunk serve --port=80
。
此外,还有变量 TRUNK_SKIP_VERSION_CHECK
,它允许控制更新检查(如果已编译到 trunk 版本中)。
CLI 参数 & 选项
最终配置层是 CLI 本身。CLI 上提供的任何参数/选项都将优先于任何其他配置层。
代理
Trunk 附带一个内置代理,可以在运行 trunk serve
时启用。有两种配置代理的方法,每种方法将在下面讨论。所有 Trunk 代理都将透明地将请求体、标头和查询参数传递到代理后端。
代理 CLI 标志
trunk serve
命令接受两个与代理相关的标志。
--proxy-backend
指定后端服务器的 URL,请求应代理到该服务器。给定 URL 的 URI 段将用作 Trunk 服务器上处理代理请求的路径。例如,trunk serve --proxy-backend=https://127.0.0.1:9000/api/
将把路径 /api/
上收到的任何请求代理到监听在 https://127.0.0.1:9000/api/
的服务器。进一步的路径段或查询参数将无缝传递。
--proxy-rewrite
指定 Trunk 服务器要监听代理请求的备用 URI。在给定 URI 上收到的任何请求都将被重写以匹配代理后端的 URI,有效地剥离重写前缀。例如,trunk serve --proxy-backend=https://127.0.0.1:9000/ --proxy-rewrite=/api/
将把在 /api/
上收到的任何请求代理到 https://127.0.0.1:9000/
,并从请求中剥离 /api/
前缀,而 /api/
前缀之后的所有内容将保持不变。
--proxy-insecure
允许 --proxy-backend
url 为 https 使用自签名证书(或任何官方 无效 证书,包括过期的证书)。这将在代理到 https 时使用,例如 trunk serve --proxy-backend=https://127.0.0.1:3001/ --proxy-insecure
,其中 ssl 证书是自签名的,例如使用 mkcert,并通过 https 反向代理路由到后端,例如 local-ssl-proxy 或 caddy。
--proxy-no-sytem-proxy
绕过系统代理以连接代理后端。
--proxy-ws
指定代理用于 WebSocket 端点。