Skip to content

Commit b0d5425

Browse files
committed
Update and fix getResolvedUrl
1 parent 4e5807d commit b0d5425

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

lib/plugin/css-variables-collector.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ 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)
17+
CSSVariablesCollectorPlugin.getResolvedUrl = function({rawUrl, filename, libraryName}) {
18+
if (!libraryName) {
19+
return null;
20+
}
21+
const libraryNamespace = libraryName.replace(/\./g, "/");
22+
const namespaceIndex = filename.indexOf(libraryNamespace);
23+
if (namespaceIndex === -1) {
24+
// TODO: log warning/error?
2725
return null;
2826
}
27+
if (rawUrl.startsWith("/")) {
28+
return rawUrl;
29+
}
30+
const baseUi5Url = "ui5://" + filename.substr(namespaceIndex);
31+
// rawUrl will be resolved against ui5:// path if it is not absolute
32+
// An absolute rawUrl will be returned as-is
33+
const url = new URL(rawUrl, baseUi5Url);
34+
return url.href;
2935
},
3036

3137
CSSVariablesCollectorPlugin.prototype = {
@@ -218,7 +224,8 @@ CSSVariablesCollectorPlugin.prototype = {
218224
if (urlMatch) {
219225
const resolvedUrl = CSSVariablesCollectorPlugin.getResolvedUrl({
220226
rawUrl: urlMatch[1],
221-
filename: value.currentFileInfo.filename
227+
filename: value.currentFileInfo.filename,
228+
libraryName: this.config.libName
222229
});
223230
if (resolvedUrl) {
224231
const resolvedUrlVariableName = `${variableName}__asResolvedUrl`;

test/test-css-variables-collector-plugin.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,32 @@ const assert = require("assert");
77
const CSSVariablesCollectorPlugin = require("../lib/plugin/css-variables-collector");
88

99
describe("CSSVariablesCollectorPlugin.getResolvedUrl", function() {
10-
it("should resolve relative url to ui5:// url", function() {
10+
it("should resolve relative url to ui5:// url (filename with /resources/ - UI5 Tooling)", function() {
1111
assert.strictEqual(
1212
CSSVariablesCollectorPlugin.getResolvedUrl({
1313
rawUrl: "img/foo.png",
14-
filename: "/resources/sap/ui/foo/themes/base/Foo.less"
14+
filename: "/resources/sap/ui/foo/themes/base/Foo.less",
15+
libraryName: "sap.ui.foo"
16+
}),
17+
"ui5://sap/ui/foo/themes/base/img/foo.png"
18+
);
19+
});
20+
it("should resolve relative url to ui5:// url (filename with / - grunt-openui5/connect-openui5)", function() {
21+
assert.strictEqual(
22+
CSSVariablesCollectorPlugin.getResolvedUrl({
23+
rawUrl: "img/foo.png",
24+
filename: "/sap/ui/foo/themes/base/Foo.less",
25+
libraryName: "sap.ui.foo"
26+
}),
27+
"ui5://sap/ui/foo/themes/base/img/foo.png"
28+
);
29+
});
30+
it("should resolve relative url to ui5:// url (filename absolute fs path - custom usage)", function() {
31+
assert.strictEqual(
32+
CSSVariablesCollectorPlugin.getResolvedUrl({
33+
rawUrl: "img/foo.png",
34+
filename: "/Users/root/sap/ui/foo/themes/base/Foo.less",
35+
libraryName: "sap.ui.foo"
1536
}),
1637
"ui5://sap/ui/foo/themes/base/img/foo.png"
1738
);
@@ -20,7 +41,8 @@ describe("CSSVariablesCollectorPlugin.getResolvedUrl", function() {
2041
assert.strictEqual(
2142
CSSVariablesCollectorPlugin.getResolvedUrl({
2243
rawUrl: "/assets/img/foo.png",
23-
filename: "/resources/sap/ui/foo/themes/base/Foo.less"
44+
filename: "/resources/sap/ui/foo/themes/base/Foo.less",
45+
libraryName: "sap.ui.foo"
2446
}),
2547
"/assets/img/foo.png"
2648
);
@@ -29,9 +51,20 @@ describe("CSSVariablesCollectorPlugin.getResolvedUrl", function() {
2951
assert.strictEqual(
3052
CSSVariablesCollectorPlugin.getResolvedUrl({
3153
rawUrl: "http://example.com/assets/img/foo.png",
32-
filename: "/resources/sap/ui/foo/themes/base/Foo.less"
54+
filename: "/resources/sap/ui/foo/themes/base/Foo.less",
55+
libraryName: "sap.ui.foo"
3356
}),
3457
"http://example.com/assets/img/foo.png"
3558
);
3659
});
60+
it("Error: should return null when library namespace is not part of filename", function() {
61+
assert.strictEqual(
62+
CSSVariablesCollectorPlugin.getResolvedUrl({
63+
rawUrl: "img/foo.png",
64+
filename: "/resources/sap/ui/foo/themes/base/Foo.less",
65+
libraryName: "sap.ui.bar"
66+
}),
67+
null
68+
);
69+
});
3770
});

0 commit comments

Comments
 (0)