Skip to content

Commit c3acd6f

Browse files
committed
Enhance url resolving / update expected build output
1 parent 09f47f0 commit c3acd6f

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

lib/plugin/css-variables-collector.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ const CSSVariablesCollectorPlugin = module.exports = function(config) {
1414
this.importStack = [];
1515
};
1616

17+
CSSVariablesCollectorPlugin.getResolvedUrl = function({rawUrl, filename}) {
18+
if (filename.startsWith("/resources/")) {
19+
if (rawUrl.startsWith("/")) {
20+
return rawUrl;
21+
}
22+
const baseUrl = "ui5://" + filename.substr("/resources/".length);
23+
const url = new URL(rawUrl, baseUrl);
24+
return url.href;
25+
} else {
26+
// TODO: Check whether and when this can happen (except within test cases)
27+
return null;
28+
}
29+
},
30+
1731
CSSVariablesCollectorPlugin.prototype = {
1832

1933
isPreEvalVisitor: true,
@@ -37,18 +51,6 @@ CSSVariablesCollectorPlugin.prototype = {
3751
return !this._isInMixinOrParen() && this._isVarInRule();
3852
},
3953

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-
5254
toLessVariables() {
5355
let lessVariables = "";
5456
Object.keys(this.vars).forEach((value, index) => {
@@ -214,12 +216,18 @@ CSSVariablesCollectorPlugin.prototype = {
214216
// Create additional _asResolvedUrl variable for runtime resolution of relative urls
215217
const urlMatch = /url[\s]*\('?"?([^'")]*)'?"?\)/.exec(variableEntry.css);
216218
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;
219+
const resolvedUrl = CSSVariablesCollectorPlugin.getResolvedUrl({
220+
rawUrl: urlMatch[1],
221+
filename: value.currentFileInfo.filename
222+
});
223+
if (resolvedUrl) {
224+
const resolvedUrlVariableName = `${variableName}__asResolvedUrl`;
225+
const resolvedUrlVariableEntry = {
226+
css: `"${resolvedUrl}"`,
227+
export: true
228+
};
229+
this.vars[resolvedUrlVariableName] = resolvedUrlVariableEntry;
230+
}
223231
}
224232
}
225233
}

test/expected/inlineCssVariables/lib3/my/other/ui/lib/themes/base/library-RTL.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
:root {
3+
--sapUiTheme-my-other-ui-lib: true;
4+
--sapThemeMetaData-UI5-my-other-ui-lib: {"Path":"UI5.my/other/ui/lib.<theme-name>.library","PathPattern":"/%frameworkId%/%libId%/themes/%themeId%/%fileId%.css","Extends":["base"],"Scopes":[],"Engine":{"Version":"0.11.1","Name":"less-openui5"},"Version":{"Build":"<TODO>","Source":"<TODO>"}};
5+
}
16
:root {
27
--_my_other_ui_lib_MyControl_color1: var(--color1);
38
--_my_other_ui_lib_MyOtherControl_color1: var(--color1);

test/expected/inlineCssVariables/lib3/my/other/ui/lib/themes/base/library.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
:root {
3+
--sapUiTheme-my-other-ui-lib: true;
4+
--sapThemeMetaData-UI5-my-other-ui-lib: {"Path":"UI5.my/other/ui/lib.<theme-name>.library","PathPattern":"/%frameworkId%/%libId%/themes/%themeId%/%fileId%.css","Extends":["base"],"Scopes":[],"Engine":{"Version":"0.11.1","Name":"less-openui5"},"Version":{"Build":"<TODO>","Source":"<TODO>"}};
5+
}
16
:root {
27
--_my_other_ui_lib_MyControl_color1: var(--color1);
38
--_my_other_ui_lib_MyOtherControl_color1: var(--color1);

0 commit comments

Comments
 (0)