Skip to content

Commit

Permalink
Add SHINYLIVE_WASM_PACKAGES environment variable (#116)
Browse files Browse the repository at this point in the history
* Add `SHINYLIVE_WASM_PACKAGES` environment var

* Move parse checking into shared helper function

* Update NEWS.md
  • Loading branch information
georgestagg authored Aug 2, 2024
1 parent b14bea1 commit e05d9bd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
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
#' 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

0 comments on commit e05d9bd

Please sign in to comment.