| 1 |
#' Compile Sass to CSS |
| 2 |
#' |
| 3 |
#' Compile Sass to CSS using LibSass. |
| 4 |
#' |
| 5 |
#' @section Caching: |
| 6 |
#' |
| 7 |
#' By default, caching is enabled, meaning that `sass()` avoids the possibly |
| 8 |
#' expensive re-compilation of CSS whenever the same `options` and `input` are |
| 9 |
#' requested. Unfortunately, in some cases, `options` and `input` alone aren't |
| 10 |
#' enough to determine whether new CSS output must be generated. For example, |
| 11 |
#' changes in local file |
| 12 |
#' [imports](https://sass-lang.com/documentation/at-rules/import) that aren't |
| 13 |
#' captured through [sass_file()]/[sass_import()], may lead to a |
| 14 |
#' false-positive cache hit. For this reason, developers are encouraged to |
| 15 |
#' capture such information in `cache_key_extra` (possibly with |
| 16 |
#' `packageVersion('myPackage')` if shipping Sass with a package), and users |
| 17 |
#' may want to disable caching altogether during local development by calling |
| 18 |
#' `options(sass.cache=FALSE)`. |
| 19 |
#' |
| 20 |
#' In some cases when developing and modifying .scss files, [sass()] might not |
| 21 |
#' detect changes, and keep using cached .css files instead of rebuilding |
| 22 |
#' them. To be safe, if you are developing a theme with sass, it's best to |
| 23 |
#' turn off caching by calling `options(sass.cache=FALSE)`. |
| 24 |
#' |
| 25 |
#' If caching is enabled, [sass()] will attempt to bypass the compilation |
| 26 |
#' process by reusing output from previous [sass()] calls that used equivalent |
| 27 |
#' inputs. This mechanism works by computing a _cache key_ from each [sass()] |
| 28 |
#' call's `input`, `option`, and `cache_key_extra` arguments. If an object |
| 29 |
#' with that hash already exists within the cache directory, its contents are |
| 30 |
#' used instead of performing the compilation. If it does not exist, then |
| 31 |
#' compilation is performed and usual and the results are stored in the cache. |
| 32 |
#' |
| 33 |
#' If a file that is included using [sass_file()] changes on disk (i.e. its |
| 34 |
#' last-modified time changes), its previous cache entries will effectively be |
| 35 |
#' invalidated (not removed from disk, but they'll no longer be matched). |
| 36 |
#' However, if a file imported using [sass_file()] itself imports other sass |
| 37 |
#' files using `@import`, changes to those files are invisible to the |
| 38 |
#' cache and you can end up with stale results. To avoid this problem when |
| 39 |
#' developing sass code, it's best to disable caching with |
| 40 |
#' `options(sass.cache=FALSE)`. |
| 41 |
#' |
| 42 |
#' By default, the maximum size of the cache is 40 MB. If it grows past that |
| 43 |
#' size, the least-recently-used objects will be evicted from the cache to |
| 44 |
#' keep it under that size. Also by default, the maximum age of objects in the |
| 45 |
#' cache is one week. Older objects will be evicted from the cache. |
| 46 |
#' |
| 47 |
#' To clear the default cache, call `sass_cache_get()$reset()`. |
| 48 |
#' |
| 49 |
#' |
| 50 |
#' @inheritParams as_sass |
| 51 |
#' @param options Compiler [sass_options()]. |
| 52 |
#' @param output Specifies path to output file for compiled CSS. May be a |
| 53 |
#' character string or [output_template()] |
| 54 |
#' @param write_attachments If the input contains [sass_layer()] objects that |
| 55 |
#' have file attachments, and `output` is not `NULL`, then copy the file |
| 56 |
#' attachments to the directory of `output`. (Defaults to `NA`, which merely |
| 57 |
#' emits a warning if file attachments are present, but does not write them to |
| 58 |
#' disk; the side-effect of writing extra files is subtle and potentially |
| 59 |
#' destructive, as files may be overwritten.) |
| 60 |
#' @param cache This can be a directory to use for the cache, a [FileCache] |
| 61 |
#' object created by [sass_file_cache()], or `FALSE` or `NULL` for no caching. |
| 62 |
#' @param cache_key_extra additional information to considering when computing |
| 63 |
#' the cache key. This should include any information that could possibly |
| 64 |
#' influence the resulting CSS that isn't already captured by `input`. For |
| 65 |
#' example, if `input` contains something like `"@import sass_file.scss"` you |
| 66 |
#' may want to include the [file.mtime()] of `sass_file.scss` (or, perhaps, a |
| 67 |
#' [packageVersion()] if `sass_file.scss` is bundled with an R package). |
| 68 |
#' |
| 69 |
#' @return If `output = NULL`, the function returns a string value of the |
| 70 |
#' compiled CSS. If `output` is specified, the compiled CSS is written to a |
| 71 |
#' file and the filename is returned. |
| 72 |
#' |
| 73 |
#' @seealso <https://sass-lang.com/guide> |
| 74 |
#' @export |
| 75 |
#' @examples |
| 76 |
#' # Raw Sass input |
| 77 |
#' sass("foo { margin: 122px * .3; }") |
| 78 |
#' |
| 79 |
#' # List of inputs, including named variables |
| 80 |
#' sass(list( |
| 81 |
#' list(width = "122px"), |
| 82 |
#' "foo { margin: $width * .3; }" |
| 83 |
#' )) |
| 84 |
#' |
| 85 |
#' # Compile a .scss file |
| 86 |
#' example_file <- system.file("examples/example-full.scss", package = "sass") |
| 87 |
#' sass(sass_file(example_file)) |
| 88 |
#' |
| 89 |
#' # Import a file |
| 90 |
#' tmp_file <- tempfile() |
| 91 |
#' writeLines("foo { margin: $width * .3; }", tmp_file) |
| 92 |
#' sass(list( |
| 93 |
#' list(width = "122px"), |
| 94 |
#' sass_file(tmp_file) |
| 95 |
#' )) |
| 96 |
#' |
| 97 |
#' \dontrun{ |
| 98 |
#' # ====================== |
| 99 |
#' # Caching examples |
| 100 |
#' # ====================== |
| 101 |
#' # Very slow to compile |
| 102 |
#' fib_sass <- "@function fib($x) { |
| 103 |
#' @if $x <= 1 { |
| 104 |
#' @return $x |
| 105 |
#' } |
| 106 |
#' @return fib($x - 2) + fib($x - 1); |
| 107 |
#' } |
| 108 |
#' |
| 109 |
#' body { |
| 110 |
#' width: fib(27); |
| 111 |
#' }" |
| 112 |
#' |
| 113 |
#' # The first time this runs it will be very slow |
| 114 |
#' system.time(sass(fib_sass)) |
| 115 |
#' |
| 116 |
#' # But on subsequent calls, it should be very fast |
| 117 |
#' system.time(sass(fib_sass)) |
| 118 |
#' |
| 119 |
#' # sass() can be called with cache=NULL; it will be slow |
| 120 |
#' system.time(sass(fib_sass, cache = NULL)) |
| 121 |
#' |
| 122 |
#' # Clear the cache |
| 123 |
#' sass_cache_get()$reset() |
| 124 |
#' } |
| 125 |
#' |
| 126 |
#' \dontrun{ |
| 127 |
#' # Example of disabling cache by setting the default cache to NULL. |
| 128 |
#' |
| 129 |
#' # Disable the default cache (save the original one first, so we can restore) |
| 130 |
#' old_cache <- sass_cache_get() |
| 131 |
#' sass_cache_set(NULL) |
| 132 |
#' # Will be slow, because no cache |
| 133 |
#' system.time(sass(fib_sass)) |
| 134 |
#' |
| 135 |
#' # Restore the original cache |
| 136 |
#' sass_cache_set(old_cache) |
| 137 |
#' } |
| 138 |
sass <- function( |
| 139 |
input = NULL, |
| 140 |
options = sass_options_get(), |
| 141 |
output = NULL, |
| 142 |
write_attachments = NA, |
| 143 |
cache = sass_cache_get(), |
| 144 |
cache_key_extra = NULL) |
| 145 |
{ |
| 146 |
|
| 147 |
if (!inherits(options, "sass_options")) { |
| 148 |
stop("Please construct the compile options using `sass_options()`.") |
| 149 |
} |
| 150 |
if (is.null(output) && isTRUE(write_attachments)) { |
| 151 |
stop("sass(write_attachments=TRUE) cannot be used when output=NULL") |
| 152 |
} |
| 153 |
|
| 154 |
if (identical(cache, FALSE)) { |
| 155 |
cache <- NULL |
| 156 |
} else if (is.character(cache)) { |
| 157 |
# In case it's a directory name |
| 158 |
cache <- sass_cache_get_dir(cache, create = TRUE) |
| 159 |
} |
| 160 |
|
| 161 |
if (!is.null(cache) && !inherits(cache, "FileCache")) { |
| 162 |
stop("Please use FALSE or NULL (no cache), a string with a directory name, or a FileCache object for `cache`.") |
| 163 |
} |
| 164 |
|
| 165 |
css <- NULL |
| 166 |
sass_input <- as_sass(input) |
| 167 |
html_deps <- htmlDependencies(sass_input) |
| 168 |
file_attachments <- extract_file_attachments(input) |
| 169 |
|
| 170 |
# If caching is active, compute the hash key |
| 171 |
cache_key <- if (!is.null(cache)) { |
| 172 |
sass_hash(list( |
| 173 |
# Don't include htmlDependency()s in cache key since they: |
| 174 |
# 1. Are always attached to the return value of sass() |
| 175 |
# 2. Generally won't invalidate Sass->CSS compilation |
| 176 |
# 3. May include a temp directory |
| 177 |
discard_dependencies(input), |
| 178 |
options, cache_key_extra, |
| 179 |
|
| 180 |
if (isTRUE(write_attachments)) get_file_mtimes(file_attachments) |
| 181 |
)) |
| 182 |
} |
| 183 |
|
| 184 |
# Resolve output_template(), if need be |
| 185 |
if (is.function(output)) { |
| 186 |
output <- output(options, cache_key) |
| 187 |
} |
| 188 |
|
| 189 |
# Check the output dir exists (if relevant) |
| 190 |
outdir <- NULL |
| 191 |
if (!is.null(output)) { |
| 192 |
outdir <- fs::path_dir(output) |
| 193 |
if (!dir.exists(outdir)) { |
| 194 |
stop("The output directory '", outdir, "' does not exist") |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (!is.null(cache)) { |
| 199 |
cache_hit <- FALSE |
| 200 |
if (is.null(output)) { |
| 201 |
# If no output is specified, we need to return a character vector |
| 202 |
css <- cache$get_content(cache_key) |
| 203 |
if (!is.null(css)) { |
| 204 |
cache_hit <- TRUE |
| 205 |
} |
| 206 |
} else { |
| 207 |
if (cache$exists(cache_key)) { |
| 208 |
key_file <- file.path(outdir, ".sass_cache_keys") |
| 209 |
cache_hit <- file.exists(output) && file.exists(key_file) && |
| 210 |
isTRUE(cache_key %in% readLines(key_file, warn = FALSE)) |
| 211 |
} |
| 212 |
if (cache_hit) { |
| 213 |
return(attachDependencies(output, html_deps)) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (!cache_hit) { |
| 218 |
# We had a cache miss, so write to disk now |
| 219 |
css <- compile_data(sass_input, options) |
| 220 |
Encoding(css) <- "UTF-8" |
| 221 |
|
| 222 |
# Save a note in the output directory that we've already written |
| 223 |
# all the necessary files to this location |
| 224 |
if (!is.null(output)) { |
| 225 |
cat(cache_key, file = file.path(outdir, ".sass_cache_keys"), append = TRUE) |
| 226 |
} |
| 227 |
|
| 228 |
# In case this same code is running in two processes pointed at the same |
| 229 |
# cache dir, this could return FALSE (if the file didn't exist when we |
| 230 |
# tried to get it, but does exist when we try to write it here), but |
| 231 |
# that's OK -- it should have the same content. |
| 232 |
cache$set_content(cache_key, css) |
| 233 |
} |
| 234 |
|
| 235 |
} else { |
| 236 |
# If we got here, we're not using a cache. |
| 237 |
css <- compile_data(sass_input, options) |
| 238 |
Encoding(css) <- "UTF-8" |
| 239 |
} |
| 240 |
|
| 241 |
css <- as_html(css, "css") |
| 242 |
|
| 243 |
if (!is.null(output)) { |
| 244 |
write_utf8(css, output) |
| 245 |
maybe_write_attachments(file_attachments, outdir, write_attachments) |
| 246 |
return( |
| 247 |
attachDependencies(output, html_deps) |
| 248 |
) |
| 249 |
} |
| 250 |
|
| 251 |
# Attach HTML dependencies so that placing a sass::sass() call within HTML tags |
| 252 |
# will include the dependencies |
| 253 |
attachDependencies(css, html_deps) |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
#' Compile rules against a Sass Bundle or Sass Layer object |
| 258 |
#' |
| 259 |
#' Replaces the rules for a [sass_layer()] object with new rules, and compile it. |
| 260 |
#' This is useful when (for example) you want to compile a set of rules using |
| 261 |
#' variables derived from a theme, but you do not want the resulting CSS for the |
| 262 |
#' entire theme -- just the CSS for the specific rules passed in. |
| 263 |
#' |
| 264 |
#' @param rules A set of sass rules, which will be used instead of the rules |
| 265 |
#' from `layer`. |
| 266 |
#' @param bundle A [sass_bundle()] or [sass_layer()] object. |
| 267 |
#' @inheritParams sass |
| 268 |
#' |
| 269 |
#' @examples |
| 270 |
#' theme <- sass_layer( |
| 271 |
#' defaults = sass_file(system.file("examples/variables.scss", package = "sass")), |
| 272 |
#' rules = sass_file(system.file("examples/rules.scss", package = "sass")) |
| 273 |
#' ) |
| 274 |
#' |
| 275 |
#' # Compile the theme |
| 276 |
#' sass(theme) |
| 277 |
#' |
| 278 |
#' # Sometimes we want to use the variables from the theme to compile other sass |
| 279 |
#' my_rules <- ".someclass { background-color: $bg; color: $fg; }" |
| 280 |
#' sass_partial(my_rules, theme) |
| 281 |
#' |
| 282 |
#' @export |
| 283 |
sass_partial <- function( |
| 284 |
rules, |
| 285 |
bundle, |
| 286 |
options = sass_options_get(), |
| 287 |
output = NULL, |
| 288 |
write_attachments = NA, |
| 289 |
cache = sass_cache_get(), |
| 290 |
cache_key_extra = NULL) |
| 291 |
{ |
| 292 |
if (!is_sass_bundle(bundle)) { |
| 293 |
stop("`bundle` must be a `sass_bundle()` object.", call. = FALSE) |
| 294 |
} |
| 295 |
|
| 296 |
layer <- as_sass_layer(bundle) |
| 297 |
|
| 298 |
rules <- as_sass(rules) |
| 299 |
layer$rules <- rules |
| 300 |
sass(layer, options, output, write_attachments, cache, cache_key_extra) |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
#' An intelligent (temporary) output file |
| 305 |
#' |
| 306 |
#' Intended for use with [sass()]'s `output` argument for temporary file |
| 307 |
#' generation that is `cache` and `options` aware. In particular, this ensures |
| 308 |
#' that new redundant file(s) aren't generated on a [sass()] cache hit, and that |
| 309 |
#' the file's extension is suitable for the [sass_options()]'s `output_style`. |
| 310 |
#' |
| 311 |
#' @param basename a non-empty character string giving the outfile name (without |
| 312 |
#' the extension). |
| 313 |
#' @param dirname a non-empty character string giving the initial part of the |
| 314 |
#' directory name. |
| 315 |
#' @param fileext the output file extension. The default is `".min.css"` for |
| 316 |
#' compressed and compact output styles; otherwise, its `".css"`. |
| 317 |
#' @param path the output file's root directory path. |
| 318 |
#' |
| 319 |
#' @return A function with two arguments: `options` and `suffix`. When called inside |
| 320 |
#' [sass()] with caching enabled, the caching key is supplied to `suffix`. |
| 321 |
#' |
| 322 |
#' @export |
| 323 |
#' @examples |
| 324 |
#' sass("body {color: red}", output = output_template()) |
| 325 |
#' |
| 326 |
#' func <- output_template(basename = "foo", dirname = "bar-") |
| 327 |
#' func(suffix = "baz") |
| 328 |
#' |
| 329 |
output_template <- function(basename = "sass", dirname = basename, fileext = NULL, path = tempdir()) { |
| 330 |
function(options = list(), suffix = NULL) { |
| 331 |
fileext <- fileext %||% if (isTRUE(options$output_style %in% c(2, 3))) ".min.css" else ".css" |
| 332 |
# If caching is enabled, then make sure the out dir is unique to the cache key; |
| 333 |
# otherwise, do the more conservative thing of making sure there is a fresh start everytime |
| 334 |
out_dir <- if (is.null(suffix)) { |
| 335 |
tempfile(tmpdir = path, pattern = dirname) |
| 336 |
} else { |
| 337 |
file.path(path, paste0(dirname, suffix)) |
| 338 |
} |
| 339 |
if (!dir.exists(out_dir)) { |
| 340 |
dir.create(out_dir, recursive = TRUE) |
| 341 |
} |
| 342 |
file.path(out_dir, paste0(basename, fileext)) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
maybe_write_attachments <- function(file_attachments, outdir, write_attachments) { |
| 347 |
if (length(file_attachments) == 0) { |
| 348 |
return() |
| 349 |
} |
| 350 |
|
| 351 |
if (isTRUE(write_attachments)) { |
| 352 |
write_file_attachments(file_attachments, outdir) |
| 353 |
return() |
| 354 |
} |
| 355 |
|
| 356 |
if (isTRUE(is.na(write_attachments))) { |
| 357 |
warning( |
| 358 |
"sass() input contains file attachments that are being ignored. Pass ", |
| 359 |
"write_attachments=TRUE to write these files to disk, or FALSE to ", |
| 360 |
"suppress this warning.", call. = FALSE |
| 361 |
) |
| 362 |
} |
| 363 |
} |