Skip to content

Commit

Permalink
feat: added size optimizations support into cli
Browse files Browse the repository at this point in the history
This can be customized with the `PERSEUS_WASM_RELEASE_RUSTFLAGS`
environment variable. In the basic example, this reduced 356.5kb to 281.1kb!
  • Loading branch information
arctic-hen7 committed Jul 2, 2022
1 parent bd8a702 commit b083173
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
]
resolver = "2"

[profile.release]
lto = true
opt-level = "s"
codegen-units = 1
# [profile.release]
# lto = true
# opt-level = "s"
# codegen-units = 1
16 changes: 14 additions & 2 deletions packages/perseus-cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ pub fn build_internal(
BUILDING
);

// Prepare the optimization flags for the Wasm build (only used in release mode)
let wasm_opt_flags = if is_release {
env::var("PERSEUS_WASM_RELEASE_RUSTFLAGS")
.unwrap_or_else(|_| "-C opt-level=z -C codegen-units=1".to_string())
} else {
String::new()
};

// We parallelize the first two spinners (static generation and Wasm building)
// We make sure to add them at the top (the server spinner may have already been instantiated)
let sg_spinner = spinners.insert(0, ProgressBar::new_spinner());
Expand All @@ -86,7 +94,7 @@ pub fn build_internal(
&sg_dir,
&sg_spinner,
&sg_msg,
"build"
vec![("PERSEUS_ENGINE_OPERATION", "build")]
)?);

Ok(0)
Expand All @@ -102,7 +110,11 @@ pub fn build_internal(
&wb_dir,
&wb_spinner,
&wb_msg,
"" // Not a builder command
if is_release {
vec![("RUSTFLAGS", &wasm_opt_flags)]
} else {
vec![]
}
)?);

Ok(0)
Expand Down
17 changes: 10 additions & 7 deletions packages/perseus-cli/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub static FAILURE: Emoji<'_, '_> = Emoji("❌", "failed!");
pub fn run_cmd(
cmd: String,
dir: &Path,
// This is only relevant for builder-related commands, but since that's most things, we may as well (it's only an env var)
op: &str,
envs: Vec<(&str, &str)>,
pre_dump: impl Fn(),
) -> Result<(String, String, i32), ExecutionError> {
// We run the command in a shell so that NPM/Yarn binaries can be recognized (see #5)
Expand All @@ -31,7 +30,7 @@ pub fn run_cmd(
// This will NOT pipe output/errors to the console
let output = Command::new(shell_exec)
.args([shell_param, &cmd])
.env("PERSEUS_ENGINE_OPERATION", op)
.envs(envs)
.current_dir(dir)
.output()
.map_err(|err| ExecutionError::CmdExecFailed { cmd, source: err })?;
Expand Down Expand Up @@ -82,13 +81,13 @@ pub fn run_stage(
target: &Path,
spinner: &ProgressBar,
message: &str,
op: &str,
envs: Vec<(&str, &str)>,
) -> Result<(String, String, i32), ExecutionError> {
let mut last_output = (String::new(), String::new());
// Run the commands
for cmd in cmds {
// We make sure all commands run in the target directory ('.perseus/' itself)
let (stdout, stderr, exit_code) = run_cmd(cmd.to_string(), target, op, || {
let (stdout, stderr, exit_code) = run_cmd(cmd.to_string(), target, envs.to_vec(), || {
// This stage has failed
fail_spinner(spinner, message);
})?;
Expand All @@ -107,7 +106,11 @@ pub fn run_stage(

/// Runs a command directly, piping its output and errors to the streams of this program. This allows the user to investigate the innards of
/// Perseus, or just see their own `dbg!` calls. This will return the exit code of the command, which should be passed through to this program.
pub fn run_cmd_directly(cmd: String, dir: &Path, op: &str) -> Result<i32, ExecutionError> {
pub fn run_cmd_directly(
cmd: String,
dir: &Path,
envs: Vec<(&str, &str)>,
) -> Result<i32, ExecutionError> {
// The shell configurations for Windows and Unix
#[cfg(unix)]
let shell_exec = "sh";
Expand All @@ -121,7 +124,7 @@ pub fn run_cmd_directly(cmd: String, dir: &Path, op: &str) -> Result<i32, Execut
let output = Command::new(shell_exec)
.args([shell_param, &cmd])
.current_dir(dir)
.env("PERSEUS_ENGINE_OPERATION", op)
.envs(envs)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub fn export_internal(
&ep_target,
&ep_spinner,
&ep_msg,
"export"
vec![("PERSEUS_ENGINE_OPERATION", "export")]
)?);

Ok(0)
Expand All @@ -175,7 +175,7 @@ pub fn export_internal(
&wb_target,
&wb_spinner,
&wb_msg,
"" // Not a builder command
vec![]
)?);

Ok(0)
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus-cli/src/export_error_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use std::path::PathBuf;
pub fn export_error_page(dir: PathBuf, opts: ExportErrorPageOpts) -> Result<i32, ExecutionError> {
run_cmd_directly(
format!(
"{} {} run {} {}",
"{} run {} -- {} {}",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
env::var("PERSEUS_CARGO_ARGS").unwrap_or_else(|_| String::new()),
// These are mandatory
opts.code,
opts.output,
),
&dir,
"export_error_page",
vec![("PERSEUS_ENGINE_OPERATION", "export_error_page")],
)
}
2 changes: 1 addition & 1 deletion packages/perseus-cli/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn build_server(
&sb_target,
&sb_spinner,
&sb_msg,
"" // The server will be built if we build for the server-side (builder and server are currently one for Cargo)
vec![]
)?);

let msgs: Vec<&str> = stdout.trim().split('\n').collect();
Expand Down
19 changes: 11 additions & 8 deletions packages/perseus-cli/src/snoop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ use std::path::PathBuf;
pub fn snoop_build(dir: PathBuf) -> Result<i32, ExecutionError> {
run_cmd_directly(
format!(
"{} run",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
"{} run {}",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
env::var("PERSEUS_CARGO_ARGS").unwrap_or_else(|_| String::new())
),
&dir,
"build",
vec![("PERSEUS_ENGINE_OPERATION", "build")],
)
}

/// Runs the commands to build the user's app to Wasm directly so they can see detailed logs.
pub fn snoop_wasm_build(dir: PathBuf, opts: SnoopWasmOpts) -> Result<i32, ExecutionError> {
run_cmd_directly(
format!(
"{} build --out-dir dist/pkg --out-name perseus_engine --target web {}",
"{} build --out-dir dist/pkg --out-name perseus_engine --target web {} {}",
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string()),
if opts.profiling {
"--profiling"
} else {
"--dev"
}
},
env::var("PERSEUS_WASM_PACK_ARGS").unwrap_or_else(|_| String::new())
),
&dir,
"", // Not a builder command
vec![],
)
}

Expand All @@ -42,10 +44,11 @@ pub fn snoop_server(dir: PathBuf, opts: SnoopServeOpts) -> Result<i32, Execution

run_cmd_directly(
format!(
"{} run",
"{} run {}",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
env::var("PERSEUS_CARGO_ARGS").unwrap_or_else(|_| String::new())
),
&dir,
"serve", // Unlike the `serve` command, we're both building and running here, so we provide the operation
vec![("PERSEUS_ENGINE_OPERATION", "serve")], // Unlike the `serve` command, we're both building and running here, so we provide the operation
)
}
2 changes: 1 addition & 1 deletion packages/perseus-cli/src/tinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn tinker_internal(
&tk_target,
&tk_spinner,
&tk_msg,
"tinker"
vec![("PERSEUS_ENGINE_OPERATION", "tinker")]
)?);

Ok(0)
Expand Down

0 comments on commit b083173

Please sign in to comment.