@discord/metro-intl-transformer 0.10.4 → 0.11.1
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/asset-plugin.js +41 -24
- package/package.json +4 -3
package/asset-plugin.js
CHANGED
|
@@ -1,34 +1,48 @@
|
|
|
1
1
|
const fs = require('node:fs');
|
|
2
2
|
const path = require('node:path');
|
|
3
3
|
const debug = require('debug')('intl:metro-intl-transformer:asset-plugin');
|
|
4
|
+
const metro = require('metro-config');
|
|
4
5
|
|
|
5
6
|
const {
|
|
6
7
|
isMessageTranslationsFile,
|
|
7
8
|
processTranslationsFile,
|
|
8
9
|
precompileFileForLocale,
|
|
9
10
|
IntlCompiledMessageFormat,
|
|
11
|
+
findAllMessagesFiles,
|
|
12
|
+
processAllMessagesFiles,
|
|
13
|
+
database,
|
|
10
14
|
} = require('@discord/intl-loader-core');
|
|
11
15
|
|
|
12
16
|
/**
|
|
13
17
|
* @typedef ResolvedIntlAssetPluginConfig
|
|
14
18
|
*
|
|
15
|
-
* @property {string
|
|
19
|
+
* @property {string} cacheDir
|
|
16
20
|
* Directory to store compiled message assets while bundling. Defaults to `.cache/intl`. This
|
|
17
21
|
* directory is resolved relative to project root and _must_ live somewhere within it.
|
|
18
22
|
*
|
|
19
|
-
* @property {string
|
|
23
|
+
* @property {string} assetExtension
|
|
20
24
|
* The final extension to use when searching for and creating assets. Defaults to `json`. Note that
|
|
21
25
|
* all compiled assets will include the initial extension `.compiled.messages.`, followed by this
|
|
22
26
|
* configuration.
|
|
23
27
|
*
|
|
24
|
-
* @property {boolean
|
|
28
|
+
* @property {boolean} bundleSecrets
|
|
25
29
|
* Whether messages marked as `secret` will be preserved in the bundled message assets. When false,
|
|
26
30
|
* secret messages have their values replaced with obfuscated text to prevent leaking information.
|
|
27
31
|
*
|
|
28
|
-
* @property {IntlCompiledMessageFormat
|
|
32
|
+
* @property {IntlCompiledMessageFormat} format
|
|
29
33
|
* The format to which messages should be compiled during bundling. `Json` will cause the messages
|
|
30
34
|
* to be compiled to a FormatJS-like compatible format, while `KeylessJson` will use a much more
|
|
31
35
|
* compressed, `@discord/intl`-specific format. `KeylessJson` is the default.
|
|
36
|
+
*
|
|
37
|
+
* @property {string[]} watchFolders
|
|
38
|
+
* The folders Metro is configured to watch. A necessary argument to be able to preload all message
|
|
39
|
+
* definitions and ensure that merged translations files handle secrets appropriately based on
|
|
40
|
+
* matching definitions, which may otherwise be missed if the definitions files are handled by
|
|
41
|
+
* different workers. If not specified directly in the configuration file (either through the
|
|
42
|
+
* plugin's configuration or on Metro itself), this will default to `process.cwd()`.
|
|
43
|
+
*
|
|
44
|
+
* This configuration also automatically reads `projectRoot` and replaces `process.cwd()` when it
|
|
45
|
+
* has been specified.
|
|
32
46
|
*/
|
|
33
47
|
|
|
34
48
|
/**
|
|
@@ -41,6 +55,7 @@ const defaultConfig = {
|
|
|
41
55
|
assetExtension: 'json',
|
|
42
56
|
format: IntlCompiledMessageFormat.KeylessJson,
|
|
43
57
|
bundleSecrets: false,
|
|
58
|
+
watchFolders: [process.cwd()],
|
|
44
59
|
};
|
|
45
60
|
|
|
46
61
|
/**
|
|
@@ -51,24 +66,22 @@ const defaultConfig = {
|
|
|
51
66
|
* This relies on extending `metro.config.js` with a new property, `intlAssetPlugin`, providing
|
|
52
67
|
* the desired configuration for that run of the bundler. If the Metro configuration for the current
|
|
53
68
|
* process is at a non-default location, this method will likely fail.
|
|
69
|
+
*
|
|
70
|
+
* @returns {Promise<ResolvedIntlAssetPluginConfig>}
|
|
54
71
|
*/
|
|
55
72
|
async function fetchConfig() {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
debug(`Successfully loaded config metro config`);
|
|
63
|
-
} catch {
|
|
64
|
-
debug(`Failed to load Metro configuration. Using defaults instead.`);
|
|
65
|
-
return defaultConfig;
|
|
66
|
-
}
|
|
73
|
+
debug(`Loading Metro config`);
|
|
74
|
+
const metroConfig = await metro.loadConfig();
|
|
75
|
+
const defaults = {
|
|
76
|
+
...defaultConfig,
|
|
77
|
+
watchFolders: [metroConfig.projectRoot, ...metroConfig.watchFolders],
|
|
78
|
+
};
|
|
67
79
|
|
|
80
|
+
// @ts-expect-error We're latching on `intlAssetPlugin` as a field in the transformer config.
|
|
68
81
|
const config = metroConfig.transformer?.intlAssetPlugin;
|
|
69
82
|
if (config == null) {
|
|
70
|
-
debug('asset-plugin configuration was empty. Using defaults: %O',
|
|
71
|
-
return
|
|
83
|
+
debug('asset-plugin configuration was empty. Using defaults: %O', defaults);
|
|
84
|
+
return { ...defaults };
|
|
72
85
|
}
|
|
73
86
|
debug(`discovered asset-plugin configuration: %O`, config);
|
|
74
87
|
|
|
@@ -78,21 +91,25 @@ async function fetchConfig() {
|
|
|
78
91
|
);
|
|
79
92
|
}
|
|
80
93
|
|
|
81
|
-
return { ...
|
|
94
|
+
return { ...defaults, ...config };
|
|
82
95
|
}
|
|
83
96
|
|
|
84
97
|
const pluginConfig = fetchConfig();
|
|
85
98
|
|
|
99
|
+
let hasInitializedAllDefinitions = false;
|
|
100
|
+
|
|
86
101
|
/**
|
|
87
102
|
* @param {any} assetData
|
|
88
103
|
*/
|
|
89
104
|
async function transformAsset(assetData) {
|
|
90
|
-
const {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
105
|
+
const { cacheDir, assetExtension, format, bundleSecrets, watchFolders } = await pluginConfig;
|
|
106
|
+
|
|
107
|
+
if (!hasInitializedAllDefinitions) {
|
|
108
|
+
debug('Initializing database with all messages files within watch folders: %O', watchFolders);
|
|
109
|
+
processAllMessagesFiles(findAllMessagesFiles(watchFolders));
|
|
110
|
+
database.getAllSourceFilePaths();
|
|
111
|
+
hasInitializedAllDefinitions = true;
|
|
112
|
+
}
|
|
96
113
|
|
|
97
114
|
const filename = assetData.files[0] ?? '';
|
|
98
115
|
// If this isn't a translations file or if it's already a compiled artifact, then we don't want
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/metro-intl-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Metro/Babel transformer plugin for compiling, bundling, and minifying intl message definitions and usages using intl-message-database.",
|
|
6
6
|
"exports": {
|
|
@@ -13,8 +13,9 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"debug": "^4.3.6",
|
|
16
|
-
"
|
|
17
|
-
"@discord/
|
|
16
|
+
"metro-config": "^0.81.0",
|
|
17
|
+
"@discord/babel-plugin-transform-discord-intl": "0.11.1",
|
|
18
|
+
"@discord/intl-loader-core": "0.11.1"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"@types/debug": "^4.1.12"
|