1
-
2
- # This figures out which precompiled CSS files are available.
3
- # This is memoized - it runs once per R session.
4
- precompiled_css <- local({
5
- themes <- NULL
6
-
1
+ # List which precompiled CSS files are available.
2
+ .precompiled_css_versions <- local({
3
+ versions <- NULL
7
4
function () {
8
- if (is.null(themes )) {
9
- versions <- dir(system.file(" css-precompiled" , package = " bslib" ))
10
- themes <<- lapply(versions , function (version ) {
11
- list (
12
- version = version ,
13
- theme = bs_theme(version , brand = FALSE )
14
- )
15
- })
5
+ if (is.null(versions )) {
6
+ versions <<- dir(system.file(" css-precompiled" , package = " bslib" ))
16
7
}
17
- themes
8
+ versions
18
9
}
19
10
})
20
11
12
+ .precompiled_css_themes <- new.env(parent = emptyenv())
21
13
22
- precompiled_css_version <- function (theme ) {
23
- for (precompiled in precompiled_css()) {
24
- if (identical(theme , precompiled $ theme )) {
25
- return (theme_version(theme ))
26
- }
14
+ # Precompiled themes are created for base `bs_theme()`, changing only the
15
+ # Bootstrap version number. To decide if we can use the precompiled theme, we
16
+ # hash `bs_theme(version)`, which we'll compare with a hash of the user's theme.
17
+ precompiled_bs_theme_hash <- function (version ) {
18
+ theme_hash <- get0(as.character(version ), .precompiled_css_themes )
19
+
20
+ if (is.null(theme_hash )) {
21
+ theme_hash <- rlang :: hash(bs_theme(version = version , brand = FALSE ))
22
+ assign(version , theme_hash , envir = .precompiled_css_themes )
27
23
}
28
24
29
- return (NULL )
25
+ return (theme_hash )
30
26
}
31
27
32
-
33
28
# ' Get the path to a precompiled CSS file
34
29
# '
35
30
# ' This function is meant for development and debugging purposes. It can be used
@@ -42,10 +37,31 @@ precompiled_css_version <- function(theme) {
42
37
# ' @keywords internal
43
38
# ' @export
44
39
precompiled_css_path <- function (theme = bs_theme()) {
45
- version <- precompiled_css_version(theme )
40
+ version <- theme_version(theme )
41
+ theme_hash <- rlang :: hash(theme )
42
+ version_precompiled <- NULL
43
+
46
44
if (is.null(version )) {
45
+ for (v in .precompiled_css_versions()) {
46
+ if (identical(theme_hash , precompiled_bs_theme_hash(v ))) {
47
+ version_precompiled <- v
48
+ break
49
+ }
50
+ }
51
+ } else if (version %in% .precompiled_css_versions()) {
52
+ if (identical(theme_hash , precompiled_bs_theme_hash(version ))) {
53
+ version_precompiled <- version
54
+ }
55
+ }
56
+
57
+ if (is.null(version_precompiled )) {
47
58
return (NULL )
48
59
}
49
60
50
- system_file(package = " bslib" , " css-precompiled" , version , " bootstrap.min.css" )
61
+ system_file(
62
+ package = " bslib" ,
63
+ " css-precompiled" ,
64
+ version_precompiled ,
65
+ " bootstrap.min.css"
66
+ )
51
67
}
0 commit comments