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 1 commit
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
18 changes: 15 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,15 @@ export <- function(
assets_version <- assets_version()
}

wasm_packages_val <- wasm_packages %||% sys_env_wasm_packages()
wasm_packages <- as.logical(wasm_packages_val)
if (is.na(wasm_packages)) {
cli::cli_abort(c(
"x" = "Could not parse `wasm_packages` value: {.code {wasm_packages_val}}"
))
georgestagg marked this conversation as resolved.
Show resolved Hide resolved
wasm_packages <- SHINYLIVE_WASM_PACKAGES
}

if (!fs::is_dir(appdir)) {
cli::cli_abort("{.var appdir} must be a directory, but was provided {.path {appdir}}.")
}
Expand Down
8 changes: 7 additions & 1 deletion R/packages.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
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
}

# Resolve package list hard dependencies
sys_env_wasm_packages <- function() {
pkgs_env <- Sys.getenv("SHINYLIVE_WASM_PACKAGES", SHINYLIVE_WASM_PACKAGES)
switch(pkgs_env, "1" = TRUE, "0" = FALSE, pkgs_env)
}

# Resolve package list, dependencies listed in Depends and Imports
resolve_dependencies <- function(pkgs, local = TRUE) {
pkg_refs <- if (local) {
refs <- find.package(pkgs, lib.loc = NULL, quiet = FALSE, !is_quiet())
Expand Down
21 changes: 16 additions & 5 deletions R/quarto_ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,22 @@ 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_val <- sys_env_wasm_packages()
wasm_packages <- as.logical(wasm_packages_val)
if (is.na(wasm_packages)) {
cli::cli_abort(c(
"x" = "Could not parse `wasm_packages` value: {.code {wasm_packages_val}}"
))
wasm_packages <- SHINYLIVE_WASM_PACKAGES
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's worth putting this in a helper function

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I realised we're only really expecting to see an NA when we're parsing the string from Sys.getenv(), so I've moved the check and cli_abort() into the sys_env_wasm_packages() function, avoiding the duplication.


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