Skip to content

Commit

Permalink
Add wasm-opt and optimized distribution profiles (#6)
Browse files Browse the repository at this point in the history
* Add wasm-opt and optimized distribution profiles

* Update cargo make makefile
  • Loading branch information
rparrett authored Jan 5, 2025
1 parent 52b779e commit 81884fb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ jobs:
- name: Build
run: |
cargo build --release --target wasm32-unknown-unknown
cargo build --profile web-dist --target wasm32-unknown-unknown
- name: Prepare package
run: |
wasm-bindgen --no-typescript --out-name bevy_game --out-dir wasm --target web target/wasm32-unknown-unknown/release/${{ env.binary }}.wasm
cp -r assets wasm/
- name: Optimize Wasm
uses: NiklasEi/wasm-opt-action@v2
with:
file: target/wasm32-unknown-unknown/web-dist/*.wasm
optimize_all: true

- name: Package as a zip
working-directory: ./wasm
run: |
Expand Down Expand Up @@ -74,7 +80,7 @@ jobs:
- name: Build
run: |
cargo build --release --target x86_64-unknown-linux-gnu
cargo build --profile dist --target x86_64-unknown-linux-gnu
- name: Prepare package
run: |
Expand Down Expand Up @@ -117,7 +123,7 @@ jobs:

- name: Build
run: |
cargo build --release --target x86_64-pc-windows-msvc
cargo build --profile dist --target x86_64-pc-windows-msvc
- name: Prepare package
run: |
Expand Down Expand Up @@ -163,7 +169,7 @@ jobs:
- name: Build
run: |
cargo build --release --target x86_64-apple-darwin
cargo build --profile dist --target x86_64-apple-darwin
- name: Prepare Package
run: |
Expand Down
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ ron = "0.8.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "*", features = ["console", "Window", "Storage"] }

[profile.dist]
inherits = "release"
lto = "thin"

[profile.web-dist]
inherits = "dist"
opt-level = "s"

# Enable only a small amount of optimization in debug mode
[profile.dev]
opt-level = 1

# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]
opt-level = 3
33 changes: 26 additions & 7 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
[env]
ENV_DIR = {source="${CARGO_MAKE_PROFILE}", default_value = "debug", mapping = {release = "release"}}
CARGO_TARGET_DIR = {value = "target", condition = {env_not_set = ["CARGO_TARGET_DIR"]}}
ENV_DIR = { source = "${CARGO_MAKE_PROFILE}", default_value = "debug", mapping = { release = "release", "web-dist" = "web-dist" } }
CARGO_TARGET_DIR = { value = "target", condition = { env_not_set = [
"CARGO_TARGET_DIR",
] } }
CARGO_WASM_PATH = "${CARGO_TARGET_DIR}/wasm32-unknown-unknown/${ENV_DIR}/${CARGO_MAKE_CRATE_NAME}.wasm"
CARGO_MANIFEST_DIR = "${CARGO_MAKE_WORKING_DIRECTORY}"
PORT=4000
PORT = 4000

[env.release]
CARGO_RELEASE_ARGS = "--release"

[env.web-dist]
CARGO_RELEASE_ARGS = "--profile web-dist"

[tasks.show-env]
command = "env"

[tasks.build]
args = ["build", "--target", "wasm32-unknown-unknown", "@@split(CARGO_RELEASE_ARGS, )"]
args = [
"build",
"--target",
"wasm32-unknown-unknown",
"@@split(CARGO_RELEASE_ARGS, )",
]
command = "cargo"

[tasks.bindgen]
install_crate = {crate_name = "wasm-bindgen-cli", binary = "wasm-bindgen", test_arg="--help"}
args = ["--out-dir", "wasm", "--out-name", "bevy_game", "--target", "web", "--no-typescript", "${CARGO_WASM_PATH}"]
install_crate = { crate_name = "wasm-bindgen-cli", binary = "wasm-bindgen", test_arg = "--help" }
args = [
"--out-dir",
"wasm",
"--out-name",
"bevy_game",
"--target",
"web",
"--no-typescript",
"${CARGO_WASM_PATH}",
]
command = "wasm-bindgen"
dependencies = ["build"]

Expand All @@ -28,7 +47,7 @@ cp -r assets wasm/
dependencies = ["bindgen"]

[tasks.serve]
install_crate = {crate_name = "microserver", binary = "microserver", test_arg="--help"}
install_crate = { crate_name = "microserver", binary = "microserver", test_arg = "--help" }
command = "microserver"
args = ["--port", "${PORT}", "--no-spa", "wasm/"]
dependencies = ["static"]
Expand Down

0 comments on commit 81884fb

Please sign in to comment.