File tree 9 files changed +281
-28
lines changed
9 files changed +281
-28
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,11 @@ default-run = "bevy"
14
14
name = " bevy"
15
15
path = " src/bin/main.rs"
16
16
17
+ [features ]
18
+ # To optimize the Wasm binaries
19
+ # Increases compile times (of the CLI) quite a bit
20
+ wasm-opt = [" dep:wasm-opt" ]
21
+
17
22
[dependencies ]
18
23
# CLI argument parsing
19
24
clap = { version = " 4.5.16" , features = [" derive" ] }
@@ -42,3 +47,6 @@ actix-web = "4.9.0"
42
47
43
48
# Opening the app in the browser
44
49
webbrowser = " 1.0.2"
50
+
51
+ # Optimizing Wasm binaries
52
+ wasm-opt = { version = " 0.116.1" , optional = true }
Original file line number Diff line number Diff line change @@ -19,6 +19,12 @@ impl BuildArgs {
19
19
matches ! ( self . subcommand, Some ( BuildSubcommands :: Web ) )
20
20
}
21
21
22
+ /// Whether to build with optimizations.
23
+ #[ cfg( feature = "wasm-opt" ) ]
24
+ pub ( crate ) fn is_release ( & self ) -> bool {
25
+ self . cargo_args . compilation_args . is_release
26
+ }
27
+
22
28
/// The profile used to compile the app.
23
29
pub ( crate ) fn profile ( & self ) -> & str {
24
30
self . cargo_args . compilation_args . profile ( )
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ pub fn build(args: &BuildArgs) -> anyhow::Result<()> {
28
28
args. profile ( ) ,
29
29
) ?;
30
30
wasm_bindgen:: bundle ( & bin_target) ?;
31
+
32
+ #[ cfg( feature = "wasm-opt" ) ]
33
+ if args. is_release ( ) {
34
+ crate :: web:: wasm_opt:: optimize_bin ( & bin_target) ?;
35
+ }
31
36
} else {
32
37
cargo:: build:: command ( ) . args ( cargo_args) . ensure_status ( ) ?;
33
38
}
Original file line number Diff line number Diff line change @@ -5,3 +5,4 @@ pub mod external_cli;
5
5
pub mod lint;
6
6
pub mod run;
7
7
pub mod template;
8
+ pub ( crate ) mod web;
Original file line number Diff line number Diff line change @@ -19,6 +19,12 @@ impl RunArgs {
19
19
matches ! ( self . subcommand, Some ( RunSubcommands :: Web ( _) ) )
20
20
}
21
21
22
+ /// Whether to build with optimizations.
23
+ #[ cfg( feature = "wasm-opt" ) ]
24
+ pub ( crate ) fn is_release ( & self ) -> bool {
25
+ self . cargo_args . compilation_args . is_release
26
+ }
27
+
22
28
/// The profile used to compile the app.
23
29
pub ( crate ) fn profile ( & self ) -> & str {
24
30
self . cargo_args . compilation_args . profile ( )
Original file line number Diff line number Diff line change @@ -38,6 +38,11 @@ pub fn run(args: &RunArgs) -> anyhow::Result<()> {
38
38
) ?;
39
39
wasm_bindgen:: bundle ( & bin_target) ?;
40
40
41
+ #[ cfg( feature = "wasm-opt" ) ]
42
+ if args. is_release ( ) {
43
+ crate :: web:: wasm_opt:: optimize_bin ( & bin_target) ?;
44
+ }
45
+
41
46
let port = web_args. port ;
42
47
let url = format ! ( "http://localhost:{port}" ) ;
43
48
Original file line number Diff line number Diff line change
1
+ #[ cfg( feature = "wasm-opt" ) ]
2
+ pub ( crate ) mod wasm_opt;
Original file line number Diff line number Diff line change
1
+ use std:: { fs, path:: Path , time:: Instant } ;
2
+
3
+ use anyhow:: Context as _;
4
+
5
+ use crate :: run:: BinTarget ;
6
+
7
+ /// Optimize the binary with wasm-opt.
8
+ pub ( crate ) fn optimize_bin ( bin_target : & BinTarget ) -> anyhow:: Result < ( ) > {
9
+ let wasm_path = bin_target
10
+ . artifact_directory
11
+ . clone ( )
12
+ . join ( format ! ( "{}_bg.wasm" , bin_target. bin_name) ) ;
13
+
14
+ optimize_path ( & wasm_path)
15
+ }
16
+
17
+ /// Optimize the Wasm binary at the given path with wasm-opt.
18
+ fn optimize_path ( path : & Path ) -> anyhow:: Result < ( ) > {
19
+ println ! ( "Optimizing with wasm-opt..." ) ;
20
+
21
+ let start = Instant :: now ( ) ;
22
+ let size_before = fs:: metadata ( path) ?. len ( ) ;
23
+
24
+ wasm_opt:: OptimizationOptions :: new_optimize_for_size ( )
25
+ . run ( path, path)
26
+ . context ( "failed to optimize with wasm-opt" ) ?;
27
+
28
+ let size_after = fs:: metadata ( path) ?. len ( ) ;
29
+ let size_reduction = 1. - ( size_after as f32 ) / ( size_before as f32 ) ;
30
+ let duration = start. elapsed ( ) ;
31
+
32
+ println ! (
33
+ " Finished in {duration:.2?}. Size reduced by {:.0}%." ,
34
+ size_reduction * 100.
35
+ ) ;
36
+
37
+ Ok ( ( ) )
38
+ }
You can’t perform that action at this time.
0 commit comments