@empathyco/x-archetype-utils 2.0.0-alpha.5 → 2.0.0-alpha.7

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # x-archetype-utils
2
2
 
3
- `x-archetype-utils` is a library that provides utilities to ease development. It's used in the [Interface X Archetype](https://github.com/empathyco/x-archetype) project.
3
+ `x-archetype-utils` is a library that provides utilities to ease development. It's used in the [Interface X Archetype](https://github.com/empathyco/x-archetype) project.
4
4
 
5
5
  ## Contributing
6
6
 
@@ -4,11 +4,11 @@ const rollupCssInjectorConfig = {
4
4
  replace: {
5
5
  // Replace X CSS injector by our custom one.
6
6
  'export default injectCss;': 'export default (css) => window.xCSSInjector.addStyle({ source: css });',
7
- delimiters: ['', '']
7
+ delimiters: ['', ''],
8
8
  },
9
9
  styles: {
10
- mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`]
11
- }
10
+ mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`],
11
+ },
12
12
  };
13
13
 
14
14
  exports.rollupCssInjectorConfig = rollupCssInjectorConfig;
@@ -8,19 +8,19 @@ const webpackConfig = {
8
8
  {
9
9
  test: /\.ts|js|vue$/,
10
10
  enforce: 'pre',
11
- use: ['source-map-loader']
12
- }
13
- ]
11
+ use: ['source-map-loader'],
12
+ },
13
+ ],
14
14
  },
15
15
  output: {
16
- devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }) => resourcePath.match(/\.vue$/) &&
16
+ devtoolModuleFilenameTemplate: ({ resourcePath, hash, query, }) => resourcePath.match(/\.vue$/) &&
17
17
  !query.match(/type=script/) &&
18
18
  !query.match(/\?rollup-plugin-vue=script.ts/)
19
19
  ? `webpack-generated:///${resourcePath}?${hash}`
20
20
  : `sources://${resourcePath}`,
21
- devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'
22
- }
23
- }
21
+ devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]',
22
+ },
23
+ },
24
24
  };
25
25
 
26
26
  exports.webpackConfig = webpackConfig;
@@ -19,7 +19,7 @@ class CssInjector {
19
19
  constructor(setInWindow = true) {
20
20
  this.stylesQueue = [];
21
21
  if (!(instance instanceof CssInjector)) {
22
- // eslint-disable-next-line @typescript-eslint/no-this-alias
22
+ // eslint-disable-next-line ts/no-this-alias
23
23
  instance = this;
24
24
  }
25
25
  if (setInWindow) {
@@ -28,20 +28,19 @@ class CssInjector {
28
28
  return instance;
29
29
  }
30
30
  /**
31
- * Adds the styles to the host element.
31
+ * Adds the style to the host element.
32
32
  *
33
- * @param styles - The styles to be added.
33
+ * @param style - The styles to be added.
34
+ * @param style.source - Styles source.
34
35
  */
35
- addStyle(styles) {
36
- this.stylesQueue.push(styles.source);
37
- if (this.host) {
38
- this.stylesQueue.forEach(styles => {
39
- const styleTag = document.createElement('style');
40
- styleTag.textContent = styles;
41
- this.host.appendChild(styleTag);
42
- });
43
- this.stylesQueue = [];
36
+ addStyle(style) {
37
+ if (!this.host) {
38
+ this.stylesQueue.push(style);
39
+ return;
44
40
  }
41
+ const styleTag = document.createElement('style');
42
+ styleTag.textContent = style.source;
43
+ this.host.appendChild(styleTag);
45
44
  }
46
45
  /**
47
46
  * Sets the host element.
@@ -50,6 +49,9 @@ class CssInjector {
50
49
  */
51
50
  setHost(host) {
52
51
  this.host = host;
52
+ // Add all pending styles to the host element
53
+ this.stylesQueue.forEach(style => this.addStyle(style));
54
+ this.stylesQueue = [];
53
55
  }
54
56
  /**
55
57
  * Sets the injector instance in the window object.
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ var xDeepMerge = require('@empathyco/x-deep-merge');
3
4
  var xUtils = require('@empathyco/x-utils');
4
5
  var vueI18n = require('vue-i18n');
5
- var xDeepMerge = require('@empathyco/x-deep-merge');
6
6
 
7
7
  /**
8
8
  * I18n settings manager.
@@ -14,6 +14,10 @@ class I18n {
14
14
  * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
15
15
  *
16
16
  * @param options - The new {@link I18nOptions | i18n options}.
17
+ * @param options.locale - i18n locale.
18
+ * @param options.messages - i18n messages.
19
+ * @param options.device - i18n device.
20
+ * @param options.fallbackLocale - i18n fallbackLocale.
17
21
  */
18
22
  constructor({ locale, messages, device, fallbackLocale }) {
19
23
  this.locale = locale;
@@ -43,13 +47,13 @@ class I18n {
43
47
  legacy: false,
44
48
  locale: this.locale,
45
49
  silentFallbackWarn: true,
50
+ // eslint-disable-next-line ts/no-unsafe-assignment
46
51
  messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
47
52
  missing: (_, key) => {
48
53
  var _a;
49
54
  return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${this.locale}'`);
50
- }
55
+ },
51
56
  });
52
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
53
57
  vue.use(this.vueI18n);
54
58
  }
55
59
  /**
@@ -62,12 +66,12 @@ class I18n {
62
66
  */
63
67
  getMessageWithDotsInKey(path) {
64
68
  // TO-DO Support function or array messages
65
- const message = path
66
- .split('.')
67
- .reduce((messages, key, index, pathParts) => {
69
+ // eslint-disable-next-line ts/no-unsafe-assignment
70
+ const message = path.split('.').reduce((messages, key, index, pathParts) => {
68
71
  var _a;
72
+ // eslint-disable-next-line ts/no-unsafe-return
69
73
  return xUtils.isObject(messages)
70
- ? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
74
+ ? ((_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')])
71
75
  : messages;
72
76
  }, this.currentMessages);
73
77
  return typeof message === 'string' ? message : null;
@@ -104,7 +108,6 @@ class I18n {
104
108
  */
105
109
  async changeMessages() {
106
110
  this.currentMessages = await this.getCurrentMessages();
107
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
108
111
  this.vueI18n.global.setLocaleMessage(this.locale, this.currentMessages);
109
112
  }
110
113
  /**
@@ -116,11 +119,9 @@ class I18n {
116
119
  * @returns The messages for the current locale and device.
117
120
  */
118
121
  async getCurrentMessages() {
119
- const rawMessages = this.locale in this.messages
120
- ? this.messages[this.locale]
121
- : this.messages[this.fallbackLocale];
122
+ const rawMessages = this.locale in this.messages ? this.messages[this.locale] : this.messages[this.fallbackLocale];
122
123
  const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
123
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
124
+ // eslint-disable-next-line ts/no-unsafe-return
124
125
  return xDeepMerge.deepMerge({}, messages.base, messages[this.device]);
125
126
  }
126
127
  }
package/dist/cjs/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  'use strict';
2
2
 
3
- var i18n_plugin = require('./i18n/i18n.plugin.js');
4
- var webpack_config = require('./build/webpack/webpack.config.js');
5
3
  var rollup_config = require('./build/rollup/rollup.config.js');
4
+ var webpack_config = require('./build/webpack/webpack.config.js');
6
5
  var cssInjector = require('./css-injector/css-injector.js');
6
+ var i18n_plugin = require('./i18n/i18n.plugin.js');
7
7
 
8
8
 
9
9
 
10
- exports.I18n = i18n_plugin.I18n;
11
- exports.webpackConfig = webpack_config.webpackConfig;
12
10
  exports.rollupCssInjectorConfig = rollup_config.rollupCssInjectorConfig;
11
+ exports.webpackConfig = webpack_config.webpackConfig;
13
12
  exports.CssInjector = cssInjector.CssInjector;
14
13
  exports.cssInjector = cssInjector.cssInjector;
14
+ exports.I18n = i18n_plugin.I18n;
@@ -2,11 +2,11 @@ const rollupCssInjectorConfig = {
2
2
  replace: {
3
3
  // Replace X CSS injector by our custom one.
4
4
  'export default injectCss;': 'export default (css) => window.xCSSInjector.addStyle({ source: css });',
5
- delimiters: ['', '']
5
+ delimiters: ['', ''],
6
6
  },
7
7
  styles: {
8
- mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`]
9
- }
8
+ mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`],
9
+ },
10
10
  };
11
11
 
12
12
  export { rollupCssInjectorConfig };
@@ -6,19 +6,19 @@ const webpackConfig = {
6
6
  {
7
7
  test: /\.ts|js|vue$/,
8
8
  enforce: 'pre',
9
- use: ['source-map-loader']
10
- }
11
- ]
9
+ use: ['source-map-loader'],
10
+ },
11
+ ],
12
12
  },
13
13
  output: {
14
- devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }) => resourcePath.match(/\.vue$/) &&
14
+ devtoolModuleFilenameTemplate: ({ resourcePath, hash, query, }) => resourcePath.match(/\.vue$/) &&
15
15
  !query.match(/type=script/) &&
16
16
  !query.match(/\?rollup-plugin-vue=script.ts/)
17
17
  ? `webpack-generated:///${resourcePath}?${hash}`
18
18
  : `sources://${resourcePath}`,
19
- devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'
20
- }
21
- }
19
+ devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]',
20
+ },
21
+ },
22
22
  };
23
23
 
24
24
  export { webpackConfig };
@@ -17,7 +17,7 @@ class CssInjector {
17
17
  constructor(setInWindow = true) {
18
18
  this.stylesQueue = [];
19
19
  if (!(instance instanceof CssInjector)) {
20
- // eslint-disable-next-line @typescript-eslint/no-this-alias
20
+ // eslint-disable-next-line ts/no-this-alias
21
21
  instance = this;
22
22
  }
23
23
  if (setInWindow) {
@@ -26,20 +26,19 @@ class CssInjector {
26
26
  return instance;
27
27
  }
28
28
  /**
29
- * Adds the styles to the host element.
29
+ * Adds the style to the host element.
30
30
  *
31
- * @param styles - The styles to be added.
31
+ * @param style - The styles to be added.
32
+ * @param style.source - Styles source.
32
33
  */
33
- addStyle(styles) {
34
- this.stylesQueue.push(styles.source);
35
- if (this.host) {
36
- this.stylesQueue.forEach(styles => {
37
- const styleTag = document.createElement('style');
38
- styleTag.textContent = styles;
39
- this.host.appendChild(styleTag);
40
- });
41
- this.stylesQueue = [];
34
+ addStyle(style) {
35
+ if (!this.host) {
36
+ this.stylesQueue.push(style);
37
+ return;
42
38
  }
39
+ const styleTag = document.createElement('style');
40
+ styleTag.textContent = style.source;
41
+ this.host.appendChild(styleTag);
43
42
  }
44
43
  /**
45
44
  * Sets the host element.
@@ -48,6 +47,9 @@ class CssInjector {
48
47
  */
49
48
  setHost(host) {
50
49
  this.host = host;
50
+ // Add all pending styles to the host element
51
+ this.stylesQueue.forEach(style => this.addStyle(style));
52
+ this.stylesQueue = [];
51
53
  }
52
54
  /**
53
55
  * Sets the injector instance in the window object.
@@ -1,6 +1,6 @@
1
+ import { deepMerge } from '@empathyco/x-deep-merge';
1
2
  import { isObject } from '@empathyco/x-utils';
2
3
  import { createI18n } from 'vue-i18n';
3
- import { deepMerge } from '@empathyco/x-deep-merge';
4
4
 
5
5
  /**
6
6
  * I18n settings manager.
@@ -12,6 +12,10 @@ class I18n {
12
12
  * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
13
13
  *
14
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.
15
19
  */
16
20
  constructor({ locale, messages, device, fallbackLocale }) {
17
21
  this.locale = locale;
@@ -41,13 +45,13 @@ class I18n {
41
45
  legacy: false,
42
46
  locale: this.locale,
43
47
  silentFallbackWarn: true,
48
+ // eslint-disable-next-line ts/no-unsafe-assignment
44
49
  messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
45
50
  missing: (_, key) => {
46
51
  var _a;
47
52
  return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${this.locale}'`);
48
- }
53
+ },
49
54
  });
50
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
51
55
  vue.use(this.vueI18n);
52
56
  }
53
57
  /**
@@ -60,12 +64,12 @@ class I18n {
60
64
  */
61
65
  getMessageWithDotsInKey(path) {
62
66
  // TO-DO Support function or array messages
63
- const message = path
64
- .split('.')
65
- .reduce((messages, key, index, pathParts) => {
67
+ // eslint-disable-next-line ts/no-unsafe-assignment
68
+ const message = path.split('.').reduce((messages, key, index, pathParts) => {
66
69
  var _a;
70
+ // eslint-disable-next-line ts/no-unsafe-return
67
71
  return isObject(messages)
68
- ? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
72
+ ? ((_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')])
69
73
  : messages;
70
74
  }, this.currentMessages);
71
75
  return typeof message === 'string' ? message : null;
@@ -102,7 +106,6 @@ class I18n {
102
106
  */
103
107
  async changeMessages() {
104
108
  this.currentMessages = await this.getCurrentMessages();
105
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
106
109
  this.vueI18n.global.setLocaleMessage(this.locale, this.currentMessages);
107
110
  }
108
111
  /**
@@ -114,11 +117,9 @@ class I18n {
114
117
  * @returns The messages for the current locale and device.
115
118
  */
116
119
  async getCurrentMessages() {
117
- const rawMessages = this.locale in this.messages
118
- ? this.messages[this.locale]
119
- : this.messages[this.fallbackLocale];
120
+ const rawMessages = this.locale in this.messages ? this.messages[this.locale] : this.messages[this.fallbackLocale];
120
121
  const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
121
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
122
+ // eslint-disable-next-line ts/no-unsafe-return
122
123
  return deepMerge({}, messages.base, messages[this.device]);
123
124
  }
124
125
  }
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { I18n } from './i18n/i18n.plugin.js';
2
- export { webpackConfig } from './build/webpack/webpack.config.js';
3
1
  export { rollupCssInjectorConfig } from './build/rollup/rollup.config.js';
2
+ export { webpackConfig } from './build/webpack/webpack.config.js';
4
3
  export { CssInjector, cssInjector } from './css-injector/css-injector.js';
4
+ export { I18n } from './i18n/i18n.plugin.js';
@@ -27,7 +27,7 @@ export declare const webpackConfig: {
27
27
  }[];
28
28
  };
29
29
  output: {
30
- devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }: webpackFilenameTemplateInfo) => string;
30
+ devtoolModuleFilenameTemplate: ({ resourcePath, hash, query, }: webpackFilenameTemplateInfo) => string;
31
31
  devtoolFallbackModuleFilenameTemplate: string;
32
32
  };
33
33
  };
@@ -1,12 +1,18 @@
1
- import { XCSSInjector } from './css-injector.types';
1
+ import type { XCSSInjector } from './css-injector.types';
2
+ /**
3
+ * The style interface that will be used to inject styles into the host element.
4
+ */
5
+ interface Style {
6
+ source: string;
7
+ }
2
8
  /**
3
9
  * Custom CSS injector that allows to inject styles into a host element.
4
10
  *
5
11
  * @public
6
12
  */
7
13
  export declare class CssInjector implements XCSSInjector {
8
- protected host: Element | ShadowRoot;
9
- protected stylesQueue: string[];
14
+ protected host: Element | ShadowRoot | undefined;
15
+ protected stylesQueue: Style[];
10
16
  /**
11
17
  * Initializes the instance of the injector if it's not already initialized and sets it in the
12
18
  * window object if it's required.
@@ -15,13 +21,12 @@ export declare class CssInjector implements XCSSInjector {
15
21
  */
16
22
  constructor(setInWindow?: boolean);
17
23
  /**
18
- * Adds the styles to the host element.
24
+ * Adds the style to the host element.
19
25
  *
20
- * @param styles - The styles to be added.
26
+ * @param style - The styles to be added.
27
+ * @param style.source - Styles source.
21
28
  */
22
- addStyle(styles: {
23
- source: string;
24
- }): void;
29
+ addStyle(style: Style): void;
25
30
  /**
26
31
  * Sets the host element.
27
32
  *
@@ -45,3 +50,4 @@ export declare class CssInjector implements XCSSInjector {
45
50
  * @public
46
51
  */
47
52
  export declare const cssInjector: CssInjector;
53
+ export {};
@@ -1,6 +1,6 @@
1
- import { App } from 'vue';
2
- import { LocaleMessageObject, I18n as VueI18n } from 'vue-i18n';
3
- import { AnyMessages, Device, I18nOptions, Locale } from './i18n.types';
1
+ import type { App } from 'vue';
2
+ import type { LocaleMessageObject, I18n as VueI18n } from 'vue-i18n';
3
+ import type { AnyMessages, Device, I18nOptions, Locale } from './i18n.types';
4
4
  /**
5
5
  * I18n settings manager.
6
6
  *
@@ -17,6 +17,10 @@ export declare class I18n<SomeMessages> {
17
17
  * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
18
18
  *
19
19
  * @param options - The new {@link I18nOptions | i18n options}.
20
+ * @param options.locale - i18n locale.
21
+ * @param options.messages - i18n messages.
22
+ * @param options.device - i18n device.
23
+ * @param options.fallbackLocale - i18n fallbackLocale.
20
24
  */
21
25
  constructor({ locale, messages, device, fallbackLocale }: I18nOptions<SomeMessages>);
22
26
  /**
@@ -1,5 +1,5 @@
1
- import { DeepPartial } from '@empathyco/x-utils';
2
- import { I18n as VueI18n } from 'vue-i18n';
1
+ import type { DeepPartial } from '@empathyco/x-utils';
2
+ import type { I18n as VueI18n } from 'vue-i18n';
3
3
  /** Supported locales. */
4
4
  export type Locale = string;
5
5
  /** Supported devices. */
@@ -1,6 +1,6 @@
1
- export * from './i18n/i18n.plugin';
2
- export * from './i18n/i18n.types';
3
- export * from './build/webpack/webpack.config';
4
1
  export * from './build/rollup/rollup.config';
2
+ export * from './build/webpack/webpack.config';
5
3
  export * from './css-injector/css-injector';
6
4
  export * from './css-injector/css-injector.types';
5
+ export * from './i18n/i18n.plugin';
6
+ export * from './i18n/i18n.types';
package/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@empathyco/x-archetype-utils",
3
- "version": "2.0.0-alpha.5",
3
+ "version": "2.0.0-alpha.7",
4
4
  "description": "Utilities for x-archetype",
5
5
  "author": "Empathy Systems Corporation S.L.",
6
6
  "license": "Apache-2.0",
7
7
  "homepage": "https://github.com/empathyco/x/tree/main/packages/x-archetype-utils#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/empathyco/x.git",
11
+ "directory": "packages/x-archetype-utils"
12
+ },
8
13
  "keywords": [
9
14
  "utils",
10
15
  "x",
@@ -16,13 +21,8 @@
16
21
  "files": [
17
22
  "dist"
18
23
  ],
19
- "repository": {
20
- "type": "git",
21
- "url": "https://github.com/empathyco/x.git",
22
- "directory": "packages/x-archetype-utils"
23
- },
24
24
  "engines": {
25
- "node": ">=18"
25
+ "node": ">=22"
26
26
  },
27
27
  "scripts": {
28
28
  "prebuild": "rimraf dist ./*.tgz",
@@ -30,14 +30,22 @@
30
30
  "build:cjs": "tsc --project tsconfig.cjs.json",
31
31
  "build:esm": "tsc --project tsconfig.esm.json",
32
32
  "postbuild": "pnpm pack",
33
- "lint": "eslint . --ext .ts",
33
+ "lint": "eslint --fix",
34
+ "lint:check": "eslint",
35
+ "lint:inspect": "eslint --inspect-config .",
36
+ "format": "prettier --write .",
37
+ "format:check": "prettier --check .",
34
38
  "test": "jest",
35
39
  "test:unit": "jest",
36
40
  "prepublishOnly": "pnpm run build"
37
41
  },
42
+ "peerDependencies": {
43
+ "vue": "^3.4.31",
44
+ "vue-i18n": "^9.14.0"
45
+ },
38
46
  "dependencies": {
39
- "@empathyco/x-deep-merge": "^2.0.3-alpha.3",
40
- "@empathyco/x-utils": "^1.0.3-alpha.2",
47
+ "@empathyco/x-deep-merge": "^2.0.3-alpha.4",
48
+ "@empathyco/x-utils": "^1.0.3-alpha.3",
41
49
  "tslib": "~2.6.0",
42
50
  "vue-i18n": "~9.14.0"
43
51
  },
@@ -55,12 +63,8 @@
55
63
  "typescript": "4.9.4",
56
64
  "vue": "3.4.31"
57
65
  },
58
- "peerDependencies": {
59
- "vue": "^3.4.31",
60
- "vue-i18n": "^9.14.0"
61
- },
62
66
  "publishConfig": {
63
67
  "access": "public"
64
68
  },
65
- "gitHead": "7fb87a6957a3c93bd169e814d48cc47d46a02889"
69
+ "gitHead": "5af8c786e0efa0fa965d0ea8f14b76dc6b4271c9"
66
70
  }