@discord/jest-processor-discord-intl 0.23.1 → 0.23.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/index.js +72 -64
- package/package.json +5 -2
package/index.js
CHANGED
|
@@ -9,75 +9,83 @@ const {
|
|
|
9
9
|
IntlCompiledMessageFormat,
|
|
10
10
|
} = require('@discord/intl-loader-core');
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
bundleSecrets: true,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const forceTranslation = filePath === '?forceTranslation';
|
|
28
|
-
if (isMessageDefinitionsFile(filePath) && !forceTranslation) {
|
|
29
|
-
const result = processDefinitionsFile(filePath, source, { processTranslations: false });
|
|
30
|
-
if (!result.succeeded) {
|
|
31
|
-
throw new Error(
|
|
32
|
-
`Failed to process intl messages file ${filePath}: ` + result.errors[0].message,
|
|
33
|
-
);
|
|
34
|
-
}
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} source
|
|
14
|
+
* @param {string} filePath
|
|
15
|
+
* @param {object} _config
|
|
16
|
+
* @returns {string}
|
|
17
|
+
*/
|
|
18
|
+
function _processCode(source, filePath, _config) {
|
|
19
|
+
const precompileOptions = {
|
|
20
|
+
format: IntlCompiledMessageFormat.KeylessJson,
|
|
21
|
+
// Always bundle secrets when running tests
|
|
22
|
+
bundleSecrets: true,
|
|
23
|
+
};
|
|
35
24
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
const forceTranslation = filePath === '?forceTranslation';
|
|
26
|
+
if (isMessageDefinitionsFile(filePath) && !forceTranslation) {
|
|
27
|
+
const result = processDefinitionsFile(filePath, source, { processTranslations: false });
|
|
28
|
+
if (!result.succeeded) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Failed to process intl messages file ${filePath}: ` + result.errors[0].message,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
39
33
|
|
|
40
|
-
|
|
34
|
+
const sourceMessages = /** @type {Buffer} */ (
|
|
35
|
+
precompileFileForLocale(filePath, result.locale, undefined, precompileOptions)
|
|
36
|
+
).toString();
|
|
41
37
|
|
|
42
|
-
|
|
43
|
-
messageKeys: result.messageKeys,
|
|
44
|
-
localeMap: result.translationsLocaleMap,
|
|
45
|
-
defaultLocale: result.locale,
|
|
46
|
-
getTranslationImport: (importPath) => {
|
|
47
|
-
// Embed source translations directly into the loader, because Jest doesn't have a loader
|
|
48
|
-
// mechanism to create new dependencies while transforming a file.
|
|
49
|
-
if (importPath === 'intl$sentinel-use-local') {
|
|
50
|
-
return `Promise.resolve({default: ${sourceMessages}})`;
|
|
51
|
-
}
|
|
52
|
-
return `Promise.resolve(require("${importPath}"))`;
|
|
53
|
-
},
|
|
54
|
-
debug: true,
|
|
55
|
-
// Jest won't pass the result of this transpilation through another processor, and expects
|
|
56
|
-
// babel-like transpilation to have occurred, so this processor exports transpiled-
|
|
57
|
-
// compatible values as a safe default.
|
|
58
|
-
exportMode: 'transpiledEsModule',
|
|
59
|
-
}).getOutput();
|
|
60
|
-
} else {
|
|
61
|
-
const locale = getLocaleFromTranslationsFileName(filePath);
|
|
62
|
-
if (isMessageTranslationsFile(filePath)) {
|
|
63
|
-
processTranslationsFile(filePath, source, { locale });
|
|
64
|
-
} else if (!forceTranslation) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
'Expected a translation file or the `forceTranslation` query parameter on this import, but none was found',
|
|
67
|
-
);
|
|
68
|
-
}
|
|
38
|
+
result.translationsLocaleMap[result.locale] = 'intl$sentinel-use-local';
|
|
69
39
|
|
|
70
|
-
|
|
71
|
-
|
|
40
|
+
return new MessageDefinitionsTransformer({
|
|
41
|
+
messageKeys: result.messageKeys,
|
|
42
|
+
localeMap: result.translationsLocaleMap,
|
|
43
|
+
defaultLocale: result.locale,
|
|
44
|
+
getTranslationImport: (importPath) => {
|
|
45
|
+
// Embed source translations directly into the loader, because Jest doesn't have a loader
|
|
46
|
+
// mechanism to create new dependencies while transforming a file.
|
|
47
|
+
if (importPath === 'intl$sentinel-use-local') {
|
|
48
|
+
return `Promise.resolve({default: ${sourceMessages}})`;
|
|
49
|
+
}
|
|
50
|
+
return `Promise.resolve(require("${importPath}"))`;
|
|
51
|
+
},
|
|
52
|
+
debug: true,
|
|
53
|
+
// Jest won't pass the result of this transpilation through another processor, and expects
|
|
54
|
+
// babel-like transpilation to have occurred, so this processor exports transpiled-
|
|
55
|
+
// compatible values as a safe default.
|
|
56
|
+
exportMode: 'transpiledEsModule',
|
|
57
|
+
}).getOutput();
|
|
58
|
+
} else {
|
|
59
|
+
const locale = getLocaleFromTranslationsFileName(filePath);
|
|
60
|
+
if (isMessageTranslationsFile(filePath)) {
|
|
61
|
+
processTranslationsFile(filePath, source, { locale });
|
|
62
|
+
} else if (!forceTranslation) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
'Expected a translation file or the `forceTranslation` query parameter on this import, but none was found',
|
|
72
65
|
);
|
|
73
|
-
|
|
74
|
-
if (forceTranslation) {
|
|
75
|
-
return (
|
|
76
|
-
'module.exports.default = JSON.parse(' + JSON.stringify(compiledResult?.toString()) + ')'
|
|
77
|
-
);
|
|
78
|
-
} else {
|
|
79
|
-
return compiledResult.toString();
|
|
80
|
-
}
|
|
81
66
|
}
|
|
67
|
+
|
|
68
|
+
const compiledResult = /** @type {Buffer} */ (
|
|
69
|
+
precompileFileForLocale(filePath, locale, undefined, precompileOptions)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return forceTranslation
|
|
73
|
+
? 'module.exports.default = JSON.parse(' + JSON.stringify(compiledResult?.toString()) + ')'
|
|
74
|
+
: compiledResult.toString();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = {
|
|
79
|
+
INTL_MESSAGES_FILE_PATTERN: '\\.messages\\.(js|json|jsona)(\\?forceTranslation)?$',
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {string} source
|
|
83
|
+
* @param {string} filePath
|
|
84
|
+
* @param {object} config
|
|
85
|
+
* @returns {import('@jest/types').TransformTypes.TransformResult}
|
|
86
|
+
*/
|
|
87
|
+
process(source, filePath, config) {
|
|
88
|
+
const code = _processCode(source, filePath, config);
|
|
89
|
+
return { code, originalCode: source, sourceMapPath: null };
|
|
82
90
|
},
|
|
83
91
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/jest-processor-discord-intl",
|
|
3
|
-
"version": "0.23.
|
|
3
|
+
"version": "0.23.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Processor plugin for Jest to handle intl message definitions and translations from @discord/intl",
|
|
6
6
|
"exports": {
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"url": "https://github.com/discord/discord-intl"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@discord/intl-loader-core": "0.23.
|
|
14
|
+
"@discord/intl-loader-core": "0.23.3"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@jest/types": "^29.6.3"
|
|
15
18
|
}
|
|
16
19
|
}
|