rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 69 forks source link

FR: Render only part of the dependencies of a bundle? #273

Closed maelle closed 3 years ago

maelle commented 3 years ago

Via https://github.com/rstudio/bslib/issues/326 that also lead to #272 cc @cpsievert

After creating a sass bundle via bslib I'd like to only render the stylesheet dependency (as I'm getting the other dependencies from another similar bundle).

cpsievert commented 3 years ago

After creating a sass bundle via bslib I'd like to only render the stylesheet dependency (as I'm getting the other dependencies from another similar bundle).

Sounds like a bslib feature request. Although, I'm not sure we'd provide a shortcut for doing this, considering we can already do:

library(bslib)
deps <- bs_theme_dependencies(bs_theme())
bs_dep <- deps[[match("bootstrap", vapply(deps, "[[", character(1), "name"))]]
bs_dep$script <- NULL
htmltools::renderDependencies(list(bs_dep))
maelle commented 3 years ago

One day I'll open an issue in the right repo directly, sorry and thank you :sweat_smile:

Your snippet is most helpful, but with it I still have the issue that bs_dep still contains a script, as using htmltools::copyDependencyToDir() still leads to the folder containing the JS scripts. Am I missing something obvious once again?

library(bslib)
deps <- bs_theme_dependencies(bs_theme())
bs_dep <- deps[[match("bootstrap", vapply(deps, "[[", character(1), "name"))]]
bs_dep$script <- NULL
htmltools::renderDependencies(list(bs_dep))
dir <- withr::local_tempdir()
htmltools::copyDependencyToDir(bs_dep, dir)
fs::dir_ls(dir, recurse = TRUE)
maelle commented 3 years ago

Ah yes of course I was missing a hack! One needs to add bs_dep$all_files<- FALSE before copying the dependency to a dir. Awesome!

maelle commented 3 years ago

So the correct way to only extract the CSS is

library(bslib)
deps <- bs_theme_dependencies(bs_theme())
bs_dep <- deps[[match("bootstrap", vapply(deps, "[[", character(1), "name"))]]
bs_dep$script <- NULL
htmltools::renderDependencies(list(bs_dep))
bs_dep$all_files <- FALSE
dir <- withr::local_tempdir()
htmltools::copyDependencyToDir(bs_dep, dir)
fs::dir_ls(dir, recurse = TRUE)

both bs_dep$script <- NULL and bs_dep$all_files <- FALSE are necessary.