@empathyco/x-archetype-utils 0.1.0-alpha.2 → 0.1.0-alpha.20

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.
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const webpackConfig = {
6
+ transpileDependencies: ['@empathyco/x-components'],
7
+ configureWebpack: {
8
+ module: {
9
+ rules: [
10
+ {
11
+ test: /\.ts|js|vue$/,
12
+ enforce: 'pre',
13
+ use: ['source-map-loader']
14
+ }
15
+ ]
16
+ },
17
+ output: {
18
+ devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }) => resourcePath.match(/\.vue$/) &&
19
+ !query.match(/type=script/) &&
20
+ !query.match(/\?rollup-plugin-vue=script.ts/)
21
+ ? `webpack-generated:///${resourcePath}?${hash}`
22
+ : `sources://${resourcePath}`,
23
+ devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'
24
+ }
25
+ }
26
+ };
27
+
28
+ exports.webpackConfig = webpackConfig;
@@ -1,136 +1,141 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.I18n = void 0;
4
- const tslib_1 = require("tslib");
5
- const x_utils_1 = require("@empathyco/x-utils");
6
- const vue_i18n_1 = tslib_1.__importDefault(require("vue-i18n"));
7
- const x_deep_merge_1 = require("@empathyco/x-deep-merge");
8
- /**
9
- * I18n settings manager.
10
- *
11
- * @public
12
- */
13
- class I18n {
14
- /**
15
- * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
16
- *
17
- * @param options - The new {@link I18nOptions | i18n options}.
18
- */
19
- constructor({ locale, messages, device, fallbackLocale }) {
20
- this.locale = locale;
21
- this.device = device;
22
- this.messages = messages;
23
- this.fallbackLocale = fallbackLocale;
24
- }
25
- /**
26
- * Creates a new {@link I18n} instance.
27
- *
28
- * @param options - The new {@link I18nOptions | i18n options}.
29
- *
30
- * @returns The new instance.
31
- */
32
- static async create(options) {
33
- const instance = new I18n(options);
34
- instance.currentMessages = await instance.getCurrentMessages();
35
- return instance;
36
- }
37
- /**
38
- * Installs VueI18n plugin.
39
- *
40
- * @param vue - The Vue instance.
41
- */
42
- install(vue) {
43
- vue.use(vue_i18n_1.default);
44
- this.vueI18n = new vue_i18n_1.default({
45
- locale: this.locale,
46
- messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
47
- silentFallbackWarn: true,
48
- missing: (locale, key) => {
49
- var _a;
50
- return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
51
- }
52
- });
53
- }
54
- /**
55
- * Tries to get a value from the messages object accessing it key by key.
56
- * When a key fails, takes the remaining key parts and tries to access the message
57
- * joining them into a single key.
58
- *
59
- * @param path - A dot separated key to access the message object.
60
- * @returns The message corresponding to the provided key, or empty string if it doesn't exist.
61
- */
62
- getMessageWithDotsInKey(path) {
63
- // TO-DO Support function or array messages
64
- const message = path
65
- .split('.')
66
- .reduce((messages, key, index, pathParts) => {
67
- var _a;
68
- return (0, x_utils_1.isObject)(messages)
69
- ? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
70
- : messages;
71
- }, this.currentMessages);
72
- return typeof message === 'string' ? message : null;
73
- }
74
- /**
75
- * Sets the new locale and updates the messages accordingly.
76
- *
77
- * @param newLocale - The new locale.
78
- *
79
- * @returns The new messages.
80
- */
81
- async setLocale(newLocale) {
82
- if (this.locale !== newLocale) {
83
- this.locale = newLocale;
84
- await this.changeMessages();
85
- this.vueI18n.locale = this.locale;
86
- }
87
- }
88
- /**
89
- * Sets the new device and updates the messages accordingly.
90
- *
91
- * @param newDevice - The new device.
92
- *
93
- * @returns The new messages.
94
- */
95
- async setDevice(newDevice) {
96
- if (this.device !== newDevice) {
97
- this.device = newDevice;
98
- await this.changeMessages();
99
- }
100
- }
101
- /**
102
- * Updates the current messages based on the new locale and/or device.
103
- */
104
- async changeMessages() {
105
- this.currentMessages = await this.getCurrentMessages();
106
- this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
107
- }
108
- /**
109
- * Retrieves the corresponding messages for the set locale and device.
110
- *
111
- * @remarks By default, it merges `base` messages with the specific `device` ones. If there are
112
- * no messages for the current locale, the fallback locale is used instead.
113
- *
114
- * @returns The messages for the current locale and device.
115
- */
116
- async getCurrentMessages() {
117
- const rawMessages = this.locale in this.messages
118
- ? this.messages[this.locale]
119
- : this.messages[this.fallbackLocale];
120
- const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
121
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
122
- return (0, x_deep_merge_1.deepMerge)({}, messages.base, messages[this.device]);
123
- }
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var xUtils = require('@empathyco/x-utils');
6
+ var VueI18n = require('vue-i18n');
7
+ var xDeepMerge = require('@empathyco/x-deep-merge');
8
+
9
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
+
11
+ var VueI18n__default = /*#__PURE__*/_interopDefaultLegacy(VueI18n);
12
+
13
+ /**
14
+ * I18n settings manager.
15
+ *
16
+ * @public
17
+ */
18
+ class I18n {
19
+ /**
20
+ * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
21
+ *
22
+ * @param options - The new {@link I18nOptions | i18n options}.
23
+ */
24
+ constructor({ locale, messages, device, fallbackLocale }) {
25
+ this.locale = locale;
26
+ this.device = device;
27
+ this.messages = messages;
28
+ this.fallbackLocale = fallbackLocale;
29
+ }
30
+ /**
31
+ * Creates a new {@link I18n} instance.
32
+ *
33
+ * @param options - The new {@link I18nOptions | i18n options}.
34
+ *
35
+ * @returns The new instance.
36
+ */
37
+ static async create(options) {
38
+ const instance = new I18n(options);
39
+ instance.currentMessages = await instance.getCurrentMessages();
40
+ return instance;
41
+ }
42
+ /**
43
+ * Installs VueI18n plugin.
44
+ *
45
+ * @param vue - The Vue instance.
46
+ */
47
+ install(vue) {
48
+ vue.use(VueI18n__default["default"]);
49
+ this.vueI18n = new VueI18n__default["default"]({
50
+ locale: this.locale,
51
+ messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
52
+ silentFallbackWarn: true,
53
+ missing: (locale, key) => {
54
+ var _a;
55
+ return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
56
+ }
57
+ });
58
+ }
59
+ /**
60
+ * Tries to get a value from the messages object accessing it key by key.
61
+ * When a key fails, takes the remaining key parts and tries to access the message
62
+ * joining them into a single key.
63
+ *
64
+ * @param path - A dot separated key to access the message object.
65
+ * @returns The message corresponding to the provided key, or empty string if it doesn't exist.
66
+ */
67
+ getMessageWithDotsInKey(path) {
68
+ // TO-DO Support function or array messages
69
+ const message = path
70
+ .split('.')
71
+ .reduce((messages, key, index, pathParts) => {
72
+ var _a;
73
+ return xUtils.isObject(messages)
74
+ ? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
75
+ : messages;
76
+ }, this.currentMessages);
77
+ return typeof message === 'string' ? message : null;
78
+ }
79
+ /**
80
+ * Sets the new locale and updates the messages accordingly.
81
+ *
82
+ * @param newLocale - The new locale.
83
+ *
84
+ * @returns The new messages.
85
+ */
86
+ async setLocale(newLocale) {
87
+ if (this.locale !== newLocale) {
88
+ this.locale = newLocale;
89
+ await this.changeMessages();
90
+ this.vueI18n.locale = this.locale;
91
+ }
92
+ }
93
+ /**
94
+ * Sets the new device and updates the messages accordingly.
95
+ *
96
+ * @param newDevice - The new device.
97
+ *
98
+ * @returns The new messages.
99
+ */
100
+ async setDevice(newDevice) {
101
+ if (this.device !== newDevice) {
102
+ this.device = newDevice;
103
+ await this.changeMessages();
104
+ }
105
+ }
106
+ /**
107
+ * Updates the current messages based on the new locale and/or device.
108
+ */
109
+ async changeMessages() {
110
+ this.currentMessages = await this.getCurrentMessages();
111
+ this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
112
+ }
113
+ /**
114
+ * Retrieves the corresponding messages for the set locale and device.
115
+ *
116
+ * @remarks By default, it merges `base` messages with the specific `device` ones. If there are
117
+ * no messages for the current locale, the fallback locale is used instead.
118
+ *
119
+ * @returns The messages for the current locale and device.
120
+ */
121
+ async getCurrentMessages() {
122
+ const rawMessages = this.locale in this.messages
123
+ ? this.messages[this.locale]
124
+ : this.messages[this.fallbackLocale];
125
+ const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
126
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
127
+ return xDeepMerge.deepMerge({}, messages.base, messages[this.device]);
128
+ }
129
+ }
130
+ /**
131
+ * Type-guard to check if the passed messages should be lazy-loaded.
132
+ *
133
+ * @param messages - The messages to check.
134
+ *
135
+ * @returns True if the messages are lazy or false otherwise.
136
+ */
137
+ function areLazyMessages(messages) {
138
+ return typeof messages === 'function';
124
139
  }
140
+
125
141
  exports.I18n = I18n;
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
- //# sourceMappingURL=i18n.plugin.js.map
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./i18n/i18n.plugin"), exports);
5
- tslib_1.__exportStar(require("./i18n/i18n.types"), exports);
6
- //# sourceMappingURL=index.js.map
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var i18n_plugin = require('./i18n/i18n.plugin.js');
6
+ var webpack_config = require('./build/webpack/webpack.config.js');
7
+
8
+
9
+
10
+ exports.I18n = i18n_plugin.I18n;
11
+ exports.webpackConfig = webpack_config.webpackConfig;
@@ -0,0 +1,24 @@
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,131 +1,133 @@
1
1
  import { isObject } from '@empathyco/x-utils';
2
2
  import VueI18n from 'vue-i18n';
3
3
  import { deepMerge } from '@empathyco/x-deep-merge';
4
- /**
5
- * I18n settings manager.
6
- *
7
- * @public
8
- */
9
- export class I18n {
10
- /**
11
- * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
12
- *
13
- * @param options - The new {@link I18nOptions | i18n options}.
14
- */
15
- constructor({ locale, messages, device, fallbackLocale }) {
16
- this.locale = locale;
17
- this.device = device;
18
- this.messages = messages;
19
- this.fallbackLocale = fallbackLocale;
20
- }
21
- /**
22
- * Creates a new {@link I18n} instance.
23
- *
24
- * @param options - The new {@link I18nOptions | i18n options}.
25
- *
26
- * @returns The new instance.
27
- */
28
- static async create(options) {
29
- const instance = new I18n(options);
30
- instance.currentMessages = await instance.getCurrentMessages();
31
- return instance;
32
- }
33
- /**
34
- * Installs VueI18n plugin.
35
- *
36
- * @param vue - The Vue instance.
37
- */
38
- install(vue) {
39
- vue.use(VueI18n);
40
- this.vueI18n = new VueI18n({
41
- locale: this.locale,
42
- messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
43
- silentFallbackWarn: true,
44
- missing: (locale, key) => {
45
- var _a;
46
- return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
47
- }
48
- });
49
- }
50
- /**
51
- * Tries to get a value from the messages object accessing it key by key.
52
- * When a key fails, takes the remaining key parts and tries to access the message
53
- * joining them into a single key.
54
- *
55
- * @param path - A dot separated key to access the message object.
56
- * @returns The message corresponding to the provided key, or empty string if it doesn't exist.
57
- */
58
- getMessageWithDotsInKey(path) {
59
- // TO-DO Support function or array messages
60
- const message = path
61
- .split('.')
62
- .reduce((messages, key, index, pathParts) => {
63
- var _a;
64
- return isObject(messages)
65
- ? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
66
- : messages;
67
- }, this.currentMessages);
68
- return typeof message === 'string' ? message : null;
69
- }
70
- /**
71
- * Sets the new locale and updates the messages accordingly.
72
- *
73
- * @param newLocale - The new locale.
74
- *
75
- * @returns The new messages.
76
- */
77
- async setLocale(newLocale) {
78
- if (this.locale !== newLocale) {
79
- this.locale = newLocale;
80
- await this.changeMessages();
81
- this.vueI18n.locale = this.locale;
82
- }
83
- }
84
- /**
85
- * Sets the new device and updates the messages accordingly.
86
- *
87
- * @param newDevice - The new device.
88
- *
89
- * @returns The new messages.
90
- */
91
- async setDevice(newDevice) {
92
- if (this.device !== newDevice) {
93
- this.device = newDevice;
94
- await this.changeMessages();
95
- }
96
- }
97
- /**
98
- * Updates the current messages based on the new locale and/or device.
99
- */
100
- async changeMessages() {
101
- this.currentMessages = await this.getCurrentMessages();
102
- this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
103
- }
104
- /**
105
- * Retrieves the corresponding messages for the set locale and device.
106
- *
107
- * @remarks By default, it merges `base` messages with the specific `device` ones. If there are
108
- * no messages for the current locale, the fallback locale is used instead.
109
- *
110
- * @returns The messages for the current locale and device.
111
- */
112
- async getCurrentMessages() {
113
- const rawMessages = this.locale in this.messages
114
- ? this.messages[this.locale]
115
- : this.messages[this.fallbackLocale];
116
- const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
117
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
118
- return deepMerge({}, messages.base, messages[this.device]);
119
- }
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
+ */
16
+ constructor({ locale, messages, device, fallbackLocale }) {
17
+ this.locale = locale;
18
+ this.device = device;
19
+ this.messages = messages;
20
+ this.fallbackLocale = fallbackLocale;
21
+ }
22
+ /**
23
+ * Creates a new {@link I18n} instance.
24
+ *
25
+ * @param options - The new {@link I18nOptions | i18n options}.
26
+ *
27
+ * @returns The new instance.
28
+ */
29
+ static async create(options) {
30
+ const instance = new I18n(options);
31
+ instance.currentMessages = await instance.getCurrentMessages();
32
+ return instance;
33
+ }
34
+ /**
35
+ * Installs VueI18n plugin.
36
+ *
37
+ * @param vue - The Vue instance.
38
+ */
39
+ install(vue) {
40
+ vue.use(VueI18n);
41
+ this.vueI18n = new VueI18n({
42
+ locale: this.locale,
43
+ messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
44
+ silentFallbackWarn: true,
45
+ missing: (locale, key) => {
46
+ var _a;
47
+ return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
48
+ }
49
+ });
50
+ }
51
+ /**
52
+ * Tries to get a value from the messages object accessing it key by key.
53
+ * When a key fails, takes the remaining key parts and tries to access the message
54
+ * joining them into a single key.
55
+ *
56
+ * @param path - A dot separated key to access the message object.
57
+ * @returns The message corresponding to the provided key, or empty string if it doesn't exist.
58
+ */
59
+ getMessageWithDotsInKey(path) {
60
+ // TO-DO Support function or array messages
61
+ const message = path
62
+ .split('.')
63
+ .reduce((messages, key, index, pathParts) => {
64
+ var _a;
65
+ return isObject(messages)
66
+ ? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
67
+ : messages;
68
+ }, this.currentMessages);
69
+ return typeof message === 'string' ? message : null;
70
+ }
71
+ /**
72
+ * Sets the new locale and updates the messages accordingly.
73
+ *
74
+ * @param newLocale - The new locale.
75
+ *
76
+ * @returns The new messages.
77
+ */
78
+ async setLocale(newLocale) {
79
+ if (this.locale !== newLocale) {
80
+ this.locale = newLocale;
81
+ await this.changeMessages();
82
+ this.vueI18n.locale = this.locale;
83
+ }
84
+ }
85
+ /**
86
+ * Sets the new device and updates the messages accordingly.
87
+ *
88
+ * @param newDevice - The new device.
89
+ *
90
+ * @returns The new messages.
91
+ */
92
+ async setDevice(newDevice) {
93
+ if (this.device !== newDevice) {
94
+ this.device = newDevice;
95
+ await this.changeMessages();
96
+ }
97
+ }
98
+ /**
99
+ * Updates the current messages based on the new locale and/or device.
100
+ */
101
+ async changeMessages() {
102
+ this.currentMessages = await this.getCurrentMessages();
103
+ this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
104
+ }
105
+ /**
106
+ * Retrieves the corresponding messages for the set locale and device.
107
+ *
108
+ * @remarks By default, it merges `base` messages with the specific `device` ones. If there are
109
+ * no messages for the current locale, the fallback locale is used instead.
110
+ *
111
+ * @returns The messages for the current locale and device.
112
+ */
113
+ async getCurrentMessages() {
114
+ const rawMessages = this.locale in this.messages
115
+ ? this.messages[this.locale]
116
+ : this.messages[this.fallbackLocale];
117
+ const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
118
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
119
+ return deepMerge({}, messages.base, messages[this.device]);
120
+ }
121
+ }
122
+ /**
123
+ * Type-guard to check if the passed messages should be lazy-loaded.
124
+ *
125
+ * @param messages - The messages to check.
126
+ *
127
+ * @returns True if the messages are lazy or false otherwise.
128
+ */
129
+ function areLazyMessages(messages) {
130
+ return typeof messages === 'function';
120
131
  }
121
- /**
122
- * Type-guard to check if the passed messages should be lazy-loaded.
123
- *
124
- * @param messages - The messages to check.
125
- *
126
- * @returns True if the messages are lazy or false otherwise.
127
- */
128
- function areLazyMessages(messages) {
129
- return typeof messages === 'function';
130
- }
131
- //# sourceMappingURL=i18n.plugin.js.map
132
+
133
+ export { I18n };
package/dist/esm/index.js CHANGED
@@ -1,3 +1,2 @@
1
- export * from './i18n/i18n.plugin';
2
- export * from './i18n/i18n.types';
3
- //# sourceMappingURL=index.js.map
1
+ export { I18n } from './i18n/i18n.plugin.js';
2
+ export { webpackConfig } from './build/webpack/webpack.config.js';