@empathyco/x-archetype-utils 2.0.0-alpha.23 → 2.0.0-alpha.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +0,0 @@
1
- const rollupCssInjectorConfig = {
2
- replace: {
3
- // Replace X CSS injector by our custom one.
4
- 'export default injectCss': 'export default (css) => window.xCSSInjector.addStyle({ source: css });',
5
- delimiters: ['', ''],
6
- },
7
- styles: {
8
- mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`],
9
- },
10
- };
11
-
12
- export { rollupCssInjectorConfig };
@@ -1,24 +0,0 @@
1
- const webpackConfig = {
2
- transpileDependencies: ['@empathyco/x-components'],
3
- configureWebpack: {
4
- module: {
5
- rules: [
6
- {
7
- test: /\.ts|js|vue$/,
8
- enforce: 'pre',
9
- use: ['source-map-loader'],
10
- },
11
- ],
12
- },
13
- output: {
14
- devtoolModuleFilenameTemplate: ({ resourcePath, hash, query, }) => resourcePath.match(/\.vue$/) &&
15
- !query.match(/type=script/) &&
16
- !query.match(/\?rollup-plugin-vue=script.ts/)
17
- ? `webpack-generated:///${resourcePath}?${hash}`
18
- : `sources://${resourcePath}`,
19
- devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]',
20
- },
21
- },
22
- };
23
-
24
- export { webpackConfig };
@@ -1,91 +0,0 @@
1
- /** Singleton instance of the injector that will be used across all the initializations. */
2
- let instance = null;
3
- /**
4
- * Custom CSS injector that allows to inject styles into a host element.
5
- *
6
- * @public
7
- */
8
- class CssInjector {
9
- /**
10
- * Initializes the instance of the injector if it's not already initialized and sets it in the
11
- * window object if it's required.
12
- *
13
- * @param setInWindow - Whether to set the injector instance in the window object.
14
- */
15
- constructor(setInWindow = true) {
16
- this.hosts = new Set();
17
- this.stylesToAdopt = [];
18
- if (!(instance instanceof CssInjector)) {
19
- // eslint-disable-next-line ts/no-this-alias
20
- instance = this;
21
- }
22
- if (setInWindow) {
23
- this.setInWindow();
24
- }
25
- return instance;
26
- }
27
- /**
28
- * Adds the style to the host element.
29
- *
30
- * @param style - The styles to be added.
31
- * @param style.source - Styles source.
32
- */
33
- addStyle(style) {
34
- const sheet = new CSSStyleSheet();
35
- sheet.replaceSync(style.source);
36
- this.stylesToAdopt.push(sheet);
37
- this.hosts.forEach(host => (host.adoptedStyleSheets = this.stylesToAdopt));
38
- }
39
- /**
40
- * Sets the host element. Alias of addHost method.
41
- *
42
- * @param host - The host element.
43
- * @deprecated Use addHost instead.
44
- */
45
- setHost(host) {
46
- this.addHost(host instanceof ShadowRoot ? host : document);
47
- }
48
- /**
49
- * Adds the element to the hosts set.
50
- *
51
- * @param host - The host element.
52
- */
53
- addHost(host) {
54
- this.hosts.add(host);
55
- host.adoptedStyleSheets = this.stylesToAdopt;
56
- }
57
- /**
58
- * Removes the element from the hosts set.
59
- *
60
- * @param host - The host element to remove.
61
- */
62
- removeHost(host) {
63
- this.hosts.delete(host);
64
- }
65
- /**
66
- * Sets the injector instance in the window object.
67
- */
68
- setInWindow() {
69
- if (typeof window !== 'undefined' && instance) {
70
- window.xCSSInjector = instance;
71
- }
72
- }
73
- /**
74
- * Checks if the injector instance is in the window object.
75
- *
76
- * @returns Whether the injector instance is in the window object.
77
- */
78
- isInWindow() {
79
- return typeof window === 'undefined'
80
- ? false
81
- : window.xCSSInjector === instance;
82
- }
83
- }
84
- /**
85
- * Instance of the injector.
86
- *
87
- * @public
88
- */
89
- const cssInjector = new CssInjector(typeof window !== 'undefined');
90
-
91
- export { CssInjector, cssInjector };
@@ -1,137 +0,0 @@
1
- import { deepMerge } from '@empathyco/x-deep-merge';
2
- import { isObject } from '@empathyco/x-utils';
3
- import { createI18n } from 'vue-i18n';
4
-
5
- /**
6
- * I18n settings manager.
7
- *
8
- * @public
9
- */
10
- class I18n {
11
- /**
12
- * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
13
- *
14
- * @param options - The new {@link I18nOptions | i18n options}.
15
- * @param options.locale - i18n locale.
16
- * @param options.messages - i18n messages.
17
- * @param options.device - i18n device.
18
- * @param options.fallbackLocale - i18n fallbackLocale.
19
- */
20
- constructor({ locale, messages, device, fallbackLocale }) {
21
- this.locale = locale;
22
- this.device = device;
23
- this.messages = messages;
24
- this.fallbackLocale = fallbackLocale;
25
- }
26
- /**
27
- * Creates a new {@link I18n} instance.
28
- *
29
- * @param options - The new {@link I18nOptions | i18n options}.
30
- *
31
- * @returns The new instance.
32
- */
33
- static async create(options) {
34
- const instance = new I18n(options);
35
- instance.currentMessages = await instance.getCurrentMessages();
36
- return instance;
37
- }
38
- /**
39
- * Installs VueI18n plugin.
40
- *
41
- * @param vue - The Vue instance.
42
- */
43
- install(vue) {
44
- this.vueI18n = createI18n({
45
- legacy: false,
46
- locale: this.locale,
47
- silentFallbackWarn: true,
48
- // eslint-disable-next-line ts/no-unsafe-assignment
49
- messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
50
- missing: (_, key) => {
51
- var _a;
52
- return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${this.locale}'`);
53
- },
54
- });
55
- vue.use(this.vueI18n);
56
- }
57
- /**
58
- * Tries to get a value from the messages object accessing it key by key.
59
- * When a key fails, takes the remaining key parts and tries to access the message
60
- * joining them into a single key.
61
- *
62
- * @param path - A dot separated key to access the message object.
63
- * @returns The message corresponding to the provided key, or empty string if it doesn't exist.
64
- */
65
- getMessageWithDotsInKey(path) {
66
- // TO-DO Support function or array messages
67
- // eslint-disable-next-line ts/no-unsafe-assignment
68
- const message = path.split('.').reduce((messages, key, index, pathParts) => {
69
- var _a;
70
- // eslint-disable-next-line ts/no-unsafe-return
71
- return isObject(messages)
72
- ? ((_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')])
73
- : messages;
74
- }, this.currentMessages);
75
- return typeof message === 'string' ? message : null;
76
- }
77
- /**
78
- * Sets the new locale and updates the messages accordingly.
79
- *
80
- * @param newLocale - The new locale.
81
- *
82
- * @returns The new messages.
83
- */
84
- async setLocale(newLocale) {
85
- if (this.locale !== newLocale) {
86
- this.locale = newLocale;
87
- await this.changeMessages();
88
- this.vueI18n.global.locale.value = this.locale;
89
- }
90
- }
91
- /**
92
- * Sets the new device and updates the messages accordingly.
93
- *
94
- * @param newDevice - The new device.
95
- *
96
- * @returns The new messages.
97
- */
98
- async setDevice(newDevice) {
99
- if (this.device !== newDevice) {
100
- this.device = newDevice;
101
- await this.changeMessages();
102
- }
103
- }
104
- /**
105
- * Updates the current messages based on the new locale and/or device.
106
- */
107
- async changeMessages() {
108
- this.currentMessages = await this.getCurrentMessages();
109
- this.vueI18n.global.setLocaleMessage(this.locale, this.currentMessages);
110
- }
111
- /**
112
- * Retrieves the corresponding messages for the set locale and device.
113
- *
114
- * @remarks By default, it merges `base` messages with the specific `device` ones. If there are
115
- * no messages for the current locale, the fallback locale is used instead.
116
- *
117
- * @returns The messages for the current locale and device.
118
- */
119
- async getCurrentMessages() {
120
- const rawMessages = this.locale in this.messages ? this.messages[this.locale] : this.messages[this.fallbackLocale];
121
- const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
122
- // eslint-disable-next-line ts/no-unsafe-return
123
- return deepMerge({}, messages.base, messages[this.device]);
124
- }
125
- }
126
- /**
127
- * Type-guard to check if the passed messages should be lazy-loaded.
128
- *
129
- * @param messages - The messages to check.
130
- *
131
- * @returns True if the messages are lazy or false otherwise.
132
- */
133
- function areLazyMessages(messages) {
134
- return typeof messages === 'function';
135
- }
136
-
137
- export { I18n };
package/dist/esm/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { rollupCssInjectorConfig } from './build/rollup/rollup.config.js';
2
- export { webpackConfig } from './build/webpack/webpack.config.js';
3
- export { CssInjector, cssInjector } from './css-injector/css-injector.js';
4
- export { I18n } from './i18n/i18n.plugin.js';