@discord/babel-plugin-transform-discord-intl 0.7.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/LICENSE +21 -0
- package/index.js +35 -0
- package/package.json +20 -0
- package/traverse.js +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Discord, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const { hashMessageKey } = require('@discord/intl-loader-core');
|
|
2
|
+
const { traverseMessageAccesses } = require('./traverse');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Babel plugin for obfuscating and minifying intl message _usages_, like
|
|
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.
|
|
14
|
+
*
|
|
15
|
+
* @param {any} babel - The Babel core object.
|
|
16
|
+
* @returns {{visitor: import("babel__traverse").Visitor}} A visitor object for the Babel transform.
|
|
17
|
+
*/
|
|
18
|
+
module.exports = function babelPluginTransformDiscordIntl(babel) {
|
|
19
|
+
/** @type {{types: import("@babel/types")}} */
|
|
20
|
+
const { types: t } = babel;
|
|
21
|
+
|
|
22
|
+
return {
|
|
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',
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
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
|
+
}),
|
|
34
|
+
};
|
|
35
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@discord/babel-plugin-transform-discord-intl",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"description": "Babel plugin for transforming message accesses and definitions",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js",
|
|
10
|
+
"./traverse": "./traverse.js"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@typescript-eslint/scope-manager": "^8.8.1",
|
|
14
|
+
"@discord/intl-loader-core": "0.7.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@babel/types": "^7.24.9",
|
|
18
|
+
"@types/babel__traverse": "7.20.6"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/traverse.js
ADDED
|
@@ -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 };
|