Skip to content

Commit 3385623

Browse files
authored
Revert "feat(framework): scope theming css variables with component packages (#12491)" (#12775)
This reverts commit 43ff5de.
1 parent c9288ff commit 3385623

File tree

11 files changed

+71
-241
lines changed

11 files changed

+71
-241
lines changed

packages/base/lib/generate-styles/index.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
import fs from 'fs/promises';
22
import path from "path";
33
import CleanCSS from "clean-css";
4-
import { processComponentPackageFile } from '@ui5/webcomponents-tools/lib/css-processors/css-processor-themes.mjs';
54
import { pathToFileURL } from "url";
65

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

119
const files = (await fs.readdir("src/css/")).filter(file => file.endsWith(".css"));
1210
const filesPromises = files.map(async file => {
13-
const filePath = path.join("src/css/", file);
14-
let content = await fs.readFile(filePath);
11+
let content = await fs.readFile(path.join("src/css/", file));
1512
const res = new CleanCSS().minify(`${content}`);
16-
17-
// Scope used variables
18-
content = await processComponentPackageFile({ text: res.styles, path: filePath }, packageJSON);
19-
20-
content = `export default \`${content}\`;`;
21-
13+
content = `export default \`${res.styles}\`;`;
2214
return fs.writeFile(path.join("src/generated/css/", `${file}.ts`), content);
2315
});
2416

packages/base/src/theming/applyTheme.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { getThemeProperties, getRegisteredPackages, isThemeRegistered } from "../asset-registries/Themes.js";
2-
import { createOrUpdateStyle } from "../ManagedStyles.js";
2+
import { removeStyle, createOrUpdateStyle } from "../ManagedStyles.js";
33
import getThemeDesignerTheme from "./getThemeDesignerTheme.js";
44
import { fireThemeLoaded } from "./ThemeLoaded.js";
5+
import { getFeature } from "../FeaturesRegistry.js";
56
import { attachCustomThemeStylesToHead, getThemeRoot } from "../config/ThemeRoot.js";
7+
import type OpenUI5Support from "../features/OpenUI5Support.js";
68
import { DEFAULT_THEME } from "../generated/AssetParameters.js";
79
import { getCurrentRuntimeIndex } from "../Runtimes.js";
810

@@ -29,6 +31,10 @@ const loadThemeBase = async (theme: string) => {
2931
}
3032
};
3133

34+
const deleteThemeBase = () => {
35+
removeStyle("data-ui5-theme-properties", BASE_THEME_PACKAGE);
36+
};
37+
3238
const loadComponentPackages = async (theme: string, externalThemeName?: string) => {
3339
const registeredPackages = getRegisteredPackages();
3440

@@ -47,34 +53,42 @@ const loadComponentPackages = async (theme: string, externalThemeName?: string)
4753
};
4854

4955
const detectExternalTheme = async (theme: string) => {
50-
if (getThemeRoot()) {
51-
await attachCustomThemeStylesToHead(theme);
52-
}
53-
5456
// If theme designer theme is detected, use this
5557
const extTheme = getThemeDesignerTheme();
5658
if (extTheme) {
5759
return extTheme;
5860
}
61+
62+
// If OpenUI5Support is enabled, try to find out if it loaded variables
63+
const openUI5Support = getFeature<typeof OpenUI5Support>("OpenUI5Support");
64+
if (openUI5Support && openUI5Support.isOpenUI5Detected()) {
65+
const varsLoaded = openUI5Support.cssVariablesLoaded();
66+
if (varsLoaded) {
67+
return {
68+
themeName: openUI5Support.getConfigurationSettingsObject()?.theme, // just themeName
69+
baseThemeName: "", // baseThemeName is only relevant for custom themes
70+
};
71+
}
72+
} else if (getThemeRoot()) {
73+
await attachCustomThemeStylesToHead(theme);
74+
75+
return getThemeDesignerTheme();
76+
}
5977
};
6078

6179
const applyTheme = async (theme: string) => {
62-
// Detect external theme if available (e.g., from theme designer or custom theme root)
6380
const extTheme = await detectExternalTheme(theme);
6481

65-
// Determine which theme to use for component packages:
66-
// 1. If the requested theme is registered, use it directly
67-
// 2. If external theme exists, use its base theme (e.g., "my_custom_theme" extends "sap_fiori_3")
68-
// 3. Otherwise, fallback to the default theme
69-
const packagesTheme = isThemeRegistered(theme) ? theme : extTheme && extTheme.baseThemeName;
70-
const effectiveTheme = packagesTheme || DEFAULT_THEME;
71-
72-
// Load base theme properties
73-
await loadThemeBase(effectiveTheme);
82+
// Only load theme_base properties if there is no externally loaded theme, or there is, but it is not being loaded
83+
if (!extTheme || theme !== extTheme.themeName) {
84+
await loadThemeBase(theme);
85+
} else {
86+
deleteThemeBase();
87+
}
7488

75-
// Load component-specific theme properties
76-
// Pass external theme name only if it matches the requested theme to avoid conflicts
77-
await loadComponentPackages(effectiveTheme, extTheme && extTheme.themeName === theme ? theme : undefined);
89+
// Always load component packages properties. For non-registered themes, try with the base theme, if any
90+
const packagesTheme = isThemeRegistered(theme) ? theme : extTheme && extTheme.baseThemeName;
91+
await loadComponentPackages(packagesTheme || DEFAULT_THEME, extTheme && extTheme.themeName === theme ? theme : undefined);
7892

7993
fireThemeLoaded(theme);
8094
};

packages/main/test/pages/theming/Themes.html

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/main/test/pages/theming/Themes2.html

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/main/test/pages/theming/Themes3.html

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/main/test/pages/theming/Themes4.html

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/main/test/pages/theming/Themes5.html

Lines changed: 0 additions & 27 deletions
This file was deleted.

packages/main/test/pages/theming/Themes6.html

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/tools/lib/css-processors/css-processor-components.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fs from "fs";
44
import * as path from "path";
55
import { writeFile, mkdir } from "fs/promises";
66
import chokidar from "chokidar";
7-
import {scopeUi5Variables} from "./scope-variables.mjs";
7+
import scopeVariables from "./scope-variables.mjs";
88
import { writeFileIfChanged, getFileContent } from "./shared.mjs";
99
import { pathToFileURL } from "url";
1010

@@ -24,7 +24,7 @@ const generate = async (argv) => {
2424
build.onEnd(result => {
2525
result.outputFiles.forEach(async f => {
2626
// scoping
27-
let newText = scopeUi5Variables(f.text, packageJSON);
27+
let newText = scopeVariables(f.text, packageJSON);
2828
newText = newText.replaceAll(/\\/g, "\\\\"); // Escape backslashes as they might appear in css rules
2929
await mkdir(path.dirname(f.path), { recursive: true });
3030
writeFile(f.path, newText);

packages/tools/lib/css-processors/css-processor-themes.mjs

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,10 @@ import { writeFile, mkdir } from "fs/promises";
66
import postcss from "postcss";
77
import combineDuplicatedSelectors from "../postcss-combine-duplicated-selectors/index.js"
88
import { writeFileIfChanged, getFileContent } from "./shared.mjs";
9-
import { scopeUi5Variables, scopeThemingVariables } from "./scope-variables.mjs";
9+
import scopeVariables from "./scope-variables.mjs";
1010
import { pathToFileURL } from "url";
1111

12-
async function processThemingPackageFile(f) {
13-
const selector = ':root';
14-
const newRule = postcss.rule({ selector });
15-
const result = await postcss().process(f.text);
16-
17-
result.root.walkRules(selector, rule => {
18-
for (const decl of rule.nodes) {
19-
if (decl.type !== 'decl' ) {
20-
continue;
21-
} else if (decl.prop.startsWith('--sapFontUrl')) {
22-
continue;
23-
} else if (!decl.prop.startsWith('--sap')) {
24-
newRule.append(decl.clone());
25-
} else {
26-
const originalProp = decl.prop;
27-
const originalValue = decl.value;
28-
29-
newRule.append(decl.clone({ prop: originalProp.replace("--sap", "--ui5-sap"), value: `var(${originalProp}, ${originalValue})` }));
30-
}
31-
}
32-
});
33-
34-
return newRule.toString();
35-
};
36-
37-
async function processComponentPackageFile(f, packageJSON) {
38-
let result = await postcss(combineDuplicatedSelectors).process(f.text);
39-
40-
result = scopeUi5Variables(result.css, packageJSON, f.path);
41-
42-
result = scopeThemingVariables(result);
43-
44-
return result;
45-
}
46-
47-
async function generate(argv) {
12+
const generate = async (argv) => {
4813
const tsMode = process.env.UI5_TS === "true";
4914
const extension = tsMode ? ".css.ts" : ".css.js";
5015

@@ -55,14 +20,37 @@ async function generate(argv) {
5520
]);
5621
const restArgs = argv.slice(2);
5722

23+
const processThemingPackageFile = async (f) => {
24+
const selector = ':root';
25+
const result = await postcss().process(f.text);
26+
27+
const newRule = postcss.rule({ selector });
28+
29+
result.root.walkRules(selector, rule => {
30+
rule.walkDecls(decl => {
31+
if (!decl.prop.startsWith('--sapFontUrl')) {
32+
newRule.append(decl.clone());
33+
}
34+
});
35+
});
36+
37+
return newRule.toString();
38+
};
39+
40+
const processComponentPackageFile = async (f) => {
41+
const result = await postcss(combineDuplicatedSelectors).process(f.text);
42+
43+
return scopeVariables(result.css, packageJSON, f.path);
44+
}
45+
5846
let scopingPlugin = {
5947
name: 'scoping',
6048
setup(build) {
6149
build.initialOptions.write = false;
6250

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

6755
await mkdir(path.dirname(f.path), { recursive: true });
6856
writeFile(f.path, newText);
@@ -111,8 +99,4 @@ if (import.meta.url === fileUrl) {
11199

112100
export default {
113101
_ui5mainFn: generate
114-
}
115-
116-
export {
117-
processComponentPackageFile
118102
}

0 commit comments

Comments
 (0)