@discord/intl-loader-core 0.10.2 → 0.10.3

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.10.2",
3
+ "version": "0.10.3",
4
4
  "license": "MIT",
5
5
  "description": "Core utilities for writing loaders and transformers using @discord/intl",
6
6
  "author": "Jon Egeland",
@@ -29,7 +29,7 @@
29
29
  "chokidar": "^3.6.0",
30
30
  "debug": "^4.3.6",
31
31
  "fast-glob": "^3.3.2",
32
- "@discord/intl-message-database": "0.10.2"
32
+ "@discord/intl-message-database": "0.10.3"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/debug": "^4.1.12",
@@ -114,17 +114,24 @@ class MessageDefinitionsTransformer {
114
114
  * Return a map of key names to bound message getter functions. If `preGenerateBinds` is
115
115
  * configured to be `true`, the binds will be created as a constant object in the output.
116
116
  * Otherwise, the generation will be done at runtime through the `getBinds` method on the loader.
117
+ *
118
+ * @returns {string[]}
117
119
  */
118
- createBindsObject() {
120
+ createLoaderAndBinds() {
119
121
  if (this.options.preGenerateBinds) {
120
- /** @type {string[]} */
121
- const bindLines = [];
122
- for (const bind of Object.keys(this.options.messageKeys)) {
123
- bindLines.push(`"${bind}"(locale) { return ${this.loaderName}.get("${bind}", locale) }`);
124
- }
125
- return `{${bindLines.join(',')}}`;
122
+ const bindLines = Object.keys(this.options.messageKeys).map(
123
+ (bind) => `"${bind}"(locale) { return ${this.loaderName}.get("${bind}", locale) }`,
124
+ );
125
+ return [
126
+ `const binds = {${bindLines.join(',')}};`,
127
+ `const ${this.loaderName} = createLoader(Object.keys(binds), _locales, _defaultLocale);`,
128
+ ];
126
129
  } else {
127
- return `${this.loaderName}.getBinds()`;
130
+ return [
131
+ `const _keys = ${JSON.stringify(Object.keys(this.options.messageKeys))};`,
132
+ `const ${this.loaderName} = createLoader(_keys, _locales, _defaultLocale);`,
133
+ `const binds = ${this.loaderName}.getBinds();`,
134
+ ];
128
135
  }
129
136
  }
130
137
 
@@ -132,9 +139,9 @@ class MessageDefinitionsTransformer {
132
139
  * Return the lines to export fields from this module, as determined by the `exportMode` on this
133
140
  * transformer.
134
141
  *
135
- * @param {string} bindsName Name of the identifier to be exported as the default binds object.
142
+ * @returns {string[]}
136
143
  */
137
- exportFields(bindsName) {
144
+ exportFields() {
138
145
  switch (this.options.exportMode ?? 'esm') {
139
146
  case 'esm':
140
147
  return [`export {${this.loaderName}};`, `export default binds;`];
@@ -157,16 +164,14 @@ class MessageDefinitionsTransformer {
157
164
  * @returns {string}
158
165
  */
159
166
  getOutput() {
160
- const bindsObject = this.createBindsObject();
161
167
  return [
162
168
  this.options.getPrelude?.() ?? '// No additional prelude was configured.',
163
169
  `const {createLoader} = require('@discord/intl');`,
164
- `const binds = ${bindsObject}`,
165
170
  `const _locales = ${this.getLocaleRequireMap()};`,
166
171
  `const _defaultLocale = ${JSON.stringify(this.options.defaultLocale)};`,
167
- `const ${this.loaderName} = createLoader(Object.keys(binds), _locales, _defaultLocale);`,
172
+ ...this.createLoaderAndBinds(),
168
173
  ...this.debugModeSetup(),
169
- ...this.exportFields('binds'),
174
+ ...this.exportFields(),
170
175
  ].join('\n');
171
176
  }
172
177
  }
@@ -94,15 +94,17 @@ export class MessageDefinitionsTransformer {
94
94
  * Return a map of key names to bound message getter functions. If `preGenerateBinds` is
95
95
  * configured to be `true`, the binds will be created as a constant object in the output.
96
96
  * Otherwise, the generation will be done at runtime through the `getBinds` method on the loader.
97
+ *
98
+ * @returns {string[]}
97
99
  */
98
- createBindsObject(): string;
100
+ createLoaderAndBinds(): string[];
99
101
  /**
100
102
  * Return the lines to export fields from this module, as determined by the `exportMode` on this
101
103
  * transformer.
102
104
  *
103
- * @param {string} bindsName Name of the identifier to be exported as the default binds object.
105
+ * @returns {string[]}
104
106
  */
105
- exportFields(bindsName: string): string[];
107
+ exportFields(): string[];
106
108
  /**
107
109
  * Returns the reduced, transformed output for this file. Currently not
108
110
  * configurable, but could be told to include default messages or preserve