@discord/metro-intl-transformer 0.0.5 → 0.0.6-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/asset-plugin.js +118 -0
- package/index.js +17 -6
- package/package.json +6 -3
package/asset-plugin.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const fs = require('node:fs');
|
|
2
|
+
const path = require('node:path');
|
|
3
|
+
const debug = require('debug')('intl:metro-intl-transformer:asset-plugin');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
isMessageTranslationsFile,
|
|
7
|
+
processTranslationsFile,
|
|
8
|
+
precompileFileForLocale,
|
|
9
|
+
IntlCompiledMessageFormat,
|
|
10
|
+
} = require('@discord/intl-loader-core');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef ResolvedIntlAssetPluginConfig
|
|
14
|
+
*
|
|
15
|
+
* @property {string=} cacheDir
|
|
16
|
+
* Directory to store compiled message assets while bundling. Defaults to `.cache/intl`. This
|
|
17
|
+
* directory is resolved relative to project root and _must_ live somewhere within it.
|
|
18
|
+
*
|
|
19
|
+
* @property {string=} assetExtension
|
|
20
|
+
* The final extension to use when searching for and creating assets. Defaults to `json`. Note that
|
|
21
|
+
* all compiled assets will include the initial extension `.compiled.messages.`, followed by this
|
|
22
|
+
* configuration.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {Partial<ResolvedIntlAssetPluginConfig>} IntlAssetPluginConfig
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** @type {ResolvedIntlAssetPluginConfig} */
|
|
30
|
+
const defaultConfig = {
|
|
31
|
+
cacheDir: path.join('.cache', 'intl'),
|
|
32
|
+
assetExtension: 'json',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Configure the asset-plugin for `@discord/metro-intl-transformer`. Necessary because Metro's
|
|
37
|
+
* `assetPlugins` configuration only accepts a list of module paths for plugins without an option
|
|
38
|
+
* for configuring them inline.
|
|
39
|
+
*
|
|
40
|
+
* This relies on extending `metro.config.js` with a new property, `intlAssetPlugin`, providing
|
|
41
|
+
* the desired configuration for that run of the bundler. If the Metro configuration for the current
|
|
42
|
+
* process is at a non-default location, this method will likely fail.
|
|
43
|
+
*/
|
|
44
|
+
async function fetchConfig() {
|
|
45
|
+
const metroConfigPath = path.join(process.cwd(), 'metro.config.js');
|
|
46
|
+
|
|
47
|
+
debug(`Loading Metro config from ${metroConfigPath}`);
|
|
48
|
+
let metroConfig;
|
|
49
|
+
try {
|
|
50
|
+
metroConfig = require(metroConfigPath);
|
|
51
|
+
debug(`Successfully loaded config: %o`, metroConfig);
|
|
52
|
+
} catch {
|
|
53
|
+
debug(`Failed to load Metro configuration. Using defaults instead.`);
|
|
54
|
+
return defaultConfig;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const config = metroConfig.transformer?.intlAssetPlugin;
|
|
58
|
+
if (config == null) {
|
|
59
|
+
return defaultConfig;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (config.cacheDir != null && path.isAbsolute(config.cacheDir)) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
'`cacheDir` configuration for the intl Metro asset-plugin must be a relative path.',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { ...defaultConfig, ...config };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const pluginConfig = fetchConfig();
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @param {any} assetData
|
|
75
|
+
*/
|
|
76
|
+
async function transformAsset(assetData) {
|
|
77
|
+
const { cacheDir, assetExtension } = await pluginConfig;
|
|
78
|
+
|
|
79
|
+
const filename = assetData.files[0] ?? '';
|
|
80
|
+
// If this isn't a translations file or if it's already a compiled artifact, then we don't want
|
|
81
|
+
// to do any more processing on it.
|
|
82
|
+
if (
|
|
83
|
+
filename === '' ||
|
|
84
|
+
!isMessageTranslationsFile(filename) ||
|
|
85
|
+
/\.compiled.messages\./.test(filename)
|
|
86
|
+
) {
|
|
87
|
+
return assetData;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
debug(`[${filename}] Processing translation asset`);
|
|
91
|
+
|
|
92
|
+
const outputDir = path.resolve(assetData.fileSystemLocation, cacheDir);
|
|
93
|
+
const outputName = `${assetData.name}.${assetData.hash}.compiled.messages`;
|
|
94
|
+
const outputFile = path.join(outputDir, outputName);
|
|
95
|
+
debug(`[${filename}] Output file path: ${outputFile}`);
|
|
96
|
+
|
|
97
|
+
if (!fs.existsSync(outputDir)) {
|
|
98
|
+
debug(`[${filename}] Cache dir ${outputDir} did not exist. Creating it now`);
|
|
99
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const result = processTranslationsFile(filename);
|
|
103
|
+
precompileFileForLocale(filename, result.locale, {
|
|
104
|
+
format: IntlCompiledMessageFormat.Json,
|
|
105
|
+
outputFile: `${outputFile}.${assetExtension}`,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
...assetData,
|
|
110
|
+
fileSystemLocation: outputDir,
|
|
111
|
+
httpServerLocation: `${assetData.httpServerLocation}/${cacheDir}`,
|
|
112
|
+
files: [outputFile],
|
|
113
|
+
name: outputName,
|
|
114
|
+
type: assetExtension,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
module.exports = transformAsset;
|
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
MessageDefinitionsTransformer,
|
|
6
6
|
processTranslationsFile,
|
|
7
7
|
precompileFileForLocale,
|
|
8
|
+
IntlCompiledMessageFormat,
|
|
8
9
|
} = require('@discord/intl-loader-core');
|
|
9
10
|
const debug = require('debug')('intl:metro-intl-transformer');
|
|
10
11
|
|
|
@@ -26,28 +27,38 @@ function transformToString({
|
|
|
26
27
|
getTranslationImport,
|
|
27
28
|
}) {
|
|
28
29
|
if (isMessageDefinitionsFile(filename)) {
|
|
29
|
-
debug(`
|
|
30
|
-
const result = processDefinitionsFile(filename, src
|
|
30
|
+
debug(`[${filename}] Processing as a definitions file`);
|
|
31
|
+
const result = processDefinitionsFile(filename, src, {
|
|
32
|
+
// TODO: Make this more configurable
|
|
33
|
+
locale: 'en-US',
|
|
34
|
+
});
|
|
31
35
|
const compiledSourcePath = filename.replace(
|
|
32
36
|
/\.messages\.js$/,
|
|
33
37
|
`.compiled.messages.${getTranslationAssetExtension()}`,
|
|
34
38
|
);
|
|
35
39
|
|
|
36
|
-
debug(
|
|
40
|
+
debug('Locale map created: %O', result.translationsLocaleMap);
|
|
41
|
+
debug(
|
|
42
|
+
`[${filename}] Resolving source file to compiled translations file ${compiledSourcePath}`,
|
|
43
|
+
);
|
|
37
44
|
result.translationsLocaleMap['en-US'] = compiledSourcePath;
|
|
38
45
|
|
|
39
46
|
return new MessageDefinitionsTransformer({
|
|
40
|
-
messageKeys: result.
|
|
47
|
+
messageKeys: result.messageKeys,
|
|
41
48
|
localeMap: result.translationsLocaleMap,
|
|
49
|
+
defaultLocale: result.locale,
|
|
42
50
|
getTranslationImport,
|
|
43
51
|
getPrelude,
|
|
52
|
+
debug: process.env.NODE_ENV === 'development',
|
|
44
53
|
}).getOutput();
|
|
45
54
|
} else if (isMessageTranslationsFile(filename)) {
|
|
46
|
-
debug(`
|
|
55
|
+
debug(`[${filename}] Processing as a translations file`);
|
|
47
56
|
const result = processTranslationsFile(filename, src);
|
|
48
57
|
// @ts-expect-error Without the `outputFile` option, this always returns a Buffer, but the
|
|
49
58
|
// option allows the function to return void instead.
|
|
50
|
-
return precompileFileForLocale(filename, result.locale
|
|
59
|
+
return precompileFileForLocale(filename, result.locale, {
|
|
60
|
+
format: IntlCompiledMessageFormat.Json,
|
|
61
|
+
});
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
return src;
|
package/package.json
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/metro-intl-transformer",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6-rc.0",
|
|
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": {
|
|
7
7
|
".": "./index.js",
|
|
8
|
+
"./asset-plugin": "./asset-plugin.js",
|
|
8
9
|
"./babel-plugin": "./babel-plugin.js"
|
|
9
10
|
},
|
|
10
11
|
"dependencies": {
|
|
11
|
-
"
|
|
12
|
+
"debug": "^4.3.6",
|
|
13
|
+
"@discord/intl-loader-core": "0.0.6-rc.0"
|
|
12
14
|
},
|
|
13
15
|
"devDependencies": {
|
|
14
16
|
"@babel/types": "^7.24.9",
|
|
15
|
-
"@types/babel__traverse": "7.20.6"
|
|
17
|
+
"@types/babel__traverse": "7.20.6",
|
|
18
|
+
"@types/debug": "^4.1.12"
|
|
16
19
|
}
|
|
17
20
|
}
|