@@ -37,23 +37,39 @@ CSSVariablesCollectorPlugin.prototype = {
37
37
return ! this . _isInMixinOrParen ( ) && this . _isVarInRule ( ) ;
38
38
} ,
39
39
40
+ _getResolvedUrl ( rawUrl ) {
41
+ const parsedUrl = require ( "url" ) . parse ( rawUrl ) ;
42
+ if ( parsedUrl . protocol || rawUrl . startsWith ( "/" ) ) {
43
+ return rawUrl ;
44
+ }
45
+ let relativePath = parsedUrl . path ;
46
+ if ( relativePath . startsWith ( "./" ) ) {
47
+ relativePath = relativePath . substr ( 2 ) ;
48
+ }
49
+ return `ui5://TODO-namespace/themes/TODO-themeName/${ relativePath } ` ;
50
+ } ,
51
+
40
52
toLessVariables ( ) {
41
53
let lessVariables = "" ;
42
54
Object . keys ( this . vars ) . forEach ( ( value , index ) => {
43
- lessVariables += "@" + value + ": " + this . vars [ value ] . css + ";\n" ;
55
+ const variableName = value ;
56
+ const variableValue = this . vars [ value ] . css ;
57
+ lessVariables += `@${ variableName } : ${ variableValue } ;\n` ;
44
58
} ) ;
45
59
Object . keys ( this . calcVars ) . forEach ( ( value , index ) => {
46
- lessVariables += "@" + value + ": " + this . calcVars [ value ] . css + ";\n" ;
60
+ const variableName = value ;
61
+ const variableValue = this . calcVars [ value ] . css ;
62
+ lessVariables += `@${ variableName } : ${ variableValue } ;\n` ;
47
63
} ) ;
48
64
lessVariables += "\n:root {\n" ;
49
65
Object . keys ( this . vars ) . forEach ( ( value , index ) => {
50
66
if ( this . vars [ value ] . export ) {
51
- lessVariables += "--" + value + " : @" + value + " ;\n" ;
67
+ lessVariables += `-- ${ value } : @${ value } ;\n` ;
52
68
}
53
69
} ) ;
54
70
Object . keys ( this . calcVars ) . forEach ( ( value , index ) => {
55
71
if ( this . calcVars [ value ] . export ) {
56
- lessVariables += "--" + value + " : @" + value + " ;\n" ;
72
+ lessVariables += `-- ${ value } : @${ value } ;\n` ;
57
73
}
58
74
} ) ;
59
75
lessVariables += "}\n" ;
@@ -185,10 +201,27 @@ CSSVariablesCollectorPlugin.prototype = {
185
201
const isVarDeclaration = value instanceof less . tree . Rule && typeof value . name === "string" && value . name . startsWith ( "@" ) ;
186
202
if ( ! this . _isInMixinOrParen ( ) && isVarDeclaration ) {
187
203
// add the variable declaration to the list of vars
188
- this . vars [ value . name . substr ( 1 ) ] = {
204
+
205
+ const variableName = value . name . substr ( 1 ) ;
206
+ const variableEntry = {
189
207
css : this . _getCSS ( value . value ) ,
190
208
export : ! this . _isInGlobalOrBaseImport ( )
191
209
} ;
210
+
211
+ this . vars [ variableName ] = variableEntry ;
212
+
213
+ if ( variableEntry . export ) {
214
+ // Create additional _asResolvedUrl variable for runtime resolution of relative urls
215
+ const urlMatch = / u r l [ \s ] * \( ' ? " ? ( [ ^ ' " ) ] * ) ' ? " ? \) / . exec ( variableEntry . css ) ;
216
+ if ( urlMatch ) {
217
+ const resolvedUrlVariableName = `${ variableName } __asResolvedUrl` ;
218
+ const resolvedUrlVariableEntry = {
219
+ css : `"${ this . _getResolvedUrl ( urlMatch [ 1 ] ) } "` ,
220
+ export : true
221
+ } ;
222
+ this . vars [ resolvedUrlVariableName ] = resolvedUrlVariableEntry ;
223
+ }
224
+ }
192
225
}
193
226
} ) ;
194
227
return node ;
0 commit comments