@discord/metro-intl-transformer 0.2.1-rc.2 → 0.2.2

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.
Files changed (2) hide show
  1. package/babel-plugin.js +32 -17
  2. 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(messages.SOME_LONG_MESSAGE_KEY_NAME)` into
6
- * `intl.format(messages['f21c/3'])`. This transform gets applied to every
7
- * file in the project, but only member expressions of imports from
8
- * `.messages.js` files are considered and affected.
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, _state) {
20
- const importSource = path.node.source.value;
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 be handled.
23
- if (!isMessageDefinitionsFile(importSource)) {
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 defaultImport = path.node.specifiers.find(
28
- (specifier) => specifier.type === 'ImportDefaultSpecifier',
29
- );
30
- const bindingName = defaultImport?.local.name;
31
- if (bindingName == null) {
32
- return;
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 binding = path.scope.bindings[bindingName];
36
- for (const reference of binding.referencePaths) {
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.2.1-rc.2",
3
+ "version": "0.2.2",
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.2.1-rc.2"
13
+ "@discord/intl-loader-core": "0.2.2"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@babel/types": "^7.24.9",