Skip to content

Commit

Permalink
chore: remove hard-coded
Browse files Browse the repository at this point in the history
  • Loading branch information
vladitasev committed Nov 21, 2023
1 parent f04291f commit 5917008
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/tools/components-package/nps.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const getScripts = (options) => {
styles: {
default: `nps build.styles.themes build.styles.components ${copySrcGenerated}`,
themes: `node "${LIB}/postcss-p/postcss-p.mjs"`,
themes_debug: "postcss src/**/parameters-bundle.css --config config/postcss.themes --base src --dir dist/css/",
components: "postcss src/themes/*.css --config config/postcss.components --base src --dir dist/css/", // When updating this, also update the new files script
},
i18n: {
Expand Down
40 changes: 32 additions & 8 deletions packages/tools/lib/postcss-scope-vars/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
const path = require("path");
const name = "postcss-scope-vars";

const escapeVersion = version => "v" + version?.replaceAll(/[^0-9A-Za-z\-_]/g, "-");

/**
* Tries to detect an override for a package
* @param {*} filePath For example: /my_project/src/themes/overrides/@ui5/webcomponents/my_custom_theme/parameters-bundle.css
* @returns
*/
const getOverrideVersion = filePath =>
console.log(filePath);
if (!filePath.includes(`overrides${path.sep}`)) {
return; // The "overrides/" directory is the marker
}
const override = filePath.split(`overrides${path.sep}`)[1]; // For example, this will be: @ui5/webcomponents/my_custom_theme/parameters-bundle.css
if (!override) {
return; // There must be other directories after overrides/, the path can't end with it
}
const parts = override.split(path.sep);
if (parts.length < 3) {
return; // There must be at least a directory for the theme that is being overridden (my_custom_theme) and the name of the CSS file after the name of the package that is overridden
}
const packageName = parts.slice(0, -2).join(path.sep); // After the last 2 parts are removed (my_custom_theme and parameters-bundle.css from the example), the rest is the package

let overrideVersion;
try {
overrideVersion = require(`${packageName}${path.sep}package.json`).version;
} catch (e) {
console.log(`Error requiring package ${packageName}: ${e.message}`);
}

return overrideVersion;
}

module.exports = (options) => {
return {
postcssPlugin: name,
prepare(opts) {
const filePath = opts.root.source.input.file;
let overrideVersion;
if (filePath.includes("overrides/@ui5/webcomponents-fiori")) {
overrideVersion = require("@ui5/webcomponents-fiori/package.json").version;
} else if (filePath.includes("overrides/@ui5/webcomponents")) {
overrideVersion = require("@ui5/webcomponents/package.json").version;
}

const versionStr = escapeVersion(overrideVersion || options?.version);
const versionStr = escapeVersion(getOverrideVersion(filePath) || options?.version);

return {
Declaration: (declaration) => {
Expand Down

0 comments on commit 5917008

Please sign in to comment.