Skip to content

Commit

Permalink
Enhance url resolving / update expected build output
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Jun 18, 2021
1 parent 09f47f0 commit c3acd6f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
44 changes: 26 additions & 18 deletions lib/plugin/css-variables-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ const CSSVariablesCollectorPlugin = module.exports = function(config) {
this.importStack = [];
};

CSSVariablesCollectorPlugin.getResolvedUrl = function({rawUrl, filename}) {
if (filename.startsWith("/resources/")) {
if (rawUrl.startsWith("/")) {
return rawUrl;
}
const baseUrl = "ui5://" + filename.substr("/resources/".length);
const url = new URL(rawUrl, baseUrl);
return url.href;
} else {
// TODO: Check whether and when this can happen (except within test cases)
return null;
}
},

CSSVariablesCollectorPlugin.prototype = {

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

_getResolvedUrl(rawUrl) {
const parsedUrl = require("url").parse(rawUrl);
if (parsedUrl.protocol || rawUrl.startsWith("/")) {
return rawUrl;
}
let relativePath = parsedUrl.path;
if (relativePath.startsWith("./")) {
relativePath = relativePath.substr(2);
}
return `ui5://TODO-namespace/themes/TODO-themeName/${relativePath}`;
},

toLessVariables() {
let lessVariables = "";
Object.keys(this.vars).forEach((value, index) => {
Expand Down Expand Up @@ -214,12 +216,18 @@ CSSVariablesCollectorPlugin.prototype = {
// Create additional _asResolvedUrl variable for runtime resolution of relative urls
const urlMatch = /url[\s]*\('?"?([^'")]*)'?"?\)/.exec(variableEntry.css);
if (urlMatch) {
const resolvedUrlVariableName = `${variableName}__asResolvedUrl`;
const resolvedUrlVariableEntry = {
css: `"${this._getResolvedUrl(urlMatch[1])}"`,
export: true
};
this.vars[resolvedUrlVariableName] = resolvedUrlVariableEntry;
const resolvedUrl = CSSVariablesCollectorPlugin.getResolvedUrl({
rawUrl: urlMatch[1],
filename: value.currentFileInfo.filename
});
if (resolvedUrl) {
const resolvedUrlVariableName = `${variableName}__asResolvedUrl`;
const resolvedUrlVariableEntry = {
css: `"${resolvedUrl}"`,
export: true
};
this.vars[resolvedUrlVariableName] = resolvedUrlVariableEntry;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

:root {
--sapUiTheme-my-other-ui-lib: true;
--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>"}};
}
:root {
--_my_other_ui_lib_MyControl_color1: var(--color1);
--_my_other_ui_lib_MyOtherControl_color1: var(--color1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

:root {
--sapUiTheme-my-other-ui-lib: true;
--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>"}};
}
:root {
--_my_other_ui_lib_MyControl_color1: var(--color1);
--_my_other_ui_lib_MyOtherControl_color1: var(--color1);
Expand Down

0 comments on commit c3acd6f

Please sign in to comment.