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 new sass_html_dependencies() function for more convenient htmlDependency bundling #46

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Type: Package
Package: sass
Version: 0.2.0.9001
Version: 0.2.0.9002
Title: Syntactically Awesome Style Sheets ('Sass')
Description: An 'SCSS' compiler, powered by the 'LibSass' library. With this,
R developers can use variables, inheritance, and functions to generate
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(sass_cache_set)
export(sass_file)
export(sass_file_cache)
export(sass_hash)
export(sass_html_dependencies)
export(sass_import)
export(sass_layer)
export(sass_layer_merge)
Expand Down
58 changes: 58 additions & 0 deletions R/sass-dependencies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' Compile Sass to CSS using LibSass, then return an HTML Dependencies
#'
#' Calls `sass(input, ...)` and wraps the output into a [htmltools::htmlDependency()].
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
#' Calls `sass(input, ...)` and wraps the output into a [htmltools::htmlDependency()].
#' Calls `sass(input, ...)` and wraps the output into a list of [htmltools::htmlDependency()]s.

#'
#' @inheritParams sass
#' @inheritParams htmltools::htmlDependency
#' @param cache_key additional information to include in the `sass_hash()` call used to
#' determine the output directory. This should include any information that could possibly
#' influence the resulting CSS that isn't already captured by `input`. For example, if
#' `input` contains something like `"@import sass_file.scss"` you may want to include the
#' [file.mtime()] of `sass_file.scss` (or, perhaps, a [packageVersion()] if `sass_file.scss`
#' is bundled with an R package).
#' @param stylesheet Absolute file path(s) pointing to stylesheet(s) to include with the dependency
#'
#' @return returns a list of [htmltools::htmlDependency()] objects. The length
#' of the list use 1 unless `input` contains [sass_layer()] object(s) that
#' have `html_deps`.
#'
#' @export
#' @seealso [sass()]
#' @examples
#' str(sass_html_dependencies(
#' input = list(list(color = "red"), "body {color: $color}"),
#' name = "foo", version = "1.0.0",
#' stylesheet = "my-style.css"
#' ))
sass_html_dependencies <- function(
input, name, version, stylesheet, cache_key = NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's kind of confusing how the htmlDependency args are interleaved with the sass args, and that the order of the htmlDependency args aren't even the same as in the htmlDependency function.

One possibility: maybe all of the htmlDependency args should go after ...? We can also discuss other ways to go about this.

options = sass_options(), write_attachments = TRUE, cache = sass_cache_get(),
script = NULL, meta = NULL, head = NULL) {

key <- sass_hash(list(input, options, cache_key))
outDir <- file.path(tempdir(), paste(name, version, key, sep = "-"))
if (!dir.exists(outDir)) dir.create(outDir)
sass(
input, output = file.path(outDir, stylesheet), options = options,
write_attachments = write_attachments, cache = cache
)

if (!is.null(script)) {
if (!all(fs::is_absolute_path(script))) {
stop("script file paths must all be absolute file paths")
}
fs::file_copy(script, outDir, overwrite = TRUE)
script <- basename(script)
}

c(
list(htmltools::htmlDependency(
name = name, version = version,
src = c(file = outDir),
stylesheet = stylesheet,
script = script,
meta = meta, head = head
)),
extract_layer(input)$html_deps
)
}
2 changes: 1 addition & 1 deletion R/sass.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#' @return If \code{output = NULL}, the function returns a string value of the
#' compiled CSS. If the output path is specified, the compiled CSS is written
#' to that file and \code{invisible()} is returned.
#' @seealso \url{http://sass-lang.com/guide}
#' @seealso \url{http://sass-lang.com/guide}, [sass_html_dependencies()]
#' @export
#' @examples
#' # raw Sass input
Expand Down
2 changes: 1 addition & 1 deletion man/sass.Rd

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

76 changes: 76 additions & 0 deletions man/sass_html_dependencies.Rd

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