4 个版本 (2 个重大变更)
0.3.1 | 2023 年 12 月 11 日 |
---|---|
0.3.0 | 2023 年 11 月 17 日 |
0.2.0 | 2023 年 11 月 10 日 |
0.1.0 | 2023 年 10 月 24 日 |
#459 in 开发工具
230KB
5.5K SLoC
Glory 的构建工具
功能
- 在 watch 模式下并行构建服务器和客户端以快速提供开发者反馈。
- CSS 热重载(无需页面重载,仅更新 CSS)。
- 为水合(客户端渲染模式不支持)构建服务器和客户端。
- 支持工作空间和单包设置。
- 使用 dart-sass 进行 SCSS 编译。
- 使用 Lightning CSS 进行 CSS 转换和压缩。
- 使用 Cargo 构建(wasm)服务器和客户端二进制文件。
- 使用 wasm-bindgen 生成 JS - Wasm 绑定。
- 包括对 JS Snippets 的支持,当您需要从 WASM 调用一些 JS 代码时。
- 使用来自 Binaryen 的 wasm-opt 对 wasm 进行优化。
watch
命令,用于在浏览器实时重载的情况下自动重新构建。test
命令,用于运行组成 Glory 项目的库和 bin 包的测试。build
构建服务器和客户端。end2end
命令,用于构建、运行服务器并调用 bash shell 钩子。钩子通常会启动 Playwright 或类似工具。new
命令,用于根据模板创建新项目,使用 cargo-generate。当前模板包括https://github.com/glory-rs/start
:一个 Actix 入门项目https://github.com/glory-rs/start-salvo
:Salvo 启动器https://github.com/glory-rs/start-salvo-workspace
:在 workspace 中将客户端和服务器代码分别放在不同的 crate 中的 Salvo 启动器
- 'no_downloads' 功能允许用户管理可选依赖
入门
安装
cargoinstall --lockedglory-cli
如果你出于任何原因需要最新的超新鲜版本
cargoinstall --githttps://github.com/glory-rs/glory-cli --lockedglory-cli
帮助
cargoglory --help
设置你的项目时,请查看示例
依赖关系
当使用 sass、wasm-opt 和 cargo-generate 时,如果它们尚未安装并且被 which 找到,它们将自动安装在一个缓存目录中。依赖项的不同版本可能会积累在这个目录中,因此请随时删除它。
操作系统 | 示例 |
---|---|
Linux | /home/alice/.cache/glory-cli |
macOS | /Users/Alice/Library/Caches/glory-cli |
Windows | C:\Users\Alice\AppData\Local\glory-cli |
如果你希望强制安装依赖项,或者正在使用 Nix 或 NixOs,你可以通过启用 no_downloads
功能来安装它,以防止 glory-cli 尝试下载和安装它们。
cargoinstall --功能no_downloads --lockedglory-cli
单包设置
单包设置是将前端和服务器代码定义在单个包中的设置。
配置参数定义在包的 Cargo.toml
文件 [package.metadata.glory]
部分。有关可以使用参数的完整列表,请参阅参数参考。所有路径都是相对于包根目录的(即 Cargo.toml
文件)
工作空间设置
当使用 workspace 设置时,既支持单包项目也支持多包项目。后者是前端和服务器位于不同包中的情况。
所有定义了 Cargo.toml
中 [package.metadata.glory]
部分的 workspace 成员都将自动包含为 Glory 单包项目。多包项目在 workspace 的 Cargo.toml
文件的 [[workspace.metadata.glory]]
部分定义,它需要三个强制参数
[[workspace.metadata.glory]]
# project name
name = "glory-project"
bin-package = "server"
lib-package = "front"
# more configuration parameters...
注意双重大括号:可以定义多个项目,一个包可以用于多个项目。
构建功能
使用 glory-cli 构建时,前端库包使用目标 wasm-unknown-unknown
和功能 --no-default-features --features=web-ssr --features=web-csr
编译成 wasm。服务器二进制文件使用功能 --no-default-features --features=web-ssr
编译
参数参考
这些参数用于 workspace 部分 [[workspace.metadata.glory]]
或包,对于单包设置,部分 [package.metadata.glory]
。
请注意,货物清单中的单词目标有两种不同的含义。作为包的配置目标[[bin]]
和作为编译输出的目标三元组。在这里,后者被称为目标三元组。
编译参数
# Sets the name of the binary target used.
#
# Optional, only necessary if the bin_package defines more than one target
bin_target = "my_bin_name"
# The features to use when compiling all targets
#
# Optional. Can be extended with the command line parameter __features
features = []
# The features to use when compiling the bin target
#
# Optional. Can be over_ridden with the command line parameter __bin_features
bin_features = ["ssr"]
# If the __no_default_features flag should be used when compiling the bin target
#
# Optional. Defaults to false.
bin_default_features = false
# The profile to use for the bin target when compiling for release
#
# Optional. Defaults to "release".
bin_profile_release = "my_release_profile"
# The profile to use for the bin target when compiling for debug
#
# Optional. Defaults to "debug".
bin_profile_debug = "my_debug_profile"
# The target triple to use when compiling the bin target
#
# Optional. Env: GLORY_BIN_TARGET_TRIPLE
bin_target_triple = "x86_64_unknown_linux_gnu"
# The features to use when compiling the lib target
#
# Optional. Can be over_ridden with the command line parameter __lib_features
lib_features = ["hydrate"]
# If the __no_default_features flag should be used when compiling the lib target
#
# Optional. Defaults to false.
lib_default_features = false
# The profile to use for the lib target when compiling for release
#
# Optional. Defaults to "release".
lib_profile_release = "my_release_profile"
# The profile to use for the lib target when compiling for debug
#
# Optional. Defaults to "debug".
lib_profile_debug = "my_debug_profile"
站点参数
可以通过设置相应的环境变量来覆盖这些参数。它们也可以在.env
文件中设置,因为glory_cli会读取包或工作区目录及其任何父目录中的第一个。
# Sets the name of the output js, wasm and css files.
#
# Optional, defaults to the lib package name or, in a workspace, the project name. Env: GLORY_OUTPUT_NAME.
output_name = "myproj"
# The site root folder is where glory_cli generate all output.
# NOTE: It is relative to the workspace root when running in a workspace.
# WARNING: all content of this folder will be erased on a rebuild.
#
# Optional, defaults to "target/site". Env: GLORY_SITE_ROOT.
site_root = "target/site"
# The site_root relative folder where all compiled output (JS, WASM and CSS) is written.
#
# Optional, defaults to "pkg". Env: GLORY_SITE_PKG_DIR.
site_pkg_dir = "pkg"
# The source style file. If it ends with _.sass_ or _.scss_ then it will be compiled by `dart_sass`
# into CSS and processed by lightning css. When release is set, then it will also be minified.
#
# Optional. Env: GLORY_STYLE_FILE.
style_file = "styles/main.scss"
# The tailwind input file.
#
# Optional, Activates the tailwind build
tailwind_input_file = "styles/tailwind.css"
# The tailwind config file.
#
# Optional, defaults to "tailwind.config.js" which if is not present
# is generated for you
tailwind_config_file = "tailwind.config.js"
# The browserlist https://browsersl.ist query used for optimizing the CSS.
#
# Optional, defaults to "defaults". Env: GLORY_BROWSERQUERY.
browser_query = "defaults"
# Assets source dir. All files found here will be copied and synchronized to site_root.
# The assets_dir cannot have a sub directory with the same name/path as site_pkg_dir.
#
# Optional. Env: GLORY_ASSETS_DIR.
assets_dir = "assets"
# JS source dir. `wasm_bindgen` has the option to include JS snippets from JS files
# with `#[wasm_bindgen(module = "/js/foo.js")]`. A change in any JS file in this dir
# will trigger a rebuild.
#
# Optional. Defaults to "src"
js_dir = "src"
# The IP and port where the server serves the content. Use it in your server setup.
#
# Optional, defaults to 127.0.0.1:8000. Env: GLORY_SITE_ADDR.
site_addr = "127.0.0.1:8000"
# The port number used by the reload server (only used in watch mode).
#
# Optional, defaults 3001. Env: GLORY_RELOAD_PORT
reload_port = 3001
# The command used for running end_to_end tests. See the section about End_to_end testing.
#
# Optional. Env: GLORY_END2END_CMD.
end2end_cmd = "npx playwright test"
# The directory from which the end_to_end tests are run.
#
# Optional. Env: GLORY_END2END_DIR
end2end_dir = "integration"
环境变量
以下环境变量在编译库(前端)或bin(服务器)时以及运行服务器时设置。
来自Glory配置的输出
- GLORY_OUTPUT_NAME
- GLORY_SITE_ROOT
- GLORY_SITE_PKG_DIR
- GLORY_SITE_ADDR
- GLORY_RELOAD_PORT
构建时使用的目录
- GLORY_LIB_DIR: 相对于工作目录的库包路径
- GLORY_BIN_DIR: 相对于工作目录的二进制包路径
使用目录时的注意事项
glory-cli
在构建和运行之前将工作目录更改为项目根目录,或者在工作区中,则更改为工作区根目录。- 在单包配置中运行时,这两个值被设置为相同的值。
- 除非您能保证整个项目结构在运行时也可用,否则请避免在运行时使用它们。
端到端测试
glory-cli
提供了端到端测试支持以提高便利性。它是一个简单的shell命令end2end-cmd
的包装,该命令在特定的目录end2end-dir
中执行。
end2end-cmd
可以是任何shell命令。对于运行Playwright,它将是npx playwright test
。
它的功能相当于手动运行以下命令
- 在终端中运行
cargo glory watch
- 在另一个终端中,切换到
end2end-dir
并运行end2end-cmd
。
在测试设置时,请首先尝试上述操作。如果这样工作,但cargo glory end-to-end
不工作,那么请创建一个GitHub问题报告。
依赖关系
~68MB
~1.5M SLoC