Skip to content

Commit 4e5807d

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

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-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);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-env mocha */
2+
"use strict";
3+
4+
const assert = require("assert");
5+
6+
// tested module
7+
const CSSVariablesCollectorPlugin = require("../lib/plugin/css-variables-collector");
8+
9+
describe("CSSVariablesCollectorPlugin.getResolvedUrl", function() {
10+
it("should resolve relative url to ui5:// url", function() {
11+
assert.strictEqual(
12+
CSSVariablesCollectorPlugin.getResolvedUrl({
13+
rawUrl: "img/foo.png",
14+
filename: "/resources/sap/ui/foo/themes/base/Foo.less"
15+
}),
16+
"ui5://sap/ui/foo/themes/base/img/foo.png"
17+
);
18+
});
19+
it("should return server-absolute url as-is", function() {
20+
assert.strictEqual(
21+
CSSVariablesCollectorPlugin.getResolvedUrl({
22+
rawUrl: "/assets/img/foo.png",
23+
filename: "/resources/sap/ui/foo/themes/base/Foo.less"
24+
}),
25+
"/assets/img/foo.png"
26+
);
27+
});
28+
it("should return absolute http url as-is", function() {
29+
assert.strictEqual(
30+
CSSVariablesCollectorPlugin.getResolvedUrl({
31+
rawUrl: "http://example.com/assets/img/foo.png",
32+
filename: "/resources/sap/ui/foo/themes/base/Foo.less"
33+
}),
34+
"http://example.com/assets/img/foo.png"
35+
);
36+
});
37+
});

0 commit comments

Comments
 (0)