@@ -96,20 +96,47 @@ function getUsedCodeHikeComponentNames(
96
96
return usage
97
97
}
98
98
99
+ const styleCache = new Map < string , string > ( )
100
+
101
+ async function getStyle (
102
+ theme : CodeHikeConfig [ "theme" ] ,
103
+ themeName : string
104
+ ) {
105
+ if ( styleCache . has ( themeName ) ) {
106
+ return styleCache . get ( themeName )
107
+ }
108
+ const rules = await getCSSVariables ( theme )
109
+ const style = `[data-ch-theme="${ themeName } "] \{ ${ rules } \}`
110
+ styleCache . set ( themeName , style )
111
+ return style
112
+ }
113
+
99
114
async function getCSSVariables (
100
115
theme : CodeHikeConfig [ "theme" ]
101
116
) {
102
117
const themeColors = await getThemeColors ( theme )
118
+
119
+ if ( ! themeColors || typeof themeColors !== "object" ) {
120
+ throw new Error (
121
+ "[Code Hike error] Unknown theme format"
122
+ )
123
+ }
103
124
let rules = ""
104
125
for ( const [ first , value ] of Object . entries (
105
126
themeColors
106
127
) ) {
128
+ if ( ! value ) {
129
+ continue
130
+ }
107
131
if ( typeof value === "string" ) {
108
132
rules += `--ch-t-${ first } : ${ value } ;`
109
133
} else {
110
134
for ( const [ second , svalue ] of Object . entries (
111
135
value
112
136
) ) {
137
+ if ( ! svalue ) {
138
+ continue
139
+ }
113
140
rules += `--ch-t-${ first } -${ second } : ${ svalue } ;`
114
141
}
115
142
}
@@ -128,10 +155,9 @@ async function addConfig(
128
155
config : CodeHikeConfig
129
156
) {
130
157
const { theme } = config
131
- const rules = await getCSSVariables ( theme )
132
158
const themeName =
133
159
typeof theme === "string" ? theme : theme . name
134
- const style = `[data-ch- theme=" ${ themeName } "] \{ ${ rules } \}`
160
+ const style = await getStyle ( theme , themeName )
135
161
136
162
const codeConfig = {
137
163
staticMediaQuery : config . staticMediaQuery ,
0 commit comments