Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/base/lib/generate-styles/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import fs from 'fs/promises';
import path from "path";
import CleanCSS from "clean-css";
import { processComponentPackageFile } from '@ui5/webcomponents-tools/lib/css-processors/css-processor-themes.mjs';


const generate = async () => {
const packageJSON = JSON.parse(await fs.readFile("./package.json"))
await fs.mkdir("src/generated/css/", { recursive: true });

const files = (await fs.readdir("src/css/")).filter(file => file.endsWith(".css"));
const filesPromises = files.map(async file => {
let content = await fs.readFile(path.join("src/css/", file));
const filePath = path.join("src/css/", file);
let content = await fs.readFile(filePath);
const res = new CleanCSS().minify(`${content}`);
content = `export default \`${res.styles}\`;`;

// Scope used variables
content = await processComponentPackageFile({ text: res.styles, path: filePath }, packageJSON);

content = `export default \`${content}\`;`;

return fs.writeFile(path.join("src/generated/css/", `${file}.ts`), content);
});

Expand Down
18 changes: 5 additions & 13 deletions packages/base/src/theming/applyTheme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getThemeProperties, getRegisteredPackages, isThemeRegistered } from "../asset-registries/Themes.js";
import { removeStyle, createOrUpdateStyle } from "../ManagedStyles.js";
import { createOrUpdateStyle } from "../ManagedStyles.js";
import getThemeDesignerTheme from "./getThemeDesignerTheme.js";
import { fireThemeLoaded } from "./ThemeLoaded.js";
import { getFeature } from "../FeaturesRegistry.js";
Expand Down Expand Up @@ -31,10 +31,6 @@ const loadThemeBase = async (theme: string) => {
}
};

const deleteThemeBase = () => {
removeStyle("data-ui5-theme-properties", BASE_THEME_PACKAGE);
};

const loadComponentPackages = async (theme: string, externalThemeName?: string) => {
const registeredPackages = getRegisteredPackages();

Expand Down Expand Up @@ -79,16 +75,12 @@ const detectExternalTheme = async (theme: string) => {
const applyTheme = async (theme: string) => {
const extTheme = await detectExternalTheme(theme);

// Only load theme_base properties if there is no externally loaded theme, or there is, but it is not being loaded
if (!extTheme || theme !== extTheme.themeName) {
await loadThemeBase(theme);
} else {
deleteThemeBase();
}

// Always load component packages properties. For non-registered themes, try with the base theme, if any
const packagesTheme = isThemeRegistered(theme) ? theme : extTheme && extTheme.baseThemeName;
await loadComponentPackages(packagesTheme || DEFAULT_THEME, extTheme && extTheme.themeName === theme ? theme : undefined);
const effectiveTheme = packagesTheme || DEFAULT_THEME;

await loadThemeBase(effectiveTheme);
await loadComponentPackages(effectiveTheme, extTheme && extTheme.themeName === theme ? theme : undefined);

fireThemeLoaded(theme);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from "fs";
import * as path from "path";
import { writeFile, mkdir } from "fs/promises";
import chokidar from "chokidar";
import scopeVariables from "./scope-variables.mjs";
import {scopeUi5Variables} from "./scope-variables.mjs";
import { writeFileIfChanged, getFileContent } from "./shared.mjs";

const generate = async (argv) => {
Expand All @@ -23,7 +23,7 @@ const generate = async (argv) => {
build.onEnd(result => {
result.outputFiles.forEach(async f => {
// scoping
let newText = scopeVariables(f.text, packageJSON);
let newText = scopeUi5Variables(f.text, packageJSON);
newText = newText.replaceAll(/\\/g, "\\\\"); // Escape backslashes as they might appear in css rules
await mkdir(path.dirname(f.path), { recursive: true });
writeFile(f.path, newText);
Expand Down
66 changes: 40 additions & 26 deletions packages/tools/lib/css-processors/css-processor-themes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,42 @@ import { writeFile, mkdir } from "fs/promises";
import postcss from "postcss";
import combineDuplicatedSelectors from "../postcss-combine-duplicated-selectors/index.js"
import { writeFileIfChanged, getFileContent } from "./shared.mjs";
import scopeVariables from "./scope-variables.mjs";
import { scopeUi5Variables, scopeThemingVariables } from "./scope-variables.mjs";

const generate = async (argv) => {
async function processThemingPackageFile(f) {
const selector = ':root';
const newRule = postcss.rule({ selector });
const result = await postcss().process(f.text);

result.root.walkRules(selector, rule => {
for (const decl of rule.nodes) {
if (decl.type !== 'decl' || !decl.prop.startsWith('--sapFontUrl')) {
continue;
} else if (!decl.prop.startsWith('--sap')) {
newRule.append(decl.clone());
} else {
const originalProp = decl.prop;
const originalValue = decl.value;

newRule.append(decl.clone({ prop: originalProp.replace("--sap", "--ui5-sap"), value: `var(${originalProp}, ${originalValue})` }));
}
}
});

return newRule.toString();
};

async function processComponentPackageFile(f, packageJSON) {
let result = await postcss(combineDuplicatedSelectors).process(f.text);

result = scopeUi5Variables(result.css, packageJSON, f.path);

result = scopeThemingVariables(result);

return result;
}

async function generate(argv) {
const tsMode = process.env.UI5_TS === "true";
const extension = tsMode ? ".css.ts" : ".css.js";

Expand All @@ -19,37 +52,14 @@ const generate = async (argv) => {
]);
const restArgs = argv.slice(2);

const processThemingPackageFile = async (f) => {
const selector = ':root';
const result = await postcss().process(f.text);

const newRule = postcss.rule({ selector });

result.root.walkRules(selector, rule => {
rule.walkDecls(decl => {
if (!decl.prop.startsWith('--sapFontUrl')) {
newRule.append(decl.clone());
}
});
});

return newRule.toString();
};

const processComponentPackageFile = async (f) => {
const result = await postcss(combineDuplicatedSelectors).process(f.text);

return scopeVariables(result.css, packageJSON, f.path);
}

let scopingPlugin = {
name: 'scoping',
setup(build) {
build.initialOptions.write = false;

build.onEnd(result => {
result.outputFiles.forEach(async f => {
let newText = f.path.includes("packages/theming") ? await processThemingPackageFile(f) : await processComponentPackageFile(f);
let newText = f.path.includes("packages/theming") ? await processThemingPackageFile(f) : await processComponentPackageFile(f, packageJSON);

await mkdir(path.dirname(f.path), { recursive: true });
writeFile(f.path, newText);
Expand Down Expand Up @@ -95,4 +105,8 @@ if (import.meta.url === `file://${process.argv[1]}`) {

export default {
_ui5mainFn: generate
}

export {
processComponentPackageFile
}
25 changes: 16 additions & 9 deletions packages/tools/lib/css-processors/scope-variables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const require = createRequire(import.meta.url);
* @returns
*/
const getOverrideVersion = filePath => {
if (!filePath) {
return;
}
if (!filePath) {
return;
}

if (!filePath.includes(`overrides${path.sep}`)) {
return; // The "overrides/" directory is the marker
Expand All @@ -36,14 +36,21 @@ const getOverrideVersion = filePath => {
return overrideVersion;
}

const scopeVariables = (cssText, packageJSON, inputFile) => {
const escapeVersion = version => "v" + version?.replaceAll(/[^0-9A-Za-z\-_]/g, "-");
const versionStr = escapeVersion(getOverrideVersion(inputFile) || packageJSON.version);
const scopeUi5Variables = (cssText, packageJSON, inputFile) => {
const escapeVersion = version => "v" + version?.replaceAll(/[^0-9A-Za-z\-_]/g, "-");
const versionStr = escapeVersion(getOverrideVersion(inputFile) || packageJSON.version);
const expr = /(--_?ui5)([^\,\:\)\s]+)/g;
let newText = cssText.replaceAll(expr, `$1-${versionStr}$2`);

const expr = /(--_?ui5)([^\,\:\)\s]+)/g;
return newText.replaceAll("--sap", `--ui5-sap`);
}

return cssText.replaceAll(expr, `$1-${versionStr}$2`);
const scopeThemingVariables = (cssText) => {
return cssText.replaceAll("--sap", `--ui5-sap`);
}

export default scopeVariables;
export {
scopeUi5Variables,
scopeThemingVariables,
};

Loading