@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 +2 -2
- package/src/transformer.js +46 -72
- package/types/src/transformer.d.ts +20 -16
- package/types/types.d.ts +14 -13
- package/types.d.ts +14 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/intl-loader-core",
|
|
3
|
-
"version": "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.
|
|
31
|
+
"@discord/intl-message-database": "0.19.1-canary.f6592a8"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/debug": "^4.1.12",
|
package/src/transformer.js
CHANGED
|
@@ -18,18 +18,19 @@
|
|
|
18
18
|
*
|
|
19
19
|
* // SomeConsumer.tsx
|
|
20
20
|
* import someModuleMessages from 'SomeModule.messages.js';
|
|
21
|
-
*
|
|
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 {
|
|
29
|
-
* const
|
|
30
|
-
* const
|
|
31
|
-
* export
|
|
32
|
-
*
|
|
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
|
-
*
|
|
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
|
-
//
|
|
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 `
|
|
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
|
|
119
|
-
*
|
|
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
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
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(
|
|
127
|
-
return
|
|
128
|
-
{
|
|
129
|
-
|
|
130
|
-
|
|
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 `
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
|
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
|
-
*
|
|
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 {
|
|
29
|
-
* const
|
|
30
|
-
* const
|
|
31
|
-
* export
|
|
32
|
-
*
|
|
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
|
-
*
|
|
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 `
|
|
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
|
|
99
|
-
*
|
|
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
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
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(
|
|
110
|
+
createBindsProxy(): string[];
|
|
107
111
|
/**
|
|
108
|
-
* Return a map of key names to bound message getter functions. If `
|
|
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 `
|
|
58
|
-
* will be created
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
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
|
-
*
|
|
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
|
-
|
|
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 `
|
|
58
|
-
* will be created
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
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
|
-
*
|
|
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
|
-
|
|
72
|
+
bindMode?: 'proxy' | 'literal';
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
/**
|