@discord/metro-intl-transformer 0.4.1 → 0.5.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 +11 -60
- package/babel-traverse-message-accesses.js +77 -0
- package/package.json +4 -3
package/babel-plugin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const
|
|
2
|
-
const {
|
|
1
|
+
const { hashMessageKey } = require('@discord/intl-loader-core');
|
|
2
|
+
const { traverseMessageAccesses } = require('./babel-traverse-message-accesses');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Babel plugin for obfuscating and minifying intl message _usages_, like
|
|
@@ -20,65 +20,16 @@ module.exports = function metroIntlTransformerPlugin(babel) {
|
|
|
20
20
|
const { types: t } = babel;
|
|
21
21
|
|
|
22
22
|
return {
|
|
23
|
-
visitor: {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
nativePath.dirname(state.file.opts.filename),
|
|
28
|
-
path.node.source.value,
|
|
23
|
+
visitor: traverseMessageAccesses((access, messageName) => {
|
|
24
|
+
if (messageName == null) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
'[INTL] Encountered a member expression with neither an identifier nor string literal member node',
|
|
29
27
|
);
|
|
30
|
-
|
|
31
|
-
// @ts-expect-error state is untyped but contains `opts` from the config.
|
|
32
|
-
const extraImportSpecifiers = state.opts?.extraImports?.[importSource] ?? [];
|
|
33
|
-
// This transformer only handles usages of intl messages, so only
|
|
34
|
-
// imports of definitions files and configured extra specifiers need to
|
|
35
|
-
// be handled.
|
|
36
|
-
if (!isDefinition && extraImportSpecifiers.length === 0) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
28
|
+
}
|
|
39
29
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
extraImportSpecifiers.includes(specifier.local.name),
|
|
45
|
-
)
|
|
46
|
-
.map((specifier) => specifier.local.name);
|
|
47
|
-
|
|
48
|
-
const bindingReferences = specifiers.flatMap(
|
|
49
|
-
(binding) => path.scope.bindings[binding]?.referencePaths ?? [],
|
|
50
|
-
);
|
|
51
|
-
for (const reference of bindingReferences) {
|
|
52
|
-
const parent = reference.parent;
|
|
53
|
-
// We only care about member expressions that use the `.` syntax or
|
|
54
|
-
// use string literals, since other syntaxes could be invalid.
|
|
55
|
-
if (
|
|
56
|
-
parent.type !== 'MemberExpression' ||
|
|
57
|
-
(parent.computed && parent.property.type !== 'StringLiteral')
|
|
58
|
-
) {
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// We just want the actual value of the member being accessed.
|
|
63
|
-
/** @type {string} */
|
|
64
|
-
const memberName = (() => {
|
|
65
|
-
switch (parent.property.type) {
|
|
66
|
-
case 'StringLiteral':
|
|
67
|
-
return parent.property.value;
|
|
68
|
-
case 'Identifier':
|
|
69
|
-
return parent.property.name;
|
|
70
|
-
default:
|
|
71
|
-
throw new Error(
|
|
72
|
-
'[INTL] Encountered a member expression with neither an identifier nor string literal member node',
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
})();
|
|
76
|
-
|
|
77
|
-
// Then hash it up and re-write the member with the hashed version.
|
|
78
|
-
parent.computed = true;
|
|
79
|
-
parent.property = t.stringLiteral(hashMessageKey(memberName));
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
},
|
|
30
|
+
// Then hash it up and re-write the member with the hashed version.
|
|
31
|
+
access.computed = true;
|
|
32
|
+
access.property = t.stringLiteral(hashMessageKey(messageName));
|
|
33
|
+
}),
|
|
83
34
|
};
|
|
84
35
|
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const nativePath = require('node:path');
|
|
2
|
+
const { isMessageDefinitionsFile } = require('@discord/intl-loader-core');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Babel visitor for traversing all intl message accesses in a file. This visitor can be applied to
|
|
6
|
+
* every file in a project, but only member expressions of imports from `.messages.js` files and any
|
|
7
|
+
* imports as specified in the `extraImports` configuration are considered and affected.
|
|
8
|
+
*
|
|
9
|
+
* Configuration for `extraImports` differs from swc-intl-message-transformer in that
|
|
10
|
+
* paths here are resolved, _absolute_ paths to the desired file, since babel
|
|
11
|
+
* will often have re-written the AST to resolve import aliases by the time
|
|
12
|
+
* this plugin runs.
|
|
13
|
+
*
|
|
14
|
+
* @param {(node: import('@babel/types').MemberExpression, messageName: string | undefined) => void} callback
|
|
15
|
+
* Callback invoked for every message access that's encountered.
|
|
16
|
+
* @returns {import("babel__traverse").Visitor} A visitor object for a Babel transform.
|
|
17
|
+
*/
|
|
18
|
+
function traverseMessageAccesses(callback) {
|
|
19
|
+
return {
|
|
20
|
+
ImportDeclaration(path, state) {
|
|
21
|
+
const importSource = nativePath.resolve(
|
|
22
|
+
// @ts-expect-error state is untyped but contains `file`
|
|
23
|
+
nativePath.dirname(state.file.opts.filename),
|
|
24
|
+
path.node.source.value,
|
|
25
|
+
);
|
|
26
|
+
const isDefinition = isMessageDefinitionsFile(importSource);
|
|
27
|
+
// @ts-expect-error state is untyped but contains `opts` from the config.
|
|
28
|
+
const extraImportSpecifiers = state.opts?.extraImports?.[importSource] ?? [];
|
|
29
|
+
// This transformer only handles usages of intl messages, so only
|
|
30
|
+
// imports of definitions files and configured extra specifiers need to
|
|
31
|
+
// be handled.
|
|
32
|
+
if (!isDefinition && extraImportSpecifiers.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const specifiers = path.node.specifiers
|
|
37
|
+
.filter(
|
|
38
|
+
(specifier) =>
|
|
39
|
+
(isDefinition && specifier.type === 'ImportDefaultSpecifier') ||
|
|
40
|
+
extraImportSpecifiers.includes(specifier.local.name),
|
|
41
|
+
)
|
|
42
|
+
.map((specifier) => specifier.local.name);
|
|
43
|
+
|
|
44
|
+
const bindingReferences = specifiers.flatMap(
|
|
45
|
+
(binding) => path.scope.bindings[binding]?.referencePaths ?? [],
|
|
46
|
+
);
|
|
47
|
+
for (const reference of bindingReferences) {
|
|
48
|
+
const parent = reference.parent;
|
|
49
|
+
// We only care about member expressions that use the `.` syntax or
|
|
50
|
+
// use string literals, since other syntaxes could be invalid.
|
|
51
|
+
if (
|
|
52
|
+
parent.type !== 'MemberExpression' ||
|
|
53
|
+
(parent.computed && parent.property.type !== 'StringLiteral')
|
|
54
|
+
) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// We just want the actual value of the member being accessed.
|
|
59
|
+
/** @type {string | undefined} */
|
|
60
|
+
const messageName = (() => {
|
|
61
|
+
switch (parent.property.type) {
|
|
62
|
+
case 'StringLiteral':
|
|
63
|
+
return parent.property.value;
|
|
64
|
+
case 'Identifier':
|
|
65
|
+
return parent.property.name;
|
|
66
|
+
default:
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
})();
|
|
70
|
+
|
|
71
|
+
callback(parent, messageName);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
module.exports = { traverseMessageAccesses };
|
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord/metro-intl-transformer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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
8
|
"./asset-plugin": "./asset-plugin.js",
|
|
9
|
-
"./babel-plugin": "./babel-plugin.js"
|
|
9
|
+
"./babel-plugin": "./babel-plugin.js",
|
|
10
|
+
"./babel-traverse-message-accesses": "./babel-traverse-message-accesses.js"
|
|
10
11
|
},
|
|
11
12
|
"dependencies": {
|
|
12
13
|
"debug": "^4.3.6",
|
|
13
|
-
"@discord/intl-loader-core": "0.
|
|
14
|
+
"@discord/intl-loader-core": "0.5.0"
|
|
14
15
|
},
|
|
15
16
|
"devDependencies": {
|
|
16
17
|
"@babel/types": "^7.24.9",
|