@discord/metro-intl-transformer 0.0.3 → 0.0.4-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/babel-plugin.js +1 -1
- package/index.js +30 -35
- package/package.json +4 -9
- package/src/database.js +0 -9
- package/src/transformer.js +0 -116
- package/src/virtual-modules.js +0 -30
- package/watcher.js +0 -88
package/babel-plugin.js
CHANGED
package/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
const path = require('node:path');
|
|
2
1
|
const {
|
|
3
2
|
isMessageDefinitionsFile,
|
|
4
3
|
isMessageTranslationsFile,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
processDefinitionsFile,
|
|
5
|
+
MessageDefinitionsTransformer,
|
|
6
|
+
processTranslationsFile,
|
|
7
|
+
precompileFileForLocale,
|
|
8
|
+
} = require('@discord/intl-loader-core');
|
|
9
|
+
const debug = require('debug')('intl:metro-intl-transformer');
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* @param {{
|
|
@@ -13,49 +14,43 @@ const { MessageDefinitionsTransformer } = require('./src/transformer');
|
|
|
13
14
|
* src: string,
|
|
14
15
|
* getPrelude: () => string
|
|
15
16
|
* getTranslationAssetExtension: () => string,
|
|
16
|
-
*
|
|
17
|
+
* getTranslationImport: (importPath: string) => string,
|
|
17
18
|
* }} options
|
|
18
|
-
* @returns
|
|
19
|
+
* @returns {string | Buffer}
|
|
19
20
|
*/
|
|
20
|
-
|
|
21
|
+
function transformToString({
|
|
21
22
|
filename,
|
|
22
23
|
src,
|
|
23
24
|
getPrelude,
|
|
24
25
|
getTranslationAssetExtension,
|
|
25
|
-
|
|
26
|
+
getTranslationImport,
|
|
26
27
|
}) {
|
|
27
28
|
if (isMessageDefinitionsFile(filename)) {
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
29
|
+
debug(`Processing ${filename} as a definitions file`);
|
|
30
|
+
const result = processDefinitionsFile(filename, src);
|
|
31
|
+
const compiledSourcePath = filename.replace(
|
|
32
|
+
/\.messages\.js$/,
|
|
33
|
+
`.compiled.messages.${getTranslationAssetExtension()}`,
|
|
34
|
+
);
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const localeMap = {};
|
|
39
|
-
for (const locale of locales) {
|
|
40
|
-
localeMap[locale] =
|
|
41
|
-
`${sourceFile.meta.translationsPath}/${locale}.${getTranslationAssetExtension()}`;
|
|
42
|
-
}
|
|
43
|
-
const fileBasename = filename.substring(0, filename.lastIndexOf('.messages.js'));
|
|
44
|
-
localeMap['en-US'] = path.dirname(fileBasename + '.compiled.messages.jsona');
|
|
36
|
+
debug(`Resolving source file ${filename} to compiled translations file ${compiledSourcePath}`);
|
|
37
|
+
result.translationsLocaleMap['en-US'] = compiledSourcePath;
|
|
45
38
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
localeMap,
|
|
49
|
-
|
|
39
|
+
return new MessageDefinitionsTransformer({
|
|
40
|
+
messageKeys: result.hashedMessageKeys,
|
|
41
|
+
localeMap: result.translationsLocaleMap,
|
|
42
|
+
getTranslationImport,
|
|
50
43
|
getPrelude,
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
return transformer.getOutput();
|
|
44
|
+
}).getOutput();
|
|
54
45
|
} else if (isMessageTranslationsFile(filename)) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
46
|
+
debug(`Processing ${filename} as a translations file`);
|
|
47
|
+
const result = processTranslationsFile(filename, src);
|
|
48
|
+
// @ts-expect-error Without the `outputFile` option, this always returns a Buffer, but the
|
|
49
|
+
// option allows the function to return void instead.
|
|
50
|
+
return precompileFileForLocale(filename, result.locale);
|
|
58
51
|
}
|
|
52
|
+
|
|
53
|
+
return src;
|
|
59
54
|
}
|
|
60
55
|
|
|
61
56
|
module.exports = { transformToString };
|
package/package.json
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/metro-intl-transformer",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4-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
|
-
"./babel-plugin": "./babel-plugin.js"
|
|
9
|
-
"./watcher": "./watcher.js"
|
|
8
|
+
"./babel-plugin": "./babel-plugin.js"
|
|
10
9
|
},
|
|
11
10
|
"dependencies": {
|
|
12
|
-
"
|
|
13
|
-
"debug": "^4.3.6",
|
|
14
|
-
"fast-glob": "^3.3.2",
|
|
15
|
-
"@discord/intl-message-database": "0.0.3"
|
|
11
|
+
"@discord/intl-loader-core": "0.0.4-rc.0"
|
|
16
12
|
},
|
|
17
13
|
"devDependencies": {
|
|
18
14
|
"@babel/types": "^7.24.9",
|
|
19
|
-
"@types/babel__traverse": "7.20.6"
|
|
20
|
-
"@types/debug": "^4.1.12"
|
|
15
|
+
"@types/babel__traverse": "7.20.6"
|
|
21
16
|
}
|
|
22
17
|
}
|
package/src/database.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
const {IntlMessagesDatabase} = require('@discord/intl-message-database');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A shared message database instance that's used and shared across all parts
|
|
5
|
-
* of the loader and plugin together.
|
|
6
|
-
*/
|
|
7
|
-
const database = new IntlMessagesDatabase();
|
|
8
|
-
|
|
9
|
-
module.exports = {database};
|
package/src/transformer.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Common class for parsing and transforming the content of a messages
|
|
3
|
-
* definition file (e.g., "SomeFeature.messages.js") into a production-ready
|
|
4
|
-
* version, with string keys obfuscated, loading harnesses configured, and
|
|
5
|
-
* more.
|
|
6
|
-
*
|
|
7
|
-
* This transformation is intended to be used alongside the consumer
|
|
8
|
-
* transforms implemented as SWC and Babel plugins, which transform the
|
|
9
|
-
* callsites for messages into matching formats. Consider this input example:
|
|
10
|
-
*
|
|
11
|
-
* ```typescript
|
|
12
|
-
* // SomeModule.messages.js
|
|
13
|
-
* import {defineMessages} from '@discord/intl';
|
|
14
|
-
*
|
|
15
|
-
* export default defineMessages({
|
|
16
|
-
* THIS_IS_A_MESSAGE: 'it has some content with {values}',
|
|
17
|
-
* });
|
|
18
|
-
*
|
|
19
|
-
* // SomeConsumer.tsx
|
|
20
|
-
* import someModuleMessages from 'SomeModule.messages.js';
|
|
21
|
-
* i18n.format(someModuleMessages.THIS_IS_A_MESSAGE, {values: "i'm a value!"});
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* This transformer will only handle `SomeModule.messages.js`, and will output
|
|
25
|
-
* something like:
|
|
26
|
-
*
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const {i18n} = require('@discord/intl');
|
|
29
|
-
* const MESSAGE_KEYS = ["a9fn23"]
|
|
30
|
-
* const loader = createLoader(MESSAGE_KEYS, {"en-US": () => require('./messages/en-US.messages.jsona')});
|
|
31
|
-
* export default MESSAGE_KEYS.reduce((acc, k) => {acc[k] = loader.bindFor(k); return acc}, {});
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* Notice how the message keys have been hashed into short keys, and the
|
|
35
|
-
* dynamic imports for each locale's data have been inserted automatically.
|
|
36
|
-
*
|
|
37
|
-
* The SWC and Babel transformers then take care of the second file,
|
|
38
|
-
* transforming the usage to use the hashed keys:
|
|
39
|
-
*
|
|
40
|
-
* ```typescript
|
|
41
|
-
* import someModuleMessages from 'SomeModule.messages.js';
|
|
42
|
-
* i18n.format(someModuleMessages["a9fn23"], {values: "i'm a value!"});
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
class MessageDefinitionsTransformer {
|
|
46
|
-
/**
|
|
47
|
-
* @param {string[]} messageKeys The list of hashed message keys that this file manages.
|
|
48
|
-
* @param {Record<string, string>} localeMap A map of locale names to paths used for importing translations.
|
|
49
|
-
* @param {() => string} getPrelude A function that returns a prelude to inject at the start of the transformed file.
|
|
50
|
-
* @param {(importPath: string) => string} createAssetImport A function to generate a require statement for the compiled asset file.
|
|
51
|
-
*/
|
|
52
|
-
constructor(messageKeys, localeMap, createAssetImport, getPrelude) {
|
|
53
|
-
this.messageKeys = messageKeys;
|
|
54
|
-
this.localeMap = localeMap;
|
|
55
|
-
this.getPrelude = getPrelude;
|
|
56
|
-
this.createAssetImport = createAssetImport;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Returns a compiled string for an object that maps locale names to a
|
|
61
|
-
* dynamic require function for that locale, based on the supported locales
|
|
62
|
-
* that were determined for this file. The shape ends up as:
|
|
63
|
-
*
|
|
64
|
-
* ```typescript
|
|
65
|
-
* {
|
|
66
|
-
* "en-US": () => import("path/to/en-US.json"),
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* @returns {string}
|
|
71
|
-
*/
|
|
72
|
-
getLocaleRequireMap() {
|
|
73
|
-
const localeProperties = [];
|
|
74
|
-
for (const [locale, importPath] of Object.entries(this.localeMap)) {
|
|
75
|
-
// leading `./`, which is necessary for the bundler to be able to
|
|
76
|
-
// NOTE: This doesn't use `path.join` because it will remove any
|
|
77
|
-
// resolve the import as a relative path.
|
|
78
|
-
// This also assumes that the author has specified `translationsPath`
|
|
79
|
-
// as a properly-resolvable path for the bundler, which we can't easily
|
|
80
|
-
// enforce, unfortunately.
|
|
81
|
-
localeProperties.push(
|
|
82
|
-
`"${locale}": () => ${this.createAssetImport(importPath)}.then((data) => ({default: data}))`,
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return `{${localeProperties.join(',')}}`;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns the reduced, transformed output for this file. Currently not
|
|
91
|
-
* configurable, but could be told to include default messages or preserve
|
|
92
|
-
* information as necessary.
|
|
93
|
-
*/
|
|
94
|
-
getOutput() {
|
|
95
|
-
const messageKeys = this.messageKeys;
|
|
96
|
-
|
|
97
|
-
const i18nImport = `const {createLoader} = require('@discord/intl');`;
|
|
98
|
-
const keysDefinition = `const _keys = ${JSON.stringify(messageKeys)};`;
|
|
99
|
-
const localeMapDefinition = `const _locales = ${this.getLocaleRequireMap()};`;
|
|
100
|
-
const loaderDefinition = 'const loader = createLoader(_keys, _locales);';
|
|
101
|
-
const messagesExport = 'export default loader.getBinds();';
|
|
102
|
-
|
|
103
|
-
return [
|
|
104
|
-
this.getPrelude(),
|
|
105
|
-
i18nImport,
|
|
106
|
-
keysDefinition,
|
|
107
|
-
localeMapDefinition,
|
|
108
|
-
loaderDefinition,
|
|
109
|
-
messagesExport,
|
|
110
|
-
].join('\n');
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
module.exports = {
|
|
115
|
-
MessageDefinitionsTransformer,
|
|
116
|
-
};
|
package/src/virtual-modules.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const fs = require('node:fs');
|
|
2
|
-
const path = require('node:path');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Ensure the configured cache directory exists so that files can be read from and written to it.
|
|
6
|
-
* Call this once when Metro is first started to ensure that the folder exists and has been
|
|
7
|
-
* cleaned.
|
|
8
|
-
*
|
|
9
|
-
* @param {string} cacheDir
|
|
10
|
-
*/
|
|
11
|
-
function ensureVirtualTranslationsModulesDirectory(cacheDir) {
|
|
12
|
-
fs.mkdirSync(cacheDir, { recursive: true });
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Returns the absolute path to the virtual translations module that
|
|
17
|
-
* `moduleName` should resolve to.
|
|
18
|
-
* @param {string} cacheDir
|
|
19
|
-
* @param {string} moduleName
|
|
20
|
-
* @returns {string}
|
|
21
|
-
*/
|
|
22
|
-
function getVirtualTranslationsModulePath(cacheDir, moduleName) {
|
|
23
|
-
const resolvedModuleName = moduleName.replace(/[\/\\]+/g, '+').replace(/[:\?<>"\*\|]/g, '!');
|
|
24
|
-
return path.resolve(cacheDir, resolvedModuleName);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = {
|
|
28
|
-
ensureVirtualTranslationsModulesDirectory,
|
|
29
|
-
getVirtualTranslationsModulePath,
|
|
30
|
-
};
|
package/watcher.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
const path = require('node:path');
|
|
2
|
-
|
|
3
|
-
const chokidar = require('chokidar');
|
|
4
|
-
const fg = require('fast-glob');
|
|
5
|
-
const debug = require('debug')('intl:metro-intl-transformer:watcher');
|
|
6
|
-
|
|
7
|
-
const { database } = require('./src/database');
|
|
8
|
-
const {
|
|
9
|
-
IntlCompiledMessageFormat,
|
|
10
|
-
isMessageDefinitionsFile,
|
|
11
|
-
} = require('@discord/intl-message-database');
|
|
12
|
-
|
|
13
|
-
const IGNORE_PATTERNS = [
|
|
14
|
-
// Ignore our own compiled message files, even though they shouldn't have a matching extension.
|
|
15
|
-
'*.compiled.messages.*',
|
|
16
|
-
// Also ignore a bunch of default folders that just make globs/watches take forever.
|
|
17
|
-
'**/node_modules/**',
|
|
18
|
-
'**/target/**',
|
|
19
|
-
'**/native/**',
|
|
20
|
-
'**/dist/**',
|
|
21
|
-
'**/build/**',
|
|
22
|
-
'**/cache/**',
|
|
23
|
-
'**/.cache/**',
|
|
24
|
-
'**/__pycache__/**',
|
|
25
|
-
];
|
|
26
|
-
const MESSAGE_DEFINITION_FILE_PATTERNS = ['*.messages.js'];
|
|
27
|
-
const DEFAULT_LOCALE = 'en-US';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @param {string} filePath
|
|
31
|
-
*/
|
|
32
|
-
function processFile(filePath) {
|
|
33
|
-
if (!isMessageDefinitionsFile(filePath)) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
debug(`Processing file: ${filePath}`);
|
|
37
|
-
try {
|
|
38
|
-
// Convert the file name from `.messages.js` to `.compiled.messages.jsona` for output.
|
|
39
|
-
const fileBasename = filePath.substring(0, filePath.lastIndexOf('.messages.js'));
|
|
40
|
-
const outputPath = path.resolve(
|
|
41
|
-
path.dirname(filePath),
|
|
42
|
-
`${fileBasename}.compiled.messages.jsona`,
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
database.processDefinitionsFile(filePath);
|
|
46
|
-
database.precompile(filePath, 'en-US', outputPath, IntlCompiledMessageFormat.Json);
|
|
47
|
-
} catch (e) {
|
|
48
|
-
debug('[INTL Error] Failed to compile messages');
|
|
49
|
-
console.error(e);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @param {string[]} watchedFolders
|
|
55
|
-
* @param {{
|
|
56
|
-
* watch?: boolean
|
|
57
|
-
* }} options
|
|
58
|
-
*/
|
|
59
|
-
async function compileIntlMessageFiles(watchedFolders, { watch = true } = {}) {
|
|
60
|
-
const globs = watchedFolders.flatMap((folder) => path.join(folder, '**/*.messages.js'));
|
|
61
|
-
|
|
62
|
-
// Perform one initial scan and compilation to ensure all files exist before Metro might try to
|
|
63
|
-
// resolve them.
|
|
64
|
-
debug('Scanning for initial messages files');
|
|
65
|
-
for await (const filePath of fg.stream(globs, {
|
|
66
|
-
ignore: IGNORE_PATTERNS,
|
|
67
|
-
absolute: true,
|
|
68
|
-
onlyFiles: true,
|
|
69
|
-
followSymbolicLinks: false,
|
|
70
|
-
})) {
|
|
71
|
-
processFile(filePath.toString());
|
|
72
|
-
}
|
|
73
|
-
debug('Initial message scan completed.');
|
|
74
|
-
|
|
75
|
-
if (watch) {
|
|
76
|
-
debug(`Setting up file watching for configured paths:\n- ${globs.join('\n- ')}`);
|
|
77
|
-
chokidar
|
|
78
|
-
.watch(globs, { ignored: IGNORE_PATTERNS, ignoreInitial: true })
|
|
79
|
-
.on('all', (event, filePath) => {
|
|
80
|
-
debug(`Got event ${event} for ${filePath}`);
|
|
81
|
-
processFile(filePath);
|
|
82
|
-
});
|
|
83
|
-
} else {
|
|
84
|
-
debug('Not watching files because `watch` option was false');
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
module.exports = { compileIntlMessageFiles };
|