Skip to content

Commit 2883d89

Browse files
authored
refactor(base): improve bundle size (#161)
- configuration set from url or json script - locale is required for all controls that need it, not on central level as it used to be - new module LocaleProvider is added - it returns the set locale from the configuration or locale based on browser - all compatibility core-shim and jquery levels are moved to Calendar and DatePicker
1 parent 18a3c43 commit 2883d89

File tree

22 files changed

+110
-152
lines changed

22 files changed

+110
-152
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import "./shims/Core-shim";
2-
import "./shims/jquery-shim";
3-
41
import whenDOMReady from "./util/whenDOMReady";
52
import EventEnrichment from "./events/EventEnrichment";
63
import { insertIconFontFace } from "./IconFonts";
Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
import { isAndroid } from "@ui5/webcomponents-core/dist/sap/ui/Device";
21
import CalendarType from "@ui5/webcomponents-core/dist/sap/ui/core/CalendarType";
3-
import Locale from "./Locale";
4-
import {
5-
setConfiguration,
6-
getFormatLocale,
7-
getLegacyDateFormat,
8-
getLegacyDateCalendarCustomizing,
9-
getCustomLocaleData,
10-
} from "./FormatSettings";
11-
12-
let LocaleData;
132

143
const getDesigntimePropertyAsArray = sValue => {
154
const m = /\$([-a-z0-9A-Z._]+)(?::([^$]*))?\$/.exec(sValue);
@@ -18,32 +7,10 @@ const getDesigntimePropertyAsArray = sValue => {
187

198
const supportedLanguages = getDesigntimePropertyAsArray("$core-i18n-locales:,ar,bg,ca,cs,da,de,el,en,es,et,fi,fr,hi,hr,hu,it,iw,ja,ko,lt,lv,nl,no,pl,pt,ro,ru,sh,sk,sl,sv,th,tr,uk,vi,zh_CN,zh_TW$");
209

21-
const detectLanguage = () => {
22-
const browserLanguages = navigator.languages;
23-
24-
const navigatorLanguage = () => {
25-
if (isAndroid()) {
26-
// on Android, navigator.language is hardcoded to 'en', so check UserAgent string instead
27-
const match = navigator.userAgent.match(/\s([a-z]{2}-[a-z]{2})[;)]/i);
28-
if (match) {
29-
return match[1];
30-
}
31-
// okay, we couldn't find a language setting. It might be better to fallback to 'en' instead of having no language
32-
}
33-
return navigator.language;
34-
};
35-
36-
const rawLocale = (browserLanguages && browserLanguages[0]) || navigatorLanguage() || navigator.userLanguage || navigator.browserLanguage;
37-
38-
return rawLocale || "en";
39-
};
40-
41-
const language = detectLanguage();
42-
4310
const CONFIGURATION = {
4411
theme: "sap_fiori_3",
4512
rtl: null,
46-
language: new Locale(language),
13+
language: null,
4714
compactSize: false,
4815
supportedLanguages,
4916
calendarType: null,
@@ -52,8 +19,6 @@ const CONFIGURATION = {
5219
"xx-wc-no-conflict": false, // no URL
5320
};
5421

55-
setConfiguration(CONFIGURATION);
56-
5722
/* General settings */
5823
const getTheme = () => {
5924
return CONFIGURATION.theme;
@@ -64,7 +29,7 @@ const getRTL = () => {
6429
};
6530

6631
const getLanguage = () => {
67-
return CONFIGURATION.language.sLocaleId;
32+
return CONFIGURATION.language;
6833
};
6934

7035
const getCompactSize = () => {
@@ -94,13 +59,6 @@ const getCalendarType = () => {
9459
}
9560
}
9661

97-
/* In order to have a locale based calendar type - LocaleData should be injected to the configuration
98-
- check #injectLocaleData
99-
*/
100-
if (LocaleData) {
101-
return LocaleData.getInstance(getLocale()).getPreferredCalendarType();
102-
}
103-
10462
return CalendarType.Gregorian;
10563
};
10664

@@ -110,15 +68,6 @@ const getLocale = () => {
11068
return CONFIGURATION.language;
11169
};
11270

113-
const getFormatSettings = () => {
114-
return {
115-
getFormatLocale,
116-
getLegacyDateFormat,
117-
getLegacyDateCalendarCustomizing,
118-
getCustomLocaleData,
119-
};
120-
};
121-
12271
const _setTheme = themeName => {
12372
CONFIGURATION.theme = themeName;
12473
};
@@ -130,39 +79,6 @@ const booleanMapping = new Map([
13079

13180
let runtimeConfig = {};
13281

133-
const convertToLocaleOrNull = lang => {
134-
try {
135-
if (lang && typeof lang === "string") {
136-
return new Locale(lang);
137-
}
138-
} catch (e) {
139-
// ignore
140-
}
141-
};
142-
143-
const check = (condition, sMessage) => {
144-
if (!condition) {
145-
throw new Error(sMessage);
146-
}
147-
};
148-
149-
const getLanguageTag = () => {
150-
return CONFIGURATION.language.toString();
151-
};
152-
153-
const setLanguage = newLanguage => {
154-
const locale = convertToLocaleOrNull(newLanguage);
155-
156-
check(locale, "Configuration.setLanguage: newLanguage must be a valid BCP47 language tag");
157-
158-
if (locale.toString() !== getLanguageTag()) {
159-
CONFIGURATION.language = locale;
160-
CONFIGURATION.derivedRTL = Locale._impliesRTL(locale);
161-
}
162-
163-
return CONFIGURATION;
164-
};
165-
16682
const parseConfigurationScript = () => {
16783
const configScript = document.querySelector("[data-id='sap-ui-config']");
16884
let configJSON;
@@ -202,18 +118,10 @@ const parseURLParameters = () => {
202118

203119
const applyConfigurations = () => {
204120
Object.keys(runtimeConfig).forEach(key => {
205-
if (key === "language") {
206-
setLanguage(runtimeConfig[key]);
207-
} else {
208-
CONFIGURATION[key] = runtimeConfig[key];
209-
}
121+
CONFIGURATION[key] = runtimeConfig[key];
210122
});
211123
};
212124

213-
const injectLocaleData = localeData => {
214-
LocaleData = localeData;
215-
};
216-
217125
parseConfigurationScript();
218126
parseURLParameters();
219127
applyConfigurations();
@@ -227,9 +135,7 @@ export {
227135
getWCNoConflict,
228136
getCalendarType,
229137
getLocale,
230-
getFormatSettings,
231138
_setTheme,
232139
getSupportedLanguages,
233140
getOriginInfo,
234-
injectLocaleData,
235141
};

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Locale from "./Locale";
2+
import { getLocale } from "./LocaleProvider";
23

34
const mSettings = {};
45

56
const getFormatLocale = () => {
67
const fallback = () => {
7-
let oLocale = SETTINGS.configuration.language;
8+
let oLocale = getLocale();
89
// if any user settings have been defined, add the private use subtag "sapufmt"
910
if (!Object.keys(mSettings).length === 0) {
1011
// TODO move to Locale/LocaleData
@@ -19,7 +20,8 @@ const getFormatLocale = () => {
1920
return oLocale;
2021
};
2122

22-
return SETTINGS.configuration.formatLocale || fallback();
23+
// we do not support setting of locale, so we just leave the default behaviour
24+
return fallback();
2325
};
2426

2527
const SETTINGS = {

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

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Locale from "./Locale";
2+
import { getLanguage as getConfigLanguage } from "./Configuration";
3+
4+
const convertToLocaleOrNull = lang => {
5+
try {
6+
if (lang && typeof lang === "string") {
7+
return new Locale(lang);
8+
}
9+
} catch (e) {
10+
// ignore
11+
}
12+
};
13+
14+
/**
15+
* Detects the language based on locale of the browser
16+
*/
17+
const detectLanguage = () => {
18+
const browserLanguages = navigator.languages;
19+
20+
const navigatorLanguage = () => {
21+
return navigator.language;
22+
};
23+
24+
const rawLocale = (browserLanguages && browserLanguages[0]) || navigatorLanguage() || navigator.userLanguage || navigator.browserLanguage;
25+
26+
return rawLocale || "en";
27+
};
28+
29+
/**
30+
* Returns the locale based on the configured language Configuration#getLanguage
31+
* If no language has been configured - a new locale based on browser language is returned
32+
*/
33+
const getLocale = () => {
34+
if (getConfigLanguage()) {
35+
return new Locale(getConfigLanguage());
36+
}
37+
38+
return convertToLocaleOrNull(detectLanguage());
39+
};
40+
41+
/**
42+
* Returns the language of #getLocale return value
43+
*/
44+
const getLanguage = () => {
45+
return getLocale().sLanguage;
46+
};
47+
48+
export { getLocale, getLanguage };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ResourceBundle from "@ui5/webcomponents-core/dist/sap/base/i18n/ResourceBundle";
2-
import { getLanguage } from "./Configuration";
2+
import { getLanguage } from "./LocaleProvider";
33
import { registerModuleContent } from "./ResourceLoaderOverrides";
44
import { fetchJsonOnce } from "./util/FetchHelper";
55

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const resources = new Map();
55
// date formatters from the core do not know about this new mechanism of fetching assets,
66
// but we can use the sap.ui.loader._.getModuleContent as a hook and provide the preloaded data,
77
// so that a sync request via jQuery is never triggered.
8+
window.sap = window.sap || {};
9+
window.sap.ui = window.sap.ui || {};
10+
811
sap.ui.loader = sap.ui.loader || {};
912
sap.ui.loader._ = sap.ui.loader._ || {};
1013
const getModulecontentOrig = sap.ui.loader._.getModuleContent;

packages/base/src/sap/ui/webcomponents/base/dates/CalendarUtils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import "../LocaleDataConfigurationProvider";
21
import UniversalDate from "@ui5/webcomponents-core/dist/sap/ui/core/date/UniversalDate";
32
import Locale from "@ui5/webcomponents-core/dist/sap/ui/core/Locale";
43
import LocaleData from "@ui5/webcomponents-core/dist/sap/ui/core/LocaleData";

packages/base/src/sap/ui/webcomponents/base/shims/Core-shim.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { inject as injectCore } from "@ui5/webcomponents-core/dist/sap/ui/core/Core";
22
import * as Configuration from "../Configuration";
3+
import * as FormatSettings from "../FormatSettings";
34

45
/**
56
* Shim for the OpenUI5 core
67
* @deprecated - do not add new functionality
78
*/
9+
810
const Core = {
911
/**
1012
* @deprecated - must be here for compatibility
@@ -18,13 +20,14 @@ const Core = {
1820
*/
1921
getLibraryResourceBundle() {
2022
},
23+
24+
getFormatSettings() {
25+
return FormatSettings;
26+
},
2127
};
2228

23-
if (!window.sap) {
24-
window.sap = {
25-
ui: {},
26-
};
27-
}
29+
window.sap = window.sap || {};
30+
window.sap.ui = window.sap.ui || {};
2831

2932
/**
3033
* @deprecated

packages/base/src/sap/ui/webcomponents/base/shims/jquery-shim.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ var jQuery = {
7171
// Return the modified object
7272
return target;
7373
},
74-
isEmptyObject: function(obj) {
75-
return Object.keys(obj).length === 0;
76-
},
7774
ajaxSettings: {
7875
converters: {
7976
"text json": (data) => JSON.parse( data + "" )

0 commit comments

Comments
 (0)