@discord/metro-intl-transformer 0.2.1-rc.2 → 0.3.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 +32 -17
- package/package.json +2 -2
package/babel-plugin.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
const nativePath = require('node:path');
|
|
1
2
|
const { isMessageDefinitionsFile, hashMessageKey } = require('@discord/intl-loader-core');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Babel plugin for obfuscating and minifying intl message _usages_, like
|
|
5
|
-
* turning `intl.format(
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* turning `intl.format(t.SOME_LONG_MESSAGE_KEY_NAME)` into `intl.format(t['f21c/3'])`.
|
|
7
|
+
* This transform gets applied to every file in the project, but only member
|
|
8
|
+
* expressions of imports from `.messages.js` files are considered and affected.
|
|
9
|
+
*
|
|
10
|
+
* Configuration for `extraImports` differs from swc-intl-message-transformer in that
|
|
11
|
+
* paths here are resolved, _absolute_ paths to the desired file, since babel
|
|
12
|
+
* will often have re-written the AST to resolve import aliases by the time
|
|
13
|
+
* this plugin runs.
|
|
9
14
|
*
|
|
10
15
|
* @param {any} babel - The Babel core object.
|
|
11
16
|
* @returns {{visitor: import("babel__traverse").Visitor}} A visitor object for the Babel transform.
|
|
@@ -16,24 +21,34 @@ module.exports = function metroIntlTransformerPlugin(babel) {
|
|
|
16
21
|
|
|
17
22
|
return {
|
|
18
23
|
visitor: {
|
|
19
|
-
ImportDeclaration(path,
|
|
20
|
-
const importSource =
|
|
24
|
+
ImportDeclaration(path, state) {
|
|
25
|
+
const importSource = nativePath.resolve(
|
|
26
|
+
// @ts-expect-error state is untyped but contains `file`
|
|
27
|
+
nativePath.dirname(state.file.opts.filename),
|
|
28
|
+
path.node.source.value,
|
|
29
|
+
);
|
|
30
|
+
const isDefinition = isMessageDefinitionsFile(importSource);
|
|
31
|
+
// @ts-expect-error state is untyped but contains `opts` from the config.
|
|
32
|
+
const extraImportSpecifiers = state.opts?.extraImports?.[importSource] ?? [];
|
|
21
33
|
// This transformer only handles usages of intl messages, so only
|
|
22
|
-
// imports of definitions files need to
|
|
23
|
-
|
|
34
|
+
// imports of definitions files and configured extra specifiers need to
|
|
35
|
+
// be handled.
|
|
36
|
+
if (!isDefinition && extraImportSpecifiers.length === 0) {
|
|
24
37
|
return;
|
|
25
38
|
}
|
|
26
39
|
|
|
27
|
-
const
|
|
28
|
-
(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
const specifiers = path.node.specifiers
|
|
41
|
+
.filter(
|
|
42
|
+
(specifier) =>
|
|
43
|
+
(isDefinition && specifier.type === 'ImportDefaultSpecifier') ||
|
|
44
|
+
extraImportSpecifiers.includes(specifier.local.name),
|
|
45
|
+
)
|
|
46
|
+
.map((specifier) => specifier.local.name);
|
|
34
47
|
|
|
35
|
-
const
|
|
36
|
-
|
|
48
|
+
const bindingReferences = specifiers.flatMap(
|
|
49
|
+
(binding) => path.scope.bindings[binding]?.referencePaths ?? [],
|
|
50
|
+
);
|
|
51
|
+
for (const reference of bindingReferences) {
|
|
37
52
|
const parent = reference.parent;
|
|
38
53
|
// We only care about member expressions that use the `.` syntax or
|
|
39
54
|
// use string literals, since other syntaxes could be invalid.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/metro-intl-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"debug": "^4.3.6",
|
|
13
|
-
"@discord/intl-loader-core": "0.
|
|
13
|
+
"@discord/intl-loader-core": "0.3.0"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@babel/types": "^7.24.9",
|