@discord/intl-loader-core 0.18.0 → 0.19.1-canary.f6592a8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl-loader-core",
3
- "version": "0.18.0",
3
+ "version": "0.19.1-canary.f6592a8",
4
4
  "license": "MIT",
5
5
  "description": "Core utilities for writing loaders and transformers using @discord/intl",
6
6
  "author": "Jon Egeland",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "chokidar": "^3.6.0",
30
30
  "debug": "^4.3.6",
31
- "@discord/intl-message-database": "0.18.0"
31
+ "@discord/intl-message-database": "0.19.1-canary.f6592a8"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/debug": "^4.1.12",
@@ -18,18 +18,19 @@
18
18
  *
19
19
  * // SomeConsumer.tsx
20
20
  * import someModuleMessages from 'SomeModule.messages.js';
21
- * i18n.format(someModuleMessages.THIS_IS_A_MESSAGE, {values: "I'm a value!"});
21
+ * intl.format(someModuleMessages.THIS_IS_A_MESSAGE, {values: "I'm a value!"});
22
22
  * ```
23
23
  *
24
24
  * This transformer will only handle `SomeModule.messages.js`, and will output
25
25
  * something like:
26
26
  *
27
27
  * ```typescript
28
- * const {i18n} = require('@discord/intl');
29
- * const _keys = ["a9fn23"];
30
- * const _locales = {"en-US": () => require('./messages/en-US.messages.json')};
31
- * export const messagesLoader = createLoader(_keys, _locales);
32
- * export default messagesLoader.getBinds();
28
+ * const {createLoader} = require('@discord/intl');
29
+ * const _localeMap = {"en-US": () => require('./messages/en-US.messages.json')};
30
+ * export const messagesLoader = createLoader(_localeMap);
31
+ * export default {
32
+ * a9fn23(locale) => messagesLoader.get("a9fn23", locale)
33
+ * };
33
34
  * ```
34
35
  *
35
36
  * Notice how the message keys have been hashed into short keys, and the
@@ -41,7 +42,7 @@
41
42
  *
42
43
  * ```typescript
43
44
  * import someModuleMessages from 'SomeModule.messages.js';
44
- * i18n.format(someModuleMessages["a9fn23"], {values: "i'm a value!"});
45
+ * intl.format(someModuleMessages["a9fn23"], {values: "i'm a value!"});
45
46
  * ```
46
47
  *
47
48
  * The transformed file also contains a named export for `messagesLoader`,
@@ -87,9 +88,8 @@ class MessageDefinitionsTransformer {
87
88
  getLocaleRequireMap() {
88
89
  const localeProperties = [];
89
90
  for (const [locale, importPath] of Object.entries(this.options.localeMap)) {
90
- // This assumes that the author has specified `importPath`
91
- // as a properly-resolvable path for the bundler, which we can't easily
92
- // enforce, unfortunately.
91
+ // This assumes that the author has specified `importPath` as a properly-resolvable path for
92
+ // the bundler, which we can't easily enforce, unfortunately.
93
93
  localeProperties.push(`"${locale}": () => ${this.options.getTranslationImport(importPath)}`);
94
94
  }
95
95
 
@@ -111,81 +111,55 @@ class MessageDefinitionsTransformer {
111
111
  }
112
112
 
113
113
  /**
114
- * When `options.pregenerateBinds` is set to 'proxy', this method is invoked to create it.
114
+ * When `option.proxyBinds` is set to true, this method is invoked to create it.
115
115
  *
116
116
  * The binds proxy is a plain `Proxy` object with configuration applied to make it act and
117
117
  * function like a complete object, but without having to instantiate potentially thousands of
118
- * binds during initialization. The Proxy supports `key in proxy` queries, getter access,
119
- * spreads, and more.
118
+ * binds during initialization. The proxy intentionally does _not_ support iteration nor `key in`
119
+ * queries, as they require up-front initialization that is too costly when multiple thousands
120
+ * of message keys are included.
120
121
  *
121
- * @param {string} keyArrayName Name of an Array to use for `keys` queries.
122
- * @param {string} keySetName Name of a Set to use for `has` queries.
123
- * @param {string} bindFunc Code expression that creates a getter bind
124
- * @returns {string}
122
+ * However, the proxy _does_ implement `ownKeys`, such that the returned list of keys represents
123
+ * all of the messages that have been _accessed_ through this proxy so far. This is generally
124
+ * more of a debugging utility than anything else, but can be useful for diagnosing when messages
125
+ * are used in critical paths or otherwise.
126
+ *
127
+ * @returns {string[]}
125
128
  */
126
- createBindsProxy(keyArrayName, keySetName, bindFunc) {
127
- return `new Proxy({},
128
- {
129
- has(self, prop) {
130
- return ${keySetName}.has(prop);
131
- },
132
- ownKeys(self) {
133
- return ${keyArrayName};
134
- },
135
- getOwnPropertyDescriptor(self, prop) {
136
- return {
137
- value: self[prop] ||= ${bindFunc},
138
- configurable: true,
139
- enumerable: true,
140
- writable: false,
141
- };
142
- },
143
- get(self, prop) {
144
- if (prop === '$$typeof') {
145
- return 'object';
146
- }
147
- if (prop === Symbol.toStringTag) {
148
- return 'proxyAssign';
149
- }
150
-
151
- if(!${keySetName}.has(prop)) return undefined;
152
-
153
- self[prop] ||= ${bindFunc};
154
- return self[prop];
155
- },
156
- },
157
- )`;
129
+ createBindsProxy() {
130
+ return [
131
+ `const {makeMessagesProxy} = require('@discord/intl');`,
132
+ `const binds = makeMessagesProxy(${this.loaderName});`,
133
+ ];
158
134
  }
159
135
 
160
136
  /**
161
- * Return a map of key names to bound message getter functions. If `preGenerateBinds` is
137
+ * Return a map of key names to bound message getter functions. If `proxyBinds` is
162
138
  * configured to be `true`, the binds will be created as a constant object in the output.
163
139
  * Otherwise, the generation will be done at runtime through the `getBinds` method on the loader.
164
140
  *
165
141
  * @returns {string[]}
166
142
  */
167
143
  createLoaderAndBinds() {
168
- if (this.options.preGenerateBinds === 'proxy') {
169
- return [
170
- `const _keys = ${JSON.stringify(Object.keys(this.options.messageKeys))};`,
171
- 'const _keySet = new Set(_keys);',
172
- `const ${this.loaderName} = createLoader(_keys, _locales, _defaultLocale);`,
173
- `const binds = ${this.createBindsProxy('_keys', '_keySet', `(locale) => ${this.loaderName}.get(prop, locale)`)};`,
174
- ];
175
- } else if (this.options.preGenerateBinds === true) {
176
- const bindLines = Object.keys(this.options.messageKeys).map(
177
- (bind) => `"${bind}"(locale) { return ${this.loaderName}.get("${bind}", locale) }`,
178
- );
179
- return [
180
- `const binds = {${bindLines.join(',')}};`,
181
- `const ${this.loaderName} = createLoader(Object.keys(binds), _locales, _defaultLocale);`,
182
- ];
183
- } else {
184
- return [
185
- `const _keys = ${JSON.stringify(Object.keys(this.options.messageKeys))};`,
186
- `const ${this.loaderName} = createLoader(_keys, _locales, _defaultLocale);`,
187
- `const binds = ${this.loaderName}.getBinds();`,
188
- ];
144
+ switch (this.options.bindMode ?? 'proxy') {
145
+ case 'proxy':
146
+ return [
147
+ `const ${this.loaderName} = createLoader(_localeMap, _defaultLocale);`,
148
+ ...this.createBindsProxy(),
149
+ ];
150
+ case 'literal': {
151
+ const bindLines = Object.keys(this.options.messageKeys).map(
152
+ (bind) => `"${bind}"(locale) { return ${this.loaderName}.get("${bind}", locale) }`,
153
+ );
154
+ return [
155
+ `const binds = {${bindLines.join(',')}};`,
156
+ `const ${this.loaderName} = createLoader(_localeMap, _defaultLocale);`,
157
+ ];
158
+ }
159
+ default:
160
+ throw new Error(
161
+ `Unknown value for intl transformer option 'bindMode': ${this.options.bindMode}`,
162
+ );
189
163
  }
190
164
  }
191
165
 
@@ -221,7 +195,7 @@ class MessageDefinitionsTransformer {
221
195
  return [
222
196
  this.options.getPrelude?.() ?? '// No additional prelude was configured.',
223
197
  `const {createLoader} = require('@discord/intl');`,
224
- `const _locales = ${this.getLocaleRequireMap()};`,
198
+ `const _localeMap = ${this.getLocaleRequireMap()};`,
225
199
  `const _defaultLocale = ${JSON.stringify(this.options.defaultLocale)};`,
226
200
  ...this.createLoaderAndBinds(),
227
201
  ...this.debugModeSetup(),
@@ -18,18 +18,19 @@
18
18
  *
19
19
  * // SomeConsumer.tsx
20
20
  * import someModuleMessages from 'SomeModule.messages.js';
21
- * i18n.format(someModuleMessages.THIS_IS_A_MESSAGE, {values: "I'm a value!"});
21
+ * intl.format(someModuleMessages.THIS_IS_A_MESSAGE, {values: "I'm a value!"});
22
22
  * ```
23
23
  *
24
24
  * This transformer will only handle `SomeModule.messages.js`, and will output
25
25
  * something like:
26
26
  *
27
27
  * ```typescript
28
- * const {i18n} = require('@discord/intl');
29
- * const _keys = ["a9fn23"];
30
- * const _locales = {"en-US": () => require('./messages/en-US.messages.json')};
31
- * export const messagesLoader = createLoader(_keys, _locales);
32
- * export default messagesLoader.getBinds();
28
+ * const {createLoader} = require('@discord/intl');
29
+ * const _localeMap = {"en-US": () => require('./messages/en-US.messages.json')};
30
+ * export const messagesLoader = createLoader(_localeMap);
31
+ * export default {
32
+ * a9fn23(locale) => messagesLoader.get("a9fn23", locale)
33
+ * };
33
34
  * ```
34
35
  *
35
36
  * Notice how the message keys have been hashed into short keys, and the
@@ -41,7 +42,7 @@
41
42
  *
42
43
  * ```typescript
43
44
  * import someModuleMessages from 'SomeModule.messages.js';
44
- * i18n.format(someModuleMessages["a9fn23"], {values: "i'm a value!"});
45
+ * intl.format(someModuleMessages["a9fn23"], {values: "i'm a value!"});
45
46
  * ```
46
47
  *
47
48
  * The transformed file also contains a named export for `messagesLoader`,
@@ -91,21 +92,24 @@ export class MessageDefinitionsTransformer {
91
92
  */
92
93
  debugModeSetup(): string[];
93
94
  /**
94
- * When `options.pregenerateBinds` is set to 'proxy', this method is invoked to create it.
95
+ * When `option.proxyBinds` is set to true, this method is invoked to create it.
95
96
  *
96
97
  * The binds proxy is a plain `Proxy` object with configuration applied to make it act and
97
98
  * function like a complete object, but without having to instantiate potentially thousands of
98
- * binds during initialization. The Proxy supports `key in proxy` queries, getter access,
99
- * spreads, and more.
99
+ * binds during initialization. The proxy intentionally does _not_ support iteration nor `key in`
100
+ * queries, as they require up-front initialization that is too costly when multiple thousands
101
+ * of message keys are included.
100
102
  *
101
- * @param {string} keyArrayName Name of an Array to use for `keys` queries.
102
- * @param {string} keySetName Name of a Set to use for `has` queries.
103
- * @param {string} bindFunc Code expression that creates a getter bind
104
- * @returns {string}
103
+ * However, the proxy _does_ implement `ownKeys`, such that the returned list of keys represents
104
+ * all of the messages that have been _accessed_ through this proxy so far. This is generally
105
+ * more of a debugging utility than anything else, but can be useful for diagnosing when messages
106
+ * are used in critical paths or otherwise.
107
+ *
108
+ * @returns {string[]}
105
109
  */
106
- createBindsProxy(keyArrayName: string, keySetName: string, bindFunc: string): string;
110
+ createBindsProxy(): string[];
107
111
  /**
108
- * Return a map of key names to bound message getter functions. If `preGenerateBinds` is
112
+ * Return a map of key names to bound message getter functions. If `proxyBinds` is
109
113
  * configured to be `true`, the binds will be created as a constant object in the output.
110
114
  * Otherwise, the generation will be done at runtime through the `getBinds` method on the loader.
111
115
  *
package/types/types.d.ts CHANGED
@@ -54,21 +54,22 @@ export interface MessageDefinitionsTransformerOptions {
54
54
  */
55
55
  exportMode?: 'esm' | 'commonjs' | 'transpiledEsModule';
56
56
  /**
57
- * Control how loader binds are generated by the transformer. When `false` (the default), binds
58
- * will be created at runtime using `messagesLoader.getBinds()`. This mode saves space, at the
59
- * cost of runtime overhead to iterate and create bound functions. When `true`, the iteration of
60
- * messages will be done at bundle time to create a constant JS object containing all the message
61
- * keys as inline functions that act as binds. This mode is much faster for runtimes to
62
- * initialize, at the cost of a relatively large space increase (~70% larger than using the
63
- * runtime bind mode). However, this mode can be compressed substantially when compiled to
64
- * bytecode, as is the case when creating mobile bundles with Hermes. In that case, pre-generating
65
- * binds gains the speed increase while still staying relatively small in the output bundle.
66
- * Because of this, `metro-intl-transformer` will instead default to `true` for this option and
67
- * can be disabled when not using Hermes.
57
+ * Control how loader binds are generated by the transformer. When the value is `'proxy'` (the
58
+ * default), no binds will actually be created directly. Instead, the transformer creates and
59
+ * exports a Proxy object that intercepts all unique requests for messages and creates loader
60
+ * binds on the fly. For projects with thousands of keys in messages files, this can save time
61
+ * and processing power when loading the application, and is especially beneficial for lower-end
62
+ * mobile devices that struggle to create such large objects quickly. Note that this proxy does
63
+ * not act exactly like a normal object and will not work with some expected language features,
64
+ * like `key in messages` queries and `{...messages}` spreads.
68
65
  *
69
- * @default false
66
+ * When set to `'literal'`, the transformer instead exports an object literal with every compiled
67
+ * message key as a property defined directly on it. For smaller messages files, this reduces some
68
+ * of the runtime access overhead in exchange for more time spent during module initialization.
69
+ *
70
+ * @default 'proxy'
70
71
  */
71
- preGenerateBinds?: boolean | 'proxy';
72
+ bindMode?: 'proxy' | 'literal';
72
73
  }
73
74
 
74
75
  /**
package/types.d.ts CHANGED
@@ -54,21 +54,22 @@ export interface MessageDefinitionsTransformerOptions {
54
54
  */
55
55
  exportMode?: 'esm' | 'commonjs' | 'transpiledEsModule';
56
56
  /**
57
- * Control how loader binds are generated by the transformer. When `false` (the default), binds
58
- * will be created at runtime using `messagesLoader.getBinds()`. This mode saves space, at the
59
- * cost of runtime overhead to iterate and create bound functions. When `true`, the iteration of
60
- * messages will be done at bundle time to create a constant JS object containing all the message
61
- * keys as inline functions that act as binds. This mode is much faster for runtimes to
62
- * initialize, at the cost of a relatively large space increase (~70% larger than using the
63
- * runtime bind mode). However, this mode can be compressed substantially when compiled to
64
- * bytecode, as is the case when creating mobile bundles with Hermes. In that case, pre-generating
65
- * binds gains the speed increase while still staying relatively small in the output bundle.
66
- * Because of this, `metro-intl-transformer` will instead default to `true` for this option and
67
- * can be disabled when not using Hermes.
57
+ * Control how loader binds are generated by the transformer. When the value is `'proxy'` (the
58
+ * default), no binds will actually be created directly. Instead, the transformer creates and
59
+ * exports a Proxy object that intercepts all unique requests for messages and creates loader
60
+ * binds on the fly. For projects with thousands of keys in messages files, this can save time
61
+ * and processing power when loading the application, and is especially beneficial for lower-end
62
+ * mobile devices that struggle to create such large objects quickly. Note that this proxy does
63
+ * not act exactly like a normal object and will not work with some expected language features,
64
+ * like `key in messages` queries and `{...messages}` spreads.
68
65
  *
69
- * @default false
66
+ * When set to `'literal'`, the transformer instead exports an object literal with every compiled
67
+ * message key as a property defined directly on it. For smaller messages files, this reduces some
68
+ * of the runtime access overhead in exchange for more time spent during module initialization.
69
+ *
70
+ * @default 'proxy'
70
71
  */
71
- preGenerateBinds?: boolean | 'proxy';
72
+ bindMode?: 'proxy' | 'literal';
72
73
  }
73
74
 
74
75
  /**