Skip to content

Commit 09f47f0

Browse files
committed
WIP: Generate __asResolvedUrl variables
1 parent d7af752 commit 09f47f0

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

lib/plugin/css-variables-collector.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,39 @@ CSSVariablesCollectorPlugin.prototype = {
3737
return !this._isInMixinOrParen() && this._isVarInRule();
3838
},
3939

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+
4052
toLessVariables() {
4153
let lessVariables = "";
4254
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`;
4458
});
4559
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`;
4763
});
4864
lessVariables += "\n:root {\n";
4965
Object.keys(this.vars).forEach((value, index) => {
5066
if (this.vars[value].export) {
51-
lessVariables += "--" + value + ": @" + value + ";\n";
67+
lessVariables += `--${value}: @${value};\n`;
5268
}
5369
});
5470
Object.keys(this.calcVars).forEach((value, index) => {
5571
if (this.calcVars[value].export) {
56-
lessVariables += "--" + value + ": @" + value + ";\n";
72+
lessVariables += `--${value}: @${value};\n`;
5773
}
5874
});
5975
lessVariables += "}\n";
@@ -185,10 +201,27 @@ CSSVariablesCollectorPlugin.prototype = {
185201
const isVarDeclaration = value instanceof less.tree.Rule && typeof value.name === "string" && value.name.startsWith("@");
186202
if (!this._isInMixinOrParen() && isVarDeclaration) {
187203
// 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 = {
189207
css: this._getCSS(value.value),
190208
export: !this._isInGlobalOrBaseImport()
191209
};
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 = /url[\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+
}
192225
}
193226
});
194227
return node;

0 commit comments

Comments
 (0)