Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SHINYLIVE_WASM_PACKAGES environment variable #116

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# shinylive (development version)

* In CI and other automated workflow settings the `SHINYLIVE_WASM_PACKAGES` environment variable can now be used to control whether WebAssembly R package binaries are bundled with the exported shinylive app, in addition to the `wasm_packages` argument of the `export()` function. (#116)

* shinylive now avoids bundling WebAssembly R package dependencies listed only in the `LinkingTo` section of required packages. With this change dependencies that are only required at build time are no longer included as part of the exported WebAssembly asset bundle. This reduces the total static asset size and improves the loading time of affected shinylive apps. (#115)

# shinylive 0.2.0
Expand Down
11 changes: 8 additions & 3 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#' `shinylive.quiet` option or defaults to `FALSE` in interactive sessions if
#' not set.
#' @param verbose Deprecated, please use `quiet` instead.
#' @param wasm_packages Download and include binary WebAssembly packages as
#' part of the output app's static assets. Defaults to `TRUE`.
#' @param wasm_packages Download and include binary WebAssembly packages as part
#' of the output app's static assets. Logical, defaults to `TRUE`. The default
#' value can be changed by setting the environment variable
#' `SHINYLIVE_WASM_PACKAGES` to `TRUE` or `1` to enable, `FALSE` or `0` to
Copy link
Contributor

@gadenbuie gadenbuie Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's annoying, but I think we should allow literally "true" or "false", or the whole range of TRUE, "TRUE", "true", "1" and 1 and false variants. (Basically, `tolower(val) %in% c("true", "1").)

I would read this expecting I could set SHINYLIVE_WASM_PACKAGES=TRUE in .Renviron, but IIRC that will end up being "TRUE" when set.

Copy link
Collaborator Author

@georgestagg georgestagg Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be handled OK by:

  pkgs_env <- switch(pkgs_env, "1" = TRUE, "0" = FALSE, pkgs_env)
  wasm_packages <- as.logical(pkgs_env)

Because "1" will be caught by the switch, and then "TRUE"/"true" will be caught by the as.logical().

> as.logical("TRUE")
[1] TRUE
> as.logical("true")
[1] TRUE
> as.logical(TRUE)
[1] TRUE
> as.logical("false")
[1] FALSE
> as.logical("something-weird")
[1] NA

Saying that, I might change it just to make things clearer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice, that makes sense. I just missed that as.logical() line

#' disable.
#' @param package_cache Cache downloaded binary WebAssembly packages. Defaults
#' to `TRUE`.
#' @param max_filesize Maximum file size for bundling of WebAssembly package
Expand Down Expand Up @@ -61,7 +64,7 @@ export <- function(
...,
subdir = "",
quiet = getOption("shinylive.quiet", !is_interactive()),
wasm_packages = TRUE,
wasm_packages = NULL,
package_cache = TRUE,
max_filesize = NULL,
assets_version = NULL,
Expand All @@ -84,6 +87,8 @@ export <- function(
assets_version <- assets_version()
}

wasm_packages <- wasm_packages %||% sys_env_wasm_packages()

if (!fs::is_dir(appdir)) {
cli::cli_abort("{.var appdir} must be a directory, but was provided {.path {appdir}}.")
}
Expand Down
11 changes: 11 additions & 0 deletions R/packages.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
SHINYLIVE_DEFAULT_MAX_FILESIZE <- "100MB"
SHINYLIVE_WASM_PACKAGES <- TRUE

# Sys env maximum filesize for asset bundling
sys_env_max_filesize <- function() {
max_fs_env <- Sys.getenv("SHINYLIVE_DEFAULT_MAX_FILESIZE")
if (max_fs_env == "") NULL else max_fs_env
}

sys_env_wasm_packages <- function() {
pkgs_env <- Sys.getenv("SHINYLIVE_WASM_PACKAGES", SHINYLIVE_WASM_PACKAGES)
pkgs_env <- switch(pkgs_env, "1" = TRUE, "0" = FALSE, pkgs_env)
wasm_packages <- as.logical(pkgs_env)
if (is.na(wasm_packages)) {
cli::cli_abort("Could not parse `wasm_packages` value: {.code {pkgs_env}}")
}
wasm_packages
}

# Resolve package list, dependencies listed in Depends and Imports
resolve_dependencies <- function(pkgs, local = TRUE) {
pkg_refs <- if (local) {
Expand Down
13 changes: 8 additions & 5 deletions R/quarto_ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,14 @@ build_app_resources <- function(app_json) {
}
})

# Download wasm binaries ready to embed into Quarto deps
withr::with_options(
list(shinylive.quiet = TRUE),
download_wasm_packages(appdir, destdir, package_cache = TRUE, max_filesize = NULL)
)
wasm_packages <- sys_env_wasm_packages()
if (wasm_packages) {
# Download wasm binaries ready to embed into Quarto deps
withr::with_options(
list(shinylive.quiet = TRUE),
download_wasm_packages(appdir, destdir, package_cache = TRUE, max_filesize = NULL)
)
}

# Enumerate R package Wasm binaries and prepare the VFS images as html deps
webr_dir <- fs::path(destdir, "shinylive", "webr")
Expand Down
9 changes: 6 additions & 3 deletions man/export.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-export.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ test_that("export - include R package in wasm assets", {
app_dir <- test_path("apps", "app-utf8")
asset_package <- c("utf8")

# No external dependencies exported
expect_silent_unattended({
withr::with_envvar(
list("SHINYLIVE_WASM_PACKAGES" = "FALSE"),
export(app_dir, out_dir)
)
})
expect_length(dir(pkg_dir), 0)
unlink_path(out_dir)

# Default filesize 100MB
expect_silent_unattended({
export(app_dir, out_dir)
Expand Down