Skip to content

Commit 0e7a9bb

Browse files
pskelinvladitasev
authored andcommitted
refactor: reduce bundle size (#43)
* refactor: reduce bundle size - strip down Device.js by leaving only browser, os, system and touch objects - remove default locale fallback from LocaleData.js as we always fetch it separately - add setupSystem, use it for both html and shadow root spans - 8 KB minzip reduction
1 parent a30577e commit 0e7a9bb

File tree

10 files changed

+1064
-1331
lines changed

10 files changed

+1064
-1331
lines changed

packages/base/src/sap/ui/webcomponents/base/Core.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ShadowDOM from './compatibility/ShadowDOM';
22
import whenDOMReady from './util/whenDOMReady';
33
import setupBrowser from './util/setupBrowser';
44
import setupOS from './util/setupOS';
5+
import setupSystem from './util/setupSystem';
56
import configuration from "./Configuration";
67
import { inject as injectCore } from "@ui5/webcomponents-core/dist/sap/ui/core/Core";
78
import "./jquery-shim";
@@ -66,8 +67,9 @@ const Core = {
6667
ShadowDOM.setWebComponentRootOnHTML();
6768
attachThemeChange(ShadowDOM._applyTheme);
6869

69-
setupBrowser();
70-
setupOS();
70+
setupBrowser(document.documentElement);
71+
setupOS(document.documentElement);
72+
setupSystem(document.documentElement);
7173

7274
IconFonts.load();
7375
DOMEventHandler.start();
@@ -86,4 +88,4 @@ window.sap.ui.getWCCore = function() {
8688
};
8789

8890
injectCore(Core);
89-
export default Core;
91+
export default Core;

packages/base/src/sap/ui/webcomponents/base/compatibility/ShadowDOM.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import WCPolyfill from '../thirdparty/webcomponents-polyfill';
22
import configuration from "../Configuration";
33
import { fetchThemeBundle } from "../ThemeBundle";
4+
import setupBrowser from '../util/setupBrowser';
5+
import setupOS from '../util/setupOS';
6+
import setupSystem from '../util/setupSystem';
47

58
// Shorthands
69
const d = document;
@@ -88,8 +91,9 @@ class ShadowDOM {
8891
shadowDOM = template.content.cloneNode(true);
8992

9093
rootSpan = shadowDOM.querySelector("span[data-sap-ui-wc-placeholder]");
91-
rootSpan.setAttribute("data-sap-ui-browser", document.documentElement.getAttribute("data-sap-ui-browser"));
92-
rootSpan.setAttribute("data-sap-ui-os", document.documentElement.getAttribute("data-sap-ui-os"));
94+
setupBrowser(rootSpan);
95+
setupOS(rootSpan);
96+
setupSystem(rootSpan);
9397
}
9498

9599
if (isCompact) {
@@ -264,4 +268,4 @@ class ShadowDOM {
264268
}
265269
}
266270

267-
export default ShadowDOM;
271+
export default ShadowDOM;
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import Device from "@ui5/webcomponents-core/dist/sap/ui/Device";
22

3-
const setupBrowser = () => {
4-
var html = document.documentElement;
5-
6-
var b = Device.browser;
7-
var id = b.name;
3+
const setupBrowser = (node) => {
4+
const b = Device.browser;
5+
let id = b.name;
86

97
if (id) {
108
if (id === b.BROWSER.SAFARI && b.mobile) {
119
id = "m" + id;
1210
}
1311
id = id + (b.version === -1 ? "" : Math.floor(b.version));
14-
html.dataset.sapUiBrowser = id;
12+
node.dataset.sapUiBrowser = id;
1513
}
1614
};
1715

18-
export default setupBrowser;
16+
export default setupBrowser;

packages/base/src/sap/ui/webcomponents/base/util/setupOS.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import Device from "@ui5/webcomponents-core/dist/sap/ui/Device";
22

3-
const setupOS = () => {
4-
var html = document.documentElement;
3+
const setupOS = (node) => {
4+
node.dataset.sapUiOs = Device.os.name + Device.os.versionStr;
55

6-
html.dataset.sapUiOs = Device.os.name + Device.os.versionStr;
7-
8-
var osCSS = null;
6+
let osCSS = null;
97
switch (Device.os.name) {
108
case Device.os.OS.IOS:
119
osCSS = "sap-ios";
@@ -21,8 +19,8 @@ const setupOS = () => {
2119
break;
2220
}
2321
if (osCSS) {
24-
html.classList.add(osCSS);
22+
node.classList.add(osCSS);
2523
}
2624
};
2725

28-
export default setupOS;
26+
export default setupOS;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Device from "@ui5/webcomponents-core/dist/sap/ui/Device";
2+
3+
const setupSystem = (node) => {
4+
const sysTypes = Object.entries(Device.system.SYSTEMTYPE).map(([_key, value]) => value);
5+
6+
node.classList.remove(...sysTypes);
7+
sysTypes.forEach(sysType => {
8+
if (Device.system[sysType]) {
9+
node.classList.add("sap-" + sysType);
10+
}
11+
});
12+
};
13+
14+
export default setupSystem;

0 commit comments

Comments
 (0)