@discord/intl-loader-core 0.10.0 → 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.0",
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.0"
32
+ "@discord/intl-message-database": "0.10.3"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/debug": "^4.1.12",
@@ -110,9 +110,36 @@ class MessageDefinitionsTransformer {
110
110
  ];
111
111
  }
112
112
 
113
+ /**
114
+ * Return a map of key names to bound message getter functions. If `preGenerateBinds` is
115
+ * configured to be `true`, the binds will be created as a constant object in the output.
116
+ * Otherwise, the generation will be done at runtime through the `getBinds` method on the loader.
117
+ *
118
+ * @returns {string[]}
119
+ */
120
+ createLoaderAndBinds() {
121
+ if (this.options.preGenerateBinds) {
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
+ ];
129
+ } else {
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
+ ];
135
+ }
136
+ }
137
+
113
138
  /**
114
139
  * Return the lines to export fields from this module, as determined by the `exportMode` on this
115
140
  * transformer.
141
+ *
142
+ * @returns {string[]}
116
143
  */
117
144
  exportFields() {
118
145
  switch (this.options.exportMode ?? 'esm') {
@@ -140,12 +167,10 @@ class MessageDefinitionsTransformer {
140
167
  return [
141
168
  this.options.getPrelude?.() ?? '// No additional prelude was configured.',
142
169
  `const {createLoader} = require('@discord/intl');`,
143
- `const _keys = ${JSON.stringify(Object.keys(this.options.messageKeys))};`,
144
170
  `const _locales = ${this.getLocaleRequireMap()};`,
145
171
  `const _defaultLocale = ${JSON.stringify(this.options.defaultLocale)};`,
146
- `const ${this.loaderName} = createLoader(_keys, _locales, _defaultLocale);`,
172
+ ...this.createLoaderAndBinds(),
147
173
  ...this.debugModeSetup(),
148
- `const binds = ${this.loaderName}.getBinds();`,
149
174
  ...this.exportFields(),
150
175
  ].join('\n');
151
176
  }
@@ -90,9 +90,19 @@ export class MessageDefinitionsTransformer {
90
90
  * @returns {string[]}
91
91
  */
92
92
  debugModeSetup(): string[];
93
+ /**
94
+ * Return a map of key names to bound message getter functions. If `preGenerateBinds` is
95
+ * configured to be `true`, the binds will be created as a constant object in the output.
96
+ * Otherwise, the generation will be done at runtime through the `getBinds` method on the loader.
97
+ *
98
+ * @returns {string[]}
99
+ */
100
+ createLoaderAndBinds(): string[];
93
101
  /**
94
102
  * Return the lines to export fields from this module, as determined by the `exportMode` on this
95
103
  * transformer.
104
+ *
105
+ * @returns {string[]}
96
106
  */
97
107
  exportFields(): string[];
98
108
  /**
package/types/types.d.ts CHANGED
@@ -53,6 +53,22 @@ export interface MessageDefinitionsTransformerOptions {
53
53
  * @default esm
54
54
  */
55
55
  exportMode?: 'esm' | 'commonjs' | 'transpiledEsModule';
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.
68
+ *
69
+ * @default false
70
+ */
71
+ preGenerateBinds?: boolean;
56
72
  }
57
73
 
58
74
  /**
package/types.d.ts CHANGED
@@ -53,6 +53,22 @@ export interface MessageDefinitionsTransformerOptions {
53
53
  * @default esm
54
54
  */
55
55
  exportMode?: 'esm' | 'commonjs' | 'transpiledEsModule';
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.
68
+ *
69
+ * @default false
70
+ */
71
+ preGenerateBinds?: boolean;
56
72
  }
57
73
 
58
74
  /**