@discord/jest-processor-discord-intl 0.8.0-rc.0
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/LICENSE +21 -0
- package/README.md +39 -0
- package/index.js +76 -0
- package/package.json +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Discord, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @discord/jest-processor-discord-intl
|
|
2
|
+
|
|
3
|
+
A processor plugin for Jest to handle intl message definitions and translations from `@discord/intl`.
|
|
4
|
+
|
|
5
|
+
In your Jest config, add this as a transformer for message file patterns:
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
module.exports = {
|
|
9
|
+
transform: {
|
|
10
|
+
// Order matters here! Even though this is an Object, Jest still processes these first to
|
|
11
|
+
// last (object insertion order). These patterns can overlap, so we want to put all of the
|
|
12
|
+
// special handling first.
|
|
13
|
+
[INTL_MESSAGES_FILE_PATTERN]: require.resolve('@discord/jest-processor-discord-intl'),
|
|
14
|
+
// ...other transforms, like `*.tsx?` and more.
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
You'll also need to use the babel plugin to transform consuming code like normal bundling:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
// In a custom processor or wherever you configure Babel for Jest:
|
|
23
|
+
babel.transform({
|
|
24
|
+
// ...
|
|
25
|
+
plugins: [
|
|
26
|
+
[
|
|
27
|
+
require.resolve('@discord/babel-plugin-transform-discord-intl'),
|
|
28
|
+
{
|
|
29
|
+
// Jest does _not_ resolve these to absolute paths, so tell the plugin to use the
|
|
30
|
+
// original import paths instead of resolving them like it does for Metro.
|
|
31
|
+
preserveImportSource: true,
|
|
32
|
+
extraImports: {
|
|
33
|
+
'@app/intl': ['t', 'untranslated', 'international'],
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
const {
|
|
2
|
+
processDefinitionsFile,
|
|
3
|
+
processTranslationsFile,
|
|
4
|
+
isMessageDefinitionsFile,
|
|
5
|
+
MessageDefinitionsTransformer,
|
|
6
|
+
isMessageTranslationsFile,
|
|
7
|
+
getLocaleFromTranslationsFileName,
|
|
8
|
+
precompileFileForLocale,
|
|
9
|
+
IntlCompiledMessageFormat,
|
|
10
|
+
} = require('@discord/intl-loader-core');
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
INTL_MESSAGES_FILE_PATTERN: '\\.messages\\.(js|json|jsona)(\\?forceTranslation)?$',
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} source
|
|
16
|
+
* @param {string} filePath
|
|
17
|
+
* @param {object} _config
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
process(source, filePath, _config) {
|
|
21
|
+
const forceTranslation = filePath === '?forceTranslation';
|
|
22
|
+
if (isMessageDefinitionsFile(filePath) && !forceTranslation) {
|
|
23
|
+
const result = processDefinitionsFile(filePath, source, { processTranslations: false });
|
|
24
|
+
|
|
25
|
+
const sourceMessages = /** @type {Buffer} */ (
|
|
26
|
+
precompileFileForLocale(filePath, result.locale, {
|
|
27
|
+
format: IntlCompiledMessageFormat.KeylessJson,
|
|
28
|
+
})
|
|
29
|
+
).toString();
|
|
30
|
+
|
|
31
|
+
result.translationsLocaleMap[result.locale] = 'intl$sentinel-use-local';
|
|
32
|
+
|
|
33
|
+
return new MessageDefinitionsTransformer({
|
|
34
|
+
messageKeys: result.messageKeys,
|
|
35
|
+
localeMap: result.translationsLocaleMap,
|
|
36
|
+
defaultLocale: result.locale,
|
|
37
|
+
getTranslationImport: (importPath) => {
|
|
38
|
+
// Embed source translations directly into the loader, because Jest doesn't have a loader
|
|
39
|
+
// mechanism to create new dependencies while transforming a file.
|
|
40
|
+
if (importPath === 'intl$sentinel-use-local') {
|
|
41
|
+
return `Promise.resolve({default: ${sourceMessages}})`;
|
|
42
|
+
}
|
|
43
|
+
return `Promise.resolve(require("${importPath}"))`;
|
|
44
|
+
},
|
|
45
|
+
debug: true,
|
|
46
|
+
// Jest won't pass the result of this transpilation through another processor, and expects
|
|
47
|
+
// babel-like transpilation to have occurred, so this processor exports transpiled-
|
|
48
|
+
// compatible values as a safe default.
|
|
49
|
+
exportMode: 'transpiledEsModule',
|
|
50
|
+
}).getOutput();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const locale = getLocaleFromTranslationsFileName(filePath);
|
|
54
|
+
if (isMessageTranslationsFile(filePath)) {
|
|
55
|
+
processTranslationsFile(filePath, source, { locale });
|
|
56
|
+
} else if (!forceTranslation) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
'Expected a translation file or the `forceTranslation` query parameter on this import, but none was found',
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const compiledResult = /** @type {Buffer} */ (
|
|
63
|
+
precompileFileForLocale(filePath, locale, {
|
|
64
|
+
format: IntlCompiledMessageFormat.KeylessJson,
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (forceTranslation) {
|
|
69
|
+
return (
|
|
70
|
+
'module.exports.default = JSON.parse(' + JSON.stringify(compiledResult?.toString()) + ')'
|
|
71
|
+
);
|
|
72
|
+
} else {
|
|
73
|
+
return compiledResult.toString();
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@discord/jest-processor-discord-intl",
|
|
3
|
+
"version": "0.8.0-rc.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Processor plugin for Jest to handle intl message definitions and translations from @discord/intl",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@discord/intl-loader-core": "0.8.0-rc.0"
|
|
11
|
+
}
|
|
12
|
+
}
|