Debian Codegraph @

man/sass.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/sass.R
3 \name{sass}
4 \alias{sass}
5 \title{Compile Sass to CSS}
6 \usage{
7 sass(
8 input = NULL,
9 options = sass_options_get(),
10 output = NULL,
11 write_attachments = NA,
12 cache = sass_cache_get(),
13 cache_key_extra = NULL
14 )
15 }
16 \arguments{
17 \item{input}{Any of the following:
18 \itemize{
19 \item A character vector containing Sass code.
20 \item A named list containing variable names and values.
21 \item A \code{\link[=sass_file]{sass_file()}}, \code{\link[=sass_layer]{sass_layer()}}, and/or \code{\link[=sass_bundle]{sass_bundle()}}.
22 \item A \code{\link[=list]{list()}} containing any of the above.
23 }}
24
25 \item{options}{Compiler \code{\link[=sass_options]{sass_options()}}.}
26
27 \item{output}{Specifies path to output file for compiled CSS. May be a
28 character string or \code{\link[=output_template]{output_template()}}}
29
30 \item{write_attachments}{If the input contains \code{\link[=sass_layer]{sass_layer()}} objects that
31 have file attachments, and \code{output} is not \code{NULL}, then copy the file
32 attachments to the directory of \code{output}. (Defaults to \code{NA}, which merely
33 emits a warning if file attachments are present, but does not write them to
34 disk; the side-effect of writing extra files is subtle and potentially
35 destructive, as files may be overwritten.)}
36
37 \item{cache}{This can be a directory to use for the cache, a \link{FileCache}
38 object created by \code{\link[=sass_file_cache]{sass_file_cache()}}, or \code{FALSE} or \code{NULL} for no caching.}
39
40 \item{cache_key_extra}{additional information to considering when computing
41 the cache key. This should include any information that could possibly
42 influence the resulting CSS that isn't already captured by \code{input}. For
43 example, if \code{input} contains something like \code{"@import sass_file.scss"} you
44 may want to include the \code{\link[=file.mtime]{file.mtime()}} of \code{sass_file.scss} (or, perhaps, a
45 \code{\link[=packageVersion]{packageVersion()}} if \code{sass_file.scss} is bundled with an R package).}
46 }
47 \value{
48 If \code{output = NULL}, the function returns a string value of the
49 compiled CSS. If \code{output} is specified, the compiled CSS is written to a
50 file and the filename is returned.
51 }
52 \description{
53 Compile Sass to CSS using LibSass.
54 }
55 \section{Caching}{
56
57
58 By default, caching is enabled, meaning that \code{sass()} avoids the possibly
59 expensive re-compilation of CSS whenever the same \code{options} and \code{input} are
60 requested. Unfortunately, in some cases, \code{options} and \code{input} alone aren't
61 enough to determine whether new CSS output must be generated. For example,
62 changes in local file
63 \href{https://sass-lang.com/documentation/at-rules/import}{imports} that aren't
64 captured through \code{\link[=sass_file]{sass_file()}}/\code{\link[=sass_import]{sass_import()}}, may lead to a
65 false-positive cache hit. For this reason, developers are encouraged to
66 capture such information in \code{cache_key_extra} (possibly with
67 \code{packageVersion('myPackage')} if shipping Sass with a package), and users
68 may want to disable caching altogether during local development by calling
69 \code{options(sass.cache=FALSE)}.
70
71 In some cases when developing and modifying .scss files, \code{\link[=sass]{sass()}} might not
72 detect changes, and keep using cached .css files instead of rebuilding
73 them. To be safe, if you are developing a theme with sass, it's best to
74 turn off caching by calling \code{options(sass.cache=FALSE)}.
75
76 If caching is enabled, \code{\link[=sass]{sass()}} will attempt to bypass the compilation
77 process by reusing output from previous \code{\link[=sass]{sass()}} calls that used equivalent
78 inputs. This mechanism works by computing a \emph{cache key} from each \code{\link[=sass]{sass()}}
79 call's \code{input}, \code{option}, and \code{cache_key_extra} arguments. If an object
80 with that hash already exists within the cache directory, its contents are
81 used instead of performing the compilation. If it does not exist, then
82 compilation is performed and usual and the results are stored in the cache.
83
84 If a file that is included using \code{\link[=sass_file]{sass_file()}} changes on disk (i.e. its
85 last-modified time changes), its previous cache entries will effectively be
86 invalidated (not removed from disk, but they'll no longer be matched).
87 However, if a file imported using \code{\link[=sass_file]{sass_file()}} itself imports other sass
88 files using \verb{@import}, changes to those files are invisible to the
89 cache and you can end up with stale results. To avoid this problem when
90 developing sass code, it's best to disable caching with
91 \code{options(sass.cache=FALSE)}.
92
93 By default, the maximum size of the cache is 40 MB. If it grows past that
94 size, the least-recently-used objects will be evicted from the cache to
95 keep it under that size. Also by default, the maximum age of objects in the
96 cache is one week. Older objects will be evicted from the cache.
97
98 To clear the default cache, call \code{sass_cache_get()$reset()}.
99 }
100
101 \examples{
102 # Raw Sass input
103 sass("foo { margin: 122px * .3; }")
104
105 # List of inputs, including named variables
106 sass(list(
107 list(width = "122px"),
108 "foo { margin: $width * .3; }"
109 ))
110
111 # Compile a .scss file
112 example_file <- system.file("examples/example-full.scss", package = "sass")
113 sass(sass_file(example_file))
114
115 # Import a file
116 tmp_file <- tempfile()
117 writeLines("foo { margin: $width * .3; }", tmp_file)
118 sass(list(
119 list(width = "122px"),
120 sass_file(tmp_file)
121 ))
122
123 \dontrun{
124 # ======================
125 # Caching examples
126 # ======================
127 # Very slow to compile
128 fib_sass <- "@function fib($x) {
129 @if $x <= 1 {
130 @return $x
131 }
132 @return fib($x - 2) + fib($x - 1);
133 }
134
135 body {
136 width: fib(27);
137 }"
138
139 # The first time this runs it will be very slow
140 system.time(sass(fib_sass))
141
142 # But on subsequent calls, it should be very fast
143 system.time(sass(fib_sass))
144
145 # sass() can be called with cache=NULL; it will be slow
146 system.time(sass(fib_sass, cache = NULL))
147
148 # Clear the cache
149 sass_cache_get()$reset()
150 }
151
152 \dontrun{
153 # Example of disabling cache by setting the default cache to NULL.
154
155 # Disable the default cache (save the original one first, so we can restore)
156 old_cache <- sass_cache_get()
157 sass_cache_set(NULL)
158 # Will be slow, because no cache
159 system.time(sass(fib_sass))
160
161 # Restore the original cache
162 sass_cache_set(old_cache)
163 }
164 }
165 \seealso{
166 \url{https://sass-lang.com/guide}
167 }